Skip to content

Commit

Permalink
Merge pull request #91 from contentstack/fix/DX-1282-addParam-method-enh
Browse files Browse the repository at this point in the history
DX-1282: enhanced the implementation of addParam
  • Loading branch information
reeshika-h authored Sep 12, 2024
2 parents 2151a84 + 9f7f8df commit dfa610d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions src/main/java/com/contentstack/cms/stack/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -35,6 +38,7 @@ public class Entry implements BaseImplementation<Entry> {
protected final EntryService service;
protected final String contentTypeUid;
protected final String entryUid;
private int includeCounter = 1;

protected Entry(Retrofit instance, Map<String, Object> headers, String contentTypeUid) {
this.contentTypeUid = contentTypeUid;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<ResponseBody> 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);
}
/**
* <b>Fetches the list of all the entries of a particular content type.</b>
* It also returns the content of each entry in JSON format. You can also
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/contentstack/cms/stack/EntryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import retrofit2.Call;
import retrofit2.http.*;

import java.util.List;
import java.util.Map;

public interface EntryService {
Expand All @@ -13,7 +14,7 @@ public interface EntryService {
Call<ResponseBody> fetch(
@HeaderMap Map<String, Object> headers,
@Path("content_type_uid") String contentTypeUid,
@QueryMap(encoded = true) Map<String, Object> queryParameter);
@QueryMap(encoded = true) Map<String, Object> queryParameter);

@Headers("Content-Type: application/json")
@GET("content_types/{content_type_uid}/entries/{entry_uid}")
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit dfa610d

Please sign in to comment.