reorder(@NotNull String termUid, @NotNull JSONObject b
return this.taxonomyService.reorder(this.headers, this.taxonomyId, termUid, this.params, body);
}
+ /**
+ * Localize a term into the given locale.
+ *
+ * @param termUid the term to localize
+ * @param body the request body, e.g. {@code {"term": {"uid": "...", "name": "..."}}}
+ * @param locale the target locale, e.g. {@code hi-in}
+ * @return instance of Call Example
{@code
+ * Stack stack = new Contentstack.Builder().build().stack(headers);
+ * Term term = stack.taxonomy("taxonomyId").terms().localize("termId", body, "hi-in");
+ * }
+ */
+ public Call localize(@NotNull String termUid, @NotNull JSONObject body, @NotNull String locale) {
+ this.params.put("locale", locale);
+ return this.taxonomyService.localizeTerm(this.headers, this.taxonomyId, termUid, this.params, body);
+ }
+
+ /**
+ * Unlocalize a term from the given locale.
+ *
+ * @param termUid the term to unlocalize
+ * @param locale the locale to remove, e.g. {@code hi-in}
+ * @return instance of Call Example
{@code
+ * Stack stack = new Contentstack.Builder().build().stack(headers);
+ * Term term = stack.taxonomy("taxonomyId").terms().unlocalize("termId", "hi-in");
+ * }
+ */
+ public Call unlocalize(@NotNull String termUid, @NotNull String locale) {
+ this.params.put("locale", locale);
+ return this.taxonomyService.unlocalizeTerm(this.headers, this.taxonomyId, termUid, this.params);
+ }
+
/**
* Search call.
diff --git a/src/test/java/com/contentstack/cms/UnitTestSuite.java b/src/test/java/com/contentstack/cms/UnitTestSuite.java
index 7dd1432a..5b84fb52 100644
--- a/src/test/java/com/contentstack/cms/UnitTestSuite.java
+++ b/src/test/java/com/contentstack/cms/UnitTestSuite.java
@@ -8,6 +8,7 @@
import com.contentstack.cms.stack.GlobalFieldUnitTests;
import com.contentstack.cms.stack.LocaleUnitTest;
import com.contentstack.cms.stack.ReleaseUnitTest;
+import com.contentstack.cms.stack.TaxonomyTest;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;
@@ -34,7 +35,8 @@
EnvironmentUnitTest.class,
GlobalFieldUnitTests.class,
LocaleUnitTest.class,
- ReleaseUnitTest.class
+ ReleaseUnitTest.class,
+ TaxonomyTest.class
})
public class UnitTestSuite {
}
diff --git a/src/test/java/com/contentstack/cms/stack/TaxonomyAPITest.java b/src/test/java/com/contentstack/cms/stack/TaxonomyAPITest.java
index c1d61f3c..a8d762dd 100644
--- a/src/test/java/com/contentstack/cms/stack/TaxonomyAPITest.java
+++ b/src/test/java/com/contentstack/cms/stack/TaxonomyAPITest.java
@@ -215,6 +215,96 @@ void descendantsTerm(){
Assertions.assertNull(request.body());
Assertions.assertEquals("include_count=true", request.url().encodedQuery()); }
+ @Test
+ void publishTaxonomy() {
+ JSONObject publishBody = Utils.readJson("mocktaxonomy/publish.json");
+ Request request = taxonomy.publish(publishBody).request();
+ Assertions.assertEquals("POST", request.method());
+ Assertions.assertTrue(request.url().isHttps());
+ Assertions.assertEquals(3, request.url().pathSegments().size());
+ Assertions.assertEquals("v3", request.url().pathSegments().get(0));
+ Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1));
+ Assertions.assertEquals("publish", request.url().pathSegments().get(2));
+ Assertions.assertNotNull(request.body());
+ }
+
+ @Test
+ void unpublishTaxonomy() {
+ JSONObject unpublishBody = Utils.readJson("mocktaxonomy/publish.json");
+ Request request = taxonomy.unpublish(unpublishBody).request();
+ Assertions.assertEquals("POST", request.method());
+ Assertions.assertTrue(request.url().isHttps());
+ Assertions.assertEquals(3, request.url().pathSegments().size());
+ Assertions.assertEquals("v3", request.url().pathSegments().get(0));
+ Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1));
+ Assertions.assertEquals("unpublish", request.url().pathSegments().get(2));
+ Assertions.assertNotNull(request.body());
+ }
+
+ @Test
+ void localizeTaxonomy() {
+ Taxonomy uidTaxonomy = new Contentstack.Builder().setAuthtoken(TestClient.AUTHTOKEN).build()
+ .stack(API_KEY, MANAGEMENT_TOKEN).taxonomy(_uid);
+ JSONObject localizeBody = Utils.readJson("mocktaxonomy/localize.json");
+ Request request = uidTaxonomy.localize(localizeBody, "hi-in").request();
+ Assertions.assertEquals("POST", request.method());
+ Assertions.assertTrue(request.url().isHttps());
+ Assertions.assertEquals(3, request.url().pathSegments().size());
+ Assertions.assertEquals("v3", request.url().pathSegments().get(0));
+ Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1));
+ Assertions.assertEquals(_uid, request.url().pathSegments().get(2));
+ Assertions.assertEquals("locale=hi-in", request.url().encodedQuery());
+ Assertions.assertNotNull(request.body());
+ }
+
+ @Test
+ void unlocalizeTaxonomy() {
+ Taxonomy uidTaxonomy = new Contentstack.Builder().setAuthtoken(TestClient.AUTHTOKEN).build()
+ .stack(API_KEY, MANAGEMENT_TOKEN).taxonomy(_uid);
+ Request request = uidTaxonomy.unlocalize("hi-in").request();
+ Assertions.assertEquals("DELETE", request.method());
+ Assertions.assertTrue(request.url().isHttps());
+ Assertions.assertEquals(3, request.url().pathSegments().size());
+ Assertions.assertEquals("v3", request.url().pathSegments().get(0));
+ Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1));
+ Assertions.assertEquals(_uid, request.url().pathSegments().get(2));
+ Assertions.assertEquals("locale=hi-in", request.url().encodedQuery());
+ Assertions.assertNull(request.body());
+ }
+
+ @Test
+ void localizeTerm() throws IOException {
+ terms.clearParams();
+ JSONObject localizeBody = Utils.readJson("mocktaxonomy/localizeTerm.json");
+ Request request = terms.localize("india", localizeBody, "hi-in").request();
+ Assertions.assertEquals("POST", request.method());
+ Assertions.assertTrue(request.url().isHttps());
+ Assertions.assertEquals(5, request.url().pathSegments().size());
+ Assertions.assertEquals("v3", request.url().pathSegments().get(0));
+ Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1));
+ Assertions.assertEquals(_uid, request.url().pathSegments().get(2));
+ Assertions.assertEquals("terms", request.url().pathSegments().get(3));
+ Assertions.assertEquals("india", request.url().pathSegments().get(4));
+ Assertions.assertEquals("locale=hi-in", request.url().encodedQuery());
+ Assertions.assertNotNull(request.body());
+ }
+
+ @Test
+ void unlocalizeTerm() {
+ terms.clearParams();
+ Request request = terms.unlocalize("india", "hi-in").request();
+ Assertions.assertEquals("DELETE", request.method());
+ Assertions.assertTrue(request.url().isHttps());
+ Assertions.assertEquals(5, request.url().pathSegments().size());
+ Assertions.assertEquals("v3", request.url().pathSegments().get(0));
+ Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1));
+ Assertions.assertEquals(_uid, request.url().pathSegments().get(2));
+ Assertions.assertEquals("terms", request.url().pathSegments().get(3));
+ Assertions.assertEquals("india", request.url().pathSegments().get(4));
+ Assertions.assertEquals("locale=hi-in", request.url().encodedQuery());
+ Assertions.assertNull(request.body());
+ }
+
@Test
void moveTerms(){
terms.clearParams();
diff --git a/src/test/java/com/contentstack/cms/stack/TaxonomyTest.java b/src/test/java/com/contentstack/cms/stack/TaxonomyTest.java
index 693a7db6..d793ccbf 100644
--- a/src/test/java/com/contentstack/cms/stack/TaxonomyTest.java
+++ b/src/test/java/com/contentstack/cms/stack/TaxonomyTest.java
@@ -2,6 +2,7 @@
import com.contentstack.cms.Contentstack;
import com.contentstack.cms.TestClient;
+import com.contentstack.cms.core.ErrorMessages;
import com.contentstack.cms.core.Util;
import okhttp3.Request;
import okhttp3.ResponseBody;
@@ -12,11 +13,12 @@
import retrofit2.Response;
import java.io.IOException;
+import java.util.Collections;
import java.util.HashMap;
@Tag("unit")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
-class TaxonomyTest {
+public class TaxonomyTest {
protected static String API_KEY = TestClient.API_KEY;
protected static String _uid = TestClient.AUTHTOKEN;
@@ -277,6 +279,43 @@ void testAncestorsTerm() {
Assertions.assertEquals("include_children_count=false&include_referenced_entries_count=true",
request.url().encodedQuery()); }
+ @Test
+ void testLocalizeTerm() {
+ terms.clearParams();
+ JSONObject termDetails = new JSONObject();
+ termDetails.put("uid", "term_1");
+ termDetails.put("name", "Term 1 Hindi");
+ JSONObject term = new JSONObject();
+ term.put("term", termDetails);
+ Request request = terms.localize("term_1", term, "hi-in").request();
+ Assertions.assertEquals("POST", request.method());
+ Assertions.assertTrue(request.url().isHttps());
+ Assertions.assertEquals(5, request.url().pathSegments().size());
+ Assertions.assertEquals("v3", request.url().pathSegments().get(0));
+ Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1));
+ Assertions.assertEquals(_uid, request.url().pathSegments().get(2));
+ Assertions.assertEquals("terms", request.url().pathSegments().get(3));
+ Assertions.assertEquals("term_1", request.url().pathSegments().get(4));
+ Assertions.assertEquals("locale=hi-in", request.url().encodedQuery());
+ Assertions.assertNotNull(request.body());
+ }
+
+ @Test
+ void testUnlocalizeTerm() {
+ terms.clearParams();
+ Request request = terms.unlocalize("term_1", "hi-in").request();
+ Assertions.assertEquals("DELETE", request.method());
+ Assertions.assertTrue(request.url().isHttps());
+ Assertions.assertEquals(5, request.url().pathSegments().size());
+ Assertions.assertEquals("v3", request.url().pathSegments().get(0));
+ Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1));
+ Assertions.assertEquals(_uid, request.url().pathSegments().get(2));
+ Assertions.assertEquals("terms", request.url().pathSegments().get(3));
+ Assertions.assertEquals("term_1", request.url().pathSegments().get(4));
+ Assertions.assertEquals("locale=hi-in", request.url().encodedQuery());
+ Assertions.assertNull(request.body());
+ }
+
@Test
void findTestAPI() throws IOException {
Taxonomy taxonomy = new Contentstack.Builder()
@@ -289,6 +328,113 @@ void findTestAPI() throws IOException {
System.out.println(response);
}
+
+ private static Taxonomy newTaxonomy() {
+ HashMap headers = new HashMap<>();
+ headers.put(Util.API_KEY, API_KEY);
+ headers.put(Util.AUTHORIZATION, MANAGEMENT_TOKEN);
+ return new Contentstack.Builder().setAuthtoken(TestClient.AUTHTOKEN).build().stack(headers).taxonomy();
+ }
+
+ private static Taxonomy newTaxonomy(String uid) {
+ HashMap headers = new HashMap<>();
+ headers.put(Util.API_KEY, API_KEY);
+ headers.put(Util.AUTHORIZATION, MANAGEMENT_TOKEN);
+ return new Contentstack.Builder().setAuthtoken(TestClient.AUTHTOKEN).build().stack(headers).taxonomy(uid);
+ }
+
+ @Test
+ void publishTest() {
+ JSONObject publishBody = new JSONObject();
+ publishBody.put("locales", Collections.singletonList("en-us"));
+ publishBody.put("environments", Collections.singletonList("production"));
+ Request request = taxonomy.publish(publishBody).request();
+ Assertions.assertEquals("POST", request.method());
+ Assertions.assertTrue(request.url().isHttps());
+ Assertions.assertEquals(3, request.url().pathSegments().size());
+ Assertions.assertEquals("v3", request.url().pathSegments().get(0));
+ Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1));
+ Assertions.assertEquals("publish", request.url().pathSegments().get(2));
+ Assertions.assertNotNull(request.body());
+ }
+
+ @Test
+ void publishTestWithBranchHeader() {
+ Taxonomy localTaxonomy = newTaxonomy();
+ localTaxonomy.addHeader("branch", "dev");
+ JSONObject publishBody = new JSONObject();
+ publishBody.put("locales", Collections.singletonList("en-us"));
+ Request request = localTaxonomy.publish(publishBody).request();
+ Assertions.assertEquals("dev", request.header("branch"));
+ }
+
+ @Test
+ void unpublishTest() {
+ JSONObject unpublishBody = new JSONObject();
+ unpublishBody.put("locales", Collections.singletonList("en-us"));
+ unpublishBody.put("environments", Collections.singletonList("production"));
+ Request request = taxonomy.unpublish(unpublishBody).request();
+ Assertions.assertEquals("POST", request.method());
+ Assertions.assertTrue(request.url().isHttps());
+ Assertions.assertEquals(3, request.url().pathSegments().size());
+ Assertions.assertEquals("v3", request.url().pathSegments().get(0));
+ Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1));
+ Assertions.assertEquals("unpublish", request.url().pathSegments().get(2));
+ Assertions.assertNotNull(request.body());
+ }
+
+ @Test
+ void localizeTest() {
+ Taxonomy uidTaxonomy = newTaxonomy(_uid);
+ JSONObject taxonomyDetails = new JSONObject();
+ taxonomyDetails.put("uid", _uid);
+ taxonomyDetails.put("name", "Taxonomy 1");
+ JSONObject localizeBody = new JSONObject();
+ localizeBody.put("taxonomy", taxonomyDetails);
+ Request request = uidTaxonomy.localize(localizeBody, "hi-in").request();
+ Assertions.assertEquals("POST", request.method());
+ Assertions.assertTrue(request.url().isHttps());
+ Assertions.assertEquals(3, request.url().pathSegments().size());
+ Assertions.assertEquals("v3", request.url().pathSegments().get(0));
+ Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1));
+ Assertions.assertEquals(_uid, request.url().pathSegments().get(2));
+ Assertions.assertEquals("locale=hi-in", request.url().encodedQuery());
+ Assertions.assertNotNull(request.body());
+ }
+
+ @Test
+ void localizeTestWithoutUidThrows() {
+ JSONObject taxonomyDetails = new JSONObject();
+ taxonomyDetails.put("uid", _uid);
+ taxonomyDetails.put("name", "Taxonomy 1");
+ JSONObject localizeBody = new JSONObject();
+ localizeBody.put("taxonomy", taxonomyDetails);
+ NullPointerException exception = Assertions.assertThrows(NullPointerException.class,
+ () -> taxonomy.localize(localizeBody, "hi-in"));
+ Assertions.assertEquals(ErrorMessages.TAXONOMY_UID_REQUIRED, exception.getMessage());
+ }
+
+ @Test
+ void unlocalizeTest() {
+ Taxonomy uidTaxonomy = newTaxonomy(_uid);
+ Request request = uidTaxonomy.unlocalize("hi-in").request();
+ Assertions.assertEquals("DELETE", request.method());
+ Assertions.assertTrue(request.url().isHttps());
+ Assertions.assertEquals(3, request.url().pathSegments().size());
+ Assertions.assertEquals("v3", request.url().pathSegments().get(0));
+ Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1));
+ Assertions.assertEquals(_uid, request.url().pathSegments().get(2));
+ Assertions.assertEquals("locale=hi-in", request.url().encodedQuery());
+ Assertions.assertNull(request.body());
+ }
+
+ @Test
+ void unlocalizeTestWithoutUidThrows() {
+ NullPointerException exception = Assertions.assertThrows(NullPointerException.class,
+ () -> taxonomy.unlocalize("hi-in"));
+ Assertions.assertEquals(ErrorMessages.TAXONOMY_UID_REQUIRED, exception.getMessage());
+ }
+
@Test
void queryFiltersOnTaxonomy() {
Taxonomy taxonomy = new Contentstack.Builder()
diff --git a/src/test/resources/mocktaxonomy/localize.json b/src/test/resources/mocktaxonomy/localize.json
new file mode 100644
index 00000000..18ae725d
--- /dev/null
+++ b/src/test/resources/mocktaxonomy/localize.json
@@ -0,0 +1,7 @@
+{
+ "taxonomy": {
+ "uid": "sample_one",
+ "name": "Sample One in Hindi",
+ "description": "Description for the sample one taxonomy in Hindi."
+ }
+ }
\ No newline at end of file
diff --git a/src/test/resources/mocktaxonomy/localizeTerm.json b/src/test/resources/mocktaxonomy/localizeTerm.json
new file mode 100644
index 00000000..b2d516c9
--- /dev/null
+++ b/src/test/resources/mocktaxonomy/localizeTerm.json
@@ -0,0 +1,8 @@
+{
+ "term": {
+ "uid": "india",
+ "name": "India in Hindi",
+ "parent_uid": null,
+ "order": 1
+ }
+}
\ No newline at end of file
diff --git a/src/test/resources/mocktaxonomy/publish.json b/src/test/resources/mocktaxonomy/publish.json
new file mode 100644
index 00000000..931580a1
--- /dev/null
+++ b/src/test/resources/mocktaxonomy/publish.json
@@ -0,0 +1,6 @@
+
+{
+ "locales": ["en-us"],
+ "environments": ["production"],
+ "items": [{ "uid": "sample_one" }]
+ }
\ No newline at end of file