Skip to content

Commit aa58ae3

Browse files
committed
chore: testcases added for variants support
1 parent 0f2fb2e commit aa58ae3

File tree

5 files changed

+219
-2
lines changed

5 files changed

+219
-2
lines changed

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 USER_ID = (env.get("userId") != null) ? env.get("userId") : "c11e668e0295477f";
1818
public final static String OWNERSHIP = (env.get("ownershipToken") != null) ? env.get("ownershipToken")
1919
: "ownershipTokenId";
20+
// file deepcode ignore NonCryptoHardcodedSecret/test: <please specify a reason of ignoring this>
2021
public final static String API_KEY = (env.get("apiKey") != null) ? env.get("apiKey") : "apiKey99999999";
2122
public final static String MANAGEMENT_TOKEN = (env.get("managementToken") != null) ? env.get("managementToken")
2223
: "managementToken99999999";

src/test/java/com/contentstack/cms/models/LoginDetailTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ void getterSetterUserModelLastName() {
102102
@Test
103103
void getterSetterUserModelUsername() {
104104
UserModel userModel = new UserModel();
105+
// deepcode ignore NoHardcodedCredentials/test: <please specify a reason of ignoring this>
105106
userModel.setUsername("***REMOVED***");
106107
Assertions.assertEquals("***REMOVED***",
107108
userModel.getUsername());

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
StackAPITest.class,
2020
TokenAPITest.class,
2121
OrgApiTests.class,
22-
GlobalFieldAPITest.class
22+
GlobalFieldAPITest.class,
23+
VariantGroupAPITest.class,
24+
VariantGroupTest.class
2325

24-
})
26+
})
2527
public class APISanityTestSuite {
2628

2729
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.contentstack.cms.stack;
2+
3+
import java.io.IOException;
4+
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
8+
import com.contentstack.cms.TestClient;
9+
10+
import okhttp3.Request;
11+
12+
class VariantGroupAPITest {
13+
14+
private static final String API_KEY = TestClient.API_KEY;
15+
private static final String MANAGEMENT_TOKEN = TestClient.MANAGEMENT_TOKEN;
16+
private static final String VARIANT_GROUP_UID = TestClient.VARIANT_GROUP_UID;
17+
private final VariantGroup variantGroup = TestClient.getClient().stack(API_KEY, MANAGEMENT_TOKEN).variantGroup();
18+
19+
@Test
20+
void testFetchVariantGroups() throws IOException, InterruptedException {
21+
variantGroup.addParam("include_count", true);
22+
variantGroup.addParam("include_variant_info", true);
23+
Request request = variantGroup.find().request();
24+
Assertions.assertEquals("GET", request.method());
25+
Assertions.assertEquals("https://api.contentstack.io/v3/variant_groups?include_variant_info=true&include_count=true", request.url().toString());
26+
}
27+
28+
@Test
29+
void testLinkContentTypes() throws IOException, InterruptedException {
30+
VariantGroup variantGroupWithUID = TestClient.getClient().stack(API_KEY, MANAGEMENT_TOKEN).variantGroup(VARIANT_GROUP_UID);
31+
Request request = variantGroupWithUID.linkContentTypes("author", "page").request();
32+
Assertions.assertEquals("PUT", request.method());
33+
Assertions.assertEquals("https://api.contentstack.io/v3/variant_groups/" + VARIANT_GROUP_UID + "/variants", request.url().toString());
34+
}
35+
36+
@Test
37+
void testUnlinkContentTypes() throws IOException, InterruptedException {
38+
VariantGroup variantGroupWithUID = TestClient.getClient().stack(API_KEY, MANAGEMENT_TOKEN).variantGroup(VARIANT_GROUP_UID);
39+
Request request = variantGroupWithUID.unlinkContentTypes("author", "page").request();
40+
Assertions.assertEquals("PUT", request.method());
41+
Assertions.assertEquals("https://api.contentstack.io/v3/variant_groups/" + VARIANT_GROUP_UID + "/variants", request.url().toString());
42+
}
43+
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package com.contentstack.cms.stack;
2+
3+
import java.io.IOException;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
import com.contentstack.cms.TestClient;
12+
13+
import okhttp3.Request;
14+
import okio.Buffer;
15+
16+
class VariantGroupTest {
17+
18+
private static final String API_KEY = TestClient.API_KEY;
19+
private static final String MANAGEMENT_TOKEN = TestClient.MANAGEMENT_TOKEN;
20+
private static final String VARIANT_GROUP_UID = TestClient.VARIANT_GROUP_UID;
21+
private VariantGroup variantGroup;
22+
23+
@BeforeEach
24+
void setUp() {
25+
variantGroup = TestClient.getClient().stack(API_KEY, MANAGEMENT_TOKEN).variantGroup(VARIANT_GROUP_UID);
26+
}
27+
28+
@Test
29+
void testValidate_WithValidUID() {
30+
Assertions.assertDoesNotThrow(() -> variantGroup.validate());
31+
}
32+
33+
@Test
34+
void testValidate_WithNullUID() {
35+
VariantGroup group = TestClient.getClient().stack(API_KEY, MANAGEMENT_TOKEN).variantGroup();
36+
IllegalAccessError exception = Assertions.assertThrows(IllegalAccessError.class, group::validate);
37+
Assertions.assertNotNull(exception.getMessage());
38+
}
39+
40+
@Test
41+
void testValidate_WithEmptyUID() {
42+
VariantGroup group = TestClient.getClient().stack(API_KEY, MANAGEMENT_TOKEN).variantGroup("");
43+
IllegalAccessError exception = Assertions.assertThrows(IllegalAccessError.class, group::validate);
44+
Assertions.assertNotNull(exception.getMessage());
45+
}
46+
47+
@Test
48+
void testLinkContentTypes_WithEmptyArray() {
49+
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class,
50+
() -> variantGroup.linkContentTypes());
51+
Assertions.assertNotNull(exception.getMessage());
52+
}
53+
54+
@Test
55+
void testUnlinkContentTypes_WithEmptyArray() {
56+
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class,
57+
() -> variantGroup.unlinkContentTypes());
58+
Assertions.assertNotNull(exception.getMessage());
59+
}
60+
61+
@Test
62+
void testLinkContentTypes_SingleContentType() throws IOException {
63+
Request request = variantGroup.linkContentTypes("test_content_type").request();
64+
Assertions.assertEquals("PUT", request.method());
65+
Assertions.assertEquals(
66+
"https://api.contentstack.io/v3/variant_groups/" + VARIANT_GROUP_UID + "/variants",
67+
request.url().toString()
68+
);
69+
70+
Assertions.assertNotNull(request.body(), "Request body should not be null");
71+
Buffer buffer = new Buffer();
72+
request.body().writeTo(buffer);
73+
String requestBody = buffer.readUtf8();
74+
75+
// Verify the request body contains all required fields
76+
Assertions.assertTrue(requestBody.contains("\"uid\":\"" + VARIANT_GROUP_UID + "\""), "Request body should contain variant group UID");
77+
Assertions.assertTrue(requestBody.contains("\"content_types\":["), "Request body should contain content_types array");
78+
Assertions.assertTrue(requestBody.contains("\"uid\":\"test_content_type\""), "Request body should contain content type UID");
79+
Assertions.assertTrue(requestBody.contains("\"status\":\"linked\""), "Request body should contain linked status");
80+
Assertions.assertTrue(requestBody.contains("\"branches\":["), "Request body should contain branches array");
81+
Assertions.assertTrue(requestBody.contains("\"main\""), "Request body should contain main branch");
82+
}
83+
84+
@Test
85+
void testUnlinkContentTypes_MultipleContentTypes() throws IOException {
86+
Request request = variantGroup.unlinkContentTypes("type1", "type2", "type3").request();
87+
Assertions.assertEquals("PUT", request.method());
88+
Assertions.assertEquals(
89+
"https://api.contentstack.io/v3/variant_groups/" + VARIANT_GROUP_UID + "/variants",
90+
request.url().toString()
91+
);
92+
93+
Assertions.assertNotNull(request.body(), "Request body should not be null");
94+
Buffer buffer = new Buffer();
95+
request.body().writeTo(buffer);
96+
String requestBody = buffer.readUtf8();
97+
98+
// Verify the request body contains all required fields
99+
Assertions.assertTrue(requestBody.contains("\"uid\":\"" + VARIANT_GROUP_UID + "\""), "Request body should contain variant group UID");
100+
Assertions.assertTrue(requestBody.contains("\"content_types\":["), "Request body should contain content_types array");
101+
102+
// Verify each content type is included with unlinked status
103+
Assertions.assertTrue(requestBody.contains("\"uid\":\"type1\""), "Request body should contain first content type");
104+
Assertions.assertTrue(requestBody.contains("\"uid\":\"type2\""), "Request body should contain second content type");
105+
Assertions.assertTrue(requestBody.contains("\"uid\":\"type3\""), "Request body should contain third content type");
106+
Assertions.assertTrue(requestBody.contains("\"status\":\"unlinked\""), "Request body should contain unlinked status");
107+
108+
// Verify branches
109+
Assertions.assertTrue(requestBody.contains("\"branches\":["), "Request body should contain branches array");
110+
Assertions.assertTrue(requestBody.contains("\"main\""), "Request body should contain main branch");
111+
}
112+
113+
@Test
114+
void testFind_CallsCorrectEndpoint() throws IOException {
115+
variantGroup.addParam("include_count", true);
116+
Request request = variantGroup.find().request();
117+
Assertions.assertEquals("GET", request.method());
118+
Assertions.assertTrue(
119+
request.url().toString().contains("include_count=true"),
120+
"URL should include the added parameter"
121+
);
122+
}
123+
124+
@Test
125+
void testSetBranches_WithList() throws IOException {
126+
List<String> testBranches = Arrays.asList("main", "development", "staging");
127+
variantGroup.setBranches(testBranches);
128+
129+
Request request = variantGroup.linkContentTypes("test_content_type").request();
130+
Assertions.assertNotNull(request.body(), "Request body should not be null");
131+
Buffer buffer = new Buffer();
132+
request.body().writeTo(buffer);
133+
String requestBody = buffer.readUtf8();
134+
135+
for (String branch : testBranches) {
136+
Assertions.assertTrue(
137+
requestBody.contains(branch),
138+
"Request body should contain branch: " + branch
139+
);
140+
}
141+
}
142+
143+
@Test
144+
void testSetBranches_WithVarargs() throws IOException {
145+
variantGroup.setBranches("main", "feature-1", "feature-2");
146+
147+
Request request = variantGroup.linkContentTypes("test_content_type").request();
148+
Assertions.assertNotNull(request.body(), "Request body should not be null");
149+
Buffer buffer = new Buffer();
150+
request.body().writeTo(buffer);
151+
String requestBody = buffer.readUtf8();
152+
153+
Assertions.assertTrue(requestBody.contains("main"));
154+
Assertions.assertTrue(requestBody.contains("feature-1"));
155+
Assertions.assertTrue(requestBody.contains("feature-2"));
156+
}
157+
158+
@Test
159+
void testDefaultBranch() throws IOException {
160+
VariantGroup newGroup = TestClient.getClient().stack(API_KEY, MANAGEMENT_TOKEN).variantGroup("test_uid");
161+
Request request = newGroup.linkContentTypes("test_content_type").request();
162+
Assertions.assertNotNull(request.body(), "Request body should not be null");
163+
Buffer buffer = new Buffer();
164+
request.body().writeTo(buffer);
165+
String requestBody = buffer.readUtf8();
166+
167+
Assertions.assertTrue(requestBody.contains("main"), "Default branch should be 'main'");
168+
}
169+
170+
}

0 commit comments

Comments
 (0)