CoordinatorSlotResource { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response install( AssignmentRepresentation assignmentRepresentation, @DefaultValue("1") @QueryParam("limit") int limit, @Context UriInfo uriInfo, @HeaderParam(AIRSHIP_AGENTS_VERSION_HEADER) String expectedAgentsVersion) { Preconditions.checkNotNull(assignmentRepresentation, "assignmentRepresentation must not be null"); Preconditions.checkArgument(limit > 0, "limit must be at least 1"); Assignment assignment = assignmentRepresentation.toAssignment(); Predicate<AgentStatus> agentFilter = AgentFilterBuilder.build(uriInfo, transform(coordinator.getAgents(), idGetter()), transform(coordinator.getAllSlotStatus(), uuidGetter()), false, repository); List<AgentStatus> agents = coordinator.getAgents(agentFilter); checkAgentsVersion(expectedAgentsVersion, agents); List<SlotStatus> slots = coordinator.install(agentFilter, limit, assignment); return Response.ok(transform(slots, fromSlotStatus(coordinator.getAllSlotStatus(), repository))) .header(AIRSHIP_SLOTS_VERSION_HEADER, createSlotsVersion(slots)) .build(); } @Inject  CoordinatorSlotResource(Coordinator coordinator, Repository repository); @GET @Produces(MediaType.APPLICATION_JSON) Response getAllSlots(@Context UriInfo uriInfo); @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response install(
            AssignmentRepresentation assignmentRepresentation,
            @DefaultValue("1") @QueryParam("limit") int limit,
            @Context UriInfo uriInfo,
            @HeaderParam(AIRSHIP_AGENTS_VERSION_HEADER) String expectedAgentsVersion); @DELETE @Produces(MediaType.APPLICATION_JSON) Response terminateSlots(@Context UriInfo uriInfo,
            @HeaderParam(AIRSHIP_SLOTS_VERSION_HEADER) String expectedSlotsVersion); static final int MIN_PREFIX_SIZE; }
@Test public void testInstallWithinResourceLimit() { provisioner.addAgent(UUID.randomUUID().toString(), URI.create("fake: coordinator.updateAllAgentsAndWait(); UriInfo uriInfo = MockUriInfo.from("http: Response response = resource.install(AssignmentRepresentation.from(APPLE_ASSIGNMENT), 1, uriInfo, null); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Collection<SlotStatusRepresentation> slots = (Collection<SlotStatusRepresentation>) response.getEntity(); assertEquals(slots.size(), 1); for (SlotStatusRepresentation slotRepresentation : slots) { assertAppleSlot(slotRepresentation); } assertNull(response.getMetadata().get("Content-Type")); }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "No agents have the available resources to run the specified binary and configuration.") public void testInstallNotEnoughResources() { provisioner.addAgent(UUID.randomUUID().toString(), URI.create("fake: coordinator.updateAllAgentsAndWait(); UriInfo uriInfo = MockUriInfo.from("http: Response response = resource.install(AssignmentRepresentation.from(APPLE_ASSIGNMENT), 1, uriInfo, null); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); response.getEntity(); }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "No agents have the available resources to run the specified binary and configuration.") public void testInstallResourcesConsumed() { provisioner.addAgent(UUID.randomUUID().toString(), URI.create("fake: coordinator.updateAllAgentsAndWait(); UriInfo uriInfo = MockUriInfo.from("http: Response response = resource.install(AssignmentRepresentation.from(APPLE_ASSIGNMENT), 1, uriInfo, null); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Collection<SlotStatusRepresentation> slots = (Collection<SlotStatusRepresentation>) response.getEntity(); assertEquals(slots.size(), 1); assertAppleSlot(Iterables.get(slots, 0)); response = resource.install(AssignmentRepresentation.from(BANANA_ASSIGNMENT), 1, uriInfo, null); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); response.getEntity(); }