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