LifecycleResource { @PUT @Produces(MediaType.APPLICATION_JSON) public Response setState(@HeaderParam(AIRSHIP_SLOT_VERSION_HEADER) String slotVersion, @PathParam("slotId") UUID slotId, String newState) { Preconditions.checkNotNull(slotId, "slotId must not be null"); Preconditions.checkNotNull(newState, "newState must not be null"); Slot slot = agent.getSlot(slotId); if (slot == null) { return Response.status(Response.Status.NOT_FOUND).build(); } checkSlotVersion(slot.status(), slotVersion); SlotStatus status; if ("running".equals(newState)) { status = slot.start(); } else if ("restarting".equals(newState)) { status = slot.restart(); } else if ("stopped".equals(newState)) { status = slot.stop(); } else if ("killing".equals(newState)) { status = slot.kill(); } else { return Response.status(Response.Status.BAD_REQUEST).build(); } return Response.ok(SlotStatusRepresentation.from(status)) .header(AIRSHIP_AGENT_VERSION_HEADER, agent.getAgentStatus().getVersion()) .header(AIRSHIP_SLOT_VERSION_HEADER, status.getVersion()) .build(); } @Inject  LifecycleResource(Agent agent); @PUT @Produces(MediaType.APPLICATION_JSON) Response setState(@HeaderParam(AIRSHIP_SLOT_VERSION_HEADER) String slotVersion,
            @PathParam("slotId") UUID slotId,
            String newState);  }
@Test public void testStateMachine() { assertEquals(slot.status().getState(), STOPPED); assertOkResponse(resource.setState(null, slot.getId(), "running"), RUNNING); assertEquals(slot.status().getState(), RUNNING); assertOkResponse(resource.setState(null, slot.getId(), "running"), RUNNING); assertEquals(slot.status().getState(), RUNNING); assertOkResponse(resource.setState(null, slot.getId(), "stopped"), STOPPED); assertEquals(slot.status().getState(), STOPPED); assertOkResponse(resource.setState(null, slot.getId(), "stopped"), STOPPED); assertEquals(slot.status().getState(), STOPPED); assertOkResponse(resource.setState(null, slot.getId(), "restarting"), RUNNING); assertEquals(slot.status().getState(), RUNNING); assertOkResponse(resource.setState(null, slot.getId(), "restarting"), RUNNING); assertEquals(slot.status().getState(), RUNNING); assertOkResponse(resource.setState(null, slot.getId(), "killing"), STOPPED); assertEquals(slot.status().getState(), STOPPED); assertOkResponse(resource.setState(null, slot.getId(), "killing"), STOPPED); assertEquals(slot.status().getState(), STOPPED); }
@Test public void testStateMachineWithVersions() { assertEquals(slot.status().getState(), STOPPED); assertOkResponse(resource.setState(slot.status().getVersion(), slot.getId(), "running"), RUNNING); assertEquals(slot.status().getState(), RUNNING); assertOkResponse(resource.setState(slot.status().getVersion(), slot.getId(), "running"), RUNNING); assertEquals(slot.status().getState(), RUNNING); assertOkResponse(resource.setState(slot.status().getVersion(), slot.getId(), "stopped"), STOPPED); assertEquals(slot.status().getState(), STOPPED); assertOkResponse(resource.setState(slot.status().getVersion(), slot.getId(), "stopped"), STOPPED); assertEquals(slot.status().getState(), STOPPED); assertOkResponse(resource.setState(slot.status().getVersion(), slot.getId(), "restarting"), RUNNING); assertEquals(slot.status().getState(), RUNNING); assertOkResponse(resource.setState(slot.status().getVersion(), slot.getId(), "restarting"), RUNNING); assertEquals(slot.status().getState(), RUNNING); assertOkResponse(resource.setState(slot.status().getVersion(), slot.getId(), "killing"), STOPPED); assertEquals(slot.status().getState(), STOPPED); assertOkResponse(resource.setState(slot.status().getVersion(), slot.getId(), "killing"), STOPPED); assertEquals(slot.status().getState(), STOPPED); }
@Test public void testSetStateUnknown() { Response response = resource.setState(null, UUID.randomUUID(), "start"); assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); }
@Test public void testSetStateUnknownState() { Response response = resource.setState(null, slot.getId(), "unknown"); assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); assertNull(response.getEntity()); }
@Test(expectedExceptions = NullPointerException.class) public void testSetStateNullSlotId() { resource.setState(null, null, "start"); }
@Test(expectedExceptions = NullPointerException.class) public void testSetStateNullState() { resource.setState(null, slot.getId(), null); }
@Test public void testInvalidVersion() { try { resource.setState("invalid-version", slot.getId(), "running"); fail("Expected VersionConflictException"); } catch (VersionConflictException e) { assertEquals(e.getName(), AIRSHIP_SLOT_VERSION_HEADER); assertEquals(e.getVersion(), slot.status().getVersion()); } }