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 diff --git a/src/main/java/com/contentstack/cms/stack/Entry.java b/src/main/java/com/contentstack/cms/stack/Entry.java index 3cd525d9..6de9b4cf 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; @@ -35,6 +38,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,6 +86,7 @@ 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); return this; @@ -121,6 +126,30 @@ protected Entry clearParams() { return this; } + 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(); + 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 3fe6d220..fa6ff789 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 { @@ -13,7 +14,7 @@ public interface EntryService { Call fetch( @HeaderMap Map headers, @Path("content_type_uid") String contentTypeUid, - @QueryMap(encoded = true) Map queryParameter); + @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 f9701a5a..1b2f1909 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,29 @@ 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[]1=reference&include[]2=navigation_menu.page_reference", req.url().query()); + } + + @Test + 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 void testSingleEntryEncodedPath() {