Skip to content

Commit 5b20cf7

Browse files
committed
Refactor + add tests
1 parent a54fc4c commit 5b20cf7

File tree

2 files changed

+92
-11
lines changed

2 files changed

+92
-11
lines changed

src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ public static OkHttpClient.Builder create(@Nonnull final BaseBearerTokenAuthenti
7171
@Nonnull
7272
public static OkHttpClient.Builder create(@Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider, @Nonnull final RequestOption[] requestOptions) {
7373
final GraphClientOption graphClientOption = new GraphClientOption();
74-
final Interceptor[] interceptors = createDefaultGraphInterceptors(graphClientOption, requestOptions);
74+
final List<RequestOption> requestOptionsList = new ArrayList<>(Arrays.asList(requestOptions));
75+
requestOptionsList.add(graphClientOption);
76+
final Interceptor[] interceptors = createDefaultGraphInterceptors(requestOptionsList.toArray(new RequestOption[0]));
7577
final ArrayList<Interceptor> interceptorList = new ArrayList<>(Arrays.asList(interceptors));
7678
interceptorList.add(new AuthorizationHandler(authenticationProvider));
7779
graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.AUTH_HANDLER_FLAG);
@@ -115,19 +117,17 @@ public static OkHttpClient.Builder create(@Nonnull final GraphClientOption graph
115117
*/
116118
@Nonnull
117119
public static OkHttpClient.Builder create(@Nullable final GraphClientOption graphClientOption) {
118-
return create(graphClientOption, new RequestOption[0]);
120+
return KiotaClientFactory.create(createDefaultGraphInterceptors(graphClientOption));
119121
}
120122

121123
/**
122124
* The OkHttpClient Builder with optional GraphClientOption and RequestOptions to override default graph interceptors
123-
* @param graphClientOption the GraphClientOption for use in requests.
124125
* @param requestOptions custom request options to override default graph interceptors
125126
* @return an OkHttpClient Builder instance.
126127
*/
127128
@Nonnull
128-
public static OkHttpClient.Builder create(@Nullable final GraphClientOption graphClientOption, @Nonnull final RequestOption[] requestOptions) {
129-
GraphClientOption options = graphClientOption != null ? graphClientOption : new GraphClientOption();
130-
return KiotaClientFactory.create(createDefaultGraphInterceptors(options, requestOptions));
129+
public static OkHttpClient.Builder create(@Nonnull final RequestOption[] requestOptions) {
130+
return KiotaClientFactory.create(createDefaultGraphInterceptors(requestOptions));
131131
}
132132

133133
/**
@@ -138,33 +138,43 @@ public static OkHttpClient.Builder create(@Nullable final GraphClientOption grap
138138
*/
139139
@Nonnull
140140
public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final GraphClientOption graphClientOption) {
141-
return createDefaultGraphInterceptors(graphClientOption, new RequestOption[0]);
141+
return getDefaultGraphInterceptors(new RequestOption[]{ graphClientOption }).toArray(new Interceptor[0]);
142142
}
143143

144144
/**
145145
* Creates the default Interceptors for use with Graph configured with the provided RequestOptions.
146-
* @param graphClientOption the GraphClientOption used to create the GraphTelemetryHandler with.
147146
* @param requestOptions custom request options to override default graph interceptors
148147
* @return an array of interceptors.
149148
*/
150149
@Nonnull
151-
public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final GraphClientOption graphClientOption, @Nonnull final RequestOption[] requestOptions) {
150+
public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final RequestOption[] requestOptions) {
152151
Objects.requireNonNull(requestOptions, "parameter requestOptions cannot be null");
152+
return getDefaultGraphInterceptors(requestOptions).toArray(new Interceptor[0]);
153+
}
153154

155+
/**
156+
* Creates the default Interceptors for use with Graph.
157+
* @param requestOptions custom request options to override default graph interceptors
158+
* @return a list of interceptors.
159+
*/
160+
private static List<Interceptor> getDefaultGraphInterceptors(@Nonnull final RequestOption[] requestOptions) {
161+
GraphClientOption graphClientOption = new GraphClientOption();
154162
UrlReplaceHandlerOption urlReplaceHandlerOption = new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs());
155-
156163
for (RequestOption option : requestOptions) {
157164
if (option instanceof UrlReplaceHandlerOption) {
158165
urlReplaceHandlerOption = (UrlReplaceHandlerOption) option;
159166
}
167+
if (option instanceof GraphClientOption) {
168+
graphClientOption = (GraphClientOption) option;
169+
}
160170
}
161171

162172
List<Interceptor> handlers = new ArrayList<>();
163173
handlers.add(new UrlReplaceHandler(urlReplaceHandlerOption));
164174
handlers.add(new GraphTelemetryHandler(graphClientOption));
165175
handlers.addAll(Arrays.asList(KiotaClientFactory.createDefaultInterceptors(requestOptions)));
166176
addDefaultFeatureUsages(graphClientOption);
167-
return handlers.toArray(new Interceptor[0]);
177+
return handlers;
168178
}
169179

170180
//These are the default features used by the Graph Client

src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import java.io.IOException;
1212
import java.net.URI;
1313
import java.util.ArrayList;
14+
import java.util.Arrays;
15+
import java.util.HashSet;
16+
import java.util.List;
1417

1518
import org.jetbrains.annotations.NotNull;
1619
import org.junit.jupiter.api.Assertions;
@@ -19,12 +22,17 @@
1922
import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider;
2023
import com.microsoft.graph.core.authentication.AzureIdentityAuthenticationProvider;
2124
import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler;
25+
import com.microsoft.graph.core.requests.options.GraphClientOption;
2226
import com.microsoft.kiota.RequestOption;
2327
import com.microsoft.kiota.authentication.AccessTokenProvider;
2428
import com.microsoft.kiota.authentication.AllowedHostsValidator;
2529
import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider;
30+
import com.microsoft.kiota.http.middleware.HeadersInspectionHandler;
31+
import com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler;
2632
import com.microsoft.kiota.http.middleware.RedirectHandler;
2733
import com.microsoft.kiota.http.middleware.RetryHandler;
34+
import com.microsoft.kiota.http.middleware.UrlReplaceHandler;
35+
import com.microsoft.kiota.http.middleware.UserAgentHandler;
2836
import com.microsoft.kiota.http.middleware.options.RetryHandlerOption;
2937

3038
import okhttp3.Interceptor;
@@ -36,6 +44,51 @@ class GraphClientFactoryTest {
3644

3745
private static final String ACCESS_TOKEN_STRING = "token";
3846

47+
@Test
48+
void testDefaultCreate() {
49+
final OkHttpClient.Builder clientBuilder = GraphClientFactory.create();
50+
assertDefaultHandlersPresent(clientBuilder.interceptors());
51+
}
52+
53+
@Test
54+
void testCreateWithCustomInterceptorsAddsTelemetry() {
55+
final OkHttpClient.Builder clientBuilder = GraphClientFactory.create(
56+
new RetryHandler(), new RedirectHandler()
57+
);
58+
59+
assertEquals(3, clientBuilder.interceptors().size());
60+
61+
for (Interceptor interceptor : clientBuilder.interceptors()) {
62+
assertTrue(
63+
interceptor instanceof GraphTelemetryHandler
64+
|| interceptor instanceof RetryHandler
65+
|| interceptor instanceof RedirectHandler
66+
);
67+
}
68+
}
69+
70+
@Test
71+
void testCreateWithGraphClientOption() {
72+
final OkHttpClient.Builder clientBuilder = GraphClientFactory.create(new GraphClientOption());
73+
assertDefaultHandlersPresent(clientBuilder.interceptors());
74+
}
75+
76+
@Test
77+
void testCreateDefaultInterceptorsWithCustomOptions() {
78+
Interceptor[] interceptors = GraphClientFactory.createDefaultGraphInterceptors(
79+
new RequestOption[] {new RetryHandlerOption(null, 0, 0)}
80+
);
81+
assertDefaultHandlersPresent(Arrays.asList(interceptors));
82+
83+
for (Interceptor interceptor : interceptors) {
84+
if (interceptor instanceof RetryHandler) {
85+
RetryHandlerOption retryOptions = ((RetryHandler) interceptor).getRetryOptions();
86+
Assertions.assertEquals(0, retryOptions.maxRetries());
87+
Assertions.assertEquals(0, retryOptions.delay());
88+
}
89+
}
90+
}
91+
3992
@Test
4093
void testCreateWithAuthenticationProvider() throws IOException {
4194
final BaseBearerTokenAuthenticationProvider mockAuthenticationProvider =
@@ -103,6 +156,24 @@ void testCreateWithCustomInterceptorsOverwritesDefaults() throws IOException {
103156
}
104157
}
105158

159+
private void assertDefaultHandlersPresent(final List<Interceptor> interceptors) {
160+
HashSet<Class<? extends Interceptor>> expectedInterceptors = new HashSet<>(
161+
Arrays.asList(
162+
GraphTelemetryHandler.class,
163+
RetryHandler.class,
164+
UrlReplaceHandler.class,
165+
UserAgentHandler.class,
166+
RedirectHandler.class,
167+
ParametersNameDecodingHandler.class,
168+
HeadersInspectionHandler.class
169+
)
170+
);
171+
172+
for (Interceptor interceptor : interceptors) {
173+
assertTrue(expectedInterceptors.contains(interceptor.getClass()));
174+
}
175+
}
176+
106177
private static BaseBearerTokenAuthenticationProvider getMockAuthenticationProvider() {
107178
final AccessTokenProvider mockAccessTokenProvider = mock(AzureIdentityAccessTokenProvider.class);
108179
when(mockAccessTokenProvider.getAuthorizationToken(any(URI.class), anyMap()))

0 commit comments

Comments
 (0)