From adac7705c9832d68422957388fec84935e757652 Mon Sep 17 00:00:00 2001 From: Caesar Ralf Franz Hoppen Date: Fri, 20 Aug 2021 15:13:38 +0200 Subject: [PATCH] Adds test for HttpMetricModule --- .../http/client/HttpClientModuleTest.java | 33 +++---- .../http/client/HttpMetricModuleTest.java | 98 +++++++++++++++++++ 2 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 modules/okhttp-client/src/test/java/com/spotify/apollo/http/client/HttpMetricModuleTest.java diff --git a/modules/okhttp-client/src/test/java/com/spotify/apollo/http/client/HttpClientModuleTest.java b/modules/okhttp-client/src/test/java/com/spotify/apollo/http/client/HttpClientModuleTest.java index 70b504dc7..477755766 100644 --- a/modules/okhttp-client/src/test/java/com/spotify/apollo/http/client/HttpClientModuleTest.java +++ b/modules/okhttp-client/src/test/java/com/spotify/apollo/http/client/HttpClientModuleTest.java @@ -19,39 +19,33 @@ */ package com.spotify.apollo.http.client; -import com.spotify.apollo.core.Service; -import com.spotify.apollo.core.Services; -import com.spotify.apollo.environment.ClientDecorator; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; import com.google.inject.Injector; import com.google.inject.Key; +import com.spotify.apollo.core.Service; +import com.spotify.apollo.core.Services; +import com.spotify.apollo.environment.ClientDecorator; import com.squareup.okhttp.OkHttpClient; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; - -import org.junit.Test; - import java.util.Set; - -import static org.hamcrest.CoreMatchers.hasItem; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; +import org.junit.Test; public class HttpClientModuleTest { Service service() { - return Services.usingName("ping") - .withModule(HttpClientModule.create()) - .build(); + return Services.usingName("ping").withModule(HttpClientModule.create()).build(); } @Test public void testShouldInsertHttpClientDecorator() throws Exception { try (Service.Instance i = service().start()) { final Set clientDecorators = - i.resolve(Injector.class).getInstance(new Key>() { - }); + i.resolve(Injector.class).getInstance(new Key>() {}); assertThat(clientDecorators, hasItem(instanceOf(HttpClientDecorator.class))); } } @@ -65,16 +59,13 @@ public void testShouldInsertHttpClientDecorator() throws Exception { public void testOkHttpClientIsConfigured() throws Exception { final Config config = ConfigFactory.parseString("http.client.connectTimeout: 7982"); - final Service service = Services.usingName("test") - .withModule(HttpClientModule.create()) - .build(); + final Service service = + Services.usingName("test").withModule(HttpClientModule.create()).build(); try (Service.Instance i = service.start(new String[] {}, config)) { final OkHttpClient underlying = i.resolve(OkHttpClient.class); assertThat(underlying.getConnectTimeout(), is(7982)); } - } - } diff --git a/modules/okhttp-client/src/test/java/com/spotify/apollo/http/client/HttpMetricModuleTest.java b/modules/okhttp-client/src/test/java/com/spotify/apollo/http/client/HttpMetricModuleTest.java new file mode 100644 index 000000000..3349cb24c --- /dev/null +++ b/modules/okhttp-client/src/test/java/com/spotify/apollo/http/client/HttpMetricModuleTest.java @@ -0,0 +1,98 @@ +/* + * -\-\- + * Spotify Apollo okhttp Client Module + * -- + * Copyright (C) 2013 - 2015 Spotify AB + * -- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -/-/- + */ +package com.spotify.apollo.http.client; + +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.mock; + +import com.google.inject.Binder; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.spotify.apollo.core.Service; +import com.spotify.apollo.core.Services; +import com.spotify.apollo.environment.ClientDecorator; +import com.spotify.apollo.environment.IncomingRequestAwareClient; +import com.spotify.apollo.http.client.HttpMetricModule.HttpMetricClientDecorator; +import com.spotify.apollo.module.ApolloModule; +import com.spotify.metrics.core.SemanticMetricRegistry; +import java.io.IOException; +import java.util.Collections; +import java.util.Set; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Answers; + +class HttpMetricModuleTest { + + private SemanticMetricRegistry mockedMetricRegistry; + private IncomingRequestAwareClient mockedClient; + private Service service; + + @Before + public void setup() { + service = + Services.usingName("httpMetricModuleTest") + .withModule( + new ApolloModule() { + @Override + public String getId() { + return "testing-"; + } + + @Override + public double getPriority() { + return 0; + } + + @Override + public Set> getLifecycleManaged() { + return Collections.emptySet(); + } + + @Override + public void configure(Binder binder) { + mockedMetricRegistry = mock(SemanticMetricRegistry.class); + binder.bind(SemanticMetricRegistry.class).toInstance(mockedMetricRegistry); + mockedClient = + mock(IncomingRequestAwareClient.class, Answers.CALLS_REAL_METHODS); + binder.bind(IncomingRequestAwareClient.class).toInstance(mockedClient); + } + }) + .withModule(HttpMetricModule.create()) + .build(); + } + + @Test + public void testShouldHaveDecoratorConfigured() throws IOException { + // given + try (Service.Instance i = service.start()) { + final Injector injector = i.resolve(Injector.class); + + // when + final Set clientDecorators = + injector.getInstance(new Key>() {}); + + // then + assertThat(clientDecorators, hasItem(instanceOf(HttpMetricClientDecorator.class))); + } + } +}