From 24c03d50a196c759158d4e6a4e7a9ca47e4bd57a Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Wed, 11 Sep 2024 13:06:13 +0530 Subject: [PATCH 1/5] enhanced the implementation of addParam --- .../com/contentstack/cms/stack/Entry.java | 18 ++++++++++++-- .../cms/stack/EntryFieldUnitTests.java | 24 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/contentstack/cms/stack/Entry.java b/src/main/java/com/contentstack/cms/stack/Entry.java index 6fb633af..a6e0e381 100644 --- a/src/main/java/com/contentstack/cms/stack/Entry.java +++ b/src/main/java/com/contentstack/cms/stack/Entry.java @@ -35,6 +35,7 @@ public class Entry implements BaseImplementation { protected final EntryService service; protected final String contentTypeUid; protected final String entryUid; + private int includeCounter = 1; protected Entry(Retrofit instance, Map headers, String contentTypeUid) { this.contentTypeUid = contentTypeUid; @@ -82,15 +83,28 @@ public Entry addHeader(@NotNull String key, @NotNull String value) { * @param value query param value for the request * @return instance of {@link Entry} */ + @Override public Entry addParam(@NotNull String key, @NotNull Object value) { - this.params.put(key, value); + if (key.equals("include[]")) { + if (value instanceof String[]) { + for (String item : (String[]) value) { + this.params.put(key + includeCounter++, item); + } + } else if (value instanceof String) { + this.params.put(key + includeCounter++, value); + } + } else { + this.params.put(key, value); + } return this; } @Override public Entry addParams(@NotNull HashMap params) { - this.params.putAll(params); + for (Map.Entry entry : params.entrySet()) { + addParam(entry.getKey(), entry.getValue()); + } return this; } diff --git a/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java b/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java index f9701a5a..b8efbf50 100644 --- a/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java +++ b/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java @@ -2,13 +2,20 @@ import com.contentstack.cms.Contentstack; import com.contentstack.cms.TestClient; +import com.google.gson.JsonArray; + import okhttp3.Request; + import okhttp3.ResponseBody; +import retrofit2.Response; + import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.junit.jupiter.api.*; +import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -93,6 +100,23 @@ void testSingleEntryQuery() { Request resp = entryInstance.fetch().request(); Assertions.assertEquals("include_publish_details=true&locale=en-us&include_workflow=false", resp.url().query()); } + + @Test + void testaddParam(){ + String[] array = {"reference","navigation_menu.page_reference"}; + entryInstance.addParam("include[]", array); + Request request = entryInstance.find().request(); + Assertions.assertEquals("include[]1=reference&include[]2=navigation_menu.page_reference", request.url().query()); + } + + @Test + void testaddParams() throws IOException{ + HashMap paramslist = new HashMap<>(); + paramslist.put("include[]", new String[]{"reference", "navigation_menu.page_reference"}); + entryInstance.addParams(paramslist); + Request request = entryInstance.find().request(); + Assertions.assertEquals("include[]1=reference&include[]2=navigation_menu.page_reference", request.url().query()); + } @Test void testSingleEntryEncodedPath() { From ce14fc4e268335a33ef3e0a042fe091926ebec7a Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Wed, 11 Sep 2024 14:02:31 +0530 Subject: [PATCH 2/5] chore : actions/upload-artifact version v2 -> v3 bump --- .github/workflows/coverage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index a03b7585..3ff0db47 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -22,7 +22,7 @@ jobs: run: mvn jacoco:prepare-agent jacoco:report - name: Upload Code Coverage Report - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v3 with: name: code-coverage-report path: target/site/jacoco From 88f846c64abf3dc7e8fa34b85b8f80f701908649 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Wed, 11 Sep 2024 16:03:49 +0530 Subject: [PATCH 3/5] chore: implementation of includeReference --- .../com/contentstack/cms/stack/Entry.java | 32 +++++++++++-------- .../contentstack/cms/stack/EntryService.java | 7 ++++ .../cms/stack/EntryFieldUnitTests.java | 18 +++-------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/contentstack/cms/stack/Entry.java b/src/main/java/com/contentstack/cms/stack/Entry.java index a6e0e381..2f058345 100644 --- a/src/main/java/com/contentstack/cms/stack/Entry.java +++ b/src/main/java/com/contentstack/cms/stack/Entry.java @@ -7,7 +7,10 @@ import retrofit2.Call; import retrofit2.Retrofit; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Objects; @@ -85,26 +88,14 @@ public Entry addHeader(@NotNull String key, @NotNull String value) { */ @Override public Entry addParam(@NotNull String key, @NotNull Object value) { - if (key.equals("include[]")) { - if (value instanceof String[]) { - for (String item : (String[]) value) { - this.params.put(key + includeCounter++, item); - } - } else if (value instanceof String) { - this.params.put(key + includeCounter++, value); - } - } else { - this.params.put(key, value); - } + this.params.put(key, value); return this; } @Override public Entry addParams(@NotNull HashMap params) { - for (Map.Entry entry : params.entrySet()) { - addParam(entry.getKey(), entry.getValue()); - } + this.params.putAll(params); return this; } @@ -135,6 +126,19 @@ protected Entry clearParams() { return this; } + public Call includeReference(@NotNull Object referenceFields) { + List referenceList = new ArrayList<>(); + if (referenceFields instanceof String) { + referenceList.add((String) referenceFields); + } else if (referenceFields instanceof String[]) { + referenceList.addAll(Arrays.asList((String[]) referenceFields)); + } else { + throw new IllegalArgumentException("Reference fields must be a String or an array of Strings"); + } + validateCT(); + validateEntry(); + return this.service.referCall(this.headers, this.contentTypeUid, referenceList); +} /** * Fetches the list of all the entries of a particular content type. * It also returns the content of each entry in JSON format. You can also diff --git a/src/main/java/com/contentstack/cms/stack/EntryService.java b/src/main/java/com/contentstack/cms/stack/EntryService.java index 3fe6d220..875a62ba 100644 --- a/src/main/java/com/contentstack/cms/stack/EntryService.java +++ b/src/main/java/com/contentstack/cms/stack/EntryService.java @@ -5,6 +5,7 @@ import retrofit2.Call; import retrofit2.http.*; +import java.util.List; import java.util.Map; public interface EntryService { @@ -14,6 +15,12 @@ Call fetch( @HeaderMap Map headers, @Path("content_type_uid") String contentTypeUid, @QueryMap(encoded = true) Map queryParameter); + + @GET("content_types/{content_type_uid}/entries") + Call referCall( + @HeaderMap Map headers, + @Path("content_type_uid") String contentTypeUid, + @Query("include[]") List values); @Headers("Content-Type: application/json") @GET("content_types/{content_type_uid}/entries/{entry_uid}") diff --git a/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java b/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java index b8efbf50..746803fa 100644 --- a/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java +++ b/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java @@ -100,22 +100,12 @@ void testSingleEntryQuery() { Request resp = entryInstance.fetch().request(); Assertions.assertEquals("include_publish_details=true&locale=en-us&include_workflow=false", resp.url().query()); } - - @Test - void testaddParam(){ - String[] array = {"reference","navigation_menu.page_reference"}; - entryInstance.addParam("include[]", array); - Request request = entryInstance.find().request(); - Assertions.assertEquals("include[]1=reference&include[]2=navigation_menu.page_reference", request.url().query()); - } @Test - void testaddParams() throws IOException{ - HashMap paramslist = new HashMap<>(); - paramslist.put("include[]", new String[]{"reference", "navigation_menu.page_reference"}); - entryInstance.addParams(paramslist); - Request request = entryInstance.find().request(); - Assertions.assertEquals("include[]1=reference&include[]2=navigation_menu.page_reference", request.url().query()); + void testIncludeReference() { + String[] array = {"reference","navigation_menu.page_reference"}; + Request req = entryInstance.includeReference(array).request(); + Assertions.assertEquals("include[]=reference&include[]=navigation_menu.page_reference", req.url().query()); } @Test From 022ee14582a05eae6860e62adf01856ed704dc9f Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Wed, 11 Sep 2024 16:04:13 +0530 Subject: [PATCH 4/5] testcases added --- .../com/contentstack/cms/stack/EntryFieldUnitTests.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java b/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java index 746803fa..b3a69989 100644 --- a/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java +++ b/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java @@ -102,12 +102,19 @@ void testSingleEntryQuery() { } @Test - void testIncludeReference() { + void testIncludeReferenceMultipleReferences() { String[] array = {"reference","navigation_menu.page_reference"}; Request req = entryInstance.includeReference(array).request(); Assertions.assertEquals("include[]=reference&include[]=navigation_menu.page_reference", req.url().query()); } + @Test + void testIncludeReferenceSingleReference() { + Request req = entryInstance.includeReference("reference").request(); + + Assertions.assertEquals("include[]=reference", req.url().query()); + } + @Test void testSingleEntryEncodedPath() { Request resp = entryInstance.fetch().request(); From 9f7f8dfd9684fc52fe13f74911e4ffb649de50f5 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Thu, 12 Sep 2024 16:44:46 +0530 Subject: [PATCH 5/5] chore : latest implementation for includeReference --- .../com/contentstack/cms/stack/Entry.java | 29 +++++++++++++------ .../contentstack/cms/stack/EntryService.java | 8 +---- .../cms/stack/EntryFieldUnitTests.java | 21 ++++++++++---- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/contentstack/cms/stack/Entry.java b/src/main/java/com/contentstack/cms/stack/Entry.java index 2f058345..d7893869 100644 --- a/src/main/java/com/contentstack/cms/stack/Entry.java +++ b/src/main/java/com/contentstack/cms/stack/Entry.java @@ -126,19 +126,30 @@ protected Entry clearParams() { return this; } - public Call includeReference(@NotNull Object referenceFields) { - List referenceList = new ArrayList<>(); - if (referenceFields instanceof String) { - referenceList.add((String) referenceFields); - } else if (referenceFields instanceof String[]) { - referenceList.addAll(Arrays.asList((String[]) referenceFields)); + protected Entry addToParams(@NotNull String key, @NotNull Object value){ + if (key.equals("include[]")) { + if (value instanceof String[]) { + for (String item : (String[]) value) { + this.params.put(key + includeCounter++, item); + } + } else if (value instanceof String) { + this.params.put(key, value); + } + } else { + this.params.put(key, value); + } + return this; + } + + public Call includeReference(@NotNull Object referenceField){ + if (referenceField instanceof String || referenceField instanceof String[]) { + addToParams("include[]", referenceField); } else { throw new IllegalArgumentException("Reference fields must be a String or an array of Strings"); } validateCT(); - validateEntry(); - return this.service.referCall(this.headers, this.contentTypeUid, referenceList); -} + return this.service.fetch(this.headers, this.contentTypeUid, this.params); + } /** * Fetches the list of all the entries of a particular content type. * It also returns the content of each entry in JSON format. You can also diff --git a/src/main/java/com/contentstack/cms/stack/EntryService.java b/src/main/java/com/contentstack/cms/stack/EntryService.java index 875a62ba..fa6ff789 100644 --- a/src/main/java/com/contentstack/cms/stack/EntryService.java +++ b/src/main/java/com/contentstack/cms/stack/EntryService.java @@ -14,13 +14,7 @@ public interface EntryService { Call fetch( @HeaderMap Map headers, @Path("content_type_uid") String contentTypeUid, - @QueryMap(encoded = true) Map queryParameter); - - @GET("content_types/{content_type_uid}/entries") - Call referCall( - @HeaderMap Map headers, - @Path("content_type_uid") String contentTypeUid, - @Query("include[]") List values); + @QueryMap(encoded = true) Map queryParameter); @Headers("Content-Type: application/json") @GET("content_types/{content_type_uid}/entries/{entry_uid}") diff --git a/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java b/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java index b3a69989..1b2f1909 100644 --- a/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java +++ b/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java @@ -100,19 +100,28 @@ void testSingleEntryQuery() { Request resp = entryInstance.fetch().request(); Assertions.assertEquals("include_publish_details=true&locale=en-us&include_workflow=false", resp.url().query()); } - + @Test + void testIncludeReferenceSingleReference(){ + Request request = entryInstance.includeReference("reference").request(); + Assertions.assertEquals("include[]=reference", request.url().query()); + } @Test void testIncludeReferenceMultipleReferences() { String[] array = {"reference","navigation_menu.page_reference"}; Request req = entryInstance.includeReference(array).request(); - Assertions.assertEquals("include[]=reference&include[]=navigation_menu.page_reference", req.url().query()); + Assertions.assertEquals("include[]1=reference&include[]2=navigation_menu.page_reference", req.url().query()); } @Test - void testIncludeReferenceSingleReference() { - Request req = entryInstance.includeReference("reference").request(); - - Assertions.assertEquals("include[]=reference", req.url().query()); + void testIncludeReferenceMultipleReferencesWithParams() throws IOException { + entryInstance.clearParams(); + entryInstance.addParam("locale", "en-us"); + String[] array = {"reference","navigation_menu.page_reference"}; + entryInstance.addParam("include_workflow", false); + entryInstance.addParam("include_publish_details", true); + Request req = entryInstance.includeReference(array).request(); + Assertions.assertEquals("include[]1=reference&include_publish_details=true&include[]2=navigation_menu.page_reference&locale=en-us&include_workflow=false", req.url().query()); + } @Test