diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/ResourceAllocation.java b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/ResourceAllocation.java index 1955a8241..b0c492de4 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/ResourceAllocation.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/ResourceAllocation.java @@ -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 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 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 allocations) { for (ResourceAllocationWithDesiredResourcesPerDay resourceAllocationWithDesiredResourcesPerDay : allocations) { @@ -72,6 +88,7 @@ public abstract class ResourceAllocation extends public AllocationsAndResourcesCurried withResources( List resources) { + Validate.noNullElements(resources); return new AllocationsAndResourcesCurried(task, resources, resourceAllocations); } @@ -93,6 +110,9 @@ public abstract class ResourceAllocation 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 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); diff --git a/navalplanner-business/src/test/java/org/navalplanner/business/test/planner/entities/AllocationUntilFillingHoursTest.java b/navalplanner-business/src/test/java/org/navalplanner/business/test/planner/entities/AllocationUntilFillingHoursTest.java index 08c8cbc3e..3415bec57 100644 --- a/navalplanner-business/src/test/java/org/navalplanner/business/test/planner/entities/AllocationUntilFillingHoursTest.java +++ b/navalplanner-business/src/test/java/org/navalplanner/business/test/planner/entities/AllocationUntilFillingHoursTest.java @@ -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(