StaticAnnouncementResource { @POST @Consumes("application/json") @Produces("application/json") public Response post(StaticAnnouncement announcement, @Context UriInfo uriInfo) { if (!nodeInfo.getEnvironment().equals(announcement.getEnvironment())) { return Response.status(BAD_REQUEST) .entity(format("Environment mismatch. Expected: %s, Provided: %s", nodeInfo.getEnvironment(), announcement.getEnvironment())) .build(); } Id<Service> id = Id.random(); String location = firstNonNull(announcement.getLocation(), "/somewhere/" + id); Service service = Service.copyOf(announcement) .setId(id) .setLocation(location) .build(); store.put(service); URI uri = UriBuilder.fromUri(uriInfo.getBaseUri()).path(StaticAnnouncementResource.class).path("{id}").build(id); return Response.created(uri).entity(service).build(); } @Inject  StaticAnnouncementResource(StaticStore store, NodeInfo nodeInfo); @POST @Consumes("application/json") @Produces("application/json") Response post(StaticAnnouncement announcement, @Context UriInfo uriInfo); @GET @Produces("application/json") Services get(); @DELETE @Path("{id}") void delete(@PathParam("id") Id<Service> id);  }
@Test public void testPost() { StaticAnnouncement announcement = new StaticAnnouncement("testing", "storage", "alpha", "/a/b", ImmutableMap.of("http", "http: Response response = resource.post(announcement, new MockUriInfo(URI.create("http: assertNotNull(response); assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); assertEquals(store.getAll().size(), 1); Service service = store.getAll().iterator().next(); assertNotNull(service.getId()); assertNull(service.getNodeId()); assertEquals(service.getLocation(), announcement.getLocation()); assertEquals(service.getType(), announcement.getType()); assertEquals(service.getPool(), announcement.getPool()); assertEquals(service.getProperties(), announcement.getProperties()); }
@Test public void testEnvironmentConflict() { StaticAnnouncement announcement = new StaticAnnouncement("production", "storage", "alpha", "/a/b/c", ImmutableMap.of("http", "http: Response response = resource.post(announcement, new MockUriInfo(URI.create("http: assertNotNull(response); assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); assertTrue(store.getAll().isEmpty()); }
@Test public void testMakesUpLocation() { StaticAnnouncement announcement = new StaticAnnouncement("testing", "storage", "alpha", null, ImmutableMap.of("http", "http: Response response = resource.post(announcement, new MockUriInfo(URI.create("http: assertNotNull(response); assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); assertEquals(store.getAll().size(), 1); Service service = store.getAll().iterator().next(); assertEquals(service.getId(), service.getId()); assertNotNull(service.getLocation()); }