AllocationsResource extends BaseResource { @POST public void updateAllocations(@PathParam("experimentName") String experimentName, @Valid List<AllocationRequest> requests) { final Experiment experiment = ensureExists(experiments.get(experimentName)); for (AllocationRequest request : requests) { if (request instanceof AllocationRequest.Deallocate) { experiment.deallocate(request.getTreatment(), request.getSize()); } else if (request instanceof AllocationRequest.Reallocate) { final AllocationRequest.Reallocate reallocation = (AllocationRequest.Reallocate) request; experiment.reallocate(reallocation.getTreatment(), reallocation.getTarget(), reallocation.getSize()); } else { experiment.allocate(request.getTreatment(), request.getSize()); } } experiment.save(); } @Inject  AllocationsResource(Experiments experiments, Mappers mapper); @GET Iterable<AllocationDto> getAllocations(@PathParam("experimentName") String experimentName); @POST void updateAllocations(@PathParam("experimentName") String experimentName,
                                  @Valid List<AllocationRequest> requests); @DELETE void clearAllocations(@PathParam("experimentName") String experimentName);  }
@Test public void testUpdateAllocations() { final AllocationRequests allocationRequest = AllocationRequests.of( new AllocationRequest.Deallocate(EXP_1_TREATMENT_2, 10), new AllocationRequest.Allocate(EXP_1_TREATMENT_1, 5), new AllocationRequest.Reallocate(EXP_1_TREATMENT_3, 15, EXP_1_TREATMENT_1) ); post(ALLOCATIONS_ENDPOINT, EXPERIMENT_BAD) .entity(allocationRequest) .assertStatus(Status.NOT_FOUND); final Map<String, Integer> allocations = ImmutableMap.of( EXP_1_TREATMENT_1, 50, EXP_1_TREATMENT_2, 25, EXP_1_TREATMENT_3, 25 ); final Iterable<AllocationDto> currentAllocations = MAPPER.toDto( experiment(EXPERIMENT_1).getAllocations(), AllocationDto.class ); assertEquals(allocations, countAllocations(currentAllocations)); post(ALLOCATIONS_ENDPOINT, EXPERIMENT_1) .entity(allocationRequest) .assertStatus(Status.NO_CONTENT); final Map<String, Integer> expectedAllocations = ImmutableMap.of( EXP_1_TREATMENT_1, 75, EXP_1_TREATMENT_2, 15, EXP_1_TREATMENT_3, 10 ); final Iterable<AllocationDto> newAllocations = MAPPER.toDto( experiment(EXPERIMENT_1).getAllocations(), AllocationDto.class ); assertEquals(expectedAllocations, countAllocations(newAllocations)); }