Skip to content

Commit 043e2c7

Browse files
authored
Merge pull request #205 from contentstack/fix/DX-3132-sync-setJson
Enhance SyncStack to handle ArrayList
2 parents 388dfe4 + 3ca7a29 commit 043e2c7

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

src/main/java/com/contentstack/sdk/SyncStack.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.contentstack.sdk;
22

3-
import java.util.ArrayList;
4-
import java.util.LinkedHashMap;
5-
import java.util.List;
63
import org.jetbrains.annotations.NotNull;
74
import org.json.JSONArray;
85
import org.json.JSONObject;
6+
7+
import java.util.ArrayList;
8+
import java.util.LinkedHashMap;
9+
import java.util.List;
10+
import java.util.Map;
911
import java.util.logging.Logger;
1012

1113

@@ -68,6 +70,7 @@ protected synchronized void setJSON(@NotNull JSONObject jsonobject) {
6870

6971
if (receiveJson.has("items")) {
7072
Object itemsObj = receiveJson.opt("items");
73+
7174
if (itemsObj instanceof JSONArray) {
7275
JSONArray jsonArray = (JSONArray) itemsObj;
7376
syncItems = new ArrayList<>();
@@ -77,14 +80,26 @@ protected synchronized void setJSON(@NotNull JSONObject jsonobject) {
7780
syncItems.add(sanitizeJson(jsonItem));
7881
}
7982
}
80-
} else {
81-
if (itemsObj instanceof JSONObject) {
82-
syncItems = new ArrayList<>();
83-
syncItems.add(sanitizeJson((JSONObject) itemsObj));
84-
} else {
85-
logger.warning("'items' is not a valid list. Skipping processing.");
86-
syncItems = new ArrayList<>();
83+
} else if (itemsObj instanceof JSONObject) {
84+
syncItems = new ArrayList<>();
85+
syncItems.add(sanitizeJson((JSONObject) itemsObj));
86+
} else if (itemsObj instanceof List) {
87+
List<?> itemsList = (List<?>) itemsObj;
88+
syncItems = new ArrayList<>();
89+
for (Object item : itemsList) {
90+
if (item instanceof JSONObject) {
91+
syncItems.add(sanitizeJson((JSONObject) item));
92+
} else if (item instanceof Map) {
93+
JSONObject jsonItem = new JSONObject((Map<?, ?>) item);
94+
syncItems.add(sanitizeJson(jsonItem));
95+
} else {
96+
logger.warning("Item in ArrayList is not a JSONObject or LinkedHashMap. Skipping. Type: " + item.getClass().getName());
97+
}
8798
}
99+
} else {
100+
logger.warning("'items' is not a valid JSONArray, JSONObject, or ArrayList. Type: " +
101+
(itemsObj != null ? itemsObj.getClass().getName() : "null"));
102+
syncItems = new ArrayList<>();
88103
}
89104
} else {
90105
syncItems = new ArrayList<>();

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
import org.junit.jupiter.api.Test;
77
import static org.junit.jupiter.api.Assertions.*;
88
import java.util.List;
9+
import java.util.ArrayList;
10+
import java.util.LinkedHashMap;
11+
import java.util.concurrent.CountDownLatch;
12+
import java.util.concurrent.TimeUnit;
913

1014
public class TestSyncStack {
1115
private SyncStack syncStack;
16+
private final Stack stack = Credentials.getStack();
17+
private final String host = Credentials.HOST;
1218

1319
@BeforeEach
1420
void setUp() {
@@ -176,4 +182,37 @@ void testSetJSON_ThreadSafety() throws InterruptedException {
176182

177183
assertFalse(syncStack.getItems().isEmpty()); // No race conditions
178184
}
185+
186+
/**
187+
* ✅ Test: Real API call to syncContentType
188+
*/
189+
@Test
190+
void testRealSyncContentType() throws IllegalAccessException {
191+
// Create a CountDownLatch to wait for the async call to complete
192+
CountDownLatch latch = new CountDownLatch(1);
193+
// Make the actual API call
194+
stack.syncContentType("product", new SyncResultCallBack() {
195+
@Override
196+
public void onCompletion(SyncStack syncStack, Error error) {
197+
if (error != null) {
198+
fail("Sync failed with error: " + error.getErrorMessage());
199+
}
200+
// Verify the response
201+
assertNotNull(syncStack.getJSONResponse());
202+
assertNull(syncStack.getUrl());
203+
assertNotNull(syncStack.getItems());
204+
assertFalse(syncStack.getItems().isEmpty());
205+
assertTrue(syncStack.getCount() > 0);
206+
207+
latch.countDown();
208+
}
209+
});
210+
211+
try {
212+
// Wait for the async call to complete (with timeout)
213+
assertTrue(latch.await(10, TimeUnit.SECONDS), "Sync operation timed out");
214+
} catch (InterruptedException e) {
215+
fail("Test was interrupted: " + e.getMessage());
216+
}
217+
}
179218
}

0 commit comments

Comments
 (0)