ApolloEnvironmentModule extends AbstractApolloModule { public static ApolloEnvironment environment(Service.Instance instance) { return instance.resolve(ApolloEnvironment.class); }  static ApolloEnvironmentModule create(); static ApolloEnvironment environment(Service.Instance instance); @Override String getId();  }
@Test public void shouldHandleAppInit() throws Exception { final AtomicBoolean init = new AtomicBoolean(); final AtomicBoolean destroy = new AtomicBoolean(); try (Service.Instance i = service.build().start()) { final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i); final RequestHandler handler = environment.initialize( env -> { init.set(true); env.closer().register(() -> destroy.set(true)); }); assertNotNull(handler); } catch (IOException e) { fail(e.getMessage()); } assertTrue(init.get()); assertTrue(destroy.get()); }
@Test public void shouldGetFunctioningEnvironment() throws Exception { final AtomicReference<Environment> envReference = new AtomicReference<>(); try (Service.Instance i = service.build().start()) { final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i); final RequestHandler handler = environment.initialize(new EnvApp(envReference::set)); assertNotNull(handler); final Environment e = envReference.get(); validateEnv(e); } catch (IOException e) { fail(e.getMessage()); } }
@Test public void shouldGetFunctioningEnvironmentInAppInit() throws Exception { final AtomicReference<Environment> envReference = new AtomicReference<>(); try (Service.Instance i = service.build().start()) { final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i); final RequestHandler handler = environment.initialize(envReference::set); assertNotNull(handler); final Environment e = envReference.get(); validateEnv(e); } catch (IOException e) { fail(e.getMessage()); } }
@Test public void shouldSetUpConfig() throws Exception { final AtomicReference<Environment> envReference = new AtomicReference<>(); final Map<String, String> env = ImmutableMap.of("APOLLO_APOLLO_DOMAIN", "my-domain"); try (Service.Instance i = service.build().start(ZERO_ARGS, env)) { final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i); final RequestHandler handler = environment.initialize(new EnvApp(envReference::set)); assertNotNull(handler); final Environment e = envReference.get(); assertNotNull(e); assertNotNull(e.config()); assertEquals("baz", e.config().getString("bar")); assertEquals("my-domain", e.domain()); } catch (IOException e) { fail(e.getMessage()); } }
@Test public void shouldUseRequestRunnableFactoryDecorator() throws Exception { final AtomicInteger counter = new AtomicInteger(); final Service service = this.service .withModule(new RequestInspectingModule("http: .build(); try (Service.Instance i = service.start()) { final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i); final RequestHandler handler = environment.initialize( env -> { }); assertNotNull(handler); final OngoingRequest ongoingRequest = ongoingRequest("http: handler.handle(ongoingRequest); assertEquals(1, counter.get()); handler.handle(ongoingRequest); assertEquals(2, counter.get()); } catch (IOException e) { fail(e.getMessage()); } }
@Test public void shouldUseEndpointRunnableFactoryDecorator() throws Exception { AtomicReference<String> lastResponseRef = new AtomicReference<>(); final Service service = this.service .withModule(new LastResponseModule(lastResponseRef)) .build(); try (Service.Instance i = service.start()) { final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i); final RequestHandler handler = environment.initialize( env -> { env.routingEngine() .registerAutoRoute(Route.sync("GET", "/", ctx -> "hello")); }); assertNotNull(handler); final FakeOngoingRequest ongoingRequest = ongoingRequest("http: handler.handle(ongoingRequest); assertThat(ongoingRequest.getReply(), hasStatus(Status.GONE)); assertEquals("hello", lastResponseRef.get()); } catch (IOException e) { fail(e.getMessage()); } }