### Question:
DefaultServiceFetcher { public Multimap<String, String> getServicesForContainer(String container, String host) { if (containerConfig == null) { return ImmutableMultimap.<String, String>builder().build(); } LinkedHashMultimap<String, String> endpointServices = LinkedHashMultimap.create(); @SuppressWarnings("unchecked") Map<String, Object> declaredServices = (Map<String, Object>) containerConfig.getMap(container, GADGETS_FEATURES_CONFIG).get(OSAPI_SERVICES); if (declaredServices != null) { for (Map.Entry<String, Object> entry : declaredServices.entrySet()) { @SuppressWarnings("unchecked") Iterable<String> entryValue = (Iterable<String>) entry.getValue(); endpointServices.putAll(entry.getKey(), entryValue); } } List<String> endpoints = getEndpointsFromContainerConfig(container, host); for (String endpoint : endpoints) { endpointServices.putAll(endpoint, retrieveServices(endpoint.replace("%host%", host))); } return ImmutableMultimap.copyOf(endpointServices); } @Inject  DefaultServiceFetcher(ContainerConfig config, HttpFetcher fetcher); Multimap<String, String> getServicesForContainer(String container, String host);  }### Answer:
@Test public void testReadConfigNoEndpoints() throws Exception { JSONObject config = createConfig(); config.getJSONObject("default"). getJSONObject(DefaultServiceFetcher.GADGETS_FEATURES_CONFIG) .remove(DefaultServiceFetcher.OSAPI_FEATURE_CONFIG); JsonContainerConfig containerConfig = new JsonContainerConfig(config, Expressions.forTesting(new Functions())); fetcher = new DefaultServiceFetcher(containerConfig, mockFetcher); EasyMock.expect(mockFetcher.fetch(EasyMock.isA(HttpRequest.class))).andReturn( new HttpResponse("")).anyTimes(); replay(); Multimap<String, String> services = fetcher.getServicesForContainer("default", "dontcare"); verify(); assertEquals(configuredServices, services); }
@Test public void testReadConfigEndpointsDown() throws Exception { EasyMock.expect(mockFetcher.fetch(EasyMock.isA(HttpRequest.class))).andReturn( new HttpResponse("")).anyTimes(); replay(); Multimap<String, String> services = fetcher.getServicesForContainer("default", "dontcare"); verify(); assertEquals(configuredServices, services); }
@Test public void testReadConfigWithValidEndpoints() throws Exception { List<String> endPoint1Services = ImmutableList.of("do.something", "delete.someting"); JSONObject service1 = new JSONObject(); service1.put("result", endPoint1Services); List<String> endPoint2Services = ImmutableList.of("weather.get"); JSONObject service2 = new JSONObject(); service2.put("result", endPoint2Services); EasyMock.expect(mockFetcher.fetch(EasyMock.isA(HttpRequest.class))).andReturn( new HttpResponse(service1.toString())); EasyMock.expect(mockFetcher.fetch(EasyMock.isA(HttpRequest.class))).andReturn( new HttpResponse(service2.toString())); replay(); Multimap<String, String> services = fetcher.getServicesForContainer("default", "dontcare"); verify(); Multimap<String, String> mergedServices = LinkedHashMultimap.create(configuredServices); mergedServices.putAll(endPoint1, endPoint1Services); mergedServices.putAll(endPoint2, endPoint2Services); assertEquals(mergedServices, LinkedHashMultimap.create(services)); }
@Test public void testReadConfigBadContainer() throws Exception { Multimap<String, String> multimap = fetcher.getServicesForContainer("badcontainer", "dontcare"); assertEquals(0, multimap.size()); }