ServiceResource { @GET @Path("{type}/{pool}") @Produces(MediaType.APPLICATION_JSON) public Services getServices(@PathParam("type") String type, @PathParam("pool") String pool) { return new Services(node.getEnvironment(), union(dynamicStore.get(type, pool), staticStore.get(type, pool))); } @Inject  ServiceResource(DynamicStore dynamicStore, StaticStore staticStore, NodeInfo node); @GET @Path("{type}/{pool}") @Produces(MediaType.APPLICATION_JSON) Services getServices(@PathParam("type") String type, @PathParam("pool") String pool); @GET @Path("{type}") @Produces(MediaType.APPLICATION_JSON) Services getServices(@PathParam("type") String type); @GET @Produces(MediaType.APPLICATION_JSON) Services getServices();  }
@Test public void testGetByType() { Id<Node> redNodeId = Id.random(); DynamicServiceAnnouncement redStorage = new DynamicServiceAnnouncement(Id.<Service>random() , "storage", ImmutableMap.of("key", "1")); DynamicServiceAnnouncement redWeb = new DynamicServiceAnnouncement(Id.<Service>random(), "web", ImmutableMap.of("key", "2")); DynamicAnnouncement red = new DynamicAnnouncement("testing", "alpha", "/a/b/c", of(redStorage, redWeb)); Id<Node> greenNodeId = Id.random(); DynamicServiceAnnouncement greenStorage = new DynamicServiceAnnouncement(Id.<Service>random(), "storage", ImmutableMap.of("key", "3")); DynamicAnnouncement green = new DynamicAnnouncement("testing", "alpha", "/x/y/z", of(greenStorage)); Id<Node> blueNodeId = Id.random(); DynamicServiceAnnouncement blueStorage = new DynamicServiceAnnouncement(Id.<Service>random(), "storage", ImmutableMap.of("key", "4")); DynamicAnnouncement blue = new DynamicAnnouncement("testing", "beta", "/a/b/c", of(blueStorage)); dynamicStore.put(redNodeId, red); dynamicStore.put(greenNodeId, green); dynamicStore.put(blueNodeId, blue); assertEquals(resource.getServices("storage"), new Services("testing", of( toServiceWith(redNodeId, red.getLocation(), red.getPool()).apply(redStorage), toServiceWith(greenNodeId, green.getLocation(), green.getPool()).apply(greenStorage), toServiceWith(blueNodeId, blue.getLocation(), blue.getPool()).apply(blueStorage)))); assertEquals(resource.getServices("web"), new Services("testing", ImmutableSet.of( toServiceWith(redNodeId, red.getLocation(), red.getPool()).apply(redWeb)))); assertEquals(resource.getServices("unknown"), new Services("testing", Collections.<Service>emptySet())); }
@Test public void testGetByTypeAndPool() { Id<Node> redNodeId = Id.random(); DynamicServiceAnnouncement redStorage = new DynamicServiceAnnouncement(Id.<Service>random(), "storage", ImmutableMap.of("key", "1")); DynamicServiceAnnouncement redWeb = new DynamicServiceAnnouncement(Id.<Service>random(), "web", ImmutableMap.of("key", "2")); DynamicAnnouncement red = new DynamicAnnouncement("testing", "alpha", "/a/b/c", of(redStorage, redWeb)); Id<Node> greenNodeId = Id.random(); DynamicServiceAnnouncement greenStorage = new DynamicServiceAnnouncement(Id.<Service>random(), "storage", ImmutableMap.of("key", "3")); DynamicAnnouncement green = new DynamicAnnouncement("testing", "alpha", "/x/y/z", of(greenStorage)); Id<Node> blueNodeId = Id.random(); DynamicServiceAnnouncement blueStorage = new DynamicServiceAnnouncement(Id.<Service>random(), "storage", ImmutableMap.of("key", "4")); DynamicAnnouncement blue = new DynamicAnnouncement("testing", "beta", "/a/b/c", of(blueStorage)); dynamicStore.put(redNodeId, red); dynamicStore.put(greenNodeId, green); dynamicStore.put(blueNodeId, blue); assertEquals(resource.getServices("storage", "alpha"), new Services("testing", ImmutableSet.of( toServiceWith(redNodeId, red.getLocation(), red.getPool()).apply(redStorage), toServiceWith(greenNodeId, green.getLocation(), green.getPool()).apply(greenStorage)))); assertEquals(resource.getServices("storage", "beta"), new Services("testing", ImmutableSet.of(toServiceWith(blueNodeId, blue.getLocation(), blue.getPool()).apply(blueStorage)))); assertEquals(resource.getServices("storage", "unknown"), new Services("testing", Collections.<Service>emptySet())); }
@Test public void testGetAll() { Id<Node> redNodeId = Id.random(); DynamicServiceAnnouncement redStorage = new DynamicServiceAnnouncement(Id.<Service>random(), "storage", ImmutableMap.of("key", "1")); DynamicServiceAnnouncement redWeb = new DynamicServiceAnnouncement(Id.<Service>random(), "web", ImmutableMap.of("key", "2")); DynamicAnnouncement red = new DynamicAnnouncement("testing", "alpha", "/a/b/c", of(redStorage, redWeb)); Id<Node> greenNodeId = Id.random(); DynamicServiceAnnouncement greenStorage = new DynamicServiceAnnouncement(Id.<Service>random(), "storage", ImmutableMap.of("key", "3")); DynamicAnnouncement green = new DynamicAnnouncement("testing", "alpha", "/x/y/z", of(greenStorage)); Id<Node> blueNodeId = Id.random(); DynamicServiceAnnouncement blueStorage = new DynamicServiceAnnouncement(Id.<Service>random(), "storage", ImmutableMap.of("key", "4")); DynamicAnnouncement blue = new DynamicAnnouncement("testing", "beta", "/a/b/c", of(blueStorage)); dynamicStore.put(redNodeId, red); dynamicStore.put(greenNodeId, green); dynamicStore.put(blueNodeId, blue); assertEquals(resource.getServices(), new Services("testing", ImmutableSet.of( toServiceWith(redNodeId, red.getLocation(), red.getPool()).apply(redStorage), toServiceWith(redNodeId, red.getLocation(), red.getPool()).apply(redWeb), toServiceWith(greenNodeId, green.getLocation(), green.getPool()).apply(greenStorage), toServiceWith(blueNodeId, blue.getLocation(), blue.getPool()).apply(blueStorage)))); }