ChangeVehicleCapacity implements ProblemFactChange<VehicleRoutingSolution> { @Override public void doChange(ScoreDirector<VehicleRoutingSolution> scoreDirector) { PlanningVehicle workingVehicle = scoreDirector.lookUpWorkingObject(vehicle); if (workingVehicle == null) { throw new IllegalStateException("Can't look up a working copy of " + vehicle); } scoreDirector.beforeProblemPropertyChanged(workingVehicle); workingVehicle.setCapacity(vehicle.getCapacity()); scoreDirector.afterProblemPropertyChanged(workingVehicle); scoreDirector.triggerVariableListeners(); } ChangeVehicleCapacity(PlanningVehicle vehicle); @Override void doChange(ScoreDirector<VehicleRoutingSolution> scoreDirector);  }
@Test void change_vehicle_capacity() { int oldCapacity = 100; int newCapacity = 50; PlanningVehicle workingVehicle = PlanningVehicleFactory.testVehicle(1, oldCapacity); PlanningVehicle changeVehicle = PlanningVehicleFactory.testVehicle(2, newCapacity); when(scoreDirector.lookUpWorkingObject(changeVehicle)).thenReturn(workingVehicle); ChangeVehicleCapacity changeVehicleCapacity = new ChangeVehicleCapacity(changeVehicle); changeVehicleCapacity.doChange(scoreDirector); assertThat(workingVehicle.getCapacity()).isEqualTo(newCapacity); verify(scoreDirector).beforeProblemPropertyChanged(workingVehicle); verify(scoreDirector).afterProblemPropertyChanged(workingVehicle); verify(scoreDirector).triggerVariableListeners(); }
@Test void fail_fast_if_working_object_is_null() { ChangeVehicleCapacity changeVehicleCapacity = new ChangeVehicleCapacity(PlanningVehicleFactory.testVehicle(1)); assertThatIllegalStateException() .isThrownBy(() -> changeVehicleCapacity.doChange(scoreDirector)) .withMessageContaining("working copy of"); }