CoordinatorLifecycleResource { @PUT @Produces(MediaType.APPLICATION_JSON) public Response setState(String newState, @Context UriInfo uriInfo, @HeaderParam(AIRSHIP_SLOTS_VERSION_HEADER) String expectedSlotsVersion) { Preconditions.checkNotNull(newState, "newState must not be null"); SlotLifecycleState state = SlotLifecycleState.lookup(newState); if (state == null || state == UNKNOWN) { return Response.status(Response.Status.BAD_REQUEST).build(); } List<UUID> uuids = Lists.transform(coordinator.getAllSlotStatus(), SlotStatus.uuidGetter()); Predicate<SlotStatus> slotFilter = SlotFilterBuilder.build(uriInfo, true, uuids); List<SlotStatus> results = coordinator.setState(state, slotFilter, expectedSlotsVersion); return Response.ok(transform(results, fromSlotStatus(coordinator.getAllSlotStatus(), repository))) .header(AIRSHIP_SLOTS_VERSION_HEADER, createSlotsVersion(results)) .build(); } @Inject  CoordinatorLifecycleResource(Coordinator coordinator, Repository repository); @PUT @Produces(MediaType.APPLICATION_JSON) Response setState(String newState,
            @Context UriInfo uriInfo,
            @HeaderParam(AIRSHIP_SLOTS_VERSION_HEADER) String expectedSlotsVersion);  }
@Test public void testMultipleStateMachineWithFilter() { UriInfo uriInfo = MockUriInfo.from("http: assertSlotState(apple1SlotId, STOPPED); assertSlotState(apple2SlotId, STOPPED); assertSlotState(bananaSlotId, STOPPED); assertOkResponse(resource.setState("running", uriInfo, null), RUNNING, apple1SlotId, apple2SlotId); assertSlotState(apple1SlotId, RUNNING); assertSlotState(apple2SlotId, RUNNING); assertSlotState(bananaSlotId, STOPPED); assertOkResponse(resource.setState("running", uriInfo, null), RUNNING, apple1SlotId, apple2SlotId); assertSlotState(apple1SlotId, RUNNING); assertSlotState(apple2SlotId, RUNNING); assertSlotState(bananaSlotId, STOPPED); assertOkResponse(resource.setState("stopped", uriInfo, null), STOPPED, apple1SlotId, apple2SlotId); assertSlotState(apple1SlotId, STOPPED); assertSlotState(apple2SlotId, STOPPED); assertSlotState(bananaSlotId, STOPPED); assertOkResponse(resource.setState("stopped", uriInfo, null), STOPPED, apple1SlotId, apple2SlotId); assertSlotState(apple1SlotId, STOPPED); assertSlotState(apple2SlotId, STOPPED); assertSlotState(bananaSlotId, STOPPED); assertOkResponse(resource.setState("restarting", uriInfo, null), RUNNING, apple1SlotId, apple2SlotId); assertSlotState(apple1SlotId, RUNNING); assertSlotState(apple2SlotId, RUNNING); assertSlotState(bananaSlotId, STOPPED); assertOkResponse(resource.setState("restarting", uriInfo, null), RUNNING, apple1SlotId, apple2SlotId); assertSlotState(apple1SlotId, RUNNING); assertSlotState(apple2SlotId, RUNNING); assertSlotState(bananaSlotId, STOPPED); assertOkResponse(resource.setState("killing", uriInfo, null), STOPPED, apple1SlotId, apple2SlotId); assertSlotState(apple1SlotId, STOPPED); assertSlotState(apple2SlotId, STOPPED); assertSlotState(bananaSlotId, STOPPED); assertOkResponse(resource.setState("killing", uriInfo, null), STOPPED, apple1SlotId, apple2SlotId); assertSlotState(apple1SlotId, STOPPED); assertSlotState(apple2SlotId, STOPPED); assertSlotState(bananaSlotId, STOPPED); }
@Test public void testSetStateUnknownState() { Response response = resource.setState("unknown", uriInfo, null); assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); assertNull(response.getEntity()); }
@Test(expectedExceptions = NullPointerException.class) public void testSetStateNullState() { resource.setState(null, uriInfo, null); }
@Test(expectedExceptions = InvalidSlotFilterException.class) public void testSetStateNoFilter() { resource.setState("running", MockUriInfo.from("http: }
@Test public void testInvalidVersion() { UriInfo uriInfo = MockUriInfo.from("http: try { resource.setState("running", uriInfo, "invalid-version"); fail("Expected VersionConflictException"); } catch (VersionConflictException e) { assertEquals(e.getName(), AIRSHIP_SLOTS_VERSION_HEADER); assertEquals(e.getVersion(), VersionsUtil.createSlotsVersion(coordinator.getAllSlotsStatus(SlotFilterBuilder.build(uriInfo, false, ImmutableList.<UUID>of())))); } }
@Test public void testValidVersion() { UriInfo uriInfo = MockUriInfo.from("http: String slotsVersion = VersionsUtil.createSlotsVersion(coordinator.getAllSlotsStatus(SlotFilterBuilder.build(uriInfo, false, ImmutableList.<UUID>of()))); assertOkResponse(resource.setState("running", uriInfo, slotsVersion), RUNNING, apple1SlotId, apple2SlotId); }