Skip to content

Commit b7e1ac6

Browse files
authored
Merge pull request #112 from vrohit13/oauth-migration-client-tests
Improving code coverage for OAuthMigrationClient
2 parents 2c4de22 + 2a50e62 commit b7e1ac6

File tree

4 files changed

+163
-23
lines changed

4 files changed

+163
-23
lines changed

oauth2-platform-api/src/main/java/com/intuit/oauth2/client/OAuthMigrationClient.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ protected OAuthMigrationClient() {
6161
/**
6262
* Calls the migrate API based on the the request provided and
6363
* returns an object with oauth2 tokens
64-
*
65-
* @param environment
64+
*
6665
* @return
6766
* @throws ConnectionException
6867
*/
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 Intuit
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+
package com.intuit.oauth2.client;
17+
18+
import com.intuit.oauth2.data.OAuthMigrationRequest;
19+
import com.intuit.oauth2.exception.InvalidRequestException;
20+
import com.intuit.oauth2.http.HttpRequestClient;
21+
import com.intuit.oauth2.http.Request;
22+
import com.intuit.oauth2.http.Response;
23+
import mockit.Mock;
24+
import mockit.MockUp;
25+
26+
public class MockedHttpRequestClient extends MockUp<HttpRequestClient> {
27+
28+
private Response mockResponse;
29+
private Request serviceRequestReceived; // Used for asserting the request that was received
30+
private OAuthMigrationRequest oAuthMigrationRequest;
31+
32+
void setMockResponse(Response mockResponse) {
33+
this.mockResponse = mockResponse;
34+
}
35+
36+
Request getServiceRequestReceived() {
37+
return serviceRequestReceived;
38+
}
39+
40+
@Mock
41+
public Response makeJsonRequest(Request request, OAuthMigrationRequest oAuthMigrationRequest) throws InvalidRequestException {
42+
serviceRequestReceived = request;
43+
this.oAuthMigrationRequest = oAuthMigrationRequest;
44+
return mockResponse;
45+
}
46+
47+
@Mock
48+
public Response makeRequest(Request request) throws InvalidRequestException {
49+
serviceRequestReceived = request;
50+
return mockResponse;
51+
}
52+
}

oauth2-platform-api/src/test/java/com/intuit/oauth2/client/OAuth2PlatformClientTest.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -217,25 +217,4 @@ public void validateIDTokenReturnsFalseOnInvalidTokenTest() throws OpenIdExcepti
217217
+ "6cQQWNiDpWOl_lxXjQEvQ";
218218
assertFalse(oAuth2PlatformClient.validateIDToken(idToken));
219219
}
220-
221-
private static final class MockedHttpRequestClient extends MockUp<HttpRequestClient> {
222-
223-
private Response mockResponse;
224-
private Request serviceRequestReceived; // Used for asserting the request that was received
225-
226-
void setMockResponse(Response mockResponse) {
227-
this.mockResponse = mockResponse;
228-
}
229-
230-
Request getServiceRequestReceived() {
231-
return serviceRequestReceived;
232-
}
233-
234-
@Mock
235-
public Response makeRequest(Request request) throws InvalidRequestException {
236-
serviceRequestReceived = request;
237-
return mockResponse;
238-
}
239-
}
240-
241220
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 Intuit
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+
package com.intuit.oauth2.client;
17+
18+
import com.fasterxml.jackson.databind.ObjectMapper;
19+
import com.fasterxml.jackson.databind.ObjectWriter;
20+
import com.intuit.oauth2.config.Environment;
21+
import com.intuit.oauth2.config.OAuth2Config;
22+
import com.intuit.oauth2.config.OAuth2Config.OAuth2ConfigBuilder;
23+
import com.intuit.oauth2.config.ProxyConfig;
24+
import com.intuit.oauth2.config.Scope;
25+
import com.intuit.oauth2.data.OAuthMigrationRequest;
26+
import com.intuit.oauth2.data.OAuthMigrationResponse;
27+
import com.intuit.oauth2.exception.ConnectionException;
28+
import com.intuit.oauth2.http.Response;
29+
import com.intuit.oauth2.utils.MapperImpl;
30+
import org.apache.commons.io.IOUtils;
31+
import org.testng.annotations.BeforeClass;
32+
import org.testng.annotations.Test;
33+
import java.nio.charset.StandardCharsets;
34+
import static org.testng.Assert.assertEquals;
35+
36+
public class OAuthMigrationClientTest {
37+
38+
public static final ObjectMapper mapper = MapperImpl.getInstance();
39+
private ProxyConfig proxyConfig;
40+
41+
private OAuthMigrationClient oAuth2MigrationClient;
42+
private OAuth2Config oauth2Config;
43+
private OAuthMigrationRequest oAuthMigrationRequest;
44+
private MockedHttpRequestClient mockedHttpRequestClient;
45+
46+
public OAuthMigrationClientTest() {
47+
48+
oauth2Config = new OAuth2ConfigBuilder("test-client", "test-secret")
49+
.proxyConfig(proxyConfig).buildConfig();
50+
51+
oAuthMigrationRequest = new OAuthMigrationRequest.OAuthMigrationRequestBuilder(Environment.PRODUCTION, Scope.Accounting)
52+
.oAuth2Config(oauth2Config)
53+
.consumerKey("SomeConsumerKey")
54+
.consumerSecret("SomeConsumerSecret")
55+
.redirectUri("https://somerandomredirecturi.com/callback")
56+
.build();
57+
58+
oAuth2MigrationClient = new OAuthMigrationClient(oAuthMigrationRequest);
59+
}
60+
61+
@BeforeClass
62+
public void setup() {
63+
proxyConfig = new ProxyConfig.ProxyConfigBuilder("test-host", "8080")
64+
.username("username").password("password").domain("test-domain").buildConfig();
65+
mockedHttpRequestClient = new MockedHttpRequestClient();
66+
}
67+
68+
@Test
69+
public void testMigrate() throws Exception {
70+
71+
ObjectWriter writer = mapper.writerFor(OAuthMigrationResponse.class);
72+
73+
OAuthMigrationResponse expectedOAuthMigrationResponse = new OAuthMigrationResponse();
74+
expectedOAuthMigrationResponse.setAccessToken("eyJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiZGlyIn0..2T42Mngmoyk44UniOVFSgw.Hl3mqFm4Mq92Ux1m2UTIT0lKgNIF1QpM-NpedlZpxTKsZrI5EJ-amu8K24aM1GP0s3Ul3_ryyZiKnncs407v6c-2Z8mjwDJlYH_2iHs_MgF7epcmuZE8pjr_0ICx2ciYxDO1xA0IFwAT5db2D4ZRzkCNbpijXn6nuh0HJKLQyB4J53rVNLn-0yymQ1Jua9n9ZwmQ36uB1b1lSVTc16eE286h1fMKHcI6k9kGvv93kiWbPasntyRwy7ADj0ZX-Ct-syzoSrTuvChICKIgXc3N7zYfDZi_HM9viFNSOSnJ3-B_ysaZCbdIb8jgrCENwY3Pplp10hvpNIP3JCNl6GZktiuYW-o4shtkMQaWg-0OgnWdCI94Q52Ux32uun9ds8RpGWVTN3Ln9kzVg5lnNrNS9Ll6qm7yGLAmhZH9STgvL0eBPGXKLdg3Nku6cXGB26L8gN69S-r5k99d9xPuTy3H-qq8p2ebId9ys8nVlXshryFz48lBq0Tza5YZ3xqcyNoCs-GgEzZ2yS-v9mpK53BMh6ikggeAdPR4fBx7LTD1xL4I3ED8RrrzkC1mxdrTYoA6xNzTEt979PgSbVlCRj64F9Uft5GQ6ktdV0trWdPiSaLFb7EtjXzqYmduIdN_tEx2x8paefXRsp3X078klbDWb6kLTgamLlh_veOF7e3WXC9NQgIvFfQHfCIPDjooiaYcObolm0soE0j8yO0IoMZOEg.Z-nc_cRHkEA4V2qZJ_HnTg");
75+
expectedOAuthMigrationResponse.setRefreshToken("AB11581303147XhI4cHvxvcnBo14kMgYIknAB6aRSVs2DO7Bv0");
76+
expectedOAuthMigrationResponse.setExpiresIn(3600L);
77+
expectedOAuthMigrationResponse.setTokenType("bearer");
78+
expectedOAuthMigrationResponse.setRealmId("123147196581394");
79+
expectedOAuthMigrationResponse.setXRefreshTokenExpiresIn(8726400L);
80+
81+
Response mockResponse = new Response(
82+
IOUtils.toInputStream(writer.writeValueAsString(expectedOAuthMigrationResponse), StandardCharsets.UTF_8), 200);
83+
84+
// Set the mocked response against the mocked HTTP Client
85+
mockedHttpRequestClient.setMockResponse(mockResponse);
86+
87+
OAuthMigrationResponse actualOAuthMigrationResponse = oAuth2MigrationClient.migrate();
88+
89+
assertEquals(expectedOAuthMigrationResponse.getAccessToken(), actualOAuthMigrationResponse.getAccessToken());
90+
assertEquals(expectedOAuthMigrationResponse.getRefreshToken(), actualOAuthMigrationResponse.getRefreshToken());
91+
assertEquals(expectedOAuthMigrationResponse.getExpiresIn(), actualOAuthMigrationResponse.getExpiresIn());
92+
assertEquals(expectedOAuthMigrationResponse.getTokenType(), actualOAuthMigrationResponse.getTokenType());
93+
assertEquals(expectedOAuthMigrationResponse.getRealmId(), actualOAuthMigrationResponse.getRealmId());
94+
assertEquals(expectedOAuthMigrationResponse.getXRefreshTokenExpiresIn(), actualOAuthMigrationResponse.getXRefreshTokenExpiresIn());
95+
}
96+
97+
@Test(expectedExceptions = ConnectionException.class)
98+
public void testMigrateInvalidResponse() throws Exception {
99+
100+
ObjectWriter writer = mapper.writerFor(OAuthMigrationResponse.class);
101+
102+
Response mockResponse = new Response(
103+
IOUtils.toInputStream(writer.writeValueAsString(new OAuthMigrationResponse()), StandardCharsets.UTF_8), 500);
104+
105+
// Set the mocked response against the mocked HTTP Client
106+
mockedHttpRequestClient.setMockResponse(mockResponse);
107+
108+
OAuthMigrationResponse actualOAuthMigrationResponse = oAuth2MigrationClient.migrate();
109+
}
110+
}

0 commit comments

Comments
 (0)