ExperimentsResource extends BaseResource { @POST @Path("/{experimentName}") public void updateExperiment(@PathParam("experimentName") String experimentName, @Valid UpdateExperimentRequest request) { final Experiment experiment = ensureExists(experiments.get(experimentName)); if (request.getSeed() != null && request.getSeed().isPresent()) { experiment.setSeed(request.getSeed().orElse(0)); } if (request.getDescription() != null) { experiment.setDescription(request.getDescription().orElse(null)); } if (request.getFilter() != null && request.getFilter().isPresent()) { experiment.setFilter(FilterExpression.of(request.getFilter().orElse(null))); } if (request.getHashAttributes() != null && request.getHashAttributes().isPresent()) { experiment.setHashAttributes(request.getHashAttributes().orElse(null)); } if (request.getTreatments() != null) { final Set<String> missingTreatments = Sets.newHashSet(); for (final Treatment treatment : experiment.getTreatments()) { missingTreatments.add(treatment.getName()); } if (request.getTreatments().isPresent()) { for (final TreatmentDto treatment : request.getTreatments().get()) { missingTreatments.remove(treatment.getName()); final Treatment existingTreatment = experiment.getTreatment(treatment.getName()); if (existingTreatment != null) { existingTreatment.setDescription(treatment.getDescription()); } else { experiment.addTreatment(treatment.getName(), treatment.getDescription()); } } } for (final String missingTreatment : missingTreatments) { experiment.removeTreatment(missingTreatment); } } if (request.getAllocations() != null) { experiment.deallocateAll(); if (request.getAllocations().isPresent()) { for (final AllocateRequest allocation : request.getAllocations().get()) { experiment.allocate(allocation.getTreatment(), allocation.getSize()); } } } if (request.getOverrides() != null) { experiment.clearOverrides(); if (request.getOverrides().isPresent()) { for (final TreatmentOverrideRequest override : request.getOverrides().get()) { experiment.addOverride( override.getName(), override.getTreatment(), override.getFilter() ); } } } if (request.getActive() != null && request.getActive().isPresent()) { if (request.getActive().get()) { experiment.activate(); } else { experiment.deactivate(); } } experiment.save(); } @Inject  ExperimentsResource(Experiments experiments, Mappers mapper); @GET Iterable<ExperimentDto> getExperiments(@QueryParam("filter") String filterValue,
                                                  @QueryParam("offset") Integer offset,
                                                  @QueryParam("limit") Integer limit,
                                                  @QueryParam("sort") String sort); @PUT Response addExperiment(@Valid CreateExperimentRequest request); @POST @Path("/{experimentName}") void updateExperiment(@PathParam("experimentName") String experimentName,
                                 @Valid UpdateExperimentRequest request); @GET @Path("/{experimentName}") ExperimentDto getExperiment(@PathParam("experimentName") String experimentName); @DELETE @Path("/{experimentName}") void removeExperiment(@PathParam("experimentName") String experimentName);  }
@Test public void testUpdateExperiment() { final UpdateExperimentRequest request = new UpdateExperimentRequest( Optional.empty(), Optional.of("new description"), Optional.of("device"), Optional.empty(), Optional.of(false), Optional.empty(), Optional.empty(), Optional.empty() ); final ExperimentDto expected = MAPPER.toDto( experiment(EXPERIMENT_1) .setDescription(request.getDescription().get()) .setFilter(FilterExpression.of(request.getFilter().get())) .deactivate(), ExperimentDto.class ); post(EXPERIMENT_ENDPOINT, EXPERIMENT_BAD) .entity(request) .assertStatus(Status.NOT_FOUND); post(EXPERIMENT_ENDPOINT, EXPERIMENT_1) .entity(request) .assertStatus(Status.NO_CONTENT); final ExperimentDto actual = MAPPER.toDto(experiment(EXPERIMENT_1), ExperimentDto.class); assertEquals(expected, actual); }