ScaleDownConstraintExecutor { List<VirtualMachineLease> evaluate(Collection<VirtualMachineLease> candidates) { List<Set<VirtualMachineLease>> fixedOrder = orderEvaluator.evaluate(candidates); List<VirtualMachineLease> scaleDownOrder = scoringEvaluators.isEmpty() ? fixedOrder.stream().flatMap(Set::stream).collect(Collectors.toList()) : fixedOrder.stream().flatMap(this::groupEvaluator).collect(Collectors.toList()); if (logger.isDebugEnabled()) { List<String> hosts = scaleDownOrder.stream().map(VirtualMachineLease::hostname).collect(Collectors.toList()); logger.debug("Evaluated scale down order: {}", hosts); } return scaleDownOrder; } ScaleDownConstraintExecutor(ScaleDownOrderEvaluator orderEvaluator, Map<ScaleDownConstraintEvaluator, Double> weightedScoringEvaluators);   }
@Test public void testScaleDownOrdering() throws Exception { Map<ScaleDownConstraintEvaluator, Double> scoringEvaluators = new HashMap<>(); scoringEvaluators.put(evenFirstEvaluator, 10.0); scoringEvaluators.put(oddFirstEvaluator, 2.0); executor = new ScaleDownConstraintExecutor(orderEvaluator, scoringEvaluators); List<VirtualMachineLease> candidates = leasesWithIds("l0", "l1", "l2", "l3", "l4"); when(orderEvaluator.evaluate(candidates)).thenReturn( asList(asSet(candidates.get(0), candidates.get(2), candidates.get(3)), asSet(candidates.get(1), candidates.get(4))) ); when(evenFirstEvaluator.evaluate(any(), any())).then(a -> { int idx = leaseIndexOf(a); return ScaleDownConstraintEvaluator.Result.of(idx == 0 ? 0 : (idx % 2 == 0 ? 1 : 0.5)); }); when(oddFirstEvaluator.evaluate(any(), any())).then(a -> { int idx = leaseIndexOf(a); return ScaleDownConstraintEvaluator.Result.of(idx == 0 ? 0 : (idx % 2 == 1 ? 1 : 0.5)); }); List<VirtualMachineLease> order = executor.evaluate(candidates); List<String> ids = order.stream().map(VirtualMachineLease::getId).collect(Collectors.toList()); assertThat(ids, is(equalTo(asList("l2", "l3", "l4", "l1")))); }