From 0c9b3d3ed0f32264d2d660e3475e16c0edb2d1cd Mon Sep 17 00:00:00 2001 From: Shailesh Mishra Date: Fri, 15 Dec 2023 12:22:40 +0530 Subject: [PATCH] Team Impl --- .../com/contentstack/cms/Contentstack.java | 48 ++- .../cms/core/AuthInterceptor.java | 9 + .../java/com/contentstack/cms/core/Util.java | 1 + .../contentstack/cms/stack/AliasService.java | 4 + .../com/contentstack/cms/stack/Stack.java | 11 + .../cms/stack/StackRoleMapping.java | 74 ++++ .../com/contentstack/cms/stack/Taxonomy.java | 21 +- .../cms/stack/TaxonomyService.java | 24 +- .../contentstack/cms/stack/TeamService.java | 36 ++ .../com/contentstack/cms/stack/TeamUsers.java | 67 ++++ .../com/contentstack/cms/stack/Teams.java | 89 +++++ .../com/contentstack/cms/stack/Terms.java | 41 ++- .../contentstack/cms/stack/TaxonomyTest.java | 25 +- .../contentstack/cms/stack/TeamsUnitTest.java | 333 ++++++++++++++++++ 14 files changed, 745 insertions(+), 38 deletions(-) create mode 100644 src/main/java/com/contentstack/cms/stack/StackRoleMapping.java create mode 100644 src/main/java/com/contentstack/cms/stack/TeamService.java create mode 100644 src/main/java/com/contentstack/cms/stack/TeamUsers.java create mode 100644 src/main/java/com/contentstack/cms/stack/Teams.java create mode 100644 src/test/java/com/contentstack/cms/stack/TeamsUnitTest.java diff --git a/src/main/java/com/contentstack/cms/Contentstack.java b/src/main/java/com/contentstack/cms/Contentstack.java index 0d9d4ba8..60db4e6b 100644 --- a/src/main/java/com/contentstack/cms/Contentstack.java +++ b/src/main/java/com/contentstack/cms/Contentstack.java @@ -51,6 +51,7 @@ public class Contentstack { protected final Boolean retryOnFailure; protected final Proxy proxy; protected AuthInterceptor interceptor; + protected String[] earlyAccess; protected User user; /** @@ -80,8 +81,8 @@ public class Contentstack { * @return User * @author ishaileshmishra * @see User - * + * "https://www.contentstack.com/docs/developers/apis/content-management-api/#users">User + * * @since 2022-05-19 */ public User user() { @@ -130,8 +131,8 @@ public User user() { * @throws IOException the IOException * @author ishaileshmishra * @see User - * + * "https://www.contentstack.com/docs/developers/apis/content-management-api/#users">User + * */ public Response login(String emailId, String password) throws IOException { if (this.authtoken != null) @@ -183,10 +184,10 @@ public Response login(String emailId, String password) throws IOEx * @throws IOException the IOException * @author ishaileshmishra * @see Login - * your account - * + * href= + * "https://www.contentstack.com/docs/developers/apis/content-management-api/#log-in-to-your-account">Login + * your account + * */ public Response login(String emailId, String password, String tfaToken) throws IOException { if (this.authtoken != null) @@ -287,11 +288,12 @@ public Organization organization() { * * @param organizationUid The UID of the organization that you want to retrieve * @return the organization - *
- * Example + *
+ * Example * - *
+     * 
      *         Contentstack contentstack = new Contentstack.Builder().build();
+     *         
* Organization org = contentstack.organization(); *
*/ @@ -447,6 +449,7 @@ public Contentstack(Builder builder) { this.retryOnFailure = builder.retry; this.proxy = builder.proxy; this.interceptor = builder.authInterceptor; + this.earlyAccess = builder.earlyAccess; } /** @@ -461,6 +464,7 @@ public static class Builder { private AuthInterceptor authInterceptor; private String authtoken; // authtoken for client + private String[] earlyAccess; private Retrofit instance; // client instance private String hostname = Util.HOST; // Default Host for Contentstack API (default: api.contentstack.io) private String port = Util.PORT; // Default PORT for Contentstack API @@ -488,14 +492,14 @@ public Builder() { *
*

* {@code - * -

+ * + *

* Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("hostname", 433)); * Contentstack contentstack = new Contentstack.Builder().setProxy(proxy).build(); - * -

+ * + *

* } - * + * * @param proxy the proxy * @return the Builder instance */ @@ -578,9 +582,9 @@ public Builder setTimeout(int timeout) { * unit of granularity and provides utility methods to * convert across units * @return instance of Builder - *

- * Example: - * {@code + *

+ * Example: + * {@code * Contentstack cs = new Contentstack.Builder() * .setAuthtoken(AUTHTOKEN) * .setConnectionPool(5, 400, TimeUnit.MILLISECONDS) @@ -604,6 +608,12 @@ public Builder setAuthtoken(String authtoken) { return this; } + + public Builder earlyAccess(String[] earlyAccess) { + this.earlyAccess = earlyAccess; + return this; + } + /** * Build contentstack. * diff --git a/src/main/java/com/contentstack/cms/core/AuthInterceptor.java b/src/main/java/com/contentstack/cms/core/AuthInterceptor.java index 801e5eee..d5b807dc 100644 --- a/src/main/java/com/contentstack/cms/core/AuthInterceptor.java +++ b/src/main/java/com/contentstack/cms/core/AuthInterceptor.java @@ -25,6 +25,7 @@ public class AuthInterceptor implements Interceptor { protected String authtoken; + protected String[] earlyAccess; // The `public AuthInterceptor() {}` is a default constructor for the // `AuthInterceptor` class. It is @@ -51,6 +52,10 @@ public void setAuthtoken(String authtoken) { this.authtoken = authtoken; } + public void setEarlyAccess(String[] earlyAccess) { + this.earlyAccess = earlyAccess; + } + /** * This function intercepts a request and adds headers to it, including a user * agent, content type, and @@ -73,6 +78,10 @@ public Response intercept(Chain chain) throws IOException { if (this.authtoken != null) { request.addHeader(Util.AUTHTOKEN, this.authtoken); } + if (this.earlyAccess!=null && this.earlyAccess.length > 0) { + String commaSeparated = String.join(", ", earlyAccess); + request.addHeader(Util.EARLY_ACCESS_HEADER, commaSeparated); + } return chain.proceed(request.build()); } diff --git a/src/main/java/com/contentstack/cms/core/Util.java b/src/main/java/com/contentstack/cms/core/Util.java index 25adf652..fc02577c 100644 --- a/src/main/java/com/contentstack/cms/core/Util.java +++ b/src/main/java/com/contentstack/cms/core/Util.java @@ -38,6 +38,7 @@ public class Util { public static final String API_KEY = "api_key"; public static final String AUTHORIZATION = "authorization"; public static final String AUTHTOKEN = "authtoken"; + public static final String EARLY_ACCESS_HEADER = "x-header-ea"; public static final String BRANCH = "branch"; public static final String X_USER_AGENT = "X-User-Agent"; public static final String USER_AGENT = "User-Agent"; diff --git a/src/main/java/com/contentstack/cms/stack/AliasService.java b/src/main/java/com/contentstack/cms/stack/AliasService.java index c8f0ec54..e1186f75 100644 --- a/src/main/java/com/contentstack/cms/stack/AliasService.java +++ b/src/main/java/com/contentstack/cms/stack/AliasService.java @@ -7,6 +7,10 @@ import java.util.Map; + +/** + * The interface Alias service. + */ public interface AliasService { @GET("stacks/branch_aliases") diff --git a/src/main/java/com/contentstack/cms/stack/Stack.java b/src/main/java/com/contentstack/cms/stack/Stack.java index 2bd21fd4..6ed37050 100644 --- a/src/main/java/com/contentstack/cms/stack/Stack.java +++ b/src/main/java/com/contentstack/cms/stack/Stack.java @@ -822,6 +822,17 @@ public Alias alias(String aliasUid) { } + /** + * Teams streamline the process of assigning roles and permissions by grouping users together. + * Rather than assigning roles to individual users or at the stack level, you can assign roles directly to a team. This ensures that all users within a team share the same set of role permissions, making role management more efficient. + * + * @return instance of {@link Teams} + */ + public Teams teams() { + return new Teams(this.client, this.headers); + } + + /** * The taxonomy works on data where hierarchy is configured * diff --git a/src/main/java/com/contentstack/cms/stack/StackRoleMapping.java b/src/main/java/com/contentstack/cms/stack/StackRoleMapping.java new file mode 100644 index 00000000..9be7c5cd --- /dev/null +++ b/src/main/java/com/contentstack/cms/stack/StackRoleMapping.java @@ -0,0 +1,74 @@ +package com.contentstack.cms.stack; + +import com.contentstack.cms.BaseImplementation; +import okhttp3.ResponseBody; +import org.jetbrains.annotations.NotNull; +import org.json.simple.JSONObject; +import retrofit2.Call; + +import java.util.HashMap; + +/** + * Teams streamline the process of assigning roles and permissions by grouping users together. + * Rather than assigning roles to individual users or at the stack level, + * you can assign roles directly to a team. + * This ensures that all users within a team share the same set of role permissions, + * making role management more efficient. + */ +public class StackRoleMapping implements BaseImplementation { + + private HashMap headers; + private HashMap params; + final TeamService teamService; + final String teamId; + + public StackRoleMapping(TeamService service, HashMap headers, @NotNull String teamId) { + this.headers.putAll(headers); + this.params = new HashMap<>(); + this.teamId = teamId; + this.teamService = service; + } + + @Override + public StackRoleMapping addParam(@NotNull String key, @NotNull Object value) { + this.params.put(key, value); + return this; + } + + @Override + public StackRoleMapping addHeader(@NotNull String key, @NotNull String value) { + this.headers.put(key, value); + return this; + } + + @Override + public StackRoleMapping addParams(@NotNull HashMap params) { + this.params.putAll(params); + return this; + } + + @Override + public StackRoleMapping addHeaders(@NotNull HashMap headers) { + this.headers.putAll(headers); + return this; + } + + + public Call create(@NotNull JSONObject body) { + return this.teamService.create(this.headers, body); + } + +// public Call find() { +// return this.teamService.find(this.headers, body); +// } +// +// public Call update(@NotNull String stackApiKey, @NotNull JSONObject body) { +// return this.teamService.update(this.headers, this.teamId, stackApiKey, body); +// } +// +// public Call delete(@NotNull String stackApiKey) { +// return this.teamService.delete(this.headers, this.teamId, stackApiKey); +// } + + +} diff --git a/src/main/java/com/contentstack/cms/stack/Taxonomy.java b/src/main/java/com/contentstack/cms/stack/Taxonomy.java index 65f1fb0c..020270e8 100644 --- a/src/main/java/com/contentstack/cms/stack/Taxonomy.java +++ b/src/main/java/com/contentstack/cms/stack/Taxonomy.java @@ -11,7 +11,11 @@ /** - * The type Taxonomy. + * Taxonomy is a system that classifies and organizes content in your collection or system. + * It simplifies finding and retrieving information by categorizing items based on specific + * criteria like their purpose, audience, or other relevant factors. + *

+ * This hierarchical organization streamlines navigation and search processes. */ public class Taxonomy implements BaseImplementation { @@ -147,7 +151,7 @@ public Taxonomy addParams(@NotNull HashMap params) { * * * - * @return the call

Example
     {@code
+     * @return the call Example 
     {@code
      *     Response response = taxonomy.find().execute();
      *     } 
*/ @@ -172,7 +176,7 @@ public Call find() { * * * @param taxonomyId the taxonomy id - * @return the call

Example
     {@code
+     * @return the call Example 
     {@code
      *     Response response = taxonomy.fetch("taxonomyId").execute();
      *     } 
*/ @@ -184,7 +188,7 @@ public Call fetch(@NotNull String taxonomyId) { * Create Taxonomy call. * * @param body the body - * @return the call

Example
     {@code
+     * @return the call Example 
     {@code
      *     JSONObject body = new JSONObject
      *     Response response = taxonomy.create(body).execute();
      *     } 
@@ -198,7 +202,7 @@ public Call create(@NotNull JSONObject body) { * * @param taxonomyId - The taxonomy for which we need to update the details * @param body the body - * @return the call

Example
     {@code
+     * @return the call Example 
     {@code
      *     JSONObject body = new JSONObject();
      *     JSONObject bodyContent = new JSONObject();
      *     bodyContent.put("name", "Taxonomy 1");
@@ -215,7 +219,7 @@ public Call update(@NotNull String taxonomyId, @NotNull JSONObject
      * Delete Taxonomy call.
      *
      * @param taxonomyId - The taxonomy for which we need to update the details
-     * @return the call 

Example
     {@code
+     * @return the call Example 
     {@code
      *     Response response = taxonomy.delete("taxonomyId").execute();
      *     } 
*/ @@ -223,6 +227,7 @@ public Call delete(@NotNull String taxonomyId) { return this.taxonomyService.delete(this.headers, taxonomyId); } + /** * Clear params for internal uses only for testing */ @@ -233,12 +238,11 @@ protected void clearParams() { /** * Get terms information - *

*

Examples

*
      *     {@code
      *     Term terms = stack("authtoken").taxonomy("taxonomyId").term();
-     *     }*
+     *     }
      * 
* * @return instance of {@link Terms} @@ -247,5 +251,4 @@ public Terms terms() { return new Terms(this.taxonomyService, this.headers, this.taxonomyId); } - } diff --git a/src/main/java/com/contentstack/cms/stack/TaxonomyService.java b/src/main/java/com/contentstack/cms/stack/TaxonomyService.java index aebf2cb7..f104c368 100644 --- a/src/main/java/com/contentstack/cms/stack/TaxonomyService.java +++ b/src/main/java/com/contentstack/cms/stack/TaxonomyService.java @@ -11,10 +11,15 @@ public interface TaxonomyService { @GET("taxonomies") - Call find(@HeaderMap Map headers, @QueryMap Map params); + Call find( + @HeaderMap Map headers, + @QueryMap Map params); @GET("taxonomies/{taxonomy_uid}") - Call fetch(@HeaderMap Map headers, @Path("taxonomy_uid") String uid, @QueryMap Map query); + Call fetch( + @HeaderMap Map headers, + @Path("taxonomy_uid") String uid, + @QueryMap Map query); @POST("taxonomies") Call create( @@ -32,7 +37,8 @@ Call delete( @HeaderMap Map headers, @Path("taxonomy_uid") String uid); - //--Terms-- + + // --Terms-- @POST("taxonomies/{taxonomy_uid}/terms") Call createTerm( @HeaderMap HashMap headers, @@ -73,10 +79,16 @@ Call updateTerm( @Path("term_id") String termId, @Body JSONObject body); + @PUT("taxonomies/{taxonomy_uid}/terms/{term_id}/move") + Call reorder( + @HeaderMap HashMap headers, + @Path("taxonomy_uid") String taxonomyId, + @Path("term_id") String termId, + @QueryMap Map queryParams, + @Body JSONObject body); - @GET("taxonomies/all/terms") + @GET("taxonomies/$all/terms") Call searchTerm( @HeaderMap HashMap headers, - @Query("term") String termString - ); + @Query("typeahead") String termString); } diff --git a/src/main/java/com/contentstack/cms/stack/TeamService.java b/src/main/java/com/contentstack/cms/stack/TeamService.java new file mode 100644 index 00000000..7896caf9 --- /dev/null +++ b/src/main/java/com/contentstack/cms/stack/TeamService.java @@ -0,0 +1,36 @@ +package com.contentstack.cms.stack; + +import okhttp3.ResponseBody; +import org.json.simple.JSONObject; +import retrofit2.Call; +import retrofit2.http.*; + +import java.util.Map; + +/** + * Allows all the methods related to Team API members + */ +public interface TeamService { + + @GET("teams") + Call find( + @HeaderMap Map headers, + @QueryMap Map params); + + @POST("teams") + Call create( + @HeaderMap Map headers, + @QueryMap Map params); + + @GET("teams/{teamId}") + Call fetch( + @HeaderMap Map headers, + @QueryMap Map params); + + @PUT("teams/{teamId}") + Call update( + @HeaderMap Map headers, + @Path("teamId") String uid, + @Body JSONObject body); + +} diff --git a/src/main/java/com/contentstack/cms/stack/TeamUsers.java b/src/main/java/com/contentstack/cms/stack/TeamUsers.java new file mode 100644 index 00000000..0574a2f7 --- /dev/null +++ b/src/main/java/com/contentstack/cms/stack/TeamUsers.java @@ -0,0 +1,67 @@ +package com.contentstack.cms.stack; + +import com.contentstack.cms.BaseImplementation; +import okhttp3.ResponseBody; +import org.jetbrains.annotations.NotNull; +import org.json.simple.JSONObject; +import retrofit2.Call; + +import java.util.HashMap; + +/** + * Teams streamline the process of assigning roles and permissions by grouping users together. + * Rather than assigning roles to individual users or at the stack level, + * you can assign roles directly to a team. + * This ensures that all users within a team share the same set of role permissions, + * making role management more efficient. + */ +public class TeamUsers implements BaseImplementation { + + private HashMap headers; + private HashMap params; + final TeamService teamService; + + public TeamUsers(TeamService service, HashMap headers, @NotNull String teamId) { + this.headers.putAll(headers); + this.params = new HashMap<>(); + this.teamService = service; + } + + @Override + public TeamUsers addParam(@NotNull String key, @NotNull Object value) { + this.params.put(key, value); + return this; + } + + @Override + public TeamUsers addHeader(@NotNull String key, @NotNull String value) { + this.headers.put(key, value); + return this; + } + + @Override + public TeamUsers addParams(@NotNull HashMap params) { + this.params.putAll(params); + return this; + } + + @Override + public TeamUsers addHeaders(@NotNull HashMap headers) { + this.headers.putAll(headers); + return this; + } + + + public Call create(@NotNull JSONObject body) { + return this.teamService.create(this.headers, body); + } + +// public Call find() { +// return this.teamService.find(this.headers, body); +// } +// +// public Call delete(@NotNull String teamId) { +// return this.teamService.delete(this.headers, teamId); +// } + +} diff --git a/src/main/java/com/contentstack/cms/stack/Teams.java b/src/main/java/com/contentstack/cms/stack/Teams.java new file mode 100644 index 00000000..1cf40e44 --- /dev/null +++ b/src/main/java/com/contentstack/cms/stack/Teams.java @@ -0,0 +1,89 @@ +package com.contentstack.cms.stack; + +import com.contentstack.cms.BaseImplementation; +import okhttp3.ResponseBody; +import org.jetbrains.annotations.NotNull; +import org.json.simple.JSONObject; +import retrofit2.Call; +import retrofit2.Retrofit; + +import java.util.HashMap; + +/** + * Teams streamline the process of assigning roles and permissions by grouping users together. + * Rather than assigning roles to individual users or at the stack level, + * you can assign roles directly to a team. + * This ensures that all users within a team share the same set of role permissions, + * making role management more efficient. + */ +public class Teams implements BaseImplementation { + + private HashMap headers; + private HashMap params; + final TeamService teamService; + + public Teams(Retrofit client, HashMap headers) { + this.headers.putAll(headers); + this.params = new HashMap<>(); + this.teamService = client.create(TeamService.class); + } + + @Override + public Teams addParam(@NotNull String key, @NotNull Object value) { + this.params.put(key, value); + return this; + } + + @Override + public Teams addHeader(@NotNull String key, @NotNull String value) { + this.headers.put(key, value); + return this; + } + + @Override + public Teams addParams(@NotNull HashMap params) { + this.params.putAll(params); + return this; + } + + @Override + public Teams addHeaders(@NotNull HashMap headers) { + this.headers.putAll(headers); + return this; + } + + +// /** +// * +// * @return Call that conatains response body +// */ +// public Call find() { +// return this.teamService.find(this.headers, this.params); +// } +// +// public Call create(@NotNull JSONObject body) { +// return this.teamService.create(this.headers, body); +// } +// +// public Call fetch(@NotNull String teamId) { +// return this.teamService.fetch(this.headers, teamId); +// } +// +// public Call update(@NotNull String teamId, @NotNull JSONObject body) { +// return this.teamService.update(this.headers, teamId, body); +// } +// +// public Call delete(@NotNull String teamId) { +// return this.teamService.delete(this.headers, teamId); +// } +// +// public TeamUsers users(@NotNull String teamId) { +// return new TeamUsers(this.teamService, this.headers, teamId); +// } +// +// public StackRoleMapping stackRoleMapping(@NotNull String teamId) { +// return new StackRoleMapping(this.teamService, this.headers, teamId); +// } + + +} diff --git a/src/main/java/com/contentstack/cms/stack/Terms.java b/src/main/java/com/contentstack/cms/stack/Terms.java index 61e09a09..9ab07985 100644 --- a/src/main/java/com/contentstack/cms/stack/Terms.java +++ b/src/main/java/com/contentstack/cms/stack/Terms.java @@ -9,7 +9,8 @@ import java.util.HashMap; /** - * provides an implementation + * Terms are the fundamental building blocks in a taxonomy. + * They are used to create hierarchical structures and are integrated into entries to classify and categorize information systematically. *
    *
  • Create a Term
  • *
  • Get all Terms of a Taxonomy
  • @@ -238,6 +239,44 @@ public Call update(@NotNull String termUid, @NotNull JSONObject bo } + /** + * @param termUid The term which we need to move(change the parent) + * @param body the request body + * + * //At Root Level: + * { + * "term": { + * "parent_uid": null, + * "order": 2 + * } + * } + *

    + * //Under an existing Term on a different level: + * { + * "term": { + * "parent_uid": "term_1", + * "order": 5 + * } + * } + *

    + * //Under an existing Term on the same level(Reorder Term): + * { + * "term": { + * "parent_uid": "term_3", + * "order": 1 + * } + * } + * @return Call + * @see #addParam(String, Object) to provide additional params for below : + *

    force - It’s set to false by default, which will give an error if we + * try to move a term which has got child terms within it, when set to true, + * it’ll force move the term(which will also affect the ancestors hierarchy of all it’s child terms) + */ + public Call reorder(@NotNull String termUid, @NotNull JSONObject body) { + return this.taxonomyService.reorder(this.headers, this.taxonomyId, termUid, this.params, body); + } + + /** * Search call. * diff --git a/src/test/java/com/contentstack/cms/stack/TaxonomyTest.java b/src/test/java/com/contentstack/cms/stack/TaxonomyTest.java index a3a77a28..d65d7c3b 100644 --- a/src/test/java/com/contentstack/cms/stack/TaxonomyTest.java +++ b/src/test/java/com/contentstack/cms/stack/TaxonomyTest.java @@ -201,7 +201,7 @@ void testCreateTerm() { term.put("term", termDetails); term.put("parent_uid", null); Request request = terms.create(term).request(); - Assertions.assertEquals(2, request.headers().names().size()); + Assertions.assertEquals(3, request.headers().names().size()); Assertions.assertEquals("POST", request.method()); Assertions.assertTrue(request.url().isHttps()); Assertions.assertEquals("api.contentstack.io", request.url().host()); @@ -221,7 +221,7 @@ void testFindTerm() { terms.clearParams(); terms.addParam("limit", 3); Request request = terms.find().request(); - Assertions.assertEquals(2, request.headers().names().size()); + Assertions.assertEquals(3, request.headers().names().size()); Assertions.assertEquals("GET", request.method()); Assertions.assertTrue(request.url().isHttps()); Assertions.assertEquals("api.contentstack.io", request.url().host()); @@ -279,11 +279,30 @@ void testTermUpdate() { Assertions.assertEquals(5, request.url().pathSegments().size()); } + + @Test + void testTermMoveOrReorder() { + terms.clearParams(); + HashMap headers = new HashMap<>(); + HashMap params = new HashMap<>(); + terms.addHeader("Accept-Encoding", "UTF-8"); + headers.put("Accept-Encoding", "UTF-8"); + params.put("force", true); + terms.addParams(params); + terms.addHeaders(headers); + Request request = terms.reorder(_uid, new JSONObject()).request(); + Assertions.assertEquals(3, request.headers().names().size()); + Assertions.assertEquals("PUT", request.method()); + Assertions.assertTrue(request.url().isHttps()); + Assertions.assertEquals("api.contentstack.io", request.url().host()); + Assertions.assertEquals(6, request.url().pathSegments().size()); + } + @Test void testTermSearch() { terms.clearParams(); Request request = terms.search("contentstack").request(); - Assertions.assertEquals(2, request.headers().names().size()); + Assertions.assertEquals(3, request.headers().names().size()); Assertions.assertEquals("GET", request.method()); Assertions.assertTrue(request.url().isHttps()); Assertions.assertEquals("api.contentstack.io", request.url().host()); diff --git a/src/test/java/com/contentstack/cms/stack/TeamsUnitTest.java b/src/test/java/com/contentstack/cms/stack/TeamsUnitTest.java new file mode 100644 index 00000000..1992f132 --- /dev/null +++ b/src/test/java/com/contentstack/cms/stack/TeamsUnitTest.java @@ -0,0 +1,333 @@ +package com.contentstack.cms.stack; + +import com.contentstack.cms.TestClient; +import org.json.simple.JSONObject; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.TestMethodOrder; + +@Tag("unit") +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class TeamsUnitTest { + + protected static String API_KEY = TestClient.API_KEY; + protected static String _uid = TestClient.AUTHTOKEN; + protected static String MANAGEMENT_TOKEN = TestClient.MANAGEMENT_TOKEN; + protected static Teams teams; + protected static JSONObject body; + + // Create a JSONObject, JSONObject could be created in multiple ways. + // We choose JSONParser that converts string to JSONObject + String theBody = "{\n" + + " \"taxonomy\": {\n" + + " \"name\": \"Taxonomy 1\",\n" + + " \"description\": \"Description updated for Taxonomy 1\"\n" + + " }\n" + + "}"; + +// @BeforeAll +// static void setup() { +// final String AUTHTOKEN = TestClient.AUTHTOKEN; +// HashMap headers = new HashMap<>(); +// headers.put(Util.API_KEY, API_KEY); +// headers.put(Util.AUTHORIZATION, MANAGEMENT_TOKEN); +// Stack stack = new Contentstack.Builder().setAuthtoken(AUTHTOKEN).build().stack(headers); +// teams = stack.teams(); +// +// +// } +// +// @Test +// void findTest() { +// Request request = teams.find().request(); +// Assertions.assertEquals(2, request.headers().names().size()); +// Assertions.assertEquals("GET", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// Assertions.assertEquals(2, request.url().pathSegments().size()); +// Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1)); +// Assertions.assertEquals("v3", request.url().pathSegments().get(0)); +// Assertions.assertNotNull(request.url().encodedQuery()); +// Assertions.assertEquals( +// "https://api.contentstack.io/v3/taxonomies?include_rules=true&include_permissions=true", +// request.url().toString()); +// } +// +// @Test +// void findTestWithParams() { +// teams.addParam("limit", 2); +// teams.addParam("skip", 2); +// teams.addParam("include_count", false); +// Request request = teams.find().request(); +// Assertions.assertEquals(2, request.headers().names().size()); +// Assertions.assertEquals("GET", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// Assertions.assertEquals(2, request.url().pathSegments().size()); +// Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1)); +// Assertions.assertEquals("v3", request.url().pathSegments().get(0)); +// Assertions.assertNotNull(request.url().encodedQuery()); +// Assertions.assertEquals( +// "https://api.contentstack.io/v3/taxonomies?limit=2&skip=2&include_count=false", +// request.url().toString()); +// } +// +// @Test +// void findTestCustomParams() { +// teams.clearParams(); +// teams.addParam("include_rules", true); +// teams.addParam("include_permissions", true); +// Request request = teams.find().request(); +// Assertions.assertEquals(2, request.headers().names().size()); +// Assertions.assertEquals("GET", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// Assertions.assertEquals(2, request.url().pathSegments().size()); +// Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1)); +// Assertions.assertEquals("v3", request.url().pathSegments().get(0)); +// Assertions.assertNotNull(request.url().encodedQuery()); +// Assertions.assertEquals( +// "https://api.contentstack.io/v3/taxonomies?include_rules=true&include_permissions=true", +// request.url().toString()); +// } +// +// @Test +// void fetchTest() { +// Request request = teams.fetch("taxonomyId").request(); +// Assertions.assertEquals(2, request.headers().names().size()); +// Assertions.assertEquals("GET", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// Assertions.assertEquals(3, request.url().pathSegments().size()); +// Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1)); +// Assertions.assertEquals("v3", request.url().pathSegments().get(0)); +// Assertions.assertNotNull(request.url().encodedQuery()); +// Assertions.assertEquals( +// "https://api.contentstack.io/v3/taxonomies/taxonomyId?limit=2&skip=2&include_count=false", +// request.url().toString()); +// } +// +// @Test +// void updateTest() { +// Request request = teams.update("taxonomyId", body).request(); +// Assertions.assertEquals(2, request.headers().names().size()); +// Assertions.assertEquals("PUT", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// Assertions.assertEquals(3, request.url().pathSegments().size()); +// Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1)); +// Assertions.assertNotNull(request.body()); +// Assertions.assertNull(request.url().encodedQuery()); +// Assertions.assertEquals("https://api.contentstack.io/v3/taxonomies/taxonomyId", request.url().toString()); +// } +// +// @Test +// void deleteTest() { +// Request request = teams.delete("taxonomyId").request(); +// Assertions.assertEquals(5, request.headers().names().size()); +// Assertions.assertEquals("DELETE", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// Assertions.assertEquals(3, request.url().pathSegments().size()); +// Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1)); +// Assertions.assertNull(request.body()); +// Assertions.assertNull(request.url().encodedQuery()); +// Assertions.assertEquals("https://api.contentstack.io/v3/taxonomies/taxonomyId", request.url().toString()); +// } +// +// @Test +// void deleteTestWithHeaders() { +// teams.clearParams(); +// teams.addHeader("Content-Type", "application/json"); +// HashMap headers = new HashMap<>(); +// HashMap params = new HashMap<>(); +// headers.put("key_param1", "key_param_value"); +// headers.put("key_param2", "key_param_value"); +// params.put("key_param3", "key_param_value"); +// params.put("key_param4", "key_param_value"); +// teams.addHeaders(headers); +// teams.addParams(params); +// Request request = teams.delete("taxonomyId").request(); +// Assertions.assertEquals(5, request.headers().names().size()); +// Assertions.assertEquals("DELETE", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// Assertions.assertEquals(3, request.url().pathSegments().size()); +// Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1)); +// Assertions.assertNull(request.body()); +// Assertions.assertNull(request.url().encodedQuery()); +// Assertions.assertEquals("https://api.contentstack.io/v3/taxonomies/taxonomyId", request.url().toString()); +// } +// +// @Test +// void createTest() { +// JSONObject obj = new JSONObject(); +// obj.put("name", "sample"); +// Request request = teams.create(obj).request(); +// Assertions.assertEquals(4, request.headers().names().size()); +// Assertions.assertEquals("POST", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// Assertions.assertEquals(2, request.url().pathSegments().size()); +// Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1)); +// Assertions.assertNull(request.url().encodedQuery()); +// Assertions.assertEquals("https://api.contentstack.io/v3/taxonomies", request.url().toString()); +// } +// +// @Test +// void testCreateTerm() { +// terms.clearParams(); +// JSONObject term = new JSONObject(); +// JSONObject termDetails = new JSONObject(); +// termDetails.put("uid", "term_1"); +// termDetails.put("name", "Term 1"); +// termDetails.put("description", "Term 1 Description for Taxonomy 1"); +// term.put("term", termDetails); +// term.put("parent_uid", null); +// Request request = terms.create(term).request(); +// Assertions.assertEquals(2, request.headers().names().size()); +// Assertions.assertEquals("POST", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// Assertions.assertEquals(4, 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.assertNotNull(request.body()); +// Assertions.assertNull(request.url().encodedQuery()); +// Assertions.assertEquals("https://api.contentstack.io/v3/taxonomies/auth999999999/terms", +// request.url().toString()); +// } +// +// @Test +// void testFindTerm() { +// terms.clearParams(); +// terms.addParam("limit", 3); +// Request request = terms.find().request(); +// Assertions.assertEquals(2, request.headers().names().size()); +// Assertions.assertEquals("GET", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// Assertions.assertEquals(4, 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.assertNull(request.body()); +// Assertions.assertEquals("limit=3", request.url().encodedQuery()); +// Assertions.assertEquals("https://api.contentstack.io/v3/taxonomies/" + _uid + "/terms?limit=3", +// request.url().toString()); +// } +// +// @Test +// void testFetchTerm() { +// terms.clearParams(); +// terms.addParam("include_referenced_entries_count", true); +// terms.addParam("include_children_count", false); +// Request request = terms.fetch(_uid).request(); +// Assertions.assertEquals(2, request.headers().names().size()); +// Assertions.assertEquals("GET", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// 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.assertNull(request.body()); +// Assertions.assertEquals("include_children_count=false&include_referenced_entries_count=true", +// request.url().encodedQuery()); +// Assertions.assertEquals( +// "https://api.contentstack.io/v3/taxonomies/auth999999999/terms/auth999999999?include_children_count=false&include_referenced_entries_count=true", +// request.url().toString()); +// } +// +// @Test +// void testTermUpdate() { +// terms.clearParams(); +// HashMap headers = new HashMap<>(); +// HashMap params = new HashMap<>(); +// terms.addParam("include_referenced_entries_count", true); +// terms.addParam("include_children_count", false); +// terms.addHeader("Accept-Encoding", "UTF-8"); +// headers.put("Accept-Encoding", "UTF-8"); +// params.put("include_children_count", "true"); +// terms.addParams(params); +// terms.addHeaders(headers); +// Request request = terms.update(_uid, new JSONObject()).request(); +// Assertions.assertEquals(3, request.headers().names().size()); +// Assertions.assertEquals("PUT", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// Assertions.assertEquals(5, request.url().pathSegments().size()); +// } +// +// @Test +// void testTermSearch() { +// terms.clearParams(); +// Request request = terms.search("contentstack").request(); +// Assertions.assertEquals(2, request.headers().names().size()); +// Assertions.assertEquals("GET", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// Assertions.assertEquals(4, request.url().pathSegments().size()); +// } +// +// @Test +// void testDescendantsTerm() { +// terms.clearParams(); +// terms.addParam("include_count", true); +// Request request = terms.descendants("termId45").request(); +// Assertions.assertEquals(2, request.headers().names().size()); +// Assertions.assertEquals("GET", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// Assertions.assertEquals(6, 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.assertNull(request.body()); +// Assertions.assertEquals("include_count=true", request.url().encodedQuery()); +// Assertions.assertEquals( +// "https://api.contentstack.io/v3/taxonomies/auth999999999/terms/termId45/descendants?include_count=true", +// request.url().toString()); +// } +// +// @Test +// void testAncestorsTerm() { +// terms.clearParams(); +// terms.addParam("include_referenced_entries_count", true); +// terms.addParam("include_children_count", false); +// Request request = terms.ancestors("termId45").request(); +// Assertions.assertEquals(3, request.headers().names().size()); +// Assertions.assertEquals("GET", request.method()); +// Assertions.assertTrue(request.url().isHttps()); +// Assertions.assertEquals("api.contentstack.io", request.url().host()); +// Assertions.assertEquals(6, 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.assertNull(request.body()); +// Assertions.assertEquals("include_children_count=false&include_referenced_entries_count=true", +// request.url().encodedQuery()); +// Assertions.assertEquals( +// "https://api.contentstack.io/v3/taxonomies/auth999999999/terms/termId45/ancestors?include_children_count=false&include_referenced_entries_count=true", +// request.url().toString()); +// } +// +// @Test +// void findTestAPI() throws IOException { +// Taxonomy taxonomy = new Contentstack.Builder() +// .setAuthtoken(TestClient.AUTHTOKEN) +// .setHost("api.contentstack.io") +// .build() +// .stack("blt12c1ba95c1b11e88", "") +// .taxonomy(); +// Response response = taxonomy.addHeader("authtoken", "blt67b95aeb964f5262").find().execute(); +// System.out.println(response); +// } + +}