-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTestSyncStack.java
More file actions
218 lines (185 loc) · 7.32 KB
/
TestSyncStack.java
File metadata and controls
218 lines (185 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.contentstack.sdk;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class TestSyncStack {
private SyncStack syncStack;
private final Stack stack = Credentials.getStack();
private final String host = Credentials.HOST;
@BeforeEach
void setUp() {
syncStack = new SyncStack();
}
/**
* ✅ Test: Valid JSON with correct structure
*/
@Test
void testSetJSON_WithValidData() {
JSONObject validJson = new JSONObject()
.put("items", new JSONArray()
.put(new JSONObject().put("title", "Article 1"))
.put(new JSONObject().put("title", "Article 2")))
.put("skip", 5)
.put("total_count", 100)
.put("limit", 20)
.put("pagination_token", "validToken123")
.put("sync_token", "sync123");
syncStack.setJSON(validJson);
// Assertions
assertEquals(5, syncStack.getSkip());
assertEquals(100, syncStack.getCount());
assertEquals(20, syncStack.getLimit());
assertEquals("validToken123", syncStack.getPaginationToken());
assertEquals("sync123", syncStack.getSyncToken());
List<JSONObject> items = syncStack.getItems();
assertNotNull(items);
assertEquals(2, items.size());
assertEquals("Article 1", items.get(0).optString("title"));
}
/**
* ✅ Test: Missing `items` should not cause a crash
*/
@Test
void testSetJSON_MissingItems() {
JSONObject jsonWithoutItems = new JSONObject()
.put("skip", 5)
.put("total_count", 50)
.put("limit", 10);
syncStack.setJSON(jsonWithoutItems);
// Assertions
assertEquals(5, syncStack.getSkip());
assertEquals(50, syncStack.getCount());
assertEquals(10, syncStack.getLimit());
assertTrue(syncStack.getItems().isEmpty()); // Should default to empty list
}
/**
* ✅ Test: Handling JSON Injection Attempt
*/
@Test
void testSetJSON_JSONInjection() {
JSONObject maliciousJson = new JSONObject()
.put("items", new JSONArray()
.put(new JSONObject().put("title", "<script>alert('Hacked');</script>")));
syncStack.setJSON(maliciousJson);
List<JSONObject> items = syncStack.getItems();
assertNotNull(items);
assertEquals(1, items.size());
assertEquals("<script>alert('Hacked');</script>", items.get(0).optString("title"));
}
/**
* ✅ Should treat a lone JSONObject under "items" the same as a one‑element
* array.
*/
@Test
void testSetJSON_handlesSingleItemObject() {
JSONObject input = new JSONObject()
.put("items", new JSONObject()
.put("title", "Single Entry")
.put("uid", "entry123")
.put("content_type", "blog"))
.put("skip", 0)
.put("total_count", 1)
.put("limit", 10)
.put("sync_token", "token123");
syncStack.setJSON(input);
List<JSONObject> items = syncStack.getItems();
assertNotNull(items, "Items list should be initialised");
assertEquals(1, items.size(), "Exactly one item expected");
JSONObject item = items.get(0);
assertEquals("Single Entry", item.optString("title"));
assertEquals("entry123", item.optString("uid"));
assertEquals("blog", item.optString("content_type"));
assertEquals(0, syncStack.getSkip());
assertEquals(1, syncStack.getCount());
assertEquals(10, syncStack.getLimit());
assertEquals("token123", syncStack.getSyncToken());
}
/**
* ✅ Test: Invalid `items` field (should not crash)
*/
@Test
void testSetJSON_InvalidItemsType() {
JSONObject invalidJson = new JSONObject()
.put("items", "This is not a valid array")
.put("skip", 10);
assertDoesNotThrow(() -> syncStack.setJSON(invalidJson));
assertTrue(syncStack.getItems().isEmpty());
}
/**
* ✅ Test: Null `paginationToken` and `syncToken` are handled correctly
*/
@Test
void testSetJSON_NullTokens() {
JSONObject jsonWithNullTokens = new JSONObject()
.put("pagination_token", JSONObject.NULL)
.put("sync_token", JSONObject.NULL);
syncStack.setJSON(jsonWithNullTokens);
assertNull(syncStack.getPaginationToken());
assertNull(syncStack.getSyncToken());
}
/**
* ✅ Test: Invalid characters in `paginationToken` should be rejected
*/
@Test
void testSetJSON_InvalidTokenCharacters() {
JSONObject jsonWithInvalidTokens = new JSONObject()
.put("pagination_token", "invalid!!@#")
.put("sync_token", "<script>attack</script>");
syncStack.setJSON(jsonWithInvalidTokens);
assertNull(syncStack.getPaginationToken()); // Should be sanitized
assertNull(syncStack.getSyncToken()); // Should be sanitized
}
/**
* ✅ Test: Thread-Safety - Concurrent Modification of `syncItems`
*/
@Test
void testSetJSON_ThreadSafety() throws InterruptedException {
JSONObject jsonWithItems = new JSONObject()
.put("items", new JSONArray()
.put(new JSONObject().put("title", "Safe Entry")));
Thread thread1 = new Thread(() -> syncStack.setJSON(jsonWithItems));
Thread thread2 = new Thread(() -> syncStack.setJSON(jsonWithItems));
thread1.start();
thread2.start();
thread1.join();
thread2.join();
assertFalse(syncStack.getItems().isEmpty()); // No race conditions
}
/**
* ✅ Test: Real API call to syncContentType
*/
@Test
void testRealSyncContentType() throws IllegalAccessException {
// Create a CountDownLatch to wait for the async call to complete
CountDownLatch latch = new CountDownLatch(1);
// Make the actual API call
stack.syncContentType("product", new SyncResultCallBack() {
@Override
public void onCompletion(SyncStack syncStack, Error error) {
if (error != null) {
fail("Sync failed with error: " + error.getErrorMessage());
}
// Verify the response
assertNotNull(syncStack.getJSONResponse());
assertNull(syncStack.getUrl());
assertNotNull(syncStack.getItems());
assertFalse(syncStack.getItems().isEmpty());
assertTrue(syncStack.getCount() > 0);
latch.countDown();
}
});
try {
// Wait for the async call to complete (with timeout)
assertTrue(latch.await(10, TimeUnit.SECONDS), "Sync operation timed out");
} catch (InterruptedException e) {
fail("Test was interrupted: " + e.getMessage());
}
}
}