Skip to content

Commit 1058c53

Browse files
test(web): add Retrofit2ServiceFactoryTest to test the creation of retrofit2 client using Retrofit2ServiceFactory (#1221)
1 parent b4097e5 commit 1058c53

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

kork-retrofit2/kork-retrofit2.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ dependencies {
99
implementation "com.squareup.okhttp3:logging-interceptor"
1010
implementation "io.zipkin.brave:brave-instrumentation-okhttp3"
1111

12+
testImplementation project(":kork-retrofit")
1213
testImplementation "org.springframework.boot:spring-boot-starter-test"
1314
testRuntimeOnly "cglib:cglib-nodep"
1415
testRuntimeOnly "org.objenesis:objenesis"
1516

1617
testImplementation "com.squareup.okhttp3:mockwebserver"
1718
testImplementation "com.squareup.retrofit2:retrofit-mock"
19+
testImplementation "com.github.tomakehurst:wiremock-jre8"
1820

1921
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* Copyright 2025 OpsMx, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.netflix.spinnaker.kork.retrofit;
18+
19+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
20+
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
21+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
22+
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
23+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
24+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
25+
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
28+
import com.fasterxml.jackson.databind.ObjectMapper;
29+
import com.github.tomakehurst.wiremock.WireMockServer;
30+
import com.github.tomakehurst.wiremock.client.WireMock;
31+
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
32+
import com.netflix.spinnaker.config.DefaultServiceClientProvider;
33+
import com.netflix.spinnaker.config.DefaultServiceEndpoint;
34+
import com.netflix.spinnaker.config.ServiceEndpoint;
35+
import com.netflix.spinnaker.config.okhttp3.DefaultOkHttpClientBuilderProvider;
36+
import com.netflix.spinnaker.config.okhttp3.OkHttpClientProvider;
37+
import com.netflix.spinnaker.kork.client.ServiceClientFactory;
38+
import com.netflix.spinnaker.kork.client.ServiceClientProvider;
39+
import com.netflix.spinnaker.okhttp.OkHttpClientConfigurationProperties;
40+
import java.util.List;
41+
import java.util.Map;
42+
import okhttp3.Interceptor;
43+
import okhttp3.OkHttpClient;
44+
import okhttp3.Request;
45+
import org.junit.jupiter.api.AfterAll;
46+
import org.junit.jupiter.api.BeforeAll;
47+
import org.junit.jupiter.api.Test;
48+
import org.springframework.beans.factory.annotation.Autowired;
49+
import org.springframework.boot.test.context.SpringBootTest;
50+
import org.springframework.context.annotation.Bean;
51+
import org.springframework.context.annotation.Configuration;
52+
import retrofit2.Call;
53+
import retrofit2.http.GET;
54+
55+
@SpringBootTest(
56+
webEnvironment = SpringBootTest.WebEnvironment.NONE,
57+
classes = {
58+
OkHttpClient.class,
59+
OkHttpClientConfigurationProperties.class,
60+
OkHttpClientProvider.class,
61+
Retrofit2ServiceFactoryAutoConfiguration.class,
62+
Retrofit2ServiceFactoryTest.Retrofit2TestConfig.class,
63+
DefaultOkHttpClientBuilderProvider.class
64+
})
65+
public class Retrofit2ServiceFactoryTest {
66+
67+
@Autowired ServiceClientProvider serviceClientProvider;
68+
69+
static int port;
70+
static WireMockServer wireMockServer;
71+
72+
@BeforeAll
73+
static void setUp() {
74+
wireMockServer = new WireMockServer(WireMockConfiguration.options().dynamicPort());
75+
wireMockServer.start();
76+
port = wireMockServer.port();
77+
WireMock.configureFor("localhost", port);
78+
}
79+
80+
@AfterAll
81+
static void tearDown() {
82+
wireMockServer.stop();
83+
}
84+
85+
@Test
86+
void testRetrofit2Client() {
87+
stubFor(
88+
get(urlEqualTo("/test"))
89+
.willReturn(
90+
aResponse()
91+
.withHeader("Content-Type", "application/json")
92+
.withBody("{\"message\": \"success\", \"code\": 200}")));
93+
94+
ServiceEndpoint serviceEndpoint =
95+
new DefaultServiceEndpoint("retrofit2service", "http://localhost:" + port);
96+
Retrofit2TestService retrofit2TestService =
97+
serviceClientProvider.getService(Retrofit2TestService.class, serviceEndpoint);
98+
Map<String, String> response = Retrofit2SyncCall.execute(retrofit2TestService.getSomething());
99+
100+
assertEquals(response.get("message"), "success");
101+
}
102+
103+
@Test
104+
void testRetrofit2Client_withInterceptor() {
105+
106+
Interceptor interceptor =
107+
chain -> {
108+
Request originalRequest = chain.request();
109+
Request modifiedRequest =
110+
originalRequest.newBuilder().header("Authorization", "Bearer my-token").build();
111+
return chain.proceed(modifiedRequest);
112+
};
113+
114+
stubFor(
115+
get(urlEqualTo("/test"))
116+
.willReturn(
117+
aResponse()
118+
.withHeader("Content-Type", "application/json")
119+
.withBody("{\"message\": \"success\", \"code\": 200}")));
120+
121+
ServiceEndpoint serviceEndpoint =
122+
new DefaultServiceEndpoint("retrofit2service", "http://localhost:" + port);
123+
Retrofit2TestService retrofit2TestService =
124+
serviceClientProvider.getService(
125+
Retrofit2TestService.class, serviceEndpoint, new ObjectMapper(), List.of(interceptor));
126+
Retrofit2SyncCall.execute(retrofit2TestService.getSomething());
127+
128+
verify(
129+
getRequestedFor(urlEqualTo("/test"))
130+
.withHeader("Authorization", equalTo("Bearer my-token")));
131+
}
132+
133+
@Configuration
134+
public static class Retrofit2TestConfig {
135+
136+
@Bean
137+
public ServiceClientProvider serviceClientProvider(
138+
List<ServiceClientFactory> serviceClientFactories) {
139+
return new DefaultServiceClientProvider(serviceClientFactories, new ObjectMapper());
140+
}
141+
}
142+
143+
public interface Retrofit2TestService {
144+
145+
@GET("test")
146+
Call<Map<String, String>> getSomething();
147+
}
148+
}

0 commit comments

Comments
 (0)