ItEr25S07CUAsignacionGrupoRecursosAPlanificacionItEr24S08: Adding allocation capabilities to SpecificResourceAllocation.

This commit is contained in:
Óscar González Fernández 2009-09-15 23:58:25 +02:00
parent b7765d3e24
commit 7d61c709cb
2 changed files with 125 additions and 7 deletions

View file

@ -1,19 +1,21 @@
package org.navalplanner.business.planner.entities;
import java.util.Collections;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.Validate;
import org.hibernate.validator.NotNull;
import org.joda.time.LocalDate;
import org.navalplanner.business.resources.entities.Worker;
/**
* Represents the relation between {@link Task} and a specific {@link Worker}.
*
* @author Manuel Rego Casasnovas <mrego@igalia.com>
*/
public class SpecificResourceAllocation extends ResourceAllocation {
public class SpecificResourceAllocation extends ResourceAllocation implements
IAllocatable {
public static SpecificResourceAllocation create(Task task) {
return (SpecificResourceAllocation) create(new SpecificResourceAllocation(
@ -55,12 +57,36 @@ public class SpecificResourceAllocation extends ResourceAllocation {
this.worker = worker;
}
public Set<SpecificDayAssigment> getSpecificDaysAssigment() {
return Collections.unmodifiableSet(specificDaysAssigment);
@Override
public List<SpecificDayAssigment> getAssignments() {
return DayAssigment.orderedByDay(specificDaysAssigment);
}
private void setAssignments(List<SpecificDayAssigment> assignments) {
this.specificDaysAssigment = new HashSet<SpecificDayAssigment>(
assignments);
}
@Override
protected List<? extends DayAssigment> getAssignments() {
return DayAssigment.orderedByDay(specificDaysAssigment);
public void allocate(ResourcesPerDay resourcesPerDay) {
Validate.notNull(resourcesPerDay);
Validate.notNull(worker);
AssignmentsAllocation<SpecificDayAssigment> assignmentsAllocation = new AssignmentsAllocation<SpecificDayAssigment>() {
@Override
protected List<SpecificDayAssigment> distributeForDay(
LocalDate day, int totalHours) {
return Arrays.asList(SpecificDayAssigment.create(day,
totalHours, worker));
}
@Override
protected void resetAssignmentsTo(
List<SpecificDayAssigment> assignments) {
setAssignments(assignments);
}
};
assignmentsAllocation.allocate(resourcesPerDay);
}
}

View file

@ -0,0 +1,92 @@
package org.navalplanner.business.test.planner.entities;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.easymock.classextension.EasyMock.createNiceMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.junit.Assert.assertThat;
import static org.navalplanner.business.test.planner.entities.DayAssigmentMatchers.consecutiveDays;
import static org.navalplanner.business.test.planner.entities.DayAssigmentMatchers.from;
import org.joda.time.LocalDate;
import org.junit.Test;
import org.navalplanner.business.calendars.entities.BaseCalendar;
import org.navalplanner.business.calendars.entities.ResourceCalendar;
import org.navalplanner.business.planner.entities.ResourcesPerDay;
import org.navalplanner.business.planner.entities.SpecificResourceAllocation;
import org.navalplanner.business.planner.entities.Task;
import org.navalplanner.business.resources.entities.Worker;
public class SpecificResourceAllocationTest {
private BaseCalendar baseCalendar;
private Task task;
private SpecificResourceAllocation specificResourceAllocation;
private Worker worker;
private ResourceCalendar calendar;
private int assignedHours = 0;
private void givenAssignedHours(int assignedHours){
this.assignedHours = assignedHours;
}
private void givenWorker(){
this.worker = createNiceMock(Worker.class);
expect(this.worker.getCalendar()).andReturn(calendar).anyTimes();
expect(this.worker.getAssignedHours(isA(LocalDate.class))).andReturn(assignedHours).anyTimes();
replay(this.worker);
}
private void givenTask(LocalDate start, LocalDate end) {
task = createNiceMock(Task.class);
expect(task.getCalendar()).andReturn(baseCalendar).anyTimes();
expect(task.getStartDate()).andReturn(
start.toDateTimeAtStartOfDay().toDate()).anyTimes();
expect(task.getEndDate()).andReturn(
end.toDateTimeAtStartOfDay().toDate()).anyTimes();
replay(task);
}
private void givenSpecificResourceAllocation(LocalDate start, LocalDate end) {
givenWorker();
givenTask(start, end);
specificResourceAllocation = SpecificResourceAllocation.create(task);
specificResourceAllocation.setWorker(worker);
}
private void givenSpecificResourceAllocation(LocalDate start, int days) {
givenSpecificResourceAllocation(start, start.plusDays(days));
}
@Test
public void theAllocationsDoneAreOrderedByDay() {
givenSpecificResourceAllocation(new LocalDate(2000, 2, 4), 2);
specificResourceAllocation.allocate(ResourcesPerDay.amount(1));
assertThat(specificResourceAllocation.getAssignments(),
consecutiveDays(2));
}
@Test
public void theAllocationStartsAtTheStartDate() {
LocalDate start = new LocalDate(2000, 2, 4);
givenSpecificResourceAllocation(start, 2);
specificResourceAllocation.allocate(ResourcesPerDay.amount(1));
assertThat(specificResourceAllocation.getAssignments(), from(start));
}
@Test
public void theAllocationIsDoneEvenIfThereisOvertime() {
givenAssignedHours(4);
LocalDate start = new LocalDate(2000, 2, 4);
givenSpecificResourceAllocation(start, 2);
specificResourceAllocation.allocate(ResourcesPerDay.amount(1));
assertThat(specificResourceAllocation.getAssignments(),
DayAssigmentMatchers.haveHours(8, 8));
}
}