Skip to content

Commit c3d70d1

Browse files
committed
2 parents c46d18c + cc5a947 commit c3d70d1

File tree

7 files changed

+322
-39
lines changed

7 files changed

+322
-39
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@
6767
<artifactId>slf4j-api</artifactId>
6868
<version>1.7.25</version>
6969
</dependency>
70+
<dependency>
71+
<groupId>com.jayway.jsonpath</groupId>
72+
<artifactId>json-path-assert</artifactId>
73+
<version>2.2.0</version>
74+
<scope>test</scope>
75+
</dependency>
7076

7177
</dependencies>
7278
<build>

src/main/java/io/asfjava/ui/core/GeneratorFactoryInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import javax.servlet.annotation.WebListener;
66

77
@WebListener
8-
class GeneratorFactoryInitializer implements ServletContextListener {
8+
public class GeneratorFactoryInitializer implements ServletContextListener {
99

1010
@Override
1111
public final void contextInitialized(final ServletContextEvent sce) {

src/test/java/io/asfjava/ui/AppTest.java

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.asfjava.ui.core.schema;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import io.asfjava.ui.core.form.ValuesContainer;
7+
8+
public class CivilStateValues implements ValuesContainer {
9+
10+
@Override
11+
public Map<String, String> getValues() {
12+
13+
HashMap<String, String> myMap = new HashMap<>();
14+
15+
myMap.put("Maried", "COMMITTED");
16+
myMap.put("Single", "HAPPY");
17+
myMap.put("Divorced", "RELEASED");
18+
19+
return myMap;
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.asfjava.ui.core.schema;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import io.asfjava.ui.core.form.ValuesContainer;
7+
8+
public class GenderTitleMap implements ValuesContainer {
9+
10+
@Override
11+
public Map<String, String> getValues() {
12+
HashMap<String, String> values = new HashMap<>();
13+
values.put("Male", "male");
14+
values.put("Female", "female");
15+
return values;
16+
}
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.asfjava.ui.core.schema;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import io.asfjava.ui.core.form.ValuesContainer;
7+
8+
public class MyCheckBoxValues implements ValuesContainer {
9+
10+
@Override
11+
public Map<String, String> getValues() {
12+
Map<String, String> values = new HashMap<>();
13+
values.put("Red", "red");
14+
values.put("Green", "green");
15+
values.put("Blue", "blue");
16+
return values;
17+
}
18+
}
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
package io.asfjava.ui.core.schema;
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+
8+
import java.io.Serializable;
9+
10+
import org.junit.Assert;
11+
import org.junit.BeforeClass;
12+
import org.junit.Test;
13+
14+
import com.fasterxml.jackson.core.JsonProcessingException;
15+
import com.fasterxml.jackson.databind.ObjectMapper;
16+
17+
import io.asfjava.ui.core.GeneratorFactoryInitializer;
18+
import io.asfjava.ui.core.form.CheckBox;
19+
import io.asfjava.ui.core.form.ComboBox;
20+
import io.asfjava.ui.core.form.Number;
21+
import io.asfjava.ui.core.form.Password;
22+
import io.asfjava.ui.core.form.RadioBox;
23+
import io.asfjava.ui.core.form.TextArea;
24+
import io.asfjava.ui.core.form.TextField;
25+
import io.asfjava.ui.dto.UiForm;
26+
27+
public class UiFormSchemaGeneratorTest {
28+
29+
@BeforeClass
30+
public static void init() {
31+
new GeneratorFactoryInitializer().contextInitialized(null);
32+
}
33+
34+
@Test
35+
public void testGenerate_textField() throws JsonProcessingException {
36+
UiForm ui = UiFormSchemaGenerator.get().generate(TextFieldForm.class);
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')]",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+
48+
}
49+
50+
@Test
51+
public void testGenerate_Number() throws JsonProcessingException {
52+
UiForm ui = UiFormSchemaGenerator.get().generate(NumberForm.class);
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')]",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+
63+
}
64+
65+
@Test
66+
public void testGenerate_Password() throws JsonProcessingException {
67+
UiForm ui = UiFormSchemaGenerator.get().generate(PasswordForm.class);
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')]",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)));
78+
}
79+
80+
@Test
81+
public void testGenerate_TextArea() throws JsonProcessingException {
82+
UiForm ui = UiFormSchemaGenerator.get().generate(TextAreaForm.class);
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')]",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+
94+
}
95+
96+
@Test
97+
public void testGenerate_CheckBox() throws JsonProcessingException {
98+
UiForm ui = UiFormSchemaGenerator.get().generate(CheckBoxForm.class);
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')]",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[?(@.name=='Red')].value",hasItem("red")));
105+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Blue')].value",hasItem("blue")));
106+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Green')].value",hasItem("green")));
107+
}
108+
109+
@Test
110+
public void testGenerate_CheckBox_WithCustomValuesContainer() throws JsonProcessingException {
111+
UiForm ui = UiFormSchemaGenerator.get().generate(CheckBoxForm2.class);
112+
String json = new ObjectMapper().writeValueAsString(ui);
113+
Assert.assertThat(json, hasJsonPath("$.schema.properties.color.title",equalTo("Color")));
114+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')]",hasSize(1)));
115+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].multiple",hasItem(true)));
116+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].required",hasItem(false)));
117+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Red')].value",hasItem("red")));
118+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Blue')].value",hasItem("blue")));
119+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Green')].value",hasItem("green")));
120+
}
121+
122+
@Test
123+
public void testGenerate_RadioBox() throws JsonProcessingException {
124+
UiForm ui = UiFormSchemaGenerator.get().generate(RadioBoxForm.class);
125+
String json = new ObjectMapper().writeValueAsString(ui);
126+
Assert.assertThat(json, hasJsonPath("$.schema.properties.civilState.title",equalTo("Civil State")));
127+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='civilState')]",hasSize(1)));
128+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='civilState')].readOnly",hasItem(false)));
129+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='civilState')].titleMap[?(@.name=='Maried')].value",hasItem("COMMITTED")));
130+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='civilState')].titleMap[?(@.name=='Single')].value",hasItem("HAPPY")));
131+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='civilState')].titleMap[?(@.name=='Divorced')].value",hasItem("RELEASED")));
132+
133+
}
134+
135+
@Test
136+
public void testGenerate_ComboBox() throws JsonProcessingException {
137+
UiForm ui = UiFormSchemaGenerator.get().generate(ComboBoxForm.class);
138+
139+
String json = new ObjectMapper().writeValueAsString(ui);
140+
Assert.assertThat(json, hasJsonPath("$.schema.properties.currency.title",equalTo("Currency")));
141+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')]",hasSize(1)));
142+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].disabled",hasItem(false)));
143+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].multiple",hasItem(false)));
144+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].required",hasItem(true)));
145+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].autofocus",hasItem(false)));
146+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].titleMap[?(@.name=='Euro')].value",hasItem("euro")));
147+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].titleMap[?(@.name=='Dollar')].value",hasItem("dollar")));
148+
149+
}
150+
151+
@Test
152+
public void testGenerate_ComboBox_WithCustomValuesContainer() throws JsonProcessingException {
153+
UiForm ui = UiFormSchemaGenerator.get().generate(ComboBoxForm2.class);
154+
155+
String json = new ObjectMapper().writeValueAsString(ui);
156+
Assert.assertThat(json, hasJsonPath("$.schema.properties.gender.title",equalTo("Gender")));
157+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')]",hasSize(1)));
158+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].disabled",hasItem(false)));
159+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].multiple",hasItem(false)));
160+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].required",hasItem(false)));
161+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].autofocus",hasItem(false)));
162+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].titleMap[?(@.name=='Male')].value",hasItem("male")));
163+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].titleMap[?(@.name=='Female')].value",hasItem("female")));
164+
165+
}
166+
167+
}
168+
169+
class TextFieldForm implements Serializable {
170+
171+
@TextField(title = "First Name", placeHolder = "Your first name", pattern = "[a-z]", noTitle = true, validationMessage = "this is a validation msg", description = "This is a description for your first name field")
172+
private String firstName;
173+
174+
public String getFirstName() {
175+
return firstName;
176+
}
177+
178+
}
179+
180+
class NumberForm implements Serializable {
181+
182+
@Number(title = "Number of children", placeHolder = "Number of children", description = "This is a number", noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
183+
private Integer number;
184+
185+
public Integer getNumber() {
186+
return number;
187+
}
188+
}
189+
190+
class PasswordForm implements Serializable {
191+
192+
@Password(title = "Password", placeHolder = "Please set you password", description = "This is password", noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
193+
private String password;
194+
195+
public String getPassword() {
196+
return password;
197+
}
198+
}
199+
200+
class TextAreaForm implements Serializable {
201+
202+
@TextArea(title = "Address", placeHolder = "Fill your address please", description = "This is textarea", noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
203+
private String address;
204+
205+
public String getAddress() {
206+
return address;
207+
}
208+
}
209+
210+
class CheckBoxForm implements Serializable {
211+
212+
@CheckBox(title = "Color", values = { "red", "blue", "green" }, defaultvalue = "red", required = true)
213+
private String color;
214+
215+
public String getColor() {
216+
return color;
217+
}
218+
}
219+
220+
class CheckBoxForm2 implements Serializable {
221+
222+
@CheckBox(title = "Color", titleMap = MyCheckBoxValues.class, defaultvalue = "red", multiple = true)
223+
private String color;
224+
225+
public String getColor() {
226+
return color;
227+
}
228+
}
229+
230+
class RadioBoxForm implements Serializable {
231+
232+
@RadioBox(title = "Civil State", titleMap = CivilStateValues.class)
233+
private String civilState;
234+
235+
public String getCivilState() {
236+
return civilState;
237+
}
238+
}
239+
240+
class ComboBoxForm implements Serializable {
241+
242+
@ComboBox(title = "Currency", values = { "euro", "dollar" }, required = true)
243+
private String currency;
244+
245+
public String getCurrency() {
246+
return currency;
247+
}
248+
}
249+
250+
class ComboBoxForm2 implements Serializable {
251+
252+
@ComboBox(title = "Gender", titleMap = GenderTitleMap.class)
253+
private String gender;
254+
255+
public String getGender() {
256+
return gender;
257+
}
258+
}

0 commit comments

Comments
 (0)