Skip to content

Commit 10f9fef

Browse files
Add additional unit tests for CSHttpConnection, covering form parameter handling, error setting, and URL parameter conversion, enhancing overall test coverage.
1 parent 336b07f commit 10f9fef

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed

src/test/java/com/contentstack/sdk/TestCSHttpConnection.java

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,4 +806,205 @@ public void onRequestFail(ResponseType responseType, Error error) {}
806806
// Note: send() is not called here as it requires actual network infrastructure
807807
// The complete flow with send() is covered by integration tests
808808
}
809+
810+
// ========== ADDITIONAL BRANCH COVERAGE TESTS ==========
811+
812+
@Test
813+
void testSetFormParamsGETWithNullResult() {
814+
HashMap<String, Object> params = null;
815+
816+
String result = connection.setFormParamsGET(params);
817+
818+
assertNull(result);
819+
}
820+
821+
@Test
822+
void testSetFormParamsGETWithEmptyParamsReturnsNull() {
823+
HashMap<String, Object> params = new HashMap<>();
824+
825+
String result = connection.setFormParamsGET(params);
826+
827+
assertNull(result);
828+
}
829+
830+
@Test
831+
void testSetFormParamsGETWithNonQueryNonEntryController() {
832+
connection.setInfo("ASSET");
833+
834+
HashMap<String, Object> params = new HashMap<>();
835+
params.put("key1", "value1");
836+
params.put("key2", "value2");
837+
838+
String result = connection.setFormParamsGET(params);
839+
840+
assertNotNull(result);
841+
assertTrue(result.contains("key1=value1"));
842+
assertTrue(result.contains("key2=value2"));
843+
}
844+
845+
@Test
846+
void testSetFormParamsGETWithMultipleParams() {
847+
connection.setInfo("OTHER");
848+
849+
HashMap<String, Object> params = new HashMap<>();
850+
params.put("param1", "value1");
851+
params.put("param2", "value2");
852+
params.put("param3", "value3");
853+
854+
String result = connection.setFormParamsGET(params);
855+
856+
assertNotNull(result);
857+
assertTrue(result.startsWith("?"));
858+
assertTrue(result.contains("param1=value1"));
859+
assertTrue(result.contains("&"));
860+
}
861+
862+
@Test
863+
void testGetParamsExceptionHandling() throws Exception {
864+
connection.setInfo("QUERY");
865+
866+
// Create a params map with a value that will cause encoding issues
867+
HashMap<String, Object> params = new HashMap<>();
868+
869+
// Add a mock object that will cause ClassCastException when treated as JSONObject
870+
params.put("query", new Object() {
871+
@Override
872+
public String toString() {
873+
return "{invalid}";
874+
}
875+
});
876+
877+
Method getParamsMethod = CSHttpConnection.class.getDeclaredMethod("getParams", HashMap.class);
878+
getParamsMethod.setAccessible(true);
879+
880+
// This should handle the exception and log it, returning a partial URL
881+
String result = (String) getParamsMethod.invoke(connection, params);
882+
883+
assertNotNull(result);
884+
// The method should continue despite the exception
885+
assertTrue(result.startsWith("?"));
886+
}
887+
888+
@Test
889+
void testSendWithNullParams() {
890+
connection.setInfo("QUERY");
891+
connection.setFormParams(null);
892+
893+
// Verify send can be called with null params without throwing
894+
// Note: This will fail at network call, but that's expected in unit test
895+
assertDoesNotThrow(() -> {
896+
try {
897+
// Setup minimal required fields
898+
LinkedHashMap<String, Object> headers = new LinkedHashMap<>();
899+
headers.put("api_key", "test");
900+
connection.setHeaders(headers);
901+
902+
Stack stack = Contentstack.stack("test", "test", "test");
903+
connection.setConfig(stack.config);
904+
connection.setAPIService(stack.service);
905+
connection.setStack(stack);
906+
907+
// This will fail at network level, but params handling is tested
908+
connection.send();
909+
} catch (Exception e) {
910+
// Expected - network call will fail in unit test
911+
}
912+
});
913+
}
914+
915+
@Test
916+
void testSendWithEmptyParams() {
917+
connection.setInfo("QUERY");
918+
connection.setFormParams(new HashMap<>());
919+
920+
assertDoesNotThrow(() -> {
921+
try {
922+
LinkedHashMap<String, Object> headers = new LinkedHashMap<>();
923+
headers.put("api_key", "test");
924+
connection.setHeaders(headers);
925+
926+
Stack stack = Contentstack.stack("test", "test", "test");
927+
connection.setConfig(stack.config);
928+
connection.setAPIService(stack.service);
929+
connection.setStack(stack);
930+
931+
connection.send();
932+
} catch (Exception e) {
933+
// Expected
934+
}
935+
});
936+
}
937+
938+
@Test
939+
void testConvertUrlParamWithSingleElement() throws Exception {
940+
Method convertUrlParamMethod = CSHttpConnection.class.getDeclaredMethod("convertUrlParam",
941+
String.class, Object.class, String.class);
942+
convertUrlParamMethod.setAccessible(true);
943+
944+
JSONArray array = new JSONArray();
945+
array.put("single_value");
946+
947+
String result = (String) convertUrlParamMethod.invoke(connection, "?", array, "test_key");
948+
949+
assertNotNull(result);
950+
assertTrue(result.contains("test_key=single_value"));
951+
}
952+
953+
@Test
954+
void testCreateOrderedJSONObjectWithMultipleEntries() throws Exception {
955+
Method createOrderedMethod = CSHttpConnection.class.getDeclaredMethod("createOrderedJSONObject", Map.class);
956+
createOrderedMethod.setAccessible(true);
957+
958+
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
959+
map.put("key1", "value1");
960+
map.put("key2", 123);
961+
map.put("key3", true);
962+
map.put("key4", "value4");
963+
964+
JSONObject result = (JSONObject) createOrderedMethod.invoke(connection, map);
965+
966+
assertNotNull(result);
967+
assertEquals("value1", result.get("key1"));
968+
assertEquals(123, result.get("key2"));
969+
assertEquals(true, result.get("key3"));
970+
assertEquals("value4", result.get("key4"));
971+
assertEquals(4, result.length());
972+
}
973+
974+
@Test
975+
void testSetErrorWithEmptyString() {
976+
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();
977+
CSHttpConnection conn = new CSHttpConnection("https://test.com", csConnectionRequest);
978+
979+
conn.setError("");
980+
981+
assertNotNull(csConnectionRequest.error);
982+
assertTrue(csConnectionRequest.error.has("error_message"));
983+
String errorMsg = csConnectionRequest.error.getString("error_message");
984+
assertEquals("Unexpected error: No response received from server.", errorMsg);
985+
}
986+
987+
@Test
988+
void testSetErrorWithWhitespaceOnly() {
989+
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();
990+
CSHttpConnection conn = new CSHttpConnection("https://test.com", csConnectionRequest);
991+
992+
conn.setError(" ");
993+
994+
assertNotNull(csConnectionRequest.error);
995+
assertTrue(csConnectionRequest.error.has("error_message"));
996+
}
997+
998+
@Test
999+
void testSetErrorWithValidJSONButMissingAllFields() {
1000+
MockIRequestModelHTTP csConnectionRequest = new MockIRequestModelHTTP();
1001+
CSHttpConnection conn = new CSHttpConnection("https://test.com", csConnectionRequest);
1002+
1003+
conn.setError("{\"some_field\": \"some_value\"}");
1004+
1005+
assertNotNull(csConnectionRequest.error);
1006+
assertEquals("An unknown error occurred.", csConnectionRequest.error.getString("error_message"));
1007+
assertEquals("0", csConnectionRequest.error.getString("error_code"));
1008+
assertEquals("No additional error details available.", csConnectionRequest.error.getString("errors"));
1009+
}
8091010
}

0 commit comments

Comments
 (0)