Skip to content

Commit

Permalink
v1.12.3
Browse files Browse the repository at this point in the history
- Taxonomy query support
- Early Access Feature Support
  • Loading branch information
ishaileshmishra committed Dec 13, 2023
1 parent a046fa0 commit fe28d6d
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 18 deletions.
74 changes: 73 additions & 1 deletion src/main/java/com/contentstack/sdk/Taxonomy.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import okhttp3.Request;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
import retrofit2.Call;
import retrofit2.Response;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -86,7 +88,75 @@ public Taxonomy(APIService service, Config config, LinkedHashMap<String, Object>
* @return instance of {@link Taxonomy}
*/
public Taxonomy query(Map<String, Object> queryParams) {
query.putAll(queryParams);
this.query.putAll(queryParams);
return this;
}


/**
* Get all entries for a specific taxonomy that satisfy the given conditions provided in the query.
*
* @param key the key of the taxonomy to query
* @param listOfItems the list of taxonomy fields
* Example: If you want to retrieve entries with the color taxonomy applied and linked to the term red and/or yellow.
* <code>
* String key = "taxonomies.taxonomy_uid";
* String[] listOfItem = {"term_uid1", "term_uid2"};
* taxonomy.in(key, listOfItem);
* taxonomy.query(query).find(new TaxonomyCallback() {
* @Override public void onFailure(Request request, ResponseBody errorMessage) {
* System.out.println("Failing API call : " + errorMessage.toString());
* <p>
* }
* @Override public void onResponse(ResponseBody response) {
* System.out.println("Response : " + response.toString());
* <p>
* }
* });
* </code>
*/
public Taxonomy in(@NotNull String key, @NotNull String[] listOfItems) {
HashMap<String, Object> param = new HashMap<>();
param.put("$in", listOfItems);
this.query.put(key, param);
return this;
}


public Taxonomy or(@NotNull List<HashMap<String, String>> listOfItems) {
for (int i = 0; i < listOfItems.size(); i++) {
HashMap<String, String> param = listOfItems.get(i);

if (i > 0) {
this.query.put("$or", listOfItems.toArray());
}

this.query.put("$or", param);
}

return this;
}


public Taxonomy and(@NotNull List<HashMap<String, String>> listOfItems) {
for (int i = 0; i < listOfItems.size(); i++) {
HashMap<String, String> param = listOfItems.get(i);

if (i > 0) {
this.query.put("$and", listOfItems.toArray());
}

this.query.put("$and", param);
}

return this;
}


public Taxonomy exists(@NotNull String name, @NotNull Boolean value) {
HashMap<String, Boolean> param = new HashMap<>();
param.put("$exists", value);
this.query.put(name, param);
return this;
}

Expand Down Expand Up @@ -120,6 +190,8 @@ public void find(TaxonomyCallback callback) {
throw new RuntimeException(e);
}
}


}


55 changes: 38 additions & 17 deletions src/test/java/com/contentstack/sdk/TaxonomyTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package com.contentstack.sdk;

import okhttp3.*;
import okhttp3.Request;
import okhttp3.ResponseBody;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;


public class TaxonomyTest {

private final Taxonomy taxonomy = Credentials.getStack().taxonomy();
private final Stack stack = Credentials.getStack();

@Test
void testInstance() {
Assertions.assertNotNull(taxonomy);
Assertions.assertNotNull(stack);
}

@Test
Expand All @@ -24,16 +28,16 @@ void testInOperator() {
put("$in", new String[]{"term_uid1", "term_uid2"});
}});

taxonomy.query(query).find(new TaxonomyCallback() {
stack.taxonomy().query(query).find(new TaxonomyCallback() {
@Override
public void onFailure(Request request, ResponseBody errorMessage) {
System.out.println("Failing API call : "+errorMessage.toString());
System.out.println("Failing API call : " + errorMessage.toString());

}

@Override
public void onResponse(ResponseBody response) {
System.out.println("Response : "+response.toString());
System.out.println("Response : " + response.toString());

}
});
Expand All @@ -46,7 +50,7 @@ void testUnitInOperator() {
query.put("taxonomies.taxonomy_uid", new HashMap<String, Object>() {{
put("$in", new String[]{"term_uid1", "term_uid2"});
}});

Taxonomy taxonomy = stack.taxonomy();
taxonomy.query(query);
Request req = taxonomy.makeRequest().request();
Assertions.assertEquals(3, req.headers().size());
Expand All @@ -55,35 +59,52 @@ void testUnitInOperator() {
Assertions.assertNull(req.url().encodedFragment(), "We do not expect any fragment");
Assertions.assertEquals("/v3/taxonomies/entries", req.url().encodedPath());
Assertions.assertEquals(3, Arrays.stream(req.url().encodedPathSegments().stream().toArray()).count());
Assertions.assertNotNull( req.url().query());
Assertions.assertNotNull(req.url().query());
Assertions.assertNotNull(req.url().encodedQuery());
Assertions.assertEquals("[query]", req.url().queryParameterNames().toString());
}

@Test
void testUnitOrOperator() {
HashMap<String, Object> query = new HashMap<>();
query.put("taxonomies.taxonomy_uid", new HashMap<String, Object>() {{
put("$in", new String[]{"term_uid1", "term_uid2"});
}});

taxonomy.query(query);
void testUnitINOperator() {
Taxonomy taxonomy = stack.taxonomy();
String key = "taxonomies.taxonomy_uid";
String[] listOfItem = {"term_uid1", "term_uid2"};
taxonomy.in(key, listOfItem);
Request req = taxonomy.makeRequest().request();
Assertions.assertEquals(3, req.headers().size());
Assertions.assertEquals("GET", req.method().toString(), "test method are being passed though payload");
Assertions.assertEquals("cdn.contentstack.io", req.url().host());
Assertions.assertNull(req.url().encodedFragment(), "We do not expect any fragment");
Assertions.assertEquals("/v3/taxonomies/entries", req.url().encodedPath());
Assertions.assertEquals(3, Arrays.stream(req.url().encodedPathSegments().stream().toArray()).count());
Assertions.assertNotNull( req.url().query());
Assertions.assertNotNull(req.url().query());
Assertions.assertNotNull(req.url().encodedQuery());
Assertions.assertEquals("[query]", req.url().queryParameterNames().toString());
}


@Test
void testUnitOr() {
Taxonomy taxonomy = stack.taxonomy();

List<HashMap<String, String>> listOfItems = new ArrayList<>();
HashMap<String, String> items = new HashMap<>();
items.put("taxonomies.taxonomy_uid_1", "term_uid1");
items.put("taxonomies.taxonomy_uid_2", "term_uid2");
listOfItems.add(items);


taxonomy.or(listOfItems);
Request req = taxonomy.makeRequest().request();
Assertions.assertEquals(3, req.headers().size());
Assertions.assertEquals("GET", req.method().toString(), "test method are being passed though payload");
Assertions.assertEquals("cdn.contentstack.io", req.url().host());
Assertions.assertNull(req.url().encodedFragment(), "We do not expect any fragment");
Assertions.assertEquals("/v3/taxonomies/entries", req.url().encodedPath());
Assertions.assertEquals(3, Arrays.stream(req.url().encodedPathSegments().stream().toArray()).count());
Assertions.assertNotNull(req.url().query());
Assertions.assertNotNull(req.url().encodedQuery());
Assertions.assertEquals("[query]", req.url().queryParameterNames().toString());
}


}

0 comments on commit fe28d6d

Please sign in to comment.