### Question:
LoadBalance { Result<? extends Record> run() { model.updateData(); return model.solve(VIRTUAL_MACHINES_TABLE); } LoadBalance(final List<String> constraints);   }### Answer:
@Test public void testSimpleConstraint() { final String allVmsGoToPm3 = "create view constraint_simple as " + "select * from virtual_machine " + "check controllable__physical_machine = 'pm3'"; final LoadBalance lb = new LoadBalance(Collections.singletonList(allVmsGoToPm3)); addInventory(lb); final Result<? extends Record> results = lb.run(); System.out.println(results); results.forEach(e -> assertEquals("pm3", e.get("CONTROLLABLE__PHYSICAL_MACHINE"))); }
@Test public void testCapacityConstraints() { final String capacityConstraint = "create view constraint_capacity as " + "select * from virtual_machine " + "join physical_machine " + " on physical_machine.name = virtual_machine.controllable__physical_machine " + "group by physical_machine.name, physical_machine.cpu_capacity, physical_machine.memory_capacity " + "check sum(virtual_machine.cpu) <= physical_machine.cpu_capacity and " + " sum(virtual_machine.memory) <= physical_machine.memory_capacity"; final LoadBalance lb = new LoadBalance(Collections.singletonList(capacityConstraint)); addInventory(lb); final Result<? extends Record> results = lb.run(); System.out.println(results); final Set<String> setOfPhysicalMachines = results.stream() .map(e -> e.get("CONTROLLABLE__PHYSICAL_MACHINE", String.class)) .collect(Collectors.toSet()); assertTrue(setOfPhysicalMachines.size() >= 2); }
@Test public void testDistributeLoad() { final String capacityConstraint = "create view constraint_capacity as " + "select * from virtual_machine " + "join physical_machine " + " on physical_machine.name = virtual_machine.controllable__physical_machine " + "group by physical_machine.name, physical_machine.cpu_capacity, physical_machine.memory_capacity " + "check sum(virtual_machine.cpu) <= physical_machine.cpu_capacity and " + " sum(virtual_machine.memory) <= physical_machine.memory_capacity"; final String spareCpu = "create view spare_cpu as " + "select physical_machine.cpu_capacity - sum(virtual_machine.cpu) as cpu_spare " + "from virtual_machine " + "join physical_machine " + " on physical_machine.name = virtual_machine.controllable__physical_machine " + "group by physical_machine.name, physical_machine.cpu_capacity"; final String distributeLoadCpu = "create view objective_load_cpu as select min(cpu_spare) from spare_cpu"; final LoadBalance lb = new LoadBalance(List.of(capacityConstraint, spareCpu, distributeLoadCpu)); addInventory(lb); final Result<? extends Record> result = lb.run(); final Set<String> setOfPhysicalMachines = result.stream() .map(e -> e.get("CONTROLLABLE__PHYSICAL_MACHINE", String.class)) .collect(Collectors.toSet()); System.out.println(result); assertEquals(NUM_PHYSICAL_MACHINES, setOfPhysicalMachines.size()); }