### Question:
ServiceProvision { public static <T> Map<String, T> loadServices(Configuration configuration, Environment environment, String key, Class<? extends T> serviceClass) { return configuration.get(key, JsonNode.class) .<Map<String, T>>map(node -> servicesMap(node, environment, serviceClass)) .orElse(emptyMap()); } private  ServiceProvision(); static Optional<E> loadLoadBalancer(
            Configuration configuration,
            Environment environment,
            String key,
            Class<? extends E> serviceClass,
            ActiveOrigins activeOrigins); static Optional<E> loadRetryPolicy(
            Configuration configuration,
            Environment environment,
            String key,
            Class<? extends E> serviceClass); static Map<String, T> loadServices(Configuration configuration, Environment environment, String key, Class<? extends T> serviceClass);  }### Answer:
@Test public void servicesReturnCorrectlyFromCall() { Map<String, String> services = loadServices(environment.configuration(), environment, "multi", String.class); assertThat(services, isMap(ImmutableMap.of( "one", "valueNumber1", "two", "valueNumber2", "three", "valueNumber3" ))); }
@Test public void isInstanceWorks() { Environment env = environmentWithConfig(yamlForServices); Map<String, StyxService> services = loadServices(env.configuration(), env, "multi", StyxService.class); assertThat(services.get("backendProvider"), instanceOf(BackendServiceProvider.class)); assertThat(services.get("routingProvider"), instanceOf(RoutingObjectProvider.class)); }
@Test public void loadsNewConfigurationFormat() { Environment env = environmentWithConfig(yamlForServiceFactories); Map<String, StyxService> services = loadServices(env.configuration(), env, "multi", StyxService.class); assertThat(services.get("backendProvider"), instanceOf(BackendServiceProvider.class)); assertThat(services.get("routingProvider"), instanceOf(RoutingObjectProvider.class)); }
@Test public void loadsFromMixedConfigFormat() { Environment env = environmentWithConfig(yamlForMixedServiceFactories); Map<String, StyxService> services = loadServices(env.configuration(), env, "multi", StyxService.class); assertThat(services.get("backendProvider"), instanceOf(BackendServiceProvider.class)); assertThat(services.get("routingProvider"), instanceOf(RoutingObjectProvider.class)); }
@Test public void ignoresDisabledServices() { Environment env = environmentWithConfig(mixedDisabledServices); Map<String, StyxService> services = loadServices(env.configuration(), env, "multi", StyxService.class); assertThat(services.isEmpty(), is(true)); }
@Test public void servicesReturnEmptyWhenFactoryKeyDoesNotExist() { Map<String, String> services = loadServices(environment.configuration(), environment, "invalid.key", String.class); assertThat(services, isMap(emptyMap())); }
@Test public void throwsExceptionForInvalidServiceFactoryConfig() { String config = "" + "multi:\n" + " factories:\n" + " backendProvider:\n" + " enabled: false\n" + " config:\n" + " stringValue: valueNumber1\n"; Environment env = environmentWithConfig(config); Exception e = assertThrows(ConfigurationException.class, () -> loadServices(env.configuration(), env, "multi", StyxService.class)); assertThat(e.getMessage(), matchesPattern("Unexpected configuration object 'services.factories.backendProvider', Configuration.*'")); }
@Test public void throwsExceptionForInvalidSpiExtensionFactory() { String config = "" + "multi:\n" + " factories:\n" + " backendProvider:\n" + " enabled: false\n" + " factory:\n" + " classPath: /path/to/jar\n" + " config:\n" + " attribute: x\n"; Environment env = environmentWithConfig(config); Exception e = assertThrows(ConfigurationException.class, () -> loadServices(env.configuration(), env, "multi", StyxService.class)); assertThat(e.getMessage(), matchesPattern("Unexpected configuration object 'services.factories.backendProvider', Configuration.*'")); }