Skip to content

Commit

Permalink
Refactor + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndiritu committed Nov 12, 2024
1 parent 54e061c commit 0e0f2cd
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public static OkHttpClient.Builder create(@Nonnull final BaseBearerTokenAuthenti
@Nonnull
public static OkHttpClient.Builder create(@Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider, @Nonnull final RequestOption[] requestOptions) {
final GraphClientOption graphClientOption = new GraphClientOption();
final Interceptor[] interceptors = createDefaultGraphInterceptors(graphClientOption, requestOptions);
final List<RequestOption> requestOptionsList = new ArrayList<>(Arrays.asList(requestOptions));
requestOptionsList.add(graphClientOption);
final Interceptor[] interceptors = createDefaultGraphInterceptors(requestOptionsList.toArray(new RequestOption[0]));
final ArrayList<Interceptor> interceptorList = new ArrayList<>(Arrays.asList(interceptors));
interceptorList.add(new AuthorizationHandler(authenticationProvider));
graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.AUTH_HANDLER_FLAG);
Expand Down Expand Up @@ -115,19 +117,17 @@ public static OkHttpClient.Builder create(@Nonnull final GraphClientOption graph
*/
@Nonnull
public static OkHttpClient.Builder create(@Nullable final GraphClientOption graphClientOption) {
return create(graphClientOption, new RequestOption[0]);
return KiotaClientFactory.create(createDefaultGraphInterceptors(graphClientOption));
}

/**
* The OkHttpClient Builder with optional GraphClientOption and RequestOptions to override default graph interceptors
* @param graphClientOption the GraphClientOption for use in requests.
* @param requestOptions custom request options to override default graph interceptors
* @return an OkHttpClient Builder instance.
*/
@Nonnull
public static OkHttpClient.Builder create(@Nullable final GraphClientOption graphClientOption, @Nonnull final RequestOption[] requestOptions) {
GraphClientOption options = graphClientOption != null ? graphClientOption : new GraphClientOption();
return KiotaClientFactory.create(createDefaultGraphInterceptors(options, requestOptions));
public static OkHttpClient.Builder create(@Nonnull final RequestOption[] requestOptions) {
return KiotaClientFactory.create(createDefaultGraphInterceptors(requestOptions));
}

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

/**
* Creates the default Interceptors for use with Graph configured with the provided RequestOptions.
* @param graphClientOption the GraphClientOption used to create the GraphTelemetryHandler with.
* @param requestOptions custom request options to override default graph interceptors
* @return an array of interceptors.
*/
@Nonnull
public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final GraphClientOption graphClientOption, @Nonnull final RequestOption[] requestOptions) {
public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final RequestOption[] requestOptions) {
Objects.requireNonNull(requestOptions, "parameter requestOptions cannot be null");
return getDefaultGraphInterceptors(requestOptions).toArray(new Interceptor[0]);
}

/**
* Creates the default Interceptors for use with Graph.
* @param requestOptions custom request options to override default graph interceptors
* @return a list of interceptors.
*/
private static List<Interceptor> getDefaultGraphInterceptors(@Nonnull final RequestOption[] requestOptions) {
GraphClientOption graphClientOption = new GraphClientOption();
UrlReplaceHandlerOption urlReplaceHandlerOption = new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs());

for (RequestOption option : requestOptions) {
if (option instanceof UrlReplaceHandlerOption) {
urlReplaceHandlerOption = (UrlReplaceHandlerOption) option;
}
if (option instanceof GraphClientOption) {
graphClientOption = (GraphClientOption) option;
}
}

List<Interceptor> handlers = new ArrayList<>();
handlers.add(new UrlReplaceHandler(urlReplaceHandlerOption));
handlers.add(new GraphTelemetryHandler(graphClientOption));
handlers.addAll(Arrays.asList(KiotaClientFactory.createDefaultInterceptors(requestOptions)));
addDefaultFeatureUsages(graphClientOption);
return handlers.toArray(new Interceptor[0]);
return handlers;
}

//These are the default features used by the Graph Client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;
Expand All @@ -19,12 +22,17 @@
import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider;
import com.microsoft.graph.core.authentication.AzureIdentityAuthenticationProvider;
import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler;
import com.microsoft.graph.core.requests.options.GraphClientOption;
import com.microsoft.kiota.RequestOption;
import com.microsoft.kiota.authentication.AccessTokenProvider;
import com.microsoft.kiota.authentication.AllowedHostsValidator;
import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider;
import com.microsoft.kiota.http.middleware.HeadersInspectionHandler;
import com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler;
import com.microsoft.kiota.http.middleware.RedirectHandler;
import com.microsoft.kiota.http.middleware.RetryHandler;
import com.microsoft.kiota.http.middleware.UrlReplaceHandler;
import com.microsoft.kiota.http.middleware.UserAgentHandler;
import com.microsoft.kiota.http.middleware.options.RetryHandlerOption;

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

private static final String ACCESS_TOKEN_STRING = "token";

@Test
void testDefaultCreate() {
final OkHttpClient.Builder clientBuilder = GraphClientFactory.create();
assertDefaultHandlersPresent(clientBuilder.interceptors());
}

@Test
void testCreateWithCustomInterceptorsAddsTelemetry() {
final OkHttpClient.Builder clientBuilder = GraphClientFactory.create(
new RetryHandler(), new RedirectHandler()
);

assertEquals(3, clientBuilder.interceptors().size());

for (Interceptor interceptor : clientBuilder.interceptors()) {
assertTrue(
interceptor instanceof GraphTelemetryHandler
|| interceptor instanceof RetryHandler
|| interceptor instanceof RedirectHandler
);
}
}

@Test
void testCreateWithGraphClientOption() {
final OkHttpClient.Builder clientBuilder = GraphClientFactory.create(new GraphClientOption());
assertDefaultHandlersPresent(clientBuilder.interceptors());
}

@Test
void testCreateDefaultInterceptorsWithCustomOptions() {
Interceptor[] interceptors = GraphClientFactory.createDefaultGraphInterceptors(
new RequestOption[] {new RetryHandlerOption(null, 0, 0)}
);
assertDefaultHandlersPresent(Arrays.asList(interceptors));

for (Interceptor interceptor : interceptors) {
if (interceptor instanceof RetryHandler) {
RetryHandlerOption retryOptions = ((RetryHandler) interceptor).getRetryOptions();
Assertions.assertEquals(0, retryOptions.maxRetries());
Assertions.assertEquals(0, retryOptions.delay());
}
}
}

@Test
void testCreateWithAuthenticationProvider() throws IOException {
final BaseBearerTokenAuthenticationProvider mockAuthenticationProvider =
Expand Down Expand Up @@ -103,6 +156,24 @@ void testCreateWithCustomInterceptorsOverwritesDefaults() throws IOException {
}
}

private void assertDefaultHandlersPresent(final List<Interceptor> interceptors) {
HashSet<Class<? extends Interceptor>> expectedInterceptors = new HashSet<>(
Arrays.asList(
GraphTelemetryHandler.class,
RetryHandler.class,
UrlReplaceHandler.class,
UserAgentHandler.class,
RedirectHandler.class,
ParametersNameDecodingHandler.class,
HeadersInspectionHandler.class
)
);

for (Interceptor interceptor : interceptors) {
assertTrue(expectedInterceptors.contains(interceptor.getClass()));
}
}

private static BaseBearerTokenAuthenticationProvider getMockAuthenticationProvider() {
final AccessTokenProvider mockAccessTokenProvider = mock(AzureIdentityAccessTokenProvider.class);
when(mockAccessTokenProvider.getAuthorizationToken(any(URI.class), anyMap()))
Expand Down

0 comments on commit 0e0f2cd

Please sign in to comment.