2020
2121import java .io .IOException ;
2222
23+ import static org .junit .Assert .fail ;
2324import static org .mockito .ArgumentMatchers .anyString ;
2425
2526
@@ -34,6 +35,7 @@ public String getBearerToken() throws Exception {
3435@ RunWith (PowerMockRunner .class )
3536@ PrepareForTest (fullyQualifiedNames = "com.skyflow.common.utils.TokenUtils" )
3637public class InvokeConnectionTest {
38+ private static final String INVALID_EXCEPTION_THROWN = "Should not have thrown any exception" ;
3739 private JSONObject testConfig ;
3840 private static Skyflow skyflowClient ;
3941
@@ -84,8 +86,10 @@ public void testInvokeConnectionValidInput() {
8486 Assert .assertEquals (gatewayResponse .toJSONString (), mockResponse );
8587 } catch (SkyflowException exception ) {
8688 Assert .assertNull (exception );
89+ fail (INVALID_EXCEPTION_THROWN );
8790 } catch (IOException exception ) {
8891 exception .printStackTrace ();
92+ fail (INVALID_EXCEPTION_THROWN );
8993 }
9094
9195
@@ -158,4 +162,78 @@ public void testInvokeConnectionInvalidMethodName() {
158162 }
159163 }
160164
165+ @ Test
166+ @ PrepareForTest (fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility" , "com.skyflow.common.utils.TokenUtils" })
167+ public void testInvokeConnectionWithFormEncoded () {
168+ JSONObject testConfig = new JSONObject ();
169+ testConfig .put ("connectionURL" , "https://testgatewayurl.com/" );
170+ testConfig .put ("methodName" , RequestMethod .POST );
171+
172+ JSONObject requestHeadersJson = new JSONObject ();
173+ requestHeadersJson .put ("content-type" , "application/x-www-form-urlencoded" );
174+ testConfig .put ("requestHeader" , requestHeadersJson );
175+
176+ JSONObject testJson = new JSONObject ();
177+ testJson .put ("key1" ,"value1" );
178+ JSONObject nestedObj = new JSONObject ();
179+ nestedObj .put ("key2" ,"value2" );
180+ testJson .put ("nest" ,nestedObj );
181+
182+ testConfig .put ("requestBody" , testJson );
183+
184+ try {
185+ PowerMockito .mockStatic (HttpUtility .class );
186+ String mockResponse = "{\" id\" :\" 12345\" }" ;
187+ PowerMockito .when (HttpUtility .sendRequest (anyString (), anyString (), ArgumentMatchers .<JSONObject >any (), ArgumentMatchers .<String , String >anyMap ())).thenReturn (mockResponse );
188+ JSONObject gatewayResponse = skyflowClient .invokeConnection (testConfig );
189+
190+ Assert .assertNotNull (gatewayResponse );
191+ Assert .assertEquals (gatewayResponse .toJSONString (), mockResponse );
192+ } catch (SkyflowException exception ) {
193+ Assert .assertNull (exception );
194+ fail (INVALID_EXCEPTION_THROWN );
195+ } catch (IOException exception ) {
196+ exception .printStackTrace ();
197+ fail (INVALID_EXCEPTION_THROWN );
198+ }
199+
200+ }
201+
202+ @ Test
203+ @ PrepareForTest (fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility" , "com.skyflow.common.utils.TokenUtils" })
204+ public void testInvokeConnectionWithMultipartFormData () {
205+ JSONObject testConfig = new JSONObject ();
206+ testConfig .put ("connectionURL" , "https://testgatewayurl.com/" );
207+ testConfig .put ("methodName" , RequestMethod .POST );
208+
209+ JSONObject requestHeadersJson = new JSONObject ();
210+ requestHeadersJson .put ("content-type" , "multipart/form-data" );
211+ testConfig .put ("requestHeader" , requestHeadersJson );
212+
213+ JSONObject testJson = new JSONObject ();
214+ testJson .put ("key1" ,"value1" );
215+ JSONObject nestedObj = new JSONObject ();
216+ nestedObj .put ("key2" ,"value2" );
217+ testJson .put ("nest" ,nestedObj );
218+
219+ testConfig .put ("requestBody" , testJson );
220+
221+ try {
222+ PowerMockito .mockStatic (HttpUtility .class );
223+ String mockResponse = "{\" id\" :\" 12345\" }" ;
224+ PowerMockito .when (HttpUtility .sendRequest (anyString (), anyString (), ArgumentMatchers .<JSONObject >any (), ArgumentMatchers .<String , String >anyMap ())).thenReturn (mockResponse );
225+ JSONObject gatewayResponse = skyflowClient .invokeConnection (testConfig );
226+
227+ Assert .assertNotNull (gatewayResponse );
228+ Assert .assertEquals (gatewayResponse .toJSONString (), mockResponse );
229+ } catch (SkyflowException exception ) {
230+ Assert .assertNull (exception );
231+ fail (INVALID_EXCEPTION_THROWN );
232+ } catch (IOException exception ) {
233+ exception .printStackTrace ();
234+ fail (INVALID_EXCEPTION_THROWN );
235+ }
236+
237+ }
238+
161239}
0 commit comments