Skip to content

Commit 83e0e98

Browse files
v1.12.3
- Taxonomy query support - Early Access Feature Support
1 parent a4b72fa commit 83e0e98

File tree

2 files changed

+111
-18
lines changed

2 files changed

+111
-18
lines changed

src/main/java/com/contentstack/sdk/Taxonomy.java

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import okhttp3.Request;
44
import okhttp3.ResponseBody;
5+
import org.jetbrains.annotations.NotNull;
56
import retrofit2.Call;
67
import retrofit2.Response;
78

89
import java.io.IOException;
910
import java.util.HashMap;
1011
import java.util.LinkedHashMap;
12+
import java.util.List;
1113
import java.util.Map;
1214

1315
/**
@@ -86,7 +88,75 @@ public Taxonomy(APIService service, Config config, LinkedHashMap<String, Object>
8688
* @return instance of {@link Taxonomy}
8789
*/
8890
public Taxonomy query(Map<String, Object> queryParams) {
89-
query.putAll(queryParams);
91+
this.query.putAll(queryParams);
92+
return this;
93+
}
94+
95+
96+
/**
97+
* Get all entries for a specific taxonomy that satisfy the given conditions provided in the query.
98+
*
99+
* @param key the key of the taxonomy to query
100+
* @param listOfItems the list of taxonomy fields
101+
* Example: If you want to retrieve entries with the color taxonomy applied and linked to the term red and/or yellow.
102+
* <code>
103+
* String key = "taxonomies.taxonomy_uid";
104+
* String[] listOfItem = {"term_uid1", "term_uid2"};
105+
* taxonomy.in(key, listOfItem);
106+
* taxonomy.query(query).find(new TaxonomyCallback() {
107+
* @Override public void onFailure(Request request, ResponseBody errorMessage) {
108+
* System.out.println("Failing API call : " + errorMessage.toString());
109+
* <p>
110+
* }
111+
* @Override public void onResponse(ResponseBody response) {
112+
* System.out.println("Response : " + response.toString());
113+
* <p>
114+
* }
115+
* });
116+
* </code>
117+
*/
118+
public Taxonomy in(@NotNull String key, @NotNull String[] listOfItems) {
119+
HashMap<String, Object> param = new HashMap<>();
120+
param.put("$in", listOfItems);
121+
this.query.put(key, param);
122+
return this;
123+
}
124+
125+
126+
public Taxonomy or(@NotNull List<HashMap<String, String>> listOfItems) {
127+
for (int i = 0; i < listOfItems.size(); i++) {
128+
HashMap<String, String> param = listOfItems.get(i);
129+
130+
if (i > 0) {
131+
this.query.put("$or", listOfItems.toArray());
132+
}
133+
134+
this.query.put("$or", param);
135+
}
136+
137+
return this;
138+
}
139+
140+
141+
public Taxonomy and(@NotNull List<HashMap<String, String>> listOfItems) {
142+
for (int i = 0; i < listOfItems.size(); i++) {
143+
HashMap<String, String> param = listOfItems.get(i);
144+
145+
if (i > 0) {
146+
this.query.put("$and", listOfItems.toArray());
147+
}
148+
149+
this.query.put("$and", param);
150+
}
151+
152+
return this;
153+
}
154+
155+
156+
public Taxonomy exists(@NotNull String name, @NotNull Boolean value) {
157+
HashMap<String, Boolean> param = new HashMap<>();
158+
param.put("$exists", value);
159+
this.query.put(name, param);
90160
return this;
91161
}
92162

@@ -120,6 +190,8 @@ public void find(TaxonomyCallback callback) {
120190
throw new RuntimeException(e);
121191
}
122192
}
193+
194+
123195
}
124196

125197

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
package com.contentstack.sdk;
22

3-
import okhttp3.*;
3+
import okhttp3.Request;
4+
import okhttp3.ResponseBody;
5+
import org.json.JSONObject;
46
import org.junit.jupiter.api.Assertions;
57
import org.junit.jupiter.api.Test;
68

9+
import java.util.ArrayList;
710
import java.util.Arrays;
811
import java.util.HashMap;
12+
import java.util.List;
913

1014

1115
public class TaxonomyTest {
1216

13-
private final Taxonomy taxonomy = Credentials.getStack().taxonomy();
17+
private final Stack stack = Credentials.getStack();
1418

1519
@Test
1620
void testInstance() {
17-
Assertions.assertNotNull(taxonomy);
21+
Assertions.assertNotNull(stack);
1822
}
1923

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

27-
taxonomy.query(query).find(new TaxonomyCallback() {
31+
stack.taxonomy().query(query).find(new TaxonomyCallback() {
2832
@Override
2933
public void onFailure(Request request, ResponseBody errorMessage) {
30-
System.out.println("Failing API call : "+errorMessage.toString());
34+
System.out.println("Failing API call : " + errorMessage.toString());
3135

3236
}
3337

3438
@Override
3539
public void onResponse(ResponseBody response) {
36-
System.out.println("Response : "+response.toString());
40+
System.out.println("Response : " + response.toString());
3741

3842
}
3943
});
@@ -46,7 +50,7 @@ void testUnitInOperator() {
4650
query.put("taxonomies.taxonomy_uid", new HashMap<String, Object>() {{
4751
put("$in", new String[]{"term_uid1", "term_uid2"});
4852
}});
49-
53+
Taxonomy taxonomy = stack.taxonomy();
5054
taxonomy.query(query);
5155
Request req = taxonomy.makeRequest().request();
5256
Assertions.assertEquals(3, req.headers().size());
@@ -55,35 +59,52 @@ void testUnitInOperator() {
5559
Assertions.assertNull(req.url().encodedFragment(), "We do not expect any fragment");
5660
Assertions.assertEquals("/v3/taxonomies/entries", req.url().encodedPath());
5761
Assertions.assertEquals(3, Arrays.stream(req.url().encodedPathSegments().stream().toArray()).count());
58-
Assertions.assertNotNull( req.url().query());
62+
Assertions.assertNotNull(req.url().query());
5963
Assertions.assertNotNull(req.url().encodedQuery());
6064
Assertions.assertEquals("[query]", req.url().queryParameterNames().toString());
6165
}
6266

6367
@Test
64-
void testUnitOrOperator() {
65-
HashMap<String, Object> query = new HashMap<>();
66-
query.put("taxonomies.taxonomy_uid", new HashMap<String, Object>() {{
67-
put("$in", new String[]{"term_uid1", "term_uid2"});
68-
}});
69-
70-
taxonomy.query(query);
68+
void testUnitINOperator() {
69+
Taxonomy taxonomy = stack.taxonomy();
70+
String key = "taxonomies.taxonomy_uid";
71+
String[] listOfItem = {"term_uid1", "term_uid2"};
72+
taxonomy.in(key, listOfItem);
7173
Request req = taxonomy.makeRequest().request();
7274
Assertions.assertEquals(3, req.headers().size());
7375
Assertions.assertEquals("GET", req.method().toString(), "test method are being passed though payload");
7476
Assertions.assertEquals("cdn.contentstack.io", req.url().host());
7577
Assertions.assertNull(req.url().encodedFragment(), "We do not expect any fragment");
7678
Assertions.assertEquals("/v3/taxonomies/entries", req.url().encodedPath());
7779
Assertions.assertEquals(3, Arrays.stream(req.url().encodedPathSegments().stream().toArray()).count());
78-
Assertions.assertNotNull( req.url().query());
80+
Assertions.assertNotNull(req.url().query());
7981
Assertions.assertNotNull(req.url().encodedQuery());
8082
Assertions.assertEquals("[query]", req.url().queryParameterNames().toString());
8183
}
8284

8385

86+
@Test
87+
void testUnitOr() {
88+
Taxonomy taxonomy = stack.taxonomy();
8489

90+
List<HashMap<String, String>> listOfItems = new ArrayList<>();
91+
HashMap<String, String> items = new HashMap<>();
92+
items.put("taxonomies.taxonomy_uid_1", "term_uid1");
93+
items.put("taxonomies.taxonomy_uid_2", "term_uid2");
94+
listOfItems.add(items);
8595

86-
96+
taxonomy.or(listOfItems);
97+
Request req = taxonomy.makeRequest().request();
98+
Assertions.assertEquals(3, req.headers().size());
99+
Assertions.assertEquals("GET", req.method().toString(), "test method are being passed though payload");
100+
Assertions.assertEquals("cdn.contentstack.io", req.url().host());
101+
Assertions.assertNull(req.url().encodedFragment(), "We do not expect any fragment");
102+
Assertions.assertEquals("/v3/taxonomies/entries", req.url().encodedPath());
103+
Assertions.assertEquals(3, Arrays.stream(req.url().encodedPathSegments().stream().toArray()).count());
104+
Assertions.assertNotNull(req.url().query());
105+
Assertions.assertNotNull(req.url().encodedQuery());
106+
Assertions.assertEquals("[query]", req.url().queryParameterNames().toString());
107+
}
87108

88109

89110
}

0 commit comments

Comments
 (0)