-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContentstackUnitTest.java
More file actions
415 lines (353 loc) · 14.1 KB
/
ContentstackUnitTest.java
File metadata and controls
415 lines (353 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package com.contentstack.cms;
import com.contentstack.cms.core.AuthInterceptor;
import com.contentstack.cms.organization.Organization;
import com.contentstack.cms.stack.Stack;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.ResponseBody;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import retrofit2.Response;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class ContentstackUnitTest {
@Test
void testDefaultClientInstance() {
Contentstack client = new Contentstack.Builder().build(); Assertions.assertEquals("443", client.port);
Assertions.assertEquals("v3", client.version);
Assertions.assertEquals(30, client.timeout);
Assertions.assertNull(client.authtoken);
Assertions.assertNotNull(client);
}
@Test
void testClientDefaultPort() {
Contentstack client = new Contentstack.Builder().build(); Assertions.assertEquals("443", client.port);
}
@Test
void testClientDefaultHost() {
Contentstack client = new Contentstack.Builder().build(); }
@Test
void testClientAPIDefaultVersion() {
Contentstack client = new Contentstack.Builder().build();
Assertions.assertEquals("v3", client.version);
}
@Test
void testClientDefaultTimeout() {
Contentstack client = new Contentstack.Builder().build();
Assertions.assertEquals(30, client.timeout);
}
@Test
void testClientDefaultAuthtoken() {
Contentstack client = new Contentstack.Builder().build();
Assertions.assertNull(client.authtoken);
}
@Test
void testClientIfNotNull() {
Contentstack client = new Contentstack.Builder().build();
Assertions.assertNull(client.authtoken);
}
@Test
void testClientDefaultHeaders() throws IOException {
Contentstack client = new Contentstack.Builder().build();
Response<ResponseBody> logout = client.logout();
Headers defaultHeaders = logout.headers();
Assertions.assertNotNull(defaultHeaders);
}
@Test
void testDefaultClientInstanceWithoutAuthtoken() {
Contentstack contentstack = new Contentstack.Builder().build();
Assertions.assertNotNull(contentstack);
}
@Test
void testDefaultClientPort() {
Contentstack contentstack = new Contentstack.Builder().setPort("400").build();
Assertions.assertEquals("400", contentstack.port);
}
@Test
void testDefaultClientVersion() {
Contentstack contentstack = new Contentstack.Builder().setVersion("v10").build();
Assertions.assertEquals("v10", contentstack.version);
}
@Test
void testDefaultClientTimeout() {
Contentstack contentstack = new Contentstack.Builder().setTimeout(10).build();
Assertions.assertEquals(10, contentstack.timeout);
}
@Test
void testDefaultClientInstanceWithAuthtoken() {
Contentstack contentstack = new Contentstack.Builder().setAuthtoken("fake_authtoken").build();
Assertions.assertNotNull(contentstack.authtoken);
Assertions.assertEquals("fake_authtoken", contentstack.authtoken);
}
@Test
void testClientSetPortMethod() {
Contentstack contentstack = new Contentstack.Builder()
.setPort("420")
.setHost("cdn.contentstack.io")
.setTimeout(10)
.setVersion("v2")
.build();
Assertions.assertEquals(10, contentstack.timeout);
}
@Test
void setProxy() {
// This is how a developer can set the proxy
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("api.contentstack.io", 433));
Contentstack contentstack = new Contentstack.Builder()
.setProxy(proxy)
.build();
Assertions.assertNotNull(contentstack.proxy.toString());
}
@Test
void setRetryOnFailure() {
Contentstack contentstack = new Contentstack.Builder()
.setRetry(true)
.build();
Assertions.assertEquals(true, contentstack.retryOnFailure);
}
@Test
void setHost() {
String hostDemo = "api.thehostname.com";
Contentstack contentstack = new Contentstack.Builder()
.setHost(hostDemo)
.build();
Assertions.assertEquals(hostDemo, contentstack.host);
}
@Test
void setPort() {
Contentstack contentstack = new Contentstack.Builder()
.setPort("443")
.build();
Assertions.assertEquals("443", contentstack.port);
}
@Test
void setVersion() {
Contentstack contentstack = new Contentstack.Builder()
.setVersion("v8")
.build();
Assertions.assertEquals("v8", contentstack.version);
}
@Test
void setProtocols_http1Only_buildsSuccessfully() {
List<Protocol> protocols = Collections.singletonList(Protocol.HTTP_1_1);
Contentstack contentstack = new Contentstack.Builder()
.setProtocols(protocols)
.build();
Assertions.assertNotNull(contentstack);
}
@Test
void setProtocols_http2AndHttp1_buildsSuccessfully() {
List<Protocol> protocols = List.of(Protocol.HTTP_2, Protocol.HTTP_1_1);
Contentstack contentstack = new Contentstack.Builder()
.setProtocols(protocols)
.build();
Assertions.assertNotNull(contentstack);
}
@Test
void setTimeout() {
Contentstack contentstack = new Contentstack.Builder()
.setTimeout(3)
.build();
Assertions.assertEquals(3, contentstack.timeout);
}
@Test
void setTimeoutZeroThrows() {
Assertions.assertThrows(IllegalArgumentException.class,
() -> new Contentstack.Builder().setTimeout(0));
}
@Test
void setTimeoutNegativeThrows() {
Assertions.assertThrows(IllegalArgumentException.class,
() -> new Contentstack.Builder().setTimeout(-1));
}
@Test
void setReadTimeoutZeroThrows() {
Assertions.assertThrows(IllegalArgumentException.class,
() -> new Contentstack.Builder().setReadTimeout(0));
}
@Test
void setWriteTimeoutNegativeThrows() {
Assertions.assertThrows(IllegalArgumentException.class,
() -> new Contentstack.Builder().setWriteTimeout(-5));
}
@Test
void setConnectTimeoutInvalidThrows() {
Assertions.assertThrows(IllegalArgumentException.class,
() -> new Contentstack.Builder().setConnectTimeout(-1));
}
@Test
void okHttpTimeoutsMatchSetTimeout() {
Contentstack client = new Contentstack.Builder().setTimeout(45).build();
OkHttpClient ok = (OkHttpClient) client.instance.callFactory();
Assertions.assertEquals(45_000, ok.connectTimeoutMillis());
Assertions.assertEquals(45_000, ok.readTimeoutMillis());
Assertions.assertEquals(45_000, ok.writeTimeoutMillis());
}
@Test
void okHttpDefaultTimeoutsUseUtilTimeout() {
Contentstack client = new Contentstack.Builder().build();
OkHttpClient ok = (OkHttpClient) client.instance.callFactory();
int expectedMs = (int) TimeUnit.SECONDS.toMillis(30);
Assertions.assertEquals(expectedMs, ok.connectTimeoutMillis());
Assertions.assertEquals(expectedMs, ok.readTimeoutMillis());
Assertions.assertEquals(expectedMs, ok.writeTimeoutMillis());
}
@Test
void okHttpPerLegOverridesFallBackToSetTimeout() {
Contentstack client = new Contentstack.Builder()
.setTimeout(10)
.setReadTimeout(90)
.build();
OkHttpClient ok = (OkHttpClient) client.instance.callFactory();
Assertions.assertEquals(10_000, ok.connectTimeoutMillis());
Assertions.assertEquals(90_000, ok.readTimeoutMillis());
Assertions.assertEquals(10_000, ok.writeTimeoutMillis());
Assertions.assertEquals(10, client.timeout);
}
@Test
void testSetAuthtoken() {
Contentstack contentstack = new Contentstack.Builder()
.setAuthtoken("authtoken_dummy")
.build();
Assertions.assertEquals("authtoken_dummy", contentstack.authtoken);
}
@Test
void testSetOrganizations() {
Contentstack client = new Contentstack.Builder()
.setAuthtoken(null)
.build();
Assertions.assertNull(client.authtoken);
try {
client.organization();
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
Assertions.assertEquals("Login or configure OAuth to continue. organization", e.getLocalizedMessage());
}
}
@Test
void testSetAuthtokenLogin() {
Contentstack client = new Contentstack.Builder()
.setAuthtoken("fake@authtoken")
.build();
try {
client.login("fake@email.com", "fake@password");
} catch (Exception e) {
Assertions.assertEquals("Operation not allowed. You are already logged in.", e.getMessage());
}
Assertions.assertEquals("fake@authtoken", client.authtoken);
}
@Test
void testSetAuthtokenLoginWithTfa() {
Contentstack client = new Contentstack.Builder().setAuthtoken("fake@authtoken").build();
try {
Map<String, String> params = new HashMap<>();
params.put("tfaToken", "fake@tfa");
client.login("fake@email.com", "fake@password", params);
} catch (Exception e) {
Assertions.assertEquals("Operation not allowed. You are already logged in.", e.getMessage());
}
Assertions.assertEquals("fake@authtoken", client.authtoken);
}
@Test
public void testStackWithAPIKey() {
// Arrange
String apiKey = "123456789";
Contentstack client = new Contentstack.Builder().setAuthtoken("fake@authtoken").build();
Stack stack = client.stack(apiKey);
Assertions.assertNotNull(stack);
}
@Test
public void testStackWithBranch() {
// Arrange
String apiKey = "branch";
Contentstack client = new Contentstack.Builder().setAuthtoken("fake@authtoken").build();
Stack stack = client.stack(apiKey);
Assertions.assertNotNull(stack);
}
@Test
public void testStackWithAllOtherParams() {
// Arrange
Contentstack client = new Contentstack.Builder().setAuthtoken("fake@authtoken").build();
Stack stack = client.stack("apiKey", "management_token", "branch");
Assertions.assertNotNull(stack);
}
@Test
public void testValidOrganization() {
// Arrange
String organizationUid = "org-123";
Contentstack client = new Contentstack.Builder().setAuthtoken("fake@authtoken").build();
Organization organization = client.organization(organizationUid);
Assertions.assertNotNull(organization);
Assertions.assertEquals("org-123", organizationUid);
}
@Test
public void testNullOrganizationUid() {
Contentstack client = new Contentstack.Builder().setAuthtoken("fake@authtoken").build();
Assertions.assertThrows(NullPointerException.class, () -> client.organization(null));
}
@Test
public void testEmptyOrganizationUid() {
Contentstack client = new Contentstack.Builder().setAuthtoken("fake@authtoken").build();
String emptyOrganizationUid = "";
// Act & Assert
Assertions.assertThrows(IllegalStateException.class, () -> client.organization(emptyOrganizationUid));
}
@Test
public void testEarlyAccessHeader() throws IOException, InterruptedException {
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(new MockResponse().setBody("{}"));
AuthInterceptor authInterceptor = new AuthInterceptor();
authInterceptor.setEarlyAccess(new String[]{"Taxonomy"});
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(authInterceptor)
.build();
Request request = new Request.Builder()
.url(mockWebServer.url("/"))
.build();
client.newCall(request).execute();
RecordedRequest recordedRequest = mockWebServer.takeRequest();
String earlyAccessHeader = recordedRequest.getHeader("x-header-ea");
Assertions.assertNotNull(earlyAccessHeader);
Assertions.assertEquals("Taxonomy", earlyAccessHeader);
mockWebServer.shutdown();
}
@Test
public void testEarlyAccessMultipleHeader() throws IOException, InterruptedException {
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(new MockResponse().setBody("{}"));
AuthInterceptor authInterceptor = new AuthInterceptor();
authInterceptor.setEarlyAccess(new String[]{"Taxonomy","Teams"});
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(authInterceptor)
.build();
Request request = new Request.Builder()
.url(mockWebServer.url("/"))
.build();
client.newCall(request).execute();
RecordedRequest recordedRequest = mockWebServer.takeRequest();
String earlyAccessHeader = recordedRequest.getHeader("x-header-ea");
Assertions.assertNotNull(earlyAccessHeader);
Assertions.assertEquals("Taxonomy, Teams", earlyAccessHeader);
mockWebServer.shutdown();
}
@Test
public void testEarlyAccessHeaderEmpty() {
String[] emptyEarlyAccessFeatures = {};
Contentstack client = new Contentstack.Builder()
.earlyAccess(emptyEarlyAccessFeatures)
.build();
Assertions.assertNotNull(client.earlyAccess);
Assertions.assertEquals(0, client.earlyAccess.length);
}
}