Skip to content

Commit 02673c0

Browse files
Taxonomy and Terms
1 parent 13ab719 commit 02673c0

File tree

6 files changed

+138
-30
lines changed

6 files changed

+138
-30
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dev-host=***REMOVED***

pom.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
<artifactId>cms</artifactId>
88
<packaging>jar</packaging>
99
<name>contentstack-management-java</name>
10-
<version>1.1.0</version>
10+
<version>1.1.0-SNAPSHOT</version>
1111
<description>Contentstack Java Management SDK for Content Management API, Contentstack is a headless CMS with an
1212
API-first approach
1313
</description>
1414
<url>https://github.com/contentstack/contentstack-management-java/</url>
1515

16-
<!-- Release the package using mvn clean deploy -->
16+
<!--mvn versions:display-dependency-updates-->
17+
<!--Release the package using mvn clean deploy -->
1718
<parent>
1819
<groupId>org.sonatype.oss</groupId>
1920
<artifactId>oss-parent</artifactId>
@@ -87,15 +88,14 @@
8788
<maven-source-plugin.version>2.2.1</maven-source-plugin.version>
8889
<maven-javadoc-plugin.version>3.0.0</maven-javadoc-plugin.version>
8990
<dotenv-source.version>5.2.2</dotenv-source.version>
90-
<rxjava-source.version>3.1.6</rxjava-source.version>
91+
<rxjava-source.version>3.1.7</rxjava-source.version>
9192
<retrofit-source.version>2.9.0</retrofit-source.version>
9293
<converter-gson-version>2.9.0</converter-gson-version>
93-
<logging.version>4.10.0</logging.version>
94+
<logging.version>5.0.0-alpha.11</logging.version>
9495
<jococo-plugin.version>0.8.7</jococo-plugin.version>
9596
<lombok-source.version>1.18.28</lombok-source.version>
96-
<junit-jupiter.version>5.9.2</junit-jupiter.version>
97+
<junit-jupiter.version>5.10.0</junit-jupiter.version>
9798
<junit-jupiter-engine.version>5.8.0-M1</junit-jupiter-engine.version>
98-
<junit-vintage-engine.version>5.9.2</junit-vintage-engine.version>
9999
<gson.version>2.10.1</gson.version>
100100
<maven-site-plugin.version>3.3</maven-site-plugin.version>
101101
<maven-gpg-plugin.version>1.5</maven-gpg-plugin.version>
@@ -162,13 +162,13 @@
162162
<dependency>
163163
<groupId>org.junit.vintage</groupId>
164164
<artifactId>junit-vintage-engine</artifactId>
165-
<version>${junit-vintage-engine.version}</version>
165+
<version>${junit-jupiter.version}</version>
166166
<scope>test</scope>
167167
</dependency>
168168
<dependency>
169169
<groupId>org.mockito</groupId>
170170
<artifactId>mockito-core</artifactId>
171-
<version>2.8.9</version>
171+
<version>5.5.0</version>
172172
<scope>test</scope>
173173
</dependency>
174174
<dependency>

src/main/java/com/contentstack/cms/stack/TaxonomyService.java

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,67 @@ public interface TaxonomyService {
1616
@GET("taxonomies/{taxonomy_uid}")
1717
Call<ResponseBody> fetch(@HeaderMap Map<String, Object> headers, @Path("taxonomy_uid") String uid, @QueryMap Map<String, Object> query);
1818

19-
@GET("taxonomies")
20-
Call<ResponseBody> create(@HeaderMap Map<String, Object> headers, @Body JSONObject body);
19+
@POST("taxonomies")
20+
Call<ResponseBody> create(
21+
@HeaderMap Map<String, Object> headers,
22+
@Body JSONObject body);
2123

2224
@PUT("taxonomies/{taxonomy_uid}")
23-
Call<ResponseBody> update(@HeaderMap Map<String, Object> headers, @Path("taxonomy_uid") String uid, @Body JSONObject body);
25+
Call<ResponseBody> update(
26+
@HeaderMap Map<String, Object> headers,
27+
@Path("taxonomy_uid") String uid,
28+
@Body JSONObject body);
2429

2530
@DELETE("taxonomies/{taxonomy_uid}")
26-
Call<ResponseBody> delete(@HeaderMap Map<String, Object> headers, @Path("taxonomy_uid") String uid);
31+
Call<ResponseBody> delete(
32+
@HeaderMap Map<String, Object> headers,
33+
@Path("taxonomy_uid") String uid);
2734

2835
//--Terms--
2936
@POST("taxonomies/{taxonomy_uid}/terms")
30-
Call<ResponseBody> createTerm(@HeaderMap HashMap<String, Object> headers, @Path("taxonomy_uid") String taxonomyId, @Body JSONObject body);
37+
Call<ResponseBody> createTerm(
38+
@HeaderMap HashMap<String, Object> headers,
39+
@Path("taxonomy_uid") String taxonomyId,
40+
@Body JSONObject body);
3141

3242
@GET("taxonomies/{taxonomy_uid}/terms")
33-
Call<ResponseBody> findTerm(@HeaderMap HashMap<String, Object> headers, @Path("taxonomy_uid") String taxonomyId, @QueryMap Map<String, Object> queryParams);
43+
Call<ResponseBody> findTerm(
44+
@HeaderMap HashMap<String, Object> headers,
45+
@Path("taxonomy_uid") String taxonomyId,
46+
@QueryMap Map<String, Object> queryParams);
3447

3548
@GET("taxonomies/{taxonomy_uid}/terms/{term_id}")
36-
Call<ResponseBody> fetchTerm(@HeaderMap HashMap<String, Object> headers, @Path("taxonomy_uid") String taxonomyId, @Path("term_id") String termId, @QueryMap Map<String, Object> queryParams);
49+
Call<ResponseBody> fetchTerm(
50+
@HeaderMap HashMap<String, Object> headers,
51+
@Path("taxonomy_uid") String taxonomyId,
52+
@Path("term_id") String termId,
53+
@QueryMap Map<String, Object> queryParams);
3754

3855
@GET("taxonomies/{taxonomy_uid}/terms/{term_id}/descendants")
39-
Call<ResponseBody> descendantsTerm(@HeaderMap HashMap<String, Object> headers, @Path("taxonomy_uid") String taxonomyId, @Path("term_id") String termId, @QueryMap Map<String, Object> queryParams);
56+
Call<ResponseBody> descendantsTerm(
57+
@HeaderMap HashMap<String, Object> headers,
58+
@Path("taxonomy_uid") String taxonomyId,
59+
@Path("term_id") String termId,
60+
@QueryMap Map<String, Object> queryParams);
4061

4162
@GET("taxonomies/{taxonomy_uid}/terms/{term_id}/ancestors")
42-
Call<ResponseBody> ancestorsTerm(@HeaderMap HashMap<String, Object> headers, @Path("taxonomy_uid") String taxonomyId, @Path("term_id") String termId, @QueryMap Map<String, Object> queryParams);
63+
Call<ResponseBody> ancestorsTerm(
64+
@HeaderMap HashMap<String, Object> headers,
65+
@Path("taxonomy_uid") String taxonomyId,
66+
@Path("term_id") String termId,
67+
@QueryMap Map<String, Object> queryParams);
4368

4469
@PUT("taxonomies/{taxonomy_uid}/terms/{term_id}")
45-
Call<ResponseBody> updateTerm(@HeaderMap HashMap<String, Object> headers, @Path("taxonomy_uid") String taxonomyId, @Path("term_id") String termId, @Body JSONObject body);
46-
47-
@GET("taxonomies/all/terms?term={term_string}")
48-
Call<ResponseBody> searchTerm(@HeaderMap HashMap<String, Object> headers, @Path("term_string") String termString);
49-
70+
Call<ResponseBody> updateTerm(
71+
@HeaderMap HashMap<String, Object> headers,
72+
@Path("taxonomy_uid") String taxonomyId,
73+
@Path("term_id") String termId,
74+
@Body JSONObject body);
75+
76+
77+
@GET("taxonomies/all/terms")
78+
Call<ResponseBody> searchTerm(
79+
@HeaderMap HashMap<String, Object> headers,
80+
@Query("term") String termString
81+
);
5082
}

src/main/java/com/contentstack/cms/stack/Terms.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ public Call<ResponseBody> descendants(@NotNull String termUid) {
222222
* Term term = stack.taxonomy("taxonomyId").terms().ancestors("termId");
223223
* }
224224
* </pre>
225-
*
226225
*/
227226
public Call<ResponseBody> ancestors(@NotNull String termUid) {
228227
return this.taxonomyService.ancestorsTerm(this.headers, this.taxonomyId, termUid, this.params);
@@ -231,7 +230,7 @@ public Call<ResponseBody> ancestors(@NotNull String termUid) {
231230
/**
232231
* @param termUid - The term for which we need the details
233232
* @param body the JSONObject body for the request
234-
* @return instance of Call
233+
* @return instance of Call
235234
* <p>
236235
* <b>Example</b>
237236
* <pre>
@@ -257,16 +256,20 @@ public Call<ResponseBody> update(@NotNull String termUid, @NotNull JSONObject bo
257256
* @param termString: The string for which we need to search for the matching terms, should either match with term uid or term name
258257
* @return instance of Call
259258
* <p>
260-
259+
*
261260
* <b>Example</b>
262261
* <pre>
263262
* {@code
264263
* Stack stack = new Contentstack.Builder().build().stack(headers);
265264
* Term term = stack.taxonomy("taxonomyId").terms().search("search anything");
266265
* }
267266
* </pre>
267+
* @throws IllegalArgumentException if the termString is empty
268268
*/
269269
public Call<ResponseBody> search(@NotNull String termString) {
270+
if (termString.isEmpty()) {
271+
throw new IllegalArgumentException("termString must not be empty");
272+
}
270273
return this.taxonomyService.searchTerm(this.headers, termString);
271274
}
272275

src/test/java/com/contentstack/cms/TestClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class TestClient {
1717
public final static String API_KEY = (env.get("apiKey") != null) ? env.get("apiKey") : "apiKey99999999";
1818
public final static String MANAGEMENT_TOKEN = (env.get("managementToken") != null) ? env.get("managementToken") : "managementToken99999999";
1919

20+
public final static String DEV_HOST = (env.get("dev_host") != null) ? env.get("dev_host") : "***REMOVED***";
2021
private static Contentstack instance;
2122
private static Stack stackInstance;
2223

src/test/java/com/contentstack/cms/stack/TaxonomyTest.java

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,32 @@ void updateTest() {
138138
@Test
139139
void deleteTest() {
140140
Request request = taxonomy.delete("taxonomyId").request();
141-
Assertions.assertEquals(2, request.headers().names().size());
141+
Assertions.assertEquals(5, request.headers().names().size());
142+
Assertions.assertEquals("DELETE", request.method());
143+
Assertions.assertTrue(request.url().isHttps());
144+
Assertions.assertEquals("api.contentstack.io", request.url().host());
145+
Assertions.assertEquals(3, request.url().pathSegments().size());
146+
Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1));
147+
Assertions.assertNull(request.body());
148+
Assertions.assertNull(request.url().encodedQuery());
149+
Assertions.assertEquals("https://api.contentstack.io/v3/taxonomies/taxonomyId", request.url().toString());
150+
}
151+
152+
153+
@Test
154+
void deleteTestWithHeaders() {
155+
taxonomy.clearParams();
156+
taxonomy.addHeader("Content-Type", "application/json");
157+
HashMap<String, String> headers = new HashMap<>();
158+
HashMap<String, Object> params = new HashMap<>();
159+
headers.put("key_param1", "key_param_value");
160+
headers.put("key_param2", "key_param_value");
161+
params.put("key_param3", "key_param_value");
162+
params.put("key_param4", "key_param_value");
163+
taxonomy.addHeaders(headers);
164+
taxonomy.addParams(params);
165+
Request request = taxonomy.delete("taxonomyId").request();
166+
Assertions.assertEquals(5, request.headers().names().size());
142167
Assertions.assertEquals("DELETE", request.method());
143168
Assertions.assertTrue(request.url().isHttps());
144169
Assertions.assertEquals("api.contentstack.io", request.url().host());
@@ -149,6 +174,22 @@ void deleteTest() {
149174
Assertions.assertEquals("https://api.contentstack.io/v3/taxonomies/taxonomyId", request.url().toString());
150175
}
151176

177+
178+
@Test
179+
void createTest() {
180+
JSONObject obj = new JSONObject();
181+
obj.put("name", "sample");
182+
Request request = taxonomy.create(obj).request();
183+
Assertions.assertEquals(4, request.headers().names().size());
184+
Assertions.assertEquals("POST", request.method());
185+
Assertions.assertTrue(request.url().isHttps());
186+
Assertions.assertEquals("api.contentstack.io", request.url().host());
187+
Assertions.assertEquals(2, request.url().pathSegments().size());
188+
Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1));
189+
Assertions.assertNull(request.url().encodedQuery());
190+
Assertions.assertEquals("https://api.contentstack.io/v3/taxonomies", request.url().toString());
191+
}
192+
152193
@Test
153194
void testCreateTerm() {
154195
terms.clearParams();
@@ -198,7 +239,6 @@ void testFetchTerm() {
198239
terms.clearParams();
199240
terms.addParam("include_referenced_entries_count", true);
200241
terms.addParam("include_children_count", false);
201-
202242
Request request = terms.fetch(_uid).request();
203243
Assertions.assertEquals(2, request.headers().names().size());
204244
Assertions.assertEquals("GET", request.method());
@@ -214,6 +254,38 @@ void testFetchTerm() {
214254
Assertions.assertEquals("https://api.contentstack.io/v3/taxonomies/auth999999999/terms/auth999999999?include_children_count=false&include_referenced_entries_count=true", request.url().toString());
215255
}
216256

257+
258+
@Test
259+
void testTermUpdate() {
260+
terms.clearParams();
261+
HashMap<String, String> headers = new HashMap<>();
262+
HashMap<String, Object> params = new HashMap<>();
263+
terms.addParam("include_referenced_entries_count", true);
264+
terms.addParam("include_children_count", false);
265+
terms.addHeader("Accept-Encoding", "UTF-8");
266+
headers.put("Accept-Encoding", "UTF-8");
267+
params.put("include_children_count", "true");
268+
terms.addParams(params);
269+
terms.addHeaders(headers);
270+
Request request = terms.update(_uid, new JSONObject()).request();
271+
Assertions.assertEquals(3, request.headers().names().size());
272+
Assertions.assertEquals("PUT", request.method());
273+
Assertions.assertTrue(request.url().isHttps());
274+
Assertions.assertEquals("api.contentstack.io", request.url().host());
275+
Assertions.assertEquals(5, request.url().pathSegments().size());
276+
}
277+
278+
@Test
279+
void testTermSearch() {
280+
terms.clearParams();
281+
Request request = terms.search("contentstack").request();
282+
Assertions.assertEquals(2, request.headers().names().size());
283+
Assertions.assertEquals("GET", request.method());
284+
Assertions.assertTrue(request.url().isHttps());
285+
Assertions.assertEquals("api.contentstack.io", request.url().host());
286+
Assertions.assertEquals(4, request.url().pathSegments().size());
287+
}
288+
217289
@Test
218290
void testDescendantsTerm() {
219291
terms.clearParams();
@@ -239,7 +311,7 @@ void testAncestorsTerm() {
239311
terms.addParam("include_referenced_entries_count", true);
240312
terms.addParam("include_children_count", false);
241313
Request request = terms.ancestors("termId45").request();
242-
Assertions.assertEquals(2, request.headers().names().size());
314+
Assertions.assertEquals(3, request.headers().names().size());
243315
Assertions.assertEquals("GET", request.method());
244316
Assertions.assertTrue(request.url().isHttps());
245317
Assertions.assertEquals("api.contentstack.io", request.url().host());
@@ -256,14 +328,13 @@ void testAncestorsTerm() {
256328
@Test
257329
void findTestAPI() throws IOException {
258330
Taxonomy taxonomy = new Contentstack.Builder()
259-
.setAuthtoken("blt67b95aeb964f5262")
331+
.setAuthtoken(TestClient.AUTHTOKEN)
260332
.setHost("***REMOVED***")
261333
.build()
262334
.stack("blt12c1ba95c1b11e88", "")
263335
.taxonomy();
264336
Response<ResponseBody> response = taxonomy.addHeader("authtoken", "blt67b95aeb964f5262").find().execute();
265337
System.out.println(response);
266-
//Assertions.assertEquals(2, request.headers().names().size());
267338
}
268339

269340
}

0 commit comments

Comments
 (0)