@Test public void testInstallInvalidVersion() { try { resource.installSlot("invalid-version", InstallationRepresentation.from(APPLE_INSTALLATION), uriInfo); fail("Expected WebApplicationException"); } catch (VersionConflictException e) { assertEquals(e.getName(), AIRSHIP_AGENT_VERSION_HEADER); assertEquals(e.getVersion(), agent.getAgentStatus().getVersion()); } }
public static InstallationRepresentation from(Installation installation) { Assignment assignment = installation.getAssignment(); return new InstallationRepresentation( installation.getShortName(), AssignmentRepresentation.from(assignment), installation.getBinaryFile().toString(), installation.getConfigFile().toString(), installation.getResources()); }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response installSlot(@HeaderParam(AIRSHIP_AGENT_VERSION_HEADER) String agentVersion, InstallationRepresentation installation, @Context UriInfo uriInfo) { Preconditions.checkNotNull(installation, "installation must not be null"); checkAgentVersion(agent.getAgentStatus(), agentVersion); SlotStatus slotStatus = agent.install(installation.toInstallation()); return Response .created(getSelfUri(slotStatus.getId(), uriInfo.getBaseUri())) .entity(SlotStatusRepresentation.from(slotStatus)) .header(AIRSHIP_AGENT_VERSION_HEADER, agent.getAgentStatus().getVersion()) .header(AIRSHIP_SLOT_VERSION_HEADER, slotStatus.getVersion()) .build(); }
@Test public void testInstallInvalidVersion() { try { resource.installSlot("invalid-version", InstallationRepresentation.from(APPLE_INSTALLATION), uriInfo); fail("Expected WebApplicationException"); } catch (VersionConflictException e) { assertEquals(e.getName(), AIRSHIP_AGENT_VERSION_HEADER); assertEquals(e.getVersion(), agent.getAgentStatus().getVersion()); } }
@PUT @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response assign(@HeaderParam(AIRSHIP_AGENT_VERSION_HEADER) String agentVersion, @HeaderParam(AIRSHIP_SLOT_VERSION_HEADER) String slotVersion, @PathParam("slotId") UUID slotId, InstallationRepresentation installation) { Preconditions.checkNotNull(slotId, "slotId must not be null"); Preconditions.checkNotNull(installation, "installation must not be null"); Slot slot = agent.getSlot(slotId); if (slot == null) { return Response.status(Response.Status.NOT_FOUND).build(); } checkAgentVersion(agent.getAgentStatus(), agentVersion); checkSlotVersion(slot.status(), slotVersion); SlotStatus status = slot.assign(installation.toInstallation()); return Response.ok(SlotStatusRepresentation.from(status)) .header(AIRSHIP_AGENT_VERSION_HEADER, agent.getAgentStatus().getVersion()) .header(AIRSHIP_SLOT_VERSION_HEADER, status.getVersion()) .build(); } }
public void assertInstallSlot(String agentVersion) { Response response = resource.installSlot(agentVersion, InstallationRepresentation.from(APPLE_INSTALLATION), uriInfo); // find the new slot Slot slot = agent.getAllSlots().iterator().next(); assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); assertEquals(response.getMetadata().getFirst(HttpHeaders.LOCATION), URI.create("http://localhost/v1/agent/slot/" + slot.getId().toString())); SlotStatus status = slot.status(); SlotStatus expectedStatus = status.changeAssignment(STOPPED, APPLE_ASSIGNMENT, status.getResources()); assertEquals(response.getEntity(), SlotStatusRepresentation.from(expectedStatus)); assertEquals(response.getMetadata().get(AIRSHIP_AGENT_VERSION_HEADER).get(0), agent.getAgentStatus().getVersion()); assertEquals(response.getMetadata().get(AIRSHIP_SLOT_VERSION_HEADER).get(0), expectedStatus.getVersion()); assertNull(response.getMetadata().get("Content-Type")); // content type is set by jersey based on @Produces }
public void assertInstallSlot(String agentVersion) { Response response = resource.installSlot(agentVersion, InstallationRepresentation.from(APPLE_INSTALLATION), uriInfo); // find the new slot Slot slot = agent.getAllSlots().iterator().next(); assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); assertEquals(response.getMetadata().getFirst(HttpHeaders.LOCATION), URI.create("http://localhost/v1/agent/slot/" + slot.getId().toString())); SlotStatus status = slot.status(); SlotStatus expectedStatus = status.changeAssignment(STOPPED, APPLE_ASSIGNMENT, status.getResources()); assertEquals(response.getEntity(), SlotStatusRepresentation.from(expectedStatus)); assertEquals(response.getMetadata().get(AIRSHIP_AGENT_VERSION_HEADER).get(0), agent.getAgentStatus().getVersion()); assertEquals(response.getMetadata().get(AIRSHIP_SLOT_VERSION_HEADER).get(0), expectedStatus.getVersion()); assertNull(response.getMetadata().get("Content-Type")); // content type is set by jersey based on @Produces }
@Override public SlotStatus install(Installation installation) { Preconditions.checkNotNull(installation, "installation is null"); AgentStatus agentStatus = status(); URI internalUri = agentStatus.getInternalUri(); Preconditions.checkState(internalUri != null, "agent is down"); try { Request request = Request.Builder.preparePost() .setUri(uriBuilderFrom(internalUri).replacePath("/v1/agent/slot/").build()) .setHeader(CONTENT_TYPE, APPLICATION_JSON) .setHeader(AIRSHIP_AGENT_VERSION_HEADER, status().getVersion()) .setBodyGenerator(jsonBodyGenerator(installationCodec, InstallationRepresentation.from(installation))) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.CREATED.getStatusCode())); SlotStatus slotStatus = slotStatusRepresentation.toSlotStatus(agentStatus.getInstanceId()); setStatus(agentStatus.changeSlotStatus(slotStatus)); return slotStatus; } catch (Exception e) { throw Throwables.propagate(e); } } }
@Test public void testAssign() throws Exception { SlotStatus slotStatus = agent.install(appleInstallation); String json = installationCodec.toJson(InstallationRepresentation.from(appleInstallation)); Request request = Request.Builder.preparePut() .setUri(urlFor(slotStatus, "assignment")) .setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON) .setBodyGenerator(createStaticBodyGenerator(json, UTF_8)) .build(); Map<String, Object> response = client.execute(request, createJsonResponseHandler(mapCodec, Status.OK.getStatusCode())); Map<String, Object> expected = ImmutableMap.<String, Object>builder() .put("id", slotStatus.getId().toString()) .put("shortId", slotStatus.getId().toString()) .put("binary", appleInstallation.getAssignment().getBinary()) .put("shortBinary", appleInstallation.getAssignment().getBinary()) .put("config", appleInstallation.getAssignment().getConfig()) .put("shortConfig", appleInstallation.getAssignment().getConfig()) .put("self", urlFor(slotStatus).toASCIIString()) .put("externalUri", urlFor(slotStatus).toASCIIString()) .put("location", slotStatus.getLocation()) .put("shortLocation", slotStatus.getLocation()) .put("status", STOPPED.toString()) .put("version", slotStatus.getVersion()) .put("installPath", slotStatus.getInstallPath()) .put("resources", ImmutableMap.<String, Integer>of("memory", 512)) .build(); assertEquals(response, expected); }
@Test public void testAssign() throws Exception { SlotStatus slotStatus = agent.install(appleInstallation); String json = installationCodec.toJson(InstallationRepresentation.from(appleInstallation)); Request request = Request.Builder.preparePut() .setUri(urlFor(slotStatus, "assignment")) .setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON) .setBodyGenerator(createStaticBodyGenerator(json, UTF_8)) .build(); Map<String, Object> response = client.execute(request, createJsonResponseHandler(mapCodec, Status.OK.getStatusCode())); Map<String, Object> expected = ImmutableMap.<String, Object>builder() .put("id", slotStatus.getId().toString()) .put("shortId", slotStatus.getId().toString()) .put("binary", appleInstallation.getAssignment().getBinary()) .put("shortBinary", appleInstallation.getAssignment().getBinary()) .put("config", appleInstallation.getAssignment().getConfig()) .put("shortConfig", appleInstallation.getAssignment().getConfig()) .put("self", urlFor(slotStatus).toASCIIString()) .put("externalUri", urlFor(slotStatus).toASCIIString()) .put("location", slotStatus.getLocation()) .put("shortLocation", slotStatus.getLocation()) .put("status", STOPPED.toString()) .put("version", slotStatus.getVersion()) .put("installPath", slotStatus.getInstallPath()) .put("resources", ImmutableMap.<String, Integer>of("memory", 512)) .build(); assertEquals(response, expected); }
.setUri(urlFor("/v1/agent/slot")) .setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON) .setBodyGenerator(JsonBodyGenerator.jsonBodyGenerator(installationCodec, InstallationRepresentation.from(appleInstallation))) .build(); JsonResponse<Map<String, Object>> response = client.execute(request, createFullJsonResponseHandler(mapCodec));
.setUri(urlFor("/v1/agent/slot")) .setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON) .setBodyGenerator(JsonBodyGenerator.jsonBodyGenerator(installationCodec, InstallationRepresentation.from(appleInstallation))) .build(); JsonResponse<Map<String, Object>> response = client.execute(request, createFullJsonResponseHandler(mapCodec));
@Override public SlotStatus assign(Installation installation) { try { Request request = Request.Builder.preparePut() .setUri(uriBuilderFrom(slotStatus.getSelf()).appendPath("assignment").build()) .setHeader(CONTENT_TYPE, APPLICATION_JSON) .setHeader(AIRSHIP_AGENT_VERSION_HEADER, agent.status().getVersion()) .setHeader(AIRSHIP_SLOT_VERSION_HEADER, slotStatus.getVersion()) .setBodyGenerator(jsonBodyGenerator(installationCodec, InstallationRepresentation.from(installation))) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.OK.getStatusCode())); updateStatus(slotStatusRepresentation.toSlotStatus(slotStatus.getInstanceId())); return slotStatus; } catch (Exception e) { log.error(e); return setErrorStatus(e.getMessage()); } }