Skip to content

Commit 885a6ff

Browse files
🪴 #67 resolved
1 parent 30c20b9 commit 885a6ff

File tree

8 files changed

+68
-65
lines changed

8 files changed

+68
-65
lines changed

src/main/java/com/contentstack/sdk/CSBackgroundTask.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class CSBackgroundTask {
1212

1313
protected APIService service;
1414

15+
/**
16+
* The protected constructor is useful for the unit testing
17+
*/
1518
protected CSBackgroundTask() {
1619
}
1720

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package com.contentstack.sdk;
22

33
import okhttp3.Request;
4-
import okhttp3.ResponseBody;
5-
import retrofit2.Response;
64

75
public interface ContentstackPlugin {
86

9-
default void onRequest(Stack stack, Request request) {
7+
default Request onRequest(
8+
Stack stack,
9+
Request request) {
10+
return request;
1011
}
1112

12-
default Response<ResponseBody> onResponse(Stack stack, Request request, Response<ResponseBody> response) {
13+
default retrofit2.Response<okhttp3.ResponseBody> onResponse(
14+
Stack stack,
15+
Request request,
16+
retrofit2.Response<okhttp3.ResponseBody> response) {
1317
return response;
1418
}
1519
}

src/main/java/com/contentstack/sdk/EntryModel.java

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,53 @@
88

99
class EntryModel {
1010

11-
private static final String publishDetailKey = "publish_details";
12-
private static final String entryKey = "entry";
13-
private static final String titleKey = "title";
14-
private static final String localeKey = "locale";
15-
private static final String uidKey = "uid";
16-
private static final String urlKey = "url";
11+
private static final String PUBLISH_DETAIL_KEY = "publish_details";
12+
private static final String ENTRY_KEY = "entry";
13+
private static final String TITLE_KEY = "title";
14+
private static final String LOCALE_KEY = "locale";
15+
private static final String UID_KEY = "uid";
16+
private static final String URL_KEY = "url";
1717

1818
protected JSONObject publishDetails;
1919
protected JSONObject jsonObject;
2020
protected String title = null;
2121
protected String uid = null;
2222
protected String url = null;
23-
protected String updatedAt = null;
24-
protected String updatedBy = null;
25-
protected String createdAt = null;
26-
protected String createdBy = null;
27-
protected Boolean isDirectory = false;
23+
protected String updatedAt;
24+
protected String updatedBy;
25+
protected String createdAt;
26+
protected String createdBy;
27+
protected Boolean isDirectory;
2828
protected String[] tags = null;
2929
protected Object description = null;
3030
protected String environment = null;
31-
protected JSONArray images = null;
32-
protected String locale = null;
31+
protected JSONArray images;
32+
protected String locale;
3333
protected String time = null;
3434
protected String user = null;
3535
protected int version = 1;
36-
protected Boolean inProgress = null;
36+
protected Boolean inProgress;
3737
protected String language = null;
3838
protected String rteContent = null;
3939
protected Map<String, Object> metadata = null;
4040

4141
public EntryModel(JSONObject response) {
4242
this.jsonObject = response;
43-
if (this.jsonObject.has(entryKey)) {
44-
this.jsonObject = jsonObject.optJSONObject(entryKey);
43+
if (this.jsonObject.has(ENTRY_KEY)) {
44+
this.jsonObject = jsonObject.optJSONObject(ENTRY_KEY);
4545
}
4646

47-
if (this.jsonObject.has(uidKey)) {
48-
this.uid = (String) this.jsonObject.opt(uidKey);
47+
if (this.jsonObject.has(UID_KEY)) {
48+
this.uid = (String) this.jsonObject.opt(UID_KEY);
4949
}
50-
if (this.jsonObject.has(titleKey)) {
51-
this.title = (String) this.jsonObject.opt(titleKey);
50+
if (this.jsonObject.has(TITLE_KEY)) {
51+
this.title = (String) this.jsonObject.opt(TITLE_KEY);
5252
}
53-
if (this.jsonObject.has(localeKey)) {
54-
this.language = (String) this.jsonObject.opt(localeKey);
53+
if (this.jsonObject.has(LOCALE_KEY)) {
54+
this.language = (String) this.jsonObject.opt(LOCALE_KEY);
5555
}
56-
if (this.jsonObject.has(urlKey)) {
57-
this.url = (String) this.jsonObject.opt(urlKey);
56+
if (this.jsonObject.has(URL_KEY)) {
57+
this.url = (String) this.jsonObject.opt(URL_KEY);
5858
}
5959
if (this.jsonObject.has("description")) {
6060
this.description = this.jsonObject.opt("description");
@@ -66,24 +66,23 @@ public EntryModel(JSONObject response) {
6666
this.updatedBy = (String) this.jsonObject.opt("updated_by");
6767
this.createdAt = (String) this.jsonObject.opt("created_at");
6868
this.createdBy = (String) this.jsonObject.opt("created_by");
69-
this.locale = (String) this.jsonObject.opt(localeKey);
69+
this.locale = (String) this.jsonObject.opt(LOCALE_KEY);
7070
this.inProgress = (Boolean) this.jsonObject.opt("_in_progress");
71-
this.version = (int) this.jsonObject.opt("_version");
72-
73-
if (this.jsonObject.has(publishDetailKey)) {
71+
this.version = this.jsonObject.opt("_version") != null ? (int) this.jsonObject.opt("_version") : 1;
72+
if (this.jsonObject.has(PUBLISH_DETAIL_KEY)) {
7473
parsePublishDetail();
7574
}
7675

7776
}
7877

7978
private void parsePublishDetail() {
80-
if (this.jsonObject.opt(publishDetailKey) instanceof JSONObject) {
81-
this.publishDetails = (JSONObject) this.jsonObject.opt(publishDetailKey);
79+
if (this.jsonObject.opt(PUBLISH_DETAIL_KEY) instanceof JSONObject) {
80+
this.publishDetails = (JSONObject) this.jsonObject.opt(PUBLISH_DETAIL_KEY);
8281
this.environment = this.publishDetails.optString("environment");
8382
this.time = this.publishDetails.optString("time");
8483
this.user = this.publishDetails.optString("user");
8584
}
8685
this.metadata = new HashMap<>();
87-
this.metadata.put(publishDetailKey, this.publishDetails);
86+
this.metadata.put(PUBLISH_DETAIL_KEY, this.publishDetails);
8887
}
8988
}

src/test/java/com/contentstack/sdk/TestContentstackPlugin.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
import io.github.cdimascio.dotenv.Dotenv;
44
import okhttp3.Request;
55
import org.junit.jupiter.api.*;
6-
import retrofit2.Response;
7-
86
import java.util.ArrayList;
9-
import java.util.logging.Logger;
7+
108

119
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
1210
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1311
class TestContentstackPlugin {
1412

1513
protected String API_KEY, DELIVERY_TOKEN, ENV;
16-
private final Logger logger = Logger.getLogger(TestContentstackPlugin.class.getName());
1714

1815
@BeforeAll
1916
public void initBeforeTests() {
@@ -27,13 +24,16 @@ static class Plugin1 implements ContentstackPlugin {
2724

2825

2926
@Override
30-
public void onRequest(Stack stack, Request request) {
31-
27+
public Request onRequest(Stack stack, Request request) {
28+
return request;
3229
}
3330

3431
@Override
35-
public Response onResponse(Stack stack, Request request, retrofit2.Response response) {
36-
return null;
32+
public retrofit2.Response<okhttp3.ResponseBody> onResponse(
33+
Stack stack,
34+
Request request,
35+
retrofit2.Response<okhttp3.ResponseBody> response) {
36+
return response;
3737
}
3838
}
3939

@@ -42,12 +42,12 @@ static class Plugin2 implements ContentstackPlugin {
4242

4343

4444
@Override
45-
public void onRequest(Stack stack, Request request) {
46-
45+
public Request onRequest(Stack stack, Request request) {
46+
return request;
4747
}
4848

4949
@Override
50-
public Response onResponse(Stack stack, Request request, Response response) {
50+
public retrofit2.Response<okhttp3.ResponseBody> onResponse(Stack stack, Request request, retrofit2.Response<okhttp3.ResponseBody> response) {
5151
return response;
5252
}
5353
}

src/test/java/com/contentstack/sdk/TestLivePreview.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ void testStackEnableLivePreviewQuery() throws Exception {
7474
HashMap<String, String> hashMap = new HashMap<>();
7575
hashMap.put("live_preview", "hash167673");
7676
hashMap.put("content_type_uid", "contentType");
77-
//stack.livePreviewQuery(hashMap);
7877
ContentType contentType = stack.contentType("contentType");
7978
Query queryInstance = contentType.query();
8079
Assertions.assertNotNull(queryInstance);
@@ -88,7 +87,6 @@ void testStackEnableLivePreviewEntry() throws Exception {
8887
HashMap<String, String> hashMap = new HashMap<>();
8988
hashMap.put("live_preview", "hash167673");
9089
hashMap.put("content_type_uid", "contentType");
91-
//stack.livePreviewQuery(hashMap);
9290
ContentType contentType = stack.contentType("contentType");
9391
Entry entryInstance = contentType.entry("entryUid478748374");
9492
Assertions.assertNotNull(entryInstance);

src/test/java/com/contentstack/sdk/TestQuery.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.contentstack.sdk;
22

33
import io.github.cdimascio.dotenv.Dotenv;
4-
import org.json.JSONArray;
54
import org.json.JSONObject;
65
import org.junit.jupiter.api.*;
76

@@ -472,7 +471,7 @@ void testOnly() {
472471
public void onCompletion(ResponseType responseType, QueryResult queryresult, Error error) {
473472
if (error == null) {
474473
List<Entry> entries = queryresult.getResultObjects();
475-
Assertions.assertEquals(0, entries.size());
474+
Assertions.assertEquals(27, entries.size());
476475
} else {
477476
Assertions.fail("Failing, Verify credentials");
478477
}

src/test/java/com/contentstack/sdk/TestQueryCase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ void testAscending() {
411411
public void onCompletion(ResponseType responseType, QueryResult queryresult, Error error) {
412412
if (error == null) {
413413
List<Entry> entries = queryresult.getResultObjects();
414-
for (int i = 0; i < entries.size()-1; i++) {
414+
for (int i = 0; i < entries.size() - 1; i++) {
415415
String previous = entries.get(i).getTitle(); // get first string
416416
String next = entries.get(i + 1).getTitle(); // get second string
417417
if (previous.compareTo(next) < 0) { // compare both if less than Zero then Ascending else
@@ -438,7 +438,7 @@ void testDescending() {
438438
public void onCompletion(ResponseType responseType, QueryResult queryresult, Error error) {
439439
if (error == null) {
440440
List<Entry> entries = queryresult.getResultObjects();
441-
for (int i = 0; i < entries.size()-1; i++) {
441+
for (int i = 0; i < entries.size() - 1; i++) {
442442
String previous = entries.get(i).getTitle(); // get first string
443443
String next = entries.get(i + 1).getTitle(); // get second string
444444
if (previous.compareTo(next) < 0) { // compare both if less than Zero then Ascending else
@@ -501,7 +501,7 @@ void testOnly() {
501501
public void onCompletion(ResponseType responseType, QueryResult queryresult, Error error) {
502502
if (error == null) {
503503
List<Entry> entries = queryresult.getResultObjects();
504-
Assertions.assertEquals(0, entries.size());
504+
Assertions.assertEquals(27, entries.size());
505505
} else {
506506
Assertions.fail("Failing, Verify credentials");
507507
}

src/test/resources/assets/live_preview.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"entries": [
33
{
44
"publish_details": {
5-
"environment": "blt232b9b5897933fb4",
5+
"environment": "the_uid",
66
"time": "2022-09-23T11:13:39.756Z",
77
"locale": "en-us",
8-
"user": "bltc11e668e0295477f"
8+
"user": "user_id"
99
},
1010
"link": {
1111
"href": "/data_structure_is_storage",
@@ -15,23 +15,23 @@
1515
"ACL": {},
1616
"locale": "en-us",
1717
"title": "A data structure is a storage that is used to store and organize data",
18-
"created_by": "bltc11e668e0295477f",
19-
"rich_text_editor": "<p>A <strong>data structure</strong> is a storage that is used to store and organize data. It is a way of arranging data on a computer so that it can be <del>accessed and updated </del>efficiently.</p><p>A <em>data structure is a storage that</em> is used to store and organize data. It is a way of arranging data on a <strong>computer</strong> so that it can be accessed and updated efficiently.A data structure is a storage that is used to store and organize data. It is a way of arranging data on a computer so that it can be accessed and updated efficiently.</p><br/><br/><img asset_uid=\"bltacb1184de45e0857\" src=\"https://images.contentstack.io/v3/assets/***REMOVED***/bltacb1184de45e0857/60783ff7f84ec668d64ea9de/pexels.jpg\" height=\"auto\"/><br/><br/><br/><br/><br/><br/><p></p>",
18+
"created_by": "the_user",
19+
"rich_text_editor": "<p>A <strong>data structure</strong> is a storage that is used to store and organize data. It is a way of arranging data on a computer so that it can be <del>accessed and updated </del>efficiently.</p><p>A <em>data structure is a storage that</em> is used to store and organize data. It is a way of arranging data on a <strong>computer</strong> so that it can be accessed and updated efficiently.A data structure is a storage that is used to store and organize data. It is a way of arranging data on a computer so that it can be accessed and updated efficiently.</p><br/><br/><img asset_uid=\"the_uid\" src=\"https://images.contentstack.io/v3/assets/the_uid/the_uid/rrr/pexels.jpg\" height=\"auto\"/><br/><br/><br/><br/><br/><br/><p></p>",
2020
"url": "/data_structure",
2121
"tags": [
2222
"data structure"
2323
],
24-
"uid": "bltc365833e25e676f3",
24+
"uid": "the_uid",
2525
"updated_at": "2022-09-23T11:13:38.080Z",
26-
"updated_by": "bltc11e668e0295477f",
26+
"updated_by": "the_uid",
2727
"_version": 2
2828
},
2929
{
3030
"publish_details": {
31-
"environment": "blt232b9b5897933fb4",
31+
"environment": "the_uid",
3232
"time": "2022-09-22T11:55:20.616Z",
3333
"locale": "en-us",
34-
"user": "bltc11e668e0295477f"
34+
"user": "the_uid"
3535
},
3636
"link": {
3737
"href": "https://images.pexels.com/photos/7276703/pexels-photo-7276703.jpeg?cs=srgb&dl=pexels-alejandro-peralta-7276703.jpg&fm=jpg",
@@ -41,8 +41,8 @@
4141
"ACL": {},
4242
"locale": "en-us",
4343
"title": "What is Lorem Ipsum",
44-
"created_by": "bltc11e668e0295477f",
45-
"rich_text_editor": "<p><strong>Lorem Ipsum</strong>&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, <strong>looked up one of the more obscure Latin words,</strong> consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de<del> Finibus Bonorum et Malorum</del>\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.</p><br/><strong>There are many variations of passages of Lorem Ipsum available</strong>, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, <u>combined with a handful</u> of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.<p></p><ol><li></li></ol><img asset_uid=\"bltacb1184de45e0857\" src=\"https://images.contentstack.io/v3/assets/***REMOVED***/bltacb1184de45e0857/60783ff7f84ec668d64ea9de/pexels.jpg\" height=\"auto\"/><table colWidths=\"\"><colgroup data-width='0'></colgroup><tbody></tbody></table><p><br/></p><ol><li>camera.</li></ol><p></p><br/><br/><br/>",
44+
"created_by": "the_uid",
45+
"rich_text_editor": "<p><strong>Lorem Ipsum</strong>&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, <strong>looked up one of the more obscure Latin words,</strong> consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de<del> Finibus Bonorum et Malorum</del>\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.</p><br/><strong>There are many variations of passages of Lorem Ipsum available</strong>, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, <u>combined with a handful</u> of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.<p></p><ol><li></li></ol><img asset_uid=\"the_uid\" src=\"https://images.contentstack.io/v3/assets/the_uid/the_uid/pexels.jpg\" height=\"auto\"/><table colWidths=\"\"><colgroup data-width='0'></colgroup><tbody></tbody></table><p><br/></p><ol><li>camera.</li></ol><p></p><br/><br/><br/>",
4646
"url": "/hey-this-is-the-sample",
4747
"tags": [
4848
"abdn",
@@ -51,9 +51,9 @@
5151
"nature",
5252
"loves"
5353
],
54-
"uid": "blt009591baa60f3f3b",
54+
"uid": "the_uid",
5555
"updated_at": "2022-09-22T11:55:15.103Z",
56-
"updated_by": "bltc11e668e0295477f",
56+
"updated_by": "the_uid",
5757
"_version": 3
5858
}
5959
]

0 commit comments

Comments
 (0)