Skip to content

Commit 10a8263

Browse files
committed
Add tests
1 parent e0b5675 commit 10a8263

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.microsoft.graph.core.requests;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.ArgumentMatchers.anyMap;
8+
import static org.mockito.Mockito.mock;
9+
import static org.mockito.Mockito.when;
10+
11+
import java.io.IOException;
12+
import java.net.URI;
13+
14+
import org.junit.jupiter.api.Test;
15+
16+
import com.microsoft.kiota.authentication.AccessTokenProvider;
17+
import com.microsoft.kiota.authentication.AllowedHostsValidator;
18+
import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider;
19+
20+
import okhttp3.OkHttpClient;
21+
import okhttp3.Request;
22+
import okhttp3.Response;
23+
24+
class GraphClientFactoryTest {
25+
26+
private static final String ACCESS_TOKEN_STRING = "token";
27+
28+
@Test
29+
void testCreateWithAuthenticationProvider() throws IOException {
30+
final BaseBearerTokenAuthenticationProvider mockAuthenticationProvider =
31+
getMockAuthenticationProvider();
32+
OkHttpClient graphClient = GraphClientFactory.create(mockAuthenticationProvider).addInterceptor(new MockResponseHandler()).build();
33+
34+
Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/me").build();
35+
Response response = graphClient.newCall(request).execute();
36+
37+
assertEquals(200, response.code());
38+
assertNotNull(response.request());
39+
assertTrue(response.request().headers().names().contains("Authorization"));
40+
assertEquals("Bearer " + ACCESS_TOKEN_STRING, response.request().header("Authorization"));
41+
}
42+
43+
private static BaseBearerTokenAuthenticationProvider getMockAuthenticationProvider() {
44+
final AccessTokenProvider mockAccessTokenProvider = mock(AccessTokenProvider.class);
45+
final AllowedHostsValidator allowedHostsValidator =
46+
new AllowedHostsValidator("graph.microsoft.com");
47+
when(mockAccessTokenProvider.getAllowedHostsValidator()).thenReturn(allowedHostsValidator);
48+
when(mockAccessTokenProvider.getAuthorizationToken(any(URI.class), anyMap()))
49+
.thenReturn(ACCESS_TOKEN_STRING);
50+
final BaseBearerTokenAuthenticationProvider mockAuthenticationProvider =
51+
mock(BaseBearerTokenAuthenticationProvider.class);
52+
when(mockAuthenticationProvider.getAccessTokenProvider())
53+
.thenReturn(mockAccessTokenProvider);
54+
return mockAuthenticationProvider;
55+
}
56+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.microsoft.graph.core.requests;
2+
3+
import java.io.IOException;
4+
5+
import okhttp3.Interceptor;
6+
import okhttp3.MediaType;
7+
import okhttp3.Protocol;
8+
import okhttp3.Response;
9+
import okhttp3.ResponseBody;
10+
import okio.Buffer;
11+
12+
class MockResponseHandler implements Interceptor {
13+
14+
private int statusCode;
15+
16+
public MockResponseHandler(int statusCode) {
17+
this.statusCode = statusCode;
18+
}
19+
20+
public MockResponseHandler() {
21+
this.statusCode = 200;
22+
}
23+
24+
@Override
25+
public Response intercept(Chain chain) throws IOException {
26+
final var request = chain.request();
27+
final var requestBody = request.body();
28+
if (request != null && requestBody != null) {
29+
final var buffer = new Buffer();
30+
requestBody.writeTo(buffer);
31+
return new Response.Builder()
32+
.code(this.statusCode)
33+
.message("OK")
34+
.protocol(Protocol.HTTP_1_1)
35+
.request(request)
36+
.body(
37+
ResponseBody.create(
38+
buffer.readByteArray(), MediaType.parse("application/json")))
39+
.build();
40+
}
41+
return new Response.Builder()
42+
.code(this.statusCode)
43+
.message("OK")
44+
.protocol(Protocol.HTTP_1_1)
45+
.request(request)
46+
.body(ResponseBody.create("", MediaType.parse("application/json")))
47+
.build();
48+
}
49+
50+
}

0 commit comments

Comments
 (0)