DynamicAnnouncementResource { @PUT @Consumes(MediaType.APPLICATION_JSON) public Response put(@PathParam("node_id") Id<Node> nodeId, @Context UriInfo uriInfo, DynamicAnnouncement announcement) { if (!nodeInfo.getEnvironment().equals(announcement.getEnvironment())) { return Response.status(BAD_REQUEST) .entity(format("Environment mismatch. Expected: %s, Provided: %s", nodeInfo.getEnvironment(), announcement.getEnvironment())) .build(); } String location = firstNonNull(announcement.getLocation(), "/somewhere/" + nodeId.toString()); DynamicAnnouncement announcementWithLocation = DynamicAnnouncement.copyOf(announcement) .setLocation(location) .build(); dynamicStore.put(nodeId, announcementWithLocation); return Response.status(ACCEPTED).build(); } @Inject  DynamicAnnouncementResource(DynamicStore dynamicStore, NodeInfo nodeInfo); @PUT @Consumes(MediaType.APPLICATION_JSON) Response put(@PathParam("node_id") Id<Node> nodeId, @Context UriInfo uriInfo, DynamicAnnouncement announcement); @DELETE Response delete(@PathParam("node_id") Id<Node> nodeId);  }
@Test public void testPutNew() { DynamicAnnouncement announcement = new DynamicAnnouncement("testing", "alpha", "/a/b/c", ImmutableSet.of( new DynamicServiceAnnouncement(Id.<Service>random(), "storage", ImmutableMap.of("http", "http: ); Id<Node> nodeId = Id.random(); Response response = resource.put(nodeId, new MockUriInfo(URI.create("http: assertNotNull(response); assertEquals(response.getStatus(), Response.Status.ACCEPTED.getStatusCode()); assertEqualsIgnoreOrder(store.getAll(), transform(announcement.getServiceAnnouncements(), toServiceWith(nodeId, announcement.getLocation(), announcement.getPool()))); }
@Test public void testReplace() { Id<Node> nodeId = Id.random(); DynamicAnnouncement previous = new DynamicAnnouncement("testing", "alpha", "/a/b/c", ImmutableSet.of( new DynamicServiceAnnouncement(Id.<Service>random(), "storage", ImmutableMap.of("key", "existing")) )); store.put(nodeId, previous); DynamicAnnouncement announcement = new DynamicAnnouncement("testing", "alpha", "/a/b/c", ImmutableSet.of( new DynamicServiceAnnouncement(Id.<Service>random(), "storage", ImmutableMap.of("key", "new"))) ); Response response = resource.put(nodeId, new MockUriInfo(URI.create("http: assertNotNull(response); assertEquals(response.getStatus(), Response.Status.ACCEPTED.getStatusCode()); assertEqualsIgnoreOrder(store.getAll(), transform(announcement.getServiceAnnouncements(), toServiceWith(nodeId, announcement.getLocation(), announcement.getPool()))); }
@Test public void testEnvironmentConflict() { DynamicAnnouncement announcement = new DynamicAnnouncement("production", "alpha", "/a/b/c", ImmutableSet.of( new DynamicServiceAnnouncement(Id.<Service>random(), "storage", ImmutableMap.of("http", "http: ); Id<Node> nodeId = Id.random(); Response response = resource.put(nodeId, new MockUriInfo(URI.create("http: assertNotNull(response); assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); assertTrue(store.getAll().isEmpty()); }
@Test public void testMakesUpLocation() { DynamicAnnouncement announcement = new DynamicAnnouncement("testing", "alpha", null, ImmutableSet.of( new DynamicServiceAnnouncement(Id.<Service>random(), "storage", ImmutableMap.of("http", "http: ); Id<Node> nodeId = Id.valueOf("test123"); Response response = resource.put(nodeId, new MockUriInfo(URI.create("http: assertNotNull(response); assertEquals(response.getStatus(), Response.Status.ACCEPTED.getStatusCode()); assertEquals(store.getAll().size(), 1); Service service = store.getAll().iterator().next(); assertEquals(service.getId(), service.getId()); assertNotNull(service.getLocation()); }