Skip to content

Commit 105cd5c

Browse files
Refactor test classes for consistency and add unit tests for JSONUTF8Request, verifying JSON parsing behavior for valid and invalid responses to enhance test coverage.
1 parent fd7e749 commit 105cd5c

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import static org.junit.Assert.*;
66

7-
public class ContentstackResultCallbackTest {
7+
public class TestContentstackResultCallback {
88

99
// Simple concrete implementation for testing
1010
private static class TestCallback extends ContentstackResultCallback {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.contentstack.sdk;
2+
3+
import com.android.volley.NetworkResponse;
4+
import com.android.volley.ParseError;
5+
import com.android.volley.Response;
6+
import org.json.JSONObject;
7+
import org.junit.Test;
8+
9+
import java.nio.charset.Charset;
10+
11+
import static org.junit.Assert.*;
12+
13+
public class TestJSONUTF8Request {
14+
15+
// Subclass with a public wrapper
16+
private static class TestJSONUTF8RequestImpl extends JSONUTF8Request {
17+
TestJSONUTF8RequestImpl() {
18+
super(
19+
0,
20+
"http://example.com",
21+
null,
22+
new Response.Listener<JSONObject>() {
23+
@Override
24+
public void onResponse(JSONObject response) { }
25+
},
26+
new Response.ErrorListener() {
27+
@Override
28+
public void onErrorResponse(com.android.volley.VolleyError error) { }
29+
}
30+
);
31+
}
32+
33+
// Public wrapper to access the protected method
34+
public Response<JSONObject> callParse(NetworkResponse response) {
35+
return super.parseNetworkResponse(response);
36+
}
37+
}
38+
39+
@Test
40+
public void testParseNetworkResponse_validJson() throws Exception {
41+
JSONObject original = new JSONObject();
42+
original.put("key", "value");
43+
44+
byte[] data = original.toString().getBytes(Charset.forName("UTF-8"));
45+
NetworkResponse networkResponse = new NetworkResponse(data);
46+
47+
TestJSONUTF8RequestImpl request = new TestJSONUTF8RequestImpl();
48+
Response<JSONObject> response = request.callParse(networkResponse);
49+
50+
assertNotNull(response);
51+
assertNull(response.error);
52+
assertNotNull(response.result);
53+
assertEquals("value", response.result.getString("key"));
54+
}
55+
56+
@Test
57+
public void testParseNetworkResponse_invalidJson() {
58+
byte[] data = "not-json".getBytes(Charset.forName("UTF-8"));
59+
NetworkResponse networkResponse = new NetworkResponse(data);
60+
61+
TestJSONUTF8RequestImpl request = new TestJSONUTF8RequestImpl();
62+
Response<JSONObject> response = request.callParse(networkResponse);
63+
64+
assertNotNull(response);
65+
assertNotNull(response.error);
66+
assertTrue(response.error instanceof ParseError);
67+
}
68+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import static org.junit.Assert.assertEquals;
66
import static org.junit.Assert.assertNotNull;
77

8-
public class SDKControllerTest {
8+
public class TestSDKController {
99

1010
@Test
1111
public void testConstructorCoverage() {

0 commit comments

Comments
 (0)