Skip to content

Commit 95ee546

Browse files
Add comprehensive unit tests for Stack, Taxonomy, and Sync operations, covering various methods, edge cases, and header management to ensure robust functionality and high test coverage.
1 parent d35aec6 commit 95ee546

File tree

8 files changed

+3723
-0
lines changed

8 files changed

+3723
-0
lines changed

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

Lines changed: 472 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 855 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 405 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
package com.contentstack.sdk;
2+
3+
import android.content.Context;
4+
import android.util.ArrayMap;
5+
6+
import androidx.test.core.app.ApplicationProvider;
7+
8+
import org.json.JSONObject;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.robolectric.RobolectricTestRunner;
13+
14+
import java.util.Date;
15+
16+
import static org.junit.Assert.*;
17+
18+
/**
19+
* Tests for Stack header handling (getHeader private method coverage).
20+
*/
21+
@RunWith(RobolectricTestRunner.class)
22+
@org.robolectric.annotation.Config(sdk = 28, manifest = org.robolectric.annotation.Config.NONE)
23+
public class TestStackHeaderHandling {
24+
25+
private Context context;
26+
27+
@Before
28+
public void setUp() {
29+
context = ApplicationProvider.getApplicationContext();
30+
}
31+
32+
// ========== TESTS FOR getHeader WITH NULL LOCAL HEADERS ==========
33+
34+
@Test
35+
public void testGetHeaderWithNullLocalHeaders() throws Exception {
36+
Stack stack = Contentstack.stack(context, "api_key", "token", "env");
37+
stack.localHeader = null;
38+
39+
ContentTypesCallback callback = new ContentTypesCallback() {
40+
@Override
41+
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
42+
// Mock
43+
}
44+
};
45+
46+
try {
47+
stack.getContentTypes(new JSONObject(), callback);
48+
} catch (Exception e) {
49+
assertNotNull(stack);
50+
}
51+
}
52+
53+
@Test
54+
public void testGetHeaderWithEmptyLocalHeaders() throws Exception {
55+
Stack stack = Contentstack.stack(context, "api_key", "token", "env");
56+
stack.localHeader = new ArrayMap<>();
57+
58+
ContentTypesCallback callback = new ContentTypesCallback() {
59+
@Override
60+
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
61+
// Mock
62+
}
63+
};
64+
65+
try {
66+
stack.getContentTypes(new JSONObject(), callback);
67+
} catch (Exception e) {
68+
assertNotNull(stack);
69+
}
70+
}
71+
72+
@Test
73+
public void testGetHeaderWithLocalHeadersOnly() throws Exception {
74+
Stack stack = Contentstack.stack(context, "api_key", "token", "env");
75+
stack.setHeader("custom-header-1", "value1");
76+
stack.setHeader("custom-header-2", "value2");
77+
78+
ContentTypesCallback callback = new ContentTypesCallback() {
79+
@Override
80+
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
81+
// Mock
82+
}
83+
};
84+
85+
try {
86+
stack.getContentTypes(new JSONObject(), callback);
87+
} catch (Exception e) {
88+
assertNotNull(stack);
89+
}
90+
}
91+
92+
@Test
93+
public void testGetHeaderWithMainHeadersNull() throws Exception {
94+
Stack stack = Contentstack.stack(context, "api_key", "token", "env");
95+
stack.setHeader("local-header", "local-value");
96+
stack.headerGroupApp = null;
97+
98+
ContentTypesCallback callback = new ContentTypesCallback() {
99+
@Override
100+
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
101+
// Mock
102+
}
103+
};
104+
105+
try {
106+
stack.getContentTypes(new JSONObject(), callback);
107+
} catch (Exception e) {
108+
assertNotNull(stack);
109+
}
110+
}
111+
112+
@Test
113+
public void testGetHeaderWithMainHeadersEmpty() throws Exception {
114+
Stack stack = Contentstack.stack(context, "api_key", "token", "env");
115+
stack.setHeader("local-header", "local-value");
116+
stack.headerGroupApp = new ArrayMap<>();
117+
118+
ContentTypesCallback callback = new ContentTypesCallback() {
119+
@Override
120+
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
121+
// Mock
122+
}
123+
};
124+
125+
try {
126+
stack.getContentTypes(new JSONObject(), callback);
127+
} catch (Exception e) {
128+
assertNotNull(stack);
129+
}
130+
}
131+
132+
@Test
133+
public void testGetHeaderMergesBothHeaders() throws Exception {
134+
Stack stack = Contentstack.stack(context, "api_key", "token", "env");
135+
stack.setHeader("local-header-1", "local-value-1");
136+
stack.setHeader("local-header-2", "local-value-2");
137+
138+
ContentTypesCallback callback = new ContentTypesCallback() {
139+
@Override
140+
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
141+
// Mock
142+
}
143+
};
144+
145+
try {
146+
stack.getContentTypes(new JSONObject(), callback);
147+
} catch (Exception e) {
148+
assertNotNull(stack);
149+
}
150+
}
151+
152+
@Test
153+
public void testGetHeaderViaSyncWithNullHeaders() throws Exception {
154+
Stack stack = Contentstack.stack(context, "api_key", "token", "env");
155+
stack.localHeader = null;
156+
157+
SyncResultCallBack callback = new SyncResultCallBack() {
158+
@Override
159+
public void onCompletion(SyncStack syncStack, Error error) {
160+
// Mock
161+
}
162+
};
163+
164+
try {
165+
stack.sync(callback);
166+
} catch (Exception e) {
167+
assertNotNull(stack);
168+
}
169+
}
170+
171+
@Test
172+
public void testGetHeaderViaSyncWithPopulatedHeaders() throws Exception {
173+
Stack stack = Contentstack.stack(context, "api_key", "token", "env");
174+
stack.setHeader("sync-header", "sync-value");
175+
176+
SyncResultCallBack callback = new SyncResultCallBack() {
177+
@Override
178+
public void onCompletion(SyncStack syncStack, Error error) {
179+
// Mock
180+
}
181+
};
182+
183+
try {
184+
stack.sync(callback);
185+
} catch (Exception e) {
186+
assertNotNull(stack);
187+
}
188+
}
189+
190+
@Test
191+
public void testGetHeaderViaSyncToken() throws Exception {
192+
Stack stack = Contentstack.stack(context, "api_key", "token", "env");
193+
stack.setHeader("custom", "value");
194+
195+
SyncResultCallBack callback = new SyncResultCallBack() {
196+
@Override
197+
public void onCompletion(SyncStack syncStack, Error error) {
198+
// Mock
199+
}
200+
};
201+
202+
try {
203+
stack.syncToken("token123", callback);
204+
} catch (Exception e) {
205+
assertNotNull(stack);
206+
}
207+
}
208+
209+
@Test
210+
public void testGetHeaderViaSyncFromDate() throws Exception {
211+
Stack stack = Contentstack.stack(context, "api_key", "token", "env");
212+
stack.setHeader("date-header", "date-value");
213+
214+
SyncResultCallBack callback = new SyncResultCallBack() {
215+
@Override
216+
public void onCompletion(SyncStack syncStack, Error error) {
217+
// Mock
218+
}
219+
};
220+
221+
try {
222+
stack.syncFromDate(new Date(), callback);
223+
} catch (Exception e) {
224+
assertNotNull(stack);
225+
}
226+
}
227+
228+
@Test
229+
public void testGetHeaderAfterHeaderModifications() throws Exception {
230+
Stack stack = Contentstack.stack(context, "api_key", "token", "env");
231+
stack.setHeader("header1", "value1");
232+
stack.setHeader("header2", "value2");
233+
234+
ContentTypesCallback callback = new ContentTypesCallback() {
235+
@Override
236+
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
237+
// Mock
238+
}
239+
};
240+
241+
try {
242+
stack.getContentTypes(new JSONObject(), callback);
243+
stack.setHeader("header3", "value3");
244+
stack.removeHeader("header1");
245+
stack.getContentTypes(new JSONObject(), callback);
246+
} catch (Exception e) {
247+
assertNotNull(stack);
248+
}
249+
}
250+
251+
@Test
252+
public void testGetHeaderWithManyHeaders() throws Exception {
253+
Stack stack = Contentstack.stack(context, "api_key", "token", "env");
254+
255+
for (int i = 0; i < 10; i++) {
256+
stack.setHeader("local-header-" + i, "local-value-" + i);
257+
}
258+
259+
ContentTypesCallback callback = new ContentTypesCallback() {
260+
@Override
261+
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
262+
// Mock
263+
}
264+
};
265+
266+
try {
267+
stack.getContentTypes(new JSONObject(), callback);
268+
} catch (Exception e) {
269+
assertNotNull(stack);
270+
}
271+
}
272+
}
273+

0 commit comments

Comments
 (0)