Skip to content

Commit b43e79d

Browse files
committed
Added test cases and addressed review comments
1 parent c5c6bde commit b43e79d

File tree

6 files changed

+98
-11
lines changed

6 files changed

+98
-11
lines changed

bundles/af-core/src/test/java/com/adobe/cq/forms/core/Utils.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@
5353
*/
5454
public class Utils {
5555

56-
public static InputStream getJson(Object model) {
56+
public static InputStream getJson(Object model, Class<? extends Views.Publish> viewType) {
5757
Writer writer = new StringWriter();
5858
ObjectMapper mapper = new ObjectMapper();
5959
PageModuleProvider pageModuleProvider = new PageModuleProvider();
6060
mapper.registerModule(pageModuleProvider.getModule());
6161
DefaultMethodSkippingModuleProvider defaultMethodSkippingModuleProvider = new DefaultMethodSkippingModuleProvider();
6262
mapper.registerModule(defaultMethodSkippingModuleProvider.getModule());
6363
try {
64-
mapper.writerWithView(Views.Publish.class).writeValue(writer, model);
64+
mapper.writerWithView(viewType).writeValue(writer, model);
6565
} catch (IOException e) {
6666
fail(String.format("Unable to generate JSON export for model %s: %s", model.getClass().getName(),
6767
e.getMessage()));
@@ -77,7 +77,7 @@ public static InputStream getCompleteJson(Object model) {
7777
DefaultMethodSkippingModuleProvider defaultMethodSkippingModuleProvider = new DefaultMethodSkippingModuleProvider();
7878
mapper.registerModule(defaultMethodSkippingModuleProvider.getModule());
7979
try {
80-
mapper.writer().writeValue(writer, model);
80+
mapper.writerWithView(Views.Author.class).writeValue(writer, model);
8181
} catch (IOException e) {
8282
fail(String.format("Unable to generate JSON export for model %s: %s", model.getClass().getName(),
8383
e.getMessage()));
@@ -95,8 +95,8 @@ public static InputStream getCompleteJson(Object model) {
9595
* @param expectedJsonResource
9696
* the class path resource providing the expected JSON object
9797
*/
98-
public static void testJSONExport(Object model, String expectedJsonResource) {
99-
InputStream modeInputStream = getJson(model);
98+
public static void testJSONExport(Object model, String expectedJsonResource, Class<? extends Views.Publish> viewType) {
99+
InputStream modeInputStream = getJson(model, viewType);
100100
JsonReader outputReader = Json.createReader(modeInputStream);
101101
InputStream is = Utils.class.getResourceAsStream(expectedJsonResource);
102102
if (is != null) {
@@ -113,6 +113,20 @@ public static void testJSONExport(Object model, String expectedJsonResource) {
113113
}
114114
}
115115

116+
/**
117+
* Provided a {@code model} object and an {@code expectedJsonResource} identifying a JSON file in the class path,
118+
* this method will test the JSON export of the model and compare it to the JSON object provided by the
119+
* {@code expectedJsonResource}.
120+
*
121+
* @param model
122+
* the Sling Model
123+
* @param expectedJsonResource
124+
* the class path resource providing the expected JSON object
125+
*/
126+
public static void testJSONExport(Object model, String expectedJsonResource) {
127+
testJSONExport(model, expectedJsonResource, Views.Publish.class);
128+
}
129+
116130
/**
117131
* The given model is validated against adaptive form specification
118132
*

bundles/af-core/src/test/java/com/adobe/cq/forms/core/components/internal/models/v1/form/FragmentImplTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.adobe.cq.forms.core.components.models.form.FormClientLibManager;
3939
import com.adobe.cq.forms.core.components.models.form.Fragment;
4040
import com.adobe.cq.forms.core.components.models.form.TextInput;
41+
import com.adobe.cq.forms.core.components.views.Views;
4142
import com.adobe.cq.forms.core.context.FormsCoreComponentTestContext;
4243
import com.day.cq.wcm.api.NameConstants;
4344
import com.day.cq.wcm.msm.api.MSMNameConstants;
@@ -55,6 +56,7 @@ public class FragmentImplTest {
5556
private static final String PATH_FRAGMENT = CONTENT_ROOT + "/fragment";
5657
private static final String PATH_FRAGMENT_DAMPATH = CONTENT_ROOT + "/fragment-dampath";
5758
private static final String PATH_FRAGMENT_WITHOUT_FIELDTYPE = CONTENT_ROOT + "/fragment-without-fieldtype";
59+
private static final String PATH_FRAGMENT_WITH_FRAGMENT_PATH = CONTENT_ROOT + "/fragment-with-fragment-path";
5860
private final AemContext context = FormsCoreComponentTestContext.newAemContext();
5961

6062
@BeforeEach
@@ -133,6 +135,12 @@ void testJSONExportWithDamPath() throws Exception {
133135
Utils.testJSONExport(fragment, Utils.getTestExporterJSONPath(BASE, PATH_FRAGMENT_DAMPATH));
134136
}
135137

138+
@Test
139+
void testJSONExportWithFragmentPath() throws Exception {
140+
Fragment fragment = Utils.getComponentUnderTest(PATH_FRAGMENT_WITH_FRAGMENT_PATH, Fragment.class, context);
141+
Utils.testJSONExport(fragment, Utils.getTestExporterJSONPath(BASE, PATH_FRAGMENT_WITH_FRAGMENT_PATH), Views.Author.class);
142+
}
143+
136144
@Test
137145
void testGetChildrenModels() {
138146
Fragment fragment = Utils.getComponentUnderTest(PATH_FRAGMENT, Fragment.class, context);

bundles/af-core/src/test/resources/form/formcontainer/test-content-auto-save.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"fd:enableAutoSave": "true",
99
"fd:autoSaveInterval": "2",
1010
"fd:autoSaveStrategyType": "time",
11-
"specVersion": "0.14.2",
1211
"textinput" : {
1312
"jcr:primaryType": "nt:unstructured",
1413
"sling:resourceType" : "core/fd/components/form/textinput/v1/textinput",

bundles/af-core/src/test/resources/form/formcontainer/test-page-content.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"dorTemplateRef": "xyz",
3030
"clientLibRef": "abc",
3131
"customProp": "customPropValue",
32-
"specVersion": "0.14.2",
3332
"textinput" : {
3433
"jcr:primaryType": "nt:unstructured",
3534
"sling:resourceType" : "core/fd/components/form/textinput/v1/textinput",
@@ -123,7 +122,6 @@
123122
"thankYouOption": "page",
124123
"jcr:lastModifiedBy": "[email protected]",
125124
"fd:version": "2.1",
126-
"specVersion": "0.14.2",
127125
"title": "uesprintdemo",
128126
"jcr:lastModified": "Sat Mar 08 2025 06:56:53 GMT+0000",
129127
"themeRef": "",
@@ -148,7 +146,6 @@
148146
"thankYouOption": "page",
149147
"jcr:lastModifiedBy": "[email protected]",
150148
"fd:version": "2.1",
151-
"specVersion": "0.14.2",
152149
"title": "uesprintdemo",
153150
"jcr:lastModified": "Sat Mar 08 2025 06:56:53 GMT+0000",
154151
"themeRef": "",
@@ -172,7 +169,6 @@
172169
"thankYouOption": "page",
173170
"jcr:lastModifiedBy": "[email protected]",
174171
"fd:version": "2.1",
175-
"specVersion": "0.14.2",
176172
"title": "uesprintdemo",
177173
"jcr:lastModified": "Sat Mar 08 2025 06:56:53 GMT+0000",
178174
"themeRef": "",
@@ -197,7 +193,6 @@
197193
"thankYouOption": "page",
198194
"jcr:lastModifiedBy": "[email protected]",
199195
"fd:version": "2.1",
200-
"specVersion": "0.14.2",
201196
"title": "uesprintdemo",
202197
"jcr:lastModified": "Sat Mar 08 2025 06:56:53 GMT+0000",
203198
"themeRef": "",
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"id": "fragment-c83f64bf1d",
3+
"fieldType": "panel",
4+
"name": "fragment-123",
5+
"type": "object",
6+
"repeatable": true,
7+
"minOccur": 0,
8+
"maxOccur": 4,
9+
"fragmentPath": "/content/affragment",
10+
"properties": {
11+
"customProp": "customPropValue",
12+
"fd:dor": {
13+
"dorExclusion": false,
14+
"dorExcludeTitle": false,
15+
"dorExcludeDescription": false
16+
},
17+
"fd:path": "/content/fragment-with-fragment-path",
18+
"fd:fragment": true,
19+
"fd:viewType": "fragment"
20+
},
21+
"label": {
22+
"value": "Fragment"
23+
},
24+
"events": {
25+
"custom:setProperty": [
26+
"$event.payload"
27+
]
28+
},
29+
":itemsOrder": [
30+
"textinput"
31+
],
32+
":items": {
33+
"textinput": {
34+
"id": "textinput-233cc688ba",
35+
"fieldType": "text-input",
36+
"name": "fragmenttextinput",
37+
"type": "string",
38+
"label": {
39+
"value": "Text Input"
40+
},
41+
"properties": {
42+
"fd:dor": {
43+
"dorExclusion": false
44+
},
45+
"fd:path": "/content/affragment/jcr:content/guideContainer/textinput"
46+
},
47+
"events": {
48+
"custom:setProperty": [
49+
"$event.payload"
50+
]
51+
},
52+
":type": "core/fd/components/form/textinput/v1/textinput"
53+
}
54+
},
55+
":type": "core/fd/components/form/fragment/v1/fragment"
56+
}

bundles/af-core/src/test/resources/form/fragment/test-content.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@
4141
"sling:resourceType": "core/fd/components/form/fragment/v1/fragment",
4242
"fragmentPath": "/content/affragment"
4343
},
44+
"fragment-with-fragment-path": {
45+
"jcr:primaryType": "nt:unstructured",
46+
"wrapData": true,
47+
"jcr:title": "Fragment",
48+
"minOccur": "0",
49+
"jcr:lastModifiedBy": "admin",
50+
"name": "fragment-123",
51+
"maxOccur": "4",
52+
"jcr:lastModified": "Fri Jun 02 2023 12:34:24 GMT+0530",
53+
"repeatable": true,
54+
"sling:resourceType": "core/fd/components/form/fragment/v1/fragment",
55+
"fieldType": "panel",
56+
"fragmentPath": "/content/affragment",
57+
"customProp": "customPropValue"
58+
},
4459
"affragment": {
4560
"jcr:primaryType": "nt:unstructured",
4661
"jcr:title": "AF Fragment (v2)",

0 commit comments

Comments
 (0)