Skip to content

Commit 8ce6fac

Browse files
committed
Enhance SyncStack to handle ArrayList and improve error logging for 'items' processing
Added test for real API call to syncContentType.
1 parent 388dfe4 commit 8ce6fac

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

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

Lines changed: 26 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,27 @@ 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 ArrayList) {
87+
ArrayList<?> itemsList = (ArrayList<?>) 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 LinkedHashMap) {
93+
// Convert LinkedHashMap to JSONObject
94+
JSONObject jsonItem = new JSONObject((Map<?, ?>) item);
95+
syncItems.add(sanitizeJson(jsonItem));
96+
} else {
97+
logger.warning("Item in ArrayList is not a JSONObject or LinkedHashMap. Skipping. Type: " + item.getClass().getName());
98+
}
8799
}
100+
} else {
101+
logger.warning("'items' is not a valid JSONArray, JSONObject, or ArrayList. Type: " +
102+
(itemsObj != null ? itemsObj.getClass().getName() : "null"));
103+
syncItems = new ArrayList<>();
88104
}
89105
} else {
90106
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)