Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
Adds test for HttpMetricModule
Browse files Browse the repository at this point in the history
  • Loading branch information
caesar-ralf committed Aug 20, 2021
1 parent 24a3e88 commit d8b9431
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 21 deletions.
5 changes: 5 additions & 0 deletions modules/okhttp-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClientDecorator> clientDecorators =
i.resolve(Injector.class).getInstance(new Key<Set<ClientDecorator>>() {
});
i.resolve(Injector.class).getInstance(new Key<Set<ClientDecorator>>() {});
assertThat(clientDecorators, hasItem(instanceOf(HttpClientDecorator.class)));
}
}
Expand All @@ -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));
}

}

}
Original file line number Diff line number Diff line change
@@ -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.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Answers;

class HttpMetricModuleTest {

private SemanticMetricRegistry mockedMetricRegistry;
private IncomingRequestAwareClient mockedClient;
private Service service;

@BeforeEach
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<? extends Key<?>> 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<ClientDecorator> clientDecorators =
injector.getInstance(new Key<Set<ClientDecorator>>() {});

// then
assertThat(clientDecorators, hasItem(instanceOf(HttpMetricClientDecorator.class)));
}
}
}

0 comments on commit d8b9431

Please sign in to comment.