1
1
package io .asfjava .ui .core .schema ;
2
2
3
+ import static com .jayway .jsonpath .matchers .JsonPathMatchers .hasJsonPath ;
4
+ import static org .hamcrest .Matchers .equalTo ;
5
+ import static org .hamcrest .Matchers .hasItem ;
6
+ import static org .hamcrest .Matchers .hasSize ;
7
+
3
8
import java .io .Serializable ;
4
9
10
+ import org .junit .Assert ;
5
11
import org .junit .BeforeClass ;
6
12
import org .junit .Test ;
7
13
8
14
import com .fasterxml .jackson .core .JsonProcessingException ;
9
- import com .fasterxml .jackson .databind .JsonNode ;
10
15
import com .fasterxml .jackson .databind .ObjectMapper ;
11
16
12
17
import io .asfjava .ui .core .GeneratorFactoryInitializer ;
18
23
import io .asfjava .ui .core .form .TextArea ;
19
24
import io .asfjava .ui .core .form .TextField ;
20
25
import io .asfjava .ui .dto .UiForm ;
21
- import io .asfjava .ui .test .JsonNodeAssert ;
22
26
23
27
public class UiFormSchemaGeneratorTest {
24
28
@@ -30,144 +34,138 @@ public static void init() {
30
34
@ Test
31
35
public void testGenerate_textField () throws JsonProcessingException {
32
36
UiForm ui = UiFormSchemaGenerator .get ().generate (TextFieldForm .class );
33
- JsonNode json = new ObjectMapper ().valueToTree (ui );
34
- JsonNodeAssert asserter = new JsonNodeAssert (json );
35
- asserter .assertEquals ("schema.properties.firstName.title" , "First Name" );
36
- asserter .assertEquals ("schema.properties.firstName.pattern" , "[a-z]" );
37
- asserter .assertEquals ("form.0.key" , "firstName" );
38
- asserter .assertTrue ("form.0.notitle" );
39
- asserter .assertEquals ("form.0.validationMessage" , "this is a validation msg" );
40
- asserter .assertEquals ("form.0.description" , "This is a description for your first name field" );
41
- asserter .assertEquals ("form.0.placeholder" , "Your first name" );
37
+ String json = new ObjectMapper ().writeValueAsString (ui );
38
+
39
+
40
+ Assert .assertThat (json , hasJsonPath ("$.schema.properties.firstName.title" ,equalTo ("First Name" )));
41
+ Assert .assertThat (json , hasJsonPath ("$.schema.properties.firstName.pattern" ,equalTo ("[a-z]" )));
42
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='firstName')].key" ,hasSize (1 )));
43
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='firstName')].description" ,hasItem ("This is a description for your first name field" )));
44
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='firstName')].placeholder" ,hasItem ("Your first name" )));
45
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='firstName')].validationMessage" ,hasItem ("this is a validation msg" )));
46
+ //Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='firstName')].type",hasItem("textField")));
47
+
42
48
}
43
49
44
50
@ Test
45
51
public void testGenerate_Number () throws JsonProcessingException {
46
52
UiForm ui = UiFormSchemaGenerator .get ().generate (NumberForm .class );
47
- JsonNode json = new ObjectMapper ().valueToTree (ui );
48
- JsonNodeAssert asserter = new JsonNodeAssert (json );
49
- asserter .assertEquals ("schema.properties.number.title" , "Number of children" );
50
- asserter .assertEquals ("form.0.key" , "number" );
51
- asserter .assertTrue ("form.0.notitle" );
52
- asserter .assertTrue ("form.0.readonly" );
53
- asserter .assertEquals ("form.0.validationMessage" , "this is a validation msg" );
54
- asserter .assertEquals ("form.0.description" , "This is a number" );
55
- asserter .assertEquals ("form.0.placeholder" , "Number of children" );
53
+ String json = new ObjectMapper ().writeValueAsString (ui );
54
+ //Assert.assertThat(json, hasJsonPath("$.schema.properties.number.title",equalTo("Number")));
55
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='number')].key" ,hasSize (1 )));
56
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='number')].description" ,hasItem ("This is a number" )));
57
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='number')].placeholder" ,hasItem ("Number of children" )));
58
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='number')].validationMessage" ,hasItem ("this is a validation msg" )));
59
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='number')].type" ,hasItem ("number" )));
60
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='number')].notitle" ,hasItem (true )));
61
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='number')].readonly" ,hasItem (true )));
62
+
56
63
}
57
64
58
65
@ Test
59
66
public void testGenerate_Password () throws JsonProcessingException {
60
67
UiForm ui = UiFormSchemaGenerator .get ().generate (PasswordForm .class );
61
- JsonNode json = new ObjectMapper ().valueToTree (ui );
62
- JsonNodeAssert asserter = new JsonNodeAssert (json );
63
- asserter .assertEquals ("schema.properties.password.title" , "Password" );
64
- asserter .assertEquals ("form.0.key" , "password" );
65
- asserter .assertTrue ("form.0.notitle" );
66
- asserter .assertTrue ("form.0.readonly" );
67
- asserter .assertEquals ("form.0.validationMessage" , "this is a validation msg" );
68
- asserter .assertEquals ("form.0.description" , "This is password" );
69
- asserter .assertEquals ("form.0.placeholder" , "Please set you password" );
68
+
69
+ String json = new ObjectMapper ().writeValueAsString (ui );
70
+ Assert .assertThat (json , hasJsonPath ("$.schema.properties.password.title" ,equalTo ("Password" )));
71
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='password')].key" ,hasSize (1 )));
72
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='password')].description" ,hasItem ("This is password" )));
73
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='password')].placeholder" ,hasItem ("Please set you password" )));
74
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='password')].validationMessage" ,hasItem ("this is a validation msg" )));
75
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='password')].type" ,hasItem ("password" )));
76
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='password')].notitle" ,hasItem (true )));
77
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='password')].readonly" ,hasItem (true )));
70
78
}
71
-
79
+
72
80
@ Test
73
81
public void testGenerate_TextArea () throws JsonProcessingException {
74
82
UiForm ui = UiFormSchemaGenerator .get ().generate (TextAreaForm .class );
75
- JsonNode json = new ObjectMapper ().valueToTree (ui );
76
- JsonNodeAssert asserter = new JsonNodeAssert (json );
77
- asserter .assertEquals ("schema.properties.address.title" , "Address" );
78
- asserter .assertEquals ("form.0.key" , "address" );
79
- asserter .assertTrue ("form.0.notitle" );
80
- asserter .assertTrue ("form.0.readonly" );
81
- asserter .assertEquals ("form.0.validationMessage" , "this is a validation msg" );
82
- asserter .assertEquals ("form.0.description" , "This is textarea" );
83
- asserter .assertEquals ("form.0.placeholder" , "Fill your address please" );
83
+
84
+ String json = new ObjectMapper ().writeValueAsString (ui );
85
+ Assert .assertThat (json , hasJsonPath ("$.schema.properties.address.title" ,equalTo ("Address" )));
86
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='address')].key" ,hasSize (1 )));
87
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='address')].description" ,hasItem ("This is textarea" )));
88
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='address')].placeholder" ,hasItem ("Fill your address please" )));
89
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='address')].validationMessage" ,hasItem ("this is a validation msg" )));
90
+ //Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].type",hasItem("textArea")));
91
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='address')].notitle" ,hasItem (true )));
92
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='address')].readonly" ,hasItem (true )));
93
+
84
94
}
85
-
95
+
86
96
@ Test
87
97
public void testGenerate_CheckBox () throws JsonProcessingException {
88
98
UiForm ui = UiFormSchemaGenerator .get ().generate (CheckBoxForm .class );
89
- JsonNode json = new ObjectMapper ().valueToTree (ui );
90
- JsonNodeAssert asserter = new JsonNodeAssert (json );
91
- asserter .assertEquals ("schema.properties.color.title" , "Color" );
92
- asserter .assertEquals ("form.0.key" , "color" );
93
- asserter .assertFalse ("form.0.multiple" );
94
- asserter .assertTrue ("form.0.required" );
95
- asserter .assertEquals ("form.0.titleMap.0.name" , "Red" );
96
- asserter .assertEquals ("form.0.titleMap.0.value" , "red" );
97
- asserter .assertEquals ("form.0.titleMap.1.name" , "Blue" );
98
- asserter .assertEquals ("form.0.titleMap.1.value" , "blue" );
99
- asserter .assertEquals ("form.0.titleMap.2.name" , "Green" );
100
- asserter .assertEquals ("form.0.titleMap.2.value" , "green" );
99
+ String json = new ObjectMapper ().writeValueAsString (ui );
100
+ Assert .assertThat (json , hasJsonPath ("$.schema.properties.color.title" ,equalTo ("Color" )));
101
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].key" ,hasSize (1 )));
102
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].multiple" ,hasItem (false )));
103
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].required" ,hasItem (true )));
104
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].titleMap[0].name" ,hasItem ("Red" )));
105
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].titleMap[0].value" ,hasItem ("red" )));
106
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].titleMap[1].name" ,hasItem ("Blue" )));
107
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].titleMap[1].value" ,hasItem ("blue" )));
108
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].titleMap[2].name" ,hasItem ("Green" )));
109
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].titleMap[2].value" ,hasItem ("green" )));
101
110
}
102
-
111
+
103
112
@ Test
104
113
public void testGenerate_CheckBox_WithCustomValuesContainer () throws JsonProcessingException {
105
114
UiForm ui = UiFormSchemaGenerator .get ().generate (CheckBoxForm2 .class );
106
- JsonNode json = new ObjectMapper ().valueToTree (ui );
107
- JsonNodeAssert asserter = new JsonNodeAssert (json );
108
- asserter .assertEquals ("schema.properties.color.title" , "Color" );
109
- asserter .assertEquals ("form.0.key" , "color" );
110
- asserter .assertTrue ("form.0.multiple" );
111
- asserter .assertFalse ("form.0.required" );
112
- asserter .assertEquals ("form.0.titleMap.0.name" , "Red" );
113
- asserter .assertEquals ("form.0.titleMap.0.value" , "red" );
114
- asserter .assertEquals ("form.0.titleMap.1.name" , "Blue" );
115
- asserter .assertEquals ("form.0.titleMap.1.value" , "blue" );
116
- asserter .assertEquals ("form.0.titleMap.2.name" , "Green" );
117
- asserter .assertEquals ("form.0.titleMap.2.value" , "green" );
115
+ String json = new ObjectMapper ().writeValueAsString (ui );
116
+ Assert .assertThat (json , hasJsonPath ("$.schema.properties.color.title" ,equalTo ("Color" )));
117
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].key" ,hasSize (1 )));
118
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].multiple" ,hasItem (true )));
119
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].required" ,hasItem (false )));
120
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].titleMap[?(@.name=='Red')].value" ,hasItem ("red" )));
121
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].titleMap[?(@.name=='Blue')].value" ,hasItem ("blue" )));
122
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='color')].titleMap[?(@.name=='Green')].value" ,hasItem ("green" )));
118
123
}
119
-
124
+
120
125
@ Test
121
126
public void testGenerate_RadioBox () throws JsonProcessingException {
122
127
UiForm ui = UiFormSchemaGenerator .get ().generate (RadioBoxForm .class );
123
- JsonNode json = new ObjectMapper ().valueToTree (ui );
124
- JsonNodeAssert asserter = new JsonNodeAssert (json );
125
- asserter .assertEquals ("schema.properties.civilState.title" , "Civil State" );
126
- asserter .assertEquals ("form.0.key" , "civilState" );
127
- asserter .assertFalse ("form.0.readonly" );
128
- asserter .assertEquals ("form.0.titleMap.0.name" , "Maried" );
129
- asserter .assertEquals ("form.0.titleMap.0.value" , "COMMITTED" );
130
- asserter .assertEquals ("form.0.titleMap.1.name" , "Single" );
131
- asserter .assertEquals ("form.0.titleMap.1.value" , "HAPPY" );
132
- asserter .assertEquals ("form.0.titleMap.2.name" , "Divorced" );
133
- asserter .assertEquals ("form.0.titleMap.2.value" , "RELEASED" );
128
+ String json = new ObjectMapper ().writeValueAsString (ui );
129
+ Assert .assertThat (json , hasJsonPath ("$.schema.properties.civilState.title" ,equalTo ("Civil State" )));
130
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='civilState')].key" ,hasSize (1 )));
131
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='civilState')].readOnly" ,hasItem (false )));
132
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='civilState')].titleMap[?(@.name=='Maried')].value" ,hasItem ("COMMITTED" )));
133
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='civilState')].titleMap[?(@.name=='Single')].value" ,hasItem ("HAPPY" )));
134
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='civilState')].titleMap[?(@.name=='Divorced')].value" ,hasItem ("RELEASED" )));
135
+
134
136
}
135
-
137
+
136
138
@ Test
137
139
public void testGenerate_ComboBox () throws JsonProcessingException {
138
140
UiForm ui = UiFormSchemaGenerator .get ().generate (ComboBoxForm .class );
139
- JsonNode json = new ObjectMapper ().valueToTree (ui );
140
- JsonNodeAssert asserter = new JsonNodeAssert (json );
141
- asserter .assertEquals ("schema.properties.currency.title" , "Currency" );
142
- asserter .assertEquals ("form.0.key" , "currency" );
143
- asserter .assertFalse ("form.0.autofocus" );
144
- asserter .assertFalse ("form.0.disabled" );
145
- asserter .assertFalse ("form.0.multiple" );
146
- asserter .assertTrue ("form.0.required" );
147
- asserter .assertEquals ("form.0.titleMap.0.name" , "Euro" );
148
- asserter .assertEquals ("form.0.titleMap.0.value" , "euro" );
149
- asserter .assertEquals ("form.0.titleMap.1.name" , "Dollar" );
150
- asserter .assertEquals ("form.0.titleMap.1.value" , "dollar" );
141
+
142
+ String json = new ObjectMapper ().writeValueAsString (ui );
143
+ Assert .assertThat (json , hasJsonPath ("$.schema.properties.currency.title" ,equalTo ("Currency" )));
144
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='currency')].key" ,hasSize (1 )));
145
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='currency')].disabled" ,hasItem (false )));
146
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='currency')].multiple" ,hasItem (false )));
147
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='currency')].required" ,hasItem (true )));
148
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='currency')].autofocus" ,hasItem (false )));
149
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='currency')].titleMap[?(@.name=='Euro')].value" ,hasItem ("euro" )));
150
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='currency')].titleMap[?(@.name=='Dollar')].value" ,hasItem ("dollar" )));
151
+
151
152
}
152
-
153
+
153
154
@ Test
154
155
public void testGenerate_ComboBox_WithCustomValuesContainer () throws JsonProcessingException {
155
156
UiForm ui = UiFormSchemaGenerator .get ().generate (ComboBoxForm2 .class );
156
- JsonNode json = new ObjectMapper ().valueToTree (ui );
157
- JsonNodeAssert asserter = new JsonNodeAssert (json );
158
- asserter .assertEquals ("schema.properties.gender.title" , "Gender" );
159
- asserter .assertEquals ("form.0.key" , "currency" );
160
- asserter .assertFalse ("form.0.autofocus" );
161
- asserter .assertFalse ("form.0.disabled" );
162
- asserter .assertFalse ("form.0.multiple" );
163
- asserter .assertFalse ("form.0.required" );
164
- asserter .assertEquals ("form.0.titleMap.0.name" , "Male" );
165
- asserter .assertEquals ("form.0.titleMap.0.value" , "male" );
166
- asserter .assertEquals ("form.0.titleMap.1.name" , "Female" );
167
- asserter .assertEquals ("form.0.titleMap.1.value" , "female" );
157
+
158
+ String json = new ObjectMapper ().writeValueAsString (ui );
159
+ Assert .assertThat (json , hasJsonPath ("$.schema.properties.gender.title" ,equalTo ("Gender" )));
160
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='gender')].key" ,hasSize (1 )));
161
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='gender')].disabled" ,hasItem (false )));
162
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='gender')].multiple" ,hasItem (false )));
163
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='gender')].required" ,hasItem (false )));
164
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='gender')].autofocus" ,hasItem (false )));
165
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='gender')].titleMap[?(@.name=='Male')].value" ,hasItem ("male" )));
166
+ Assert .assertThat (json , hasJsonPath ("$.form[?(@.key=='gender')].titleMap[?(@.name=='Female')].value" ,hasItem ("female" )));
167
+
168
168
}
169
-
170
-
171
169
172
170
}
173
171
@@ -212,53 +210,52 @@ public String getAddress() {
212
210
}
213
211
}
214
212
215
- class CheckBoxForm implements Serializable {
216
-
217
- @ CheckBox (title = "Color" , values = { "red" , "blue" , "green" }, defaultvalue = "red" ,required = true )
213
+ class CheckBoxForm implements Serializable {
214
+
215
+ @ CheckBox (title = "Color" , values = { "red" , "blue" , "green" }, defaultvalue = "red" , required = true )
218
216
private String color ;
219
-
220
- public String getColor (){
217
+
218
+ public String getColor () {
221
219
return color ;
222
220
}
223
221
}
224
222
225
- class CheckBoxForm2 implements Serializable {
226
-
227
- @ CheckBox (title = "Color" , titleMap = MyCheckBoxValues .class , defaultvalue = "red" ,multiple = true )
223
+ class CheckBoxForm2 implements Serializable {
224
+
225
+ @ CheckBox (title = "Color" , titleMap = MyCheckBoxValues .class , defaultvalue = "red" , multiple = true )
228
226
private String color ;
229
-
230
- public String getColor (){
227
+
228
+ public String getColor () {
231
229
return color ;
232
230
}
233
231
}
234
232
235
- class RadioBoxForm implements Serializable {
236
-
233
+ class RadioBoxForm implements Serializable {
234
+
237
235
@ RadioBox (title = "Civil State" , titleMap = CivilStateValues .class )
238
236
private String civilState ;
239
-
240
- public String getCivilState (){
237
+
238
+ public String getCivilState () {
241
239
return civilState ;
242
240
}
243
241
}
244
242
245
- class ComboBoxForm implements Serializable {
246
-
247
- @ ComboBox (title = "Currency" , values = { "euro" , "dollar" }, required = true )
243
+ class ComboBoxForm implements Serializable {
244
+
245
+ @ ComboBox (title = "Currency" , values = { "euro" , "dollar" }, required = true )
248
246
private String currency ;
249
-
250
- public String getCurrency (){
247
+
248
+ public String getCurrency () {
251
249
return currency ;
252
250
}
253
251
}
254
252
255
- class ComboBoxForm2 implements Serializable {
256
-
253
+ class ComboBoxForm2 implements Serializable {
254
+
257
255
@ ComboBox (title = "Gender" , titleMap = GenderTitleMap .class )
258
256
private String gender ;
259
-
260
- public String getGender (){
257
+
258
+ public String getGender () {
261
259
return gender ;
262
260
}
263
261
}
264
-
0 commit comments