|
2 | 2 |
|
3 | 3 | import com.contentstack.cms.core.AuthInterceptor; |
4 | 4 | import com.contentstack.cms.organization.Organization; |
| 5 | +import com.contentstack.cms.stack.Entry; |
5 | 6 | import com.contentstack.cms.stack.Stack; |
| 7 | +import com.google.gson.JsonObject; |
| 8 | +import com.google.gson.JsonParser; |
6 | 9 | import okhttp3.Headers; |
7 | 10 | import okhttp3.OkHttpClient; |
8 | 11 | import okhttp3.Protocol; |
|
12 | 15 | import okhttp3.mockwebserver.MockWebServer; |
13 | 16 | import okhttp3.mockwebserver.RecordedRequest; |
14 | 17 | import org.junit.jupiter.api.Assertions; |
| 18 | +import okio.Buffer; |
| 19 | +import org.json.simple.JSONObject; |
15 | 20 | import org.junit.jupiter.api.Test; |
| 21 | +import retrofit2.Call; |
16 | 22 | import retrofit2.Response; |
17 | 23 |
|
18 | 24 | import java.io.IOException; |
@@ -412,4 +418,71 @@ public void testEarlyAccessHeaderEmpty() { |
412 | 418 | Assertions.assertEquals(0, client.earlyAccess.length); |
413 | 419 | } |
414 | 420 |
|
| 421 | + @Test |
| 422 | + void localizeBodyKeepsNullFieldWhenSerializeNullsEnabled() throws IOException { |
| 423 | + Contentstack client = new Contentstack.Builder().setSerializeNulls(true).build(); |
| 424 | + |
| 425 | + JsonObject entry = sentEntry(entryOf(client).localize(entryBody(fieldsWith("position", null)), "de-de")); |
| 426 | + |
| 427 | + Assertions.assertTrue(entry.has("position")); |
| 428 | + Assertions.assertTrue(entry.get("position").isJsonNull()); |
| 429 | + } |
| 430 | + |
| 431 | + @Test |
| 432 | + void localizeBodyDropsNullFieldByDefault() throws IOException { |
| 433 | + Contentstack client = new Contentstack.Builder().build(); |
| 434 | + |
| 435 | + JsonObject entry = sentEntry(entryOf(client).localize(entryBody(fieldsWith("position", null)), "de-de")); |
| 436 | + |
| 437 | + Assertions.assertFalse(entry.has("position")); |
| 438 | + Assertions.assertEquals("example", entry.get("title").getAsString()); |
| 439 | + } |
| 440 | + |
| 441 | + @Test |
| 442 | + void localizeBodyKeepsNullInsideGroupFieldWhenSerializeNullsEnabled() throws IOException { |
| 443 | + Contentstack client = new Contentstack.Builder().setSerializeNulls(true).build(); |
| 444 | + Map<String, Object> group = new HashMap<>(); |
| 445 | + group.put("label", "kept"); |
| 446 | + group.put("position", null); |
| 447 | + |
| 448 | + JsonObject entry = sentEntry(entryOf(client).localize(entryBody(fieldsWith("group", group)), "de-de")); |
| 449 | + |
| 450 | + JsonObject sentGroup = entry.getAsJsonObject("group"); |
| 451 | + Assertions.assertTrue(sentGroup.get("position").isJsonNull()); |
| 452 | + Assertions.assertEquals("kept", sentGroup.get("label").getAsString()); |
| 453 | + } |
| 454 | + |
| 455 | + @Test |
| 456 | + void updateBodyKeepsNullFieldWhenSerializeNullsEnabled() throws IOException { |
| 457 | + Contentstack client = new Contentstack.Builder().setSerializeNulls(true).build(); |
| 458 | + |
| 459 | + JsonObject entry = sentEntry(entryOf(client).update(entryBody(fieldsWith("position", null)))); |
| 460 | + |
| 461 | + Assertions.assertTrue(entry.has("position")); |
| 462 | + Assertions.assertTrue(entry.get("position").isJsonNull()); |
| 463 | + } |
| 464 | + |
| 465 | + private static Map<String, Object> fieldsWith(String field, Object value) { |
| 466 | + Map<String, Object> fields = new HashMap<>(); |
| 467 | + fields.put("title", "example"); |
| 468 | + fields.put(field, value); |
| 469 | + return fields; |
| 470 | + } |
| 471 | + |
| 472 | + private static JSONObject entryBody(Map<String, Object> fields) { |
| 473 | + Map<String, Object> body = new HashMap<>(); |
| 474 | + body.put("entry", fields); |
| 475 | + return new JSONObject(body); |
| 476 | + } |
| 477 | + |
| 478 | + private static Entry entryOf(Contentstack client) { |
| 479 | + return client.stack("api_key", "management_token").contentType("newspage").entry("entry_uid"); |
| 480 | + } |
| 481 | + |
| 482 | + private static JsonObject sentEntry(Call<ResponseBody> call) throws IOException { |
| 483 | + Buffer buffer = new Buffer(); |
| 484 | + call.request().body().writeTo(buffer); |
| 485 | + return JsonParser.parseString(buffer.readUtf8()).getAsJsonObject().getAsJsonObject("entry"); |
| 486 | + } |
| 487 | + |
415 | 488 | } |
0 commit comments