ItEr25S07CUAsignacionGrupoRecursosAPlanificacionItEr24S08: Adding preconditions checks to allocation until filling hours
This commit is contained in:
parent
00cd61981f
commit
b3a579791c
2 changed files with 109 additions and 1 deletions
|
|
@ -26,7 +26,8 @@ public abstract class ResourceAllocation extends BaseEntity {
|
|||
|
||||
private final ResourcesPerDay resourcesPerDay;
|
||||
|
||||
private ResourceAllocationWithDesiredResourcesPerDay(ResourceAllocation resourceAllocation,
|
||||
public ResourceAllocationWithDesiredResourcesPerDay(
|
||||
ResourceAllocation resourceAllocation,
|
||||
ResourcesPerDay resourcesPerDay) {
|
||||
this.resourceAllocation = resourceAllocation;
|
||||
this.resourcesPerDay = resourcesPerDay;
|
||||
|
|
@ -52,9 +53,39 @@ public abstract class ResourceAllocation extends BaseEntity {
|
|||
|
||||
public AllocationsCurried(
|
||||
List<ResourceAllocationWithDesiredResourcesPerDay> resourceAllocations) {
|
||||
Validate.notNull(resourceAllocations);
|
||||
Validate.notEmpty(resourceAllocations);
|
||||
Validate.noNullElements(resourceAllocations);
|
||||
checkNoOneHasNullTask(resourceAllocations);
|
||||
checkAllHaveSameTask(resourceAllocations);
|
||||
this.resourceAllocations = resourceAllocations;
|
||||
}
|
||||
|
||||
private static void checkNoOneHasNullTask(
|
||||
List<ResourceAllocationWithDesiredResourcesPerDay> allocations) {
|
||||
for (ResourceAllocationWithDesiredResourcesPerDay resourceAllocationWithDesiredResourcesPerDay : allocations) {
|
||||
if (resourceAllocationWithDesiredResourcesPerDay
|
||||
.getResourceAllocation().getTask() == null)
|
||||
throw new IllegalArgumentException(
|
||||
"all allocations must have task");
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkAllHaveSameTask(
|
||||
List<ResourceAllocationWithDesiredResourcesPerDay> resourceAllocations) {
|
||||
Task task = null;
|
||||
for (ResourceAllocationWithDesiredResourcesPerDay r : resourceAllocations) {
|
||||
if (task == null) {
|
||||
task = r.getResourceAllocation().getTask();
|
||||
}
|
||||
if (!task.equals(r.getResourceAllocation().getTask())) {
|
||||
throw new IllegalArgumentException(
|
||||
"all allocations must belong to the same task");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public AllocationsAndResourcesCurried withResources(
|
||||
List<Resource> resources) {
|
||||
return new AllocationsAndResourcesCurried(resources,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,77 @@
|
|||
package org.navalplanner.business.test.planner.entities;
|
||||
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.classextension.EasyMock.createNiceMock;
|
||||
import static org.easymock.classextension.EasyMock.replay;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.navalplanner.business.planner.entities.ResourceAllocation;
|
||||
import org.navalplanner.business.planner.entities.ResourcesPerDay;
|
||||
import org.navalplanner.business.planner.entities.Task;
|
||||
import org.navalplanner.business.planner.entities.ResourceAllocation.ResourceAllocationWithDesiredResourcesPerDay;
|
||||
|
||||
public class AllocationUntilFillingHoursTest {
|
||||
|
||||
private List<ResourceAllocationWithDesiredResourcesPerDay> allocations = new ArrayList<ResourceAllocationWithDesiredResourcesPerDay>();
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void allTasksOfAllocationsMustBeNotNull() {
|
||||
givenAllocationsWithoutTask();
|
||||
ResourceAllocation.allocating(allocations);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void allAllocationsMustBelongToTheSameTask() {
|
||||
givenAllocationsBelongingToDifferentTasks();
|
||||
ResourceAllocation.allocating(allocations);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void mustReceiveAtLeastOneAllocation() {
|
||||
ResourceAllocation
|
||||
.allocating(new ArrayList<ResourceAllocationWithDesiredResourcesPerDay>());
|
||||
}
|
||||
|
||||
private void givenAllocationsWithoutTask() {
|
||||
allocations
|
||||
.add(new ResourceAllocationWithDesiredResourcesPerDay(
|
||||
createStubAllocationReturning(null), ResourcesPerDay
|
||||
.amount(2)));
|
||||
allocations
|
||||
.add(new ResourceAllocationWithDesiredResourcesPerDay(
|
||||
createStubAllocationReturning(null), ResourcesPerDay
|
||||
.amount(2)));
|
||||
}
|
||||
|
||||
private void givenAllocationsBelongingToDifferentTasks() {
|
||||
Task task = createStubTask();
|
||||
allocations.add(new ResourceAllocationWithDesiredResourcesPerDay(
|
||||
createStubAllocationReturning(task), ResourcesPerDay.amount(2)));
|
||||
allocations
|
||||
.add(new ResourceAllocationWithDesiredResourcesPerDay(
|
||||
createStubAllocationReturning(task), ResourcesPerDay
|
||||
.amount(2)));
|
||||
Task other = createStubTask();
|
||||
allocations
|
||||
.add(new ResourceAllocationWithDesiredResourcesPerDay(
|
||||
createStubAllocationReturning(other), ResourcesPerDay
|
||||
.amount(2)));
|
||||
}
|
||||
|
||||
private Task createStubTask() {
|
||||
Task task = createNiceMock(Task.class);
|
||||
replay(task);
|
||||
return task;
|
||||
}
|
||||
|
||||
private ResourceAllocation createStubAllocationReturning(Task task) {
|
||||
ResourceAllocation resourceAllocation = createNiceMock(ResourceAllocation.class);
|
||||
expect(resourceAllocation.getTask()).andReturn(task).anyTimes();
|
||||
replay(resourceAllocation);
|
||||
return resourceAllocation;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue