ItEr27S06CUAsignacionGrupoRecursosAPlanificacionItEr26S07: Adding preconditions
This commit is contained in:
parent
c115a01ed2
commit
5e1a0bc2f7
2 changed files with 55 additions and 0 deletions
|
|
@ -1,5 +1,6 @@
|
|||
package org.navalplanner.business.planner.entities;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -41,11 +42,26 @@ public abstract class ResourceAllocation<T extends DayAssignment> extends
|
|||
Validate.noNullElements(resourceAllocations);
|
||||
checkNoOneHasNullTask(resourceAllocations);
|
||||
checkAllHaveSameTask(resourceAllocations);
|
||||
checkNoAllocationWithZeroResourcesPerDay(resourceAllocations);
|
||||
this.resourceAllocations = resourceAllocations;
|
||||
this.task = resourceAllocations.get(0).getResourceAllocation()
|
||||
.getTask();
|
||||
}
|
||||
|
||||
private static void checkNoAllocationWithZeroResourcesPerDay(
|
||||
List<ResourceAllocationWithDesiredResourcesPerDay> allocations) {
|
||||
for (ResourceAllocationWithDesiredResourcesPerDay r : allocations) {
|
||||
if (isZero(r.getResourcesPerDay().getAmount())) {
|
||||
throw new IllegalArgumentException(
|
||||
"all resources per day must be no zero");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isZero(BigDecimal amount) {
|
||||
return amount.movePointRight(amount.scale()).intValue() == 0;
|
||||
}
|
||||
|
||||
private static void checkNoOneHasNullTask(
|
||||
List<ResourceAllocationWithDesiredResourcesPerDay> allocations) {
|
||||
for (ResourceAllocationWithDesiredResourcesPerDay resourceAllocationWithDesiredResourcesPerDay : allocations) {
|
||||
|
|
@ -72,6 +88,7 @@ public abstract class ResourceAllocation<T extends DayAssignment> extends
|
|||
|
||||
public AllocationsAndResourcesCurried withResources(
|
||||
List<Resource> resources) {
|
||||
Validate.noNullElements(resources);
|
||||
return new AllocationsAndResourcesCurried(task, resources,
|
||||
resourceAllocations);
|
||||
}
|
||||
|
|
@ -93,6 +110,9 @@ public abstract class ResourceAllocation<T extends DayAssignment> extends
|
|||
}
|
||||
|
||||
public LocalDate untilAllocating(int hoursToAllocate) {
|
||||
if (thereIsGenericAllocation()) {
|
||||
Validate.notEmpty(resources, "there must exist workers");
|
||||
}
|
||||
AllocatorForSpecifiedResourcesPerDayAndHours allocator = new AllocatorForSpecifiedResourcesPerDayAndHours(
|
||||
task, resources, allocations) {
|
||||
|
||||
|
|
@ -117,6 +137,15 @@ public abstract class ResourceAllocation<T extends DayAssignment> extends
|
|||
return allocator.untilAllocating(hoursToAllocate);
|
||||
}
|
||||
|
||||
private boolean thereIsGenericAllocation() {
|
||||
for (ResourceAllocationWithDesiredResourcesPerDay r : allocations) {
|
||||
if (r.getResourceAllocation() instanceof GenericResourceAllocation) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void allocateOnTaskLength() {
|
||||
AllocatorForTaskDurationAndSpecifiedResourcesPerDay allocator = new AllocatorForTaskDurationAndSpecifiedResourcesPerDay(
|
||||
allocations, resources);
|
||||
|
|
|
|||
|
|
@ -129,6 +129,32 @@ public class AllocationUntilFillingHoursTest {
|
|||
assertThat(secondSpecific.getAssignments(), haveHours(8, 8));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void withGenericAllocationWithNoResourcesPerDay() {
|
||||
givenWorkers(1);
|
||||
givenGenericAllocation(ResourcesPerDay.amount(0));
|
||||
ResourceAllocation.allocating(allocations).withResources(resources)
|
||||
.untilAllocating(100);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void cannotDoAGenericAllocationWithoutWorkers() {
|
||||
givenWorkers(0);
|
||||
givenGenericAllocation(ResourcesPerDay.amount(2));
|
||||
ResourceAllocation.allocating(allocations)
|
||||
.withResources(resources)
|
||||
.untilAllocating(100);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withoutWorkersYouCanDoSpecificAllocation() {
|
||||
givenWorkers(0);
|
||||
givenSpecificAllocations(ResourcesPerDay.amount(2));
|
||||
ResourceAllocation.allocating(allocations).withResources(resources)
|
||||
.untilAllocating(100);
|
||||
}
|
||||
|
||||
private void givenGenericAllocation(ResourcesPerDay resourcesPerDay) {
|
||||
createTaskIfNotCreatedYet();
|
||||
allocations.add(new ResourceAllocationWithDesiredResourcesPerDay(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue