Skip to content

Commit

Permalink
chore : latest implementation for includeReference
Browse files Browse the repository at this point in the history
  • Loading branch information
reeshika-h committed Sep 12, 2024
1 parent 022ee14 commit 9f7f8df
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
29 changes: 20 additions & 9 deletions src/main/java/com/contentstack/cms/stack/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,30 @@ protected Entry clearParams() {
return this;
}

public Call<ResponseBody> includeReference(@NotNull Object referenceFields) {
List<String> 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<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();
validateEntry();
return this.service.referCall(this.headers, this.contentTypeUid, referenceList);
}
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
8 changes: 1 addition & 7 deletions src/main/java/com/contentstack/cms/stack/EntryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +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);

@GET("content_types/{content_type_uid}/entries")
Call<ResponseBody> referCall(
@HeaderMap Map<String, Object> headers,
@Path("content_type_uid") String contentTypeUid,
@Query("include[]") List<String> values);
@QueryMap(encoded = true) Map<String, Object> queryParameter);

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

0 comments on commit 9f7f8df

Please sign in to comment.