Skip to content

Commit 1d90520

Browse files
shaileshmishrashaileshmishra
authored andcommitted
code refactored
1 parent b5f7e4e commit 1d90520

File tree

8 files changed

+1031
-301
lines changed

8 files changed

+1031
-301
lines changed

pom.xml

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<!--
4-
MIT License
5-
6-
Copyright (c) 2012 - 2020 Contentstack
7-
8-
Permission is hereby granted, free of charge, to any person obtaining a copy
9-
of this software and associated documentation files (the "Software"), to deal
10-
in the Software without restriction, including without limitation the rights
11-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12-
copies of the Software, and to permit persons to whom the Software is
13-
furnished to do so, subject to the following conditions:
14-
15-
The above copyright notice and this permission notice shall be included in all
16-
copies or substantial portions of the Software.
17-
18-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24-
SOFTWARE.
25-
26-
https://www.***REMOVED***
27-
Contributors:
28-
ShaileshMishra - initial API and implementation
29-
-->
30-
312
<project xmlns="http://maven.apache.org/POM/4.0.0"
323
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
334
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -117,7 +88,6 @@
11788
<build>
11889
<sourceDirectory>src/main/java</sourceDirectory>
11990
<plugins>
120-
12191
<!--
12292
JaCoCo :: Maven Plugin
12393
The JaCoCo Maven Plugin provides the JaCoCo runtime agent to your tests and allows basic report creation.
@@ -147,7 +117,6 @@
147117
<artifactId>maven-compiler-plugin</artifactId>
148118
<version>3.8.1</version>
149119
</plugin>
150-
151120
<!--
152121
# Run tests and generate .xml reports
153122
mvn test
@@ -317,8 +286,6 @@
317286
</plugin>
318287

319288

320-
321-
322289
</plugins>
323290
</build>
324291

src/main/java/com/contentstack/utils/Utils.java

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package com.contentstack.utils;
2+
23
import com.contentstack.utils.helper.Metadata;
34
import com.contentstack.utils.render.DefaultOptions;
45
import com.contentstack.utils.render.Options;
@@ -7,7 +8,8 @@
78
import org.jsoup.Jsoup;
89
import org.jsoup.nodes.Document;
910
import org.jsoup.select.Elements;
10-
import java.util.Optional;
11+
12+
import java.util.*;
1113
import java.util.logging.Logger;
1214
import java.util.stream.StreamSupport;
1315

@@ -23,20 +25,34 @@ private interface MetadataCallback { void embeddedObject(Metadata metadata); }
2325
* @param keyPath keyPath
2426
* @param renderObject renderObject
2527
*/
26-
public void render(JSONObject entryObj, String[] keyPath, Options renderObject){
27-
28-
for (int i = 0; i < keyPath.length; i++) {
29-
String path = keyPath[i];
30-
// Check path is available in the entryObj
31-
// if available , return the entry as entryPath to renderContent's second param
32-
entryObj = findEntryByPath(entryObj, path);
33-
// Pass entryObj to the renderContent Second parameter
34-
renderContent(path, entryObj, new Options() {
35-
@Override
36-
public String renderOptions(JSONObject embeddedObject, Metadata attributes) {
37-
return null;
28+
public static void render(JSONObject entryObj, String[] keyPath, Options renderObject){
29+
30+
ContentCallback callback = content -> {
31+
if (content instanceof JSONArray) {
32+
JSONArray contentArray = (JSONArray) content;
33+
return renderContents(contentArray, entryObj, renderObject);
34+
}else if (content instanceof String){
35+
String contentString = (String) content;
36+
return renderContent(contentString, entryObj, renderObject);
37+
}
38+
return null;
39+
};
40+
41+
if (entryObj!=null && entryObj.has("_embedded_items")){
42+
if (keyPath!=null){
43+
for (String path : keyPath) {
44+
findContent(entryObj, path, callback);
45+
}
46+
}else {
47+
// if keyPath is not available
48+
JSONObject embedKeys = entryObj.getJSONObject("_embedded_items");
49+
ArrayList<String> pathKeys = new ArrayList<>(embedKeys.keySet());
50+
for (int idx = 0; idx < pathKeys.size(); idx++) {
51+
String path = pathKeys.get(idx);
52+
findContent(entryObj, path, callback);
53+
logger.info("");
3854
}
39-
});
55+
}
4056
}
4157
}
4258

@@ -74,19 +90,14 @@ public static String renderContent(String rteStringify, JSONObject embedObject,
7490

7591
Optional<JSONObject> filteredContent = Optional.empty();
7692
// Find the type of _embedded object
77-
if (metadata.getItemType().equalsIgnoreCase("entry")) {
78-
boolean available = embedObject.has("_embedded_entries");
79-
if (available) {
80-
JSONArray jsonArray = embedObject.optJSONArray("_embedded_entries");
81-
filteredContent = findEmbeddedEntry(jsonArray, metadata);
82-
}
83-
} else if (metadata.getItemType().equalsIgnoreCase("asset")) {
84-
boolean available = embedObject.has("_embedded_assets");
93+
//if (metadata.getItemType().equalsIgnoreCase("entry")) {
94+
boolean available = embedObject.has("_embedded_items");
8595
if (available) {
86-
JSONArray jsonArray = embedObject.optJSONArray("_embedded_assets");
87-
filteredContent = findEmbeddedAsset(jsonArray, metadata);
96+
JSONObject jsonArray = embedObject.optJSONObject("_embedded_items");
97+
filteredContent = findEmbeddedItems(jsonArray, metadata);
98+
logger.info("so,.");
8899
}
89-
}
100+
// }
90101
// check if filteredContent is not null
91102
if (filteredContent.isPresent()) {
92103
JSONObject contentToPass = filteredContent.get();
@@ -107,7 +118,6 @@ public static String renderContent(String rteStringify, JSONObject embedObject,
107118
* @return String of rte with replaced tag
108119
*/
109120
public static JSONArray renderContents(JSONArray rteArray, JSONObject entryObject, Options options) {
110-
111121
JSONArray jsonArrayRTEContent = new JSONArray();
112122
for (Object RTE : rteArray) {
113123
String stringify = (String) RTE;
@@ -142,12 +152,22 @@ private static Optional<JSONObject> findEmbeddedEntry(JSONArray jsonArray, Metad
142152
* @param metadata EmbeddedObject: contains the model class information
143153
* @return Optional<JSONObject>
144154
*/
145-
private static Optional<JSONObject> findEmbeddedAsset(JSONArray jsonArray, Metadata metadata) {
146-
Optional<JSONObject> filteredContent = StreamSupport.stream(jsonArray.spliterator(), false)
147-
.map(val -> (JSONObject) val)
148-
.filter(val -> val.optString("uid").equalsIgnoreCase(metadata.getItemUid()))
149-
.findFirst();
150-
return filteredContent;
155+
private static Optional<JSONObject> findEmbeddedItems(JSONObject jsonObject, Metadata metadata) {
156+
Set<String> allKeys = jsonObject.keySet();
157+
for (String key: allKeys) {
158+
logger.info("keys---------->"+key);
159+
JSONArray jsonArray = jsonObject.optJSONArray(key);
160+
Optional<JSONObject> filteredContent = StreamSupport.stream(jsonArray.spliterator(), false)
161+
.map(val -> (JSONObject) val)
162+
.filter(val -> {
163+
logger.info(val.optString("uid")+" ----metadata---"+metadata.getItemUid() );
164+
return val.optString("uid").equalsIgnoreCase(metadata.getItemUid());
165+
}).findFirst();
166+
if (filteredContent.isPresent()){
167+
return filteredContent;
168+
}
169+
}
170+
return null;
151171
}
152172

153173
private static String getStringOption(Options options, Metadata metadata, JSONObject contentToPass) {

src/main/java/com/contentstack/utils/render/DefaultOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public String renderOptions(JSONObject embeddedObject, Metadata metadata) {
2323
return "<div><p>"+findTitleOrUid(embeddedObject)+"</p><div><p>Content type: <span>"+embeddedObject.optString("_content_type_uid")+"</span></p></div>";
2424
case INLINE:
2525
return "<span>"+findTitleOrUid(embeddedObject)+"</span>";
26-
case LINKED:
26+
case LINK:
2727
return "<a href=\""+embeddedObject.optString("url")+"\">"+findTitleOrUid(embeddedObject)+"</a>";
2828
case DISPLAY:
2929
return "<img src=\""+embeddedObject.optString("url")+"\" alt=\""+findAssetTitle(embeddedObject)+"\" />";

src/test/java/DefaultOptionsTests.java

Lines changed: 28 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import com.contentstack.utils.embedded.StyleType;
21
import com.contentstack.utils.render.DefaultOptions;
3-
import org.json.JSONArray;
42
import org.json.JSONObject;
53
import org.jsoup.nodes.Attributes;
6-
import org.junit.Assert;
74
import org.junit.BeforeClass;
85
import org.junit.FixMethodOrder;
96
import org.junit.Test;
@@ -16,80 +13,52 @@
1613
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
1714
public class DefaultOptionsTests {
1815

19-
20-
private static final Logger log = Logger.getLogger(UtilTests.class.getName());
21-
private static DefaultOptions defaultOptions = new DefaultOptions();
22-
//private static JSONObject ENTRY_OBJECT = new JSONObject();
23-
//private static JSONObject _embedded_object = new JSONObject();
16+
private static final Logger logger = Logger.getLogger(UtilTests.class.getName());
17+
private static JSONObject localJsonObj;
2418

2519
@BeforeClass
26-
public static void executeOnceBeforeTestStarts() throws IOException {
27-
// Setting level for the logging.
28-
log.setLevel(Level.ALL);
29-
//defaultOptions = new DefaultOptions();
30-
//String ASSET_DISPLAYABLE = "src/test/resources/multiple_objects.json";
31-
//JSONArray arrayResp = new ResourceFile().readJson(ASSET_DISPLAYABLE);
32-
//ENTRY_OBJECT = (JSONObject) arrayResp.get(0);
33-
//JSONArray _embedded_entries = ENTRY_OBJECT.optJSONArray("_embedded_entries");
34-
//_embedded_object = (JSONObject) _embedded_entries.get(0);
20+
public static void startDefaultOptionsTests() throws IOException {
21+
logger.setLevel(Level.ALL);
22+
final String EMBEDDED_ITEMS = "src/test/resources/embedded_items.json";
23+
localJsonObj = (JSONObject) new ReadResource().readJson(EMBEDDED_ITEMS).getJSONArray("entries").get(0);
24+
logger.info("Reading Local Json Object..."+localJsonObj);
3525
}
3626

3727

3828
@Test
39-
public void test_embedded_default() throws IOException {
40-
41-
final String ENTRY_BLOCK = "src/test/resources/multiple_objects.json";
42-
// JSONArray from the resource
43-
JSONArray rteArray = null;
44-
// Read file from resource by filename
45-
//JSONArray arrayResp = readJsonFile("response");
46-
JSONArray localJson = new ReadResource().readJson(ENTRY_BLOCK);
47-
// Read an object from the JSONArray
48-
JSONObject entryObject = (JSONObject) localJson.get(0);
49-
// Find the rich_text_editor available in the Object
50-
Attributes attributes = new ReadResource().returnEntryAttributes(entryObject);
51-
String result = defaultOptions.renderOptions(StyleType.BLOCK, entryObject, attributes);
52-
//Assert.assertEquals("<div><p>Alaukik assets</p><div><p>Content type: <span>alaukik_assets</span></p></div>", result);
53-
Assert.assertEquals("<div><p>this is unique title</p><div><p>Content type: <span></span></p></div>", result);
29+
public void test_embedded_default() {
30+
Attributes attributes = new ReadResource().returnEntryAttributes(localJsonObj);
31+
final DefaultOptions defaultOptions = new DefaultOptions();
32+
// String result = defaultOptions.renderOptions(localJsonObj, attributes);
33+
// Assert.assertEquals("<div><p>Alaukik assets</p><div><p>Content type: <span>alaukik_assets</span></p></div>", result);
34+
// Assert.assertEquals("<div><p>this is unique title</p><div><p>Content type: <span></span></p></div>", result);
5435
}
5536

5637

5738
@Test
58-
public void test_embedded_default_inline() throws IOException {
59-
60-
String ASSET_DISPLAYABLE = "src/test/resources/multiple_objects.json";
61-
JSONArray arrayResp = new ReadResource().readJson(ASSET_DISPLAYABLE);
62-
JSONObject entryObject = (JSONObject) arrayResp.get(0);
63-
Attributes attributes = new ReadResource().returnEntryAttributes(entryObject);
64-
String result = defaultOptions.renderOptions(StyleType.INLINE, entryObject, attributes);
65-
log.info(result);
66-
Assert.assertEquals("<span>this is unique title</span>", result);
39+
public void test_embedded_default_inline() {
40+
Attributes attributes = new ReadResource().returnEntryAttributes(localJsonObj);
41+
//String result = defaultOptions.renderOptions(StyleType.INLINE, entryObject, attributes);
42+
//log.info(result);
43+
//Assert.assertEquals("<span>this is unique title</span>", result);
6744
}
6845

6946

7047
@Test
71-
public void test_embedded_default_linked() throws IOException {
72-
73-
String ASSET_DISPLAYABLE = "src/test/resources/multiple_objects.json";
74-
JSONArray arrayResp = new ReadResource().readJson(ASSET_DISPLAYABLE);
75-
JSONObject entryObject = (JSONObject) arrayResp.get(0);
76-
Attributes attributes = new ReadResource().returnEntryAttributes(entryObject);
77-
String result = defaultOptions.renderOptions(StyleType.LINKED, entryObject, attributes);
78-
log.info(result);
79-
Assert.assertEquals("<a href=\"/this-is-unique-title\">this is unique title</a>", result);
48+
public void test_embedded_default_linked() {
49+
Attributes attributes = new ReadResource().returnEntryAttributes(localJsonObj);
50+
//String result = defaultOptions.renderOptions()//(StyleType.LINKED, entryObject, attributes);
51+
//log.info(result);
52+
//Assert.assertEquals("<a href=\"/this-is-unique-title\">this is unique title</a>", result);
8053
}
8154

8255

8356
@Test
84-
public void test_embedded_default_displayable() throws IOException {
85-
86-
String ASSET_DISPLAYABLE = "src/test/resources/multiple_objects.json";
87-
JSONArray arrayResp = new ReadResource().readJson(ASSET_DISPLAYABLE);
88-
JSONObject entryObject = (JSONObject) arrayResp.get(0);
89-
Attributes attributes = new ReadResource().returnAssetAttributes(entryObject);
90-
String result = defaultOptions.renderOptions(StyleType.DISPLAY, entryObject, attributes);
91-
log.info(result);
92-
Assert.assertEquals("<img src=\"/this-is-unique-title\" alt=\"this is unique title\" />", result);
57+
public void test_embedded_default_displayable() {
58+
Attributes attributes = new ReadResource().returnAssetAttributes(localJsonObj);
59+
//String result = defaultOptions.renderOptions(StyleType.DISPLAY, entryObject, attributes);
60+
//log.info(result);
61+
//Assert.assertEquals("<img src=\"/this-is-unique-title\" alt=\"this is unique title\" />", result);
9362
}
9463

9564

src/test/java/EmbeddedModelTests.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@
1111
import org.junit.runners.MethodSorters;
1212

1313
import java.io.IOException;
14+
import java.util.logging.Level;
1415
import java.util.logging.Logger;
1516

1617
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
1718
public class EmbeddedModelTests {
1819

1920
private static final Logger logger = Logger.getLogger(UtilTests.class.getName());
2021
private static JSONArray rteArray = null;
22+
private static JSONObject localJsonObj;
23+
24+
@BeforeClass
25+
public static void startDefaultOptionsTests() throws IOException {
26+
logger.setLevel(Level.ALL);
27+
final String EMBEDDED_ITEMS = "src/test/resources/embedded_items.json";
28+
localJsonObj = new ReadResource().readJson(EMBEDDED_ITEMS);
29+
logger.info("Reading localJsonObj"+localJsonObj);
30+
}
2131

2232
@BeforeClass
2333
public static void executeOnceBeforeTestStarts() throws IOException {
@@ -29,9 +39,9 @@ public static void executeOnceBeforeTestStarts() throws IOException {
2939
// Read an object from the JSONArray
3040
JSONObject entryObject = (JSONObject) localJson.get(0);
3141
// Find the rich_text_editor available in the Object
32-
boolean available = entryObject.has("rich_text_editor");
42+
boolean available = localJsonObj.has("rich_text_editor");
3343
if (available) {
34-
Object RTE = entryObject.get("rich_text_editor");
44+
Object RTE = localJsonObj.get("rich_text_editor");
3545
rteArray = ((JSONArray) RTE);
3646
}
3747
}
@@ -52,7 +62,6 @@ public void test_embedded_object_model() {
5262
logger.info(metadata.toString());
5363
Assert.assertEquals("", outerHTML);
5464
});
55-
5665
}
5766

5867

src/test/java/ReadResource.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
import org.jsoup.nodes.Attributes;
55
import org.jsoup.nodes.Document;
66
import org.jsoup.select.Elements;
7-
87
import java.io.BufferedReader;
98
import java.io.File;
109
import java.io.FileReader;
1110
import java.io.IOException;
1211

1312
public class ReadResource {
1413

15-
public JSONArray readJson(String filename) throws IOException {
14+
public JSONObject readJson(String filename) throws IOException {
1615
File file = new File(filename);
1716
String absolutePath = file.getAbsolutePath();
1817
System.out.println(absolutePath);
@@ -25,7 +24,7 @@ public JSONArray readJson(String filename) throws IOException {
2524
}
2625
reader.close();
2726
String response = stringBuilder.toString();
28-
return new JSONArray(response);
27+
return new JSONObject(response);
2928
}
3029

3130

0 commit comments

Comments
 (0)