Skip to content

Commit

Permalink
enhanced the implementation of addParam
Browse files Browse the repository at this point in the history
  • Loading branch information
reeshika-h committed Sep 11, 2024
1 parent f0035b2 commit da59bf4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main/java/com/contentstack/cms/stack/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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,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<String, Object> params) {
this.params.putAll(params);
for (Map.Entry<String, Object> entry : params.entrySet()) {
addParam(entry.getKey(), entry.getValue());
}
return this;
}

Expand Down
24 changes: 24 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,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<String, Object> 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() {
Expand Down

0 comments on commit da59bf4

Please sign in to comment.