Skip to content

Commit ad6870c

Browse files
authored
💚 🎨 Merge pull request #50 from Hdebbech/master
Refactor unit test
2 parents d20bebd + cc5bdf3 commit ad6870c

10 files changed

+888
-603
lines changed

pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3838
<maven.compiler.source>1.8</maven.compiler.source>
3939
<maven.compiler.target>1.8</maven.compiler.target>
40+
<runSuite>**/UiFormSchemaGeneratorTest.class</runSuite>
4041
</properties>
4142
<dependencies>
4243
<dependency>
@@ -77,6 +78,15 @@
7778
</dependencies>
7879
<build>
7980
<plugins>
81+
<plugin>
82+
<groupId>org.apache.maven.plugins</groupId>
83+
<artifactId>maven-surefire-plugin</artifactId>
84+
<configuration>
85+
<includes>
86+
<include>${runSuite}</include>
87+
</includes>
88+
</configuration>
89+
</plugin>
8090
<plugin>
8191
<groupId>org.jacoco</groupId>
8292
<artifactId>jacoco-maven-plugin</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.AfterClass;
11+
import org.junit.Assert;
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
15+
import com.fasterxml.jackson.core.JsonProcessingException;
16+
import com.fasterxml.jackson.databind.ObjectMapper;
17+
18+
import io.asfjava.ui.core.GeneratorFactoryInitializer;
19+
import io.asfjava.ui.core.form.CheckBox;
20+
import io.asfjava.ui.dto.UiForm;
21+
22+
public class CheckBoxFormTest {
23+
24+
static GeneratorFactoryInitializer generatorFactoryInitializer;
25+
26+
@BeforeClass
27+
public static void setUpBeforeClass() {
28+
generatorFactoryInitializer = new GeneratorFactoryInitializer();
29+
generatorFactoryInitializer.contextInitialized(null);
30+
}
31+
32+
@AfterClass
33+
public static void tearDownAfterClass() {
34+
generatorFactoryInitializer.contextDestroyed(null);
35+
}
36+
37+
@Test
38+
public void testGenerate_CheckBox() throws JsonProcessingException {
39+
UiForm ui = UiFormSchemaGenerator.get().generate(CheckBoxForm.class);
40+
String json = new ObjectMapper().writeValueAsString(ui);
41+
Assert.assertThat(json, hasJsonPath("$.schema.properties.color.title", equalTo("Color")));
42+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')]", hasSize(1)));
43+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].multiple", hasItem(false)));
44+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].required", hasItem(true)));
45+
Assert.assertThat(json,
46+
hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Red')].value", hasItem("red")));
47+
Assert.assertThat(json,
48+
hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Blue')].value", hasItem("blue")));
49+
Assert.assertThat(json,
50+
hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Green')].value", hasItem("green")));
51+
}
52+
53+
@Test
54+
public void testGenerate_CheckBox_WithCustomValuesContainer() throws JsonProcessingException {
55+
UiForm ui = UiFormSchemaGenerator.get().generate(CheckBoxForm2.class);
56+
String json = new ObjectMapper().writeValueAsString(ui);
57+
Assert.assertThat(json, hasJsonPath("$.schema.properties.color.title", equalTo("Color")));
58+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')]", hasSize(1)));
59+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].multiple", hasItem(true)));
60+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].required", hasItem(false)));
61+
Assert.assertThat(json,
62+
hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Red')].value", hasItem("red")));
63+
Assert.assertThat(json,
64+
hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Blue')].value", hasItem("blue")));
65+
Assert.assertThat(json,
66+
hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Green')].value", hasItem("green")));
67+
}
68+
}
69+
70+
class CheckBoxForm implements Serializable {
71+
72+
@CheckBox(title = "Color", values = { "red", "blue", "green" }, defaultvalue = "red", required = true)
73+
private String color;
74+
75+
public String getColor() {
76+
return color;
77+
}
78+
}
79+
80+
class CheckBoxForm2 implements Serializable {
81+
82+
@CheckBox(title = "Color", titleMap = MyCheckBoxValues.class, defaultvalue = "red", multiple = true)
83+
private String color;
84+
85+
public String getColor() {
86+
return color;
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.AfterClass;
11+
import org.junit.Assert;
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
15+
import com.fasterxml.jackson.core.JsonProcessingException;
16+
import com.fasterxml.jackson.databind.ObjectMapper;
17+
18+
import io.asfjava.ui.core.GeneratorFactoryInitializer;
19+
import io.asfjava.ui.core.form.ComboBox;
20+
import io.asfjava.ui.dto.UiForm;
21+
22+
public class ComboBoxFormTest {
23+
24+
static GeneratorFactoryInitializer generatorFactoryInitializer;
25+
26+
@BeforeClass
27+
public static void setUpBeforeClass() {
28+
generatorFactoryInitializer = new GeneratorFactoryInitializer();
29+
generatorFactoryInitializer.contextInitialized(null);
30+
}
31+
32+
@AfterClass
33+
public static void tearDownAfterClass() {
34+
generatorFactoryInitializer.contextDestroyed(null);
35+
}
36+
37+
@Test
38+
public void testGenerate_ComboBox() throws JsonProcessingException {
39+
UiForm ui = UiFormSchemaGenerator.get().generate(ComboBoxForm.class);
40+
41+
String json = new ObjectMapper().writeValueAsString(ui);
42+
Assert.assertThat(json, hasJsonPath("$.schema.properties.currency.title", equalTo("Currency")));
43+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')]", hasSize(1)));
44+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].disabled", hasItem(false)));
45+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].multiple", hasItem(false)));
46+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].required", hasItem(true)));
47+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].autofocus", hasItem(false)));
48+
Assert.assertThat(json,
49+
hasJsonPath("$.form[?(@.key=='currency')].titleMap[?(@.name=='Euro')].value", hasItem("euro")));
50+
Assert.assertThat(json,
51+
hasJsonPath("$.form[?(@.key=='currency')].titleMap[?(@.name=='Dollar')].value", hasItem("dollar")));
52+
53+
}
54+
55+
@Test
56+
public void testGenerate_ComboBox_WithCustomValuesContainer() throws JsonProcessingException {
57+
UiForm ui = UiFormSchemaGenerator.get().generate(ComboBoxForm2.class);
58+
59+
String json = new ObjectMapper().writeValueAsString(ui);
60+
Assert.assertThat(json, hasJsonPath("$.schema.properties.gender.title", equalTo("Gender")));
61+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')]", hasSize(1)));
62+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].disabled", hasItem(false)));
63+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].multiple", hasItem(false)));
64+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].required", hasItem(false)));
65+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].autofocus", hasItem(false)));
66+
Assert.assertThat(json,
67+
hasJsonPath("$.form[?(@.key=='gender')].titleMap[?(@.name=='Male')].value", hasItem("male")));
68+
Assert.assertThat(json,
69+
hasJsonPath("$.form[?(@.key=='gender')].titleMap[?(@.name=='Female')].value", hasItem("female")));
70+
71+
}
72+
73+
}
74+
75+
class ComboBoxForm implements Serializable {
76+
77+
@ComboBox(title = "Currency", values = { "euro", "dollar" }, required = true)
78+
private String currency;
79+
80+
public String getCurrency() {
81+
return currency;
82+
}
83+
}
84+
85+
class ComboBoxForm2 implements Serializable {
86+
87+
@ComboBox(title = "Gender", titleMap = GenderTitleMap.class)
88+
private String gender;
89+
90+
public String getGender() {
91+
return gender;
92+
}
93+
}
94+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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.AfterClass;
11+
import org.junit.Assert;
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
15+
import com.fasterxml.jackson.core.JsonProcessingException;
16+
import com.fasterxml.jackson.databind.ObjectMapper;
17+
18+
import io.asfjava.ui.core.GeneratorFactoryInitializer;
19+
import io.asfjava.ui.core.form.Number;
20+
import io.asfjava.ui.dto.UiForm;
21+
22+
public class NumberFormTest {
23+
24+
static GeneratorFactoryInitializer generatorFactoryInitializer;
25+
26+
@BeforeClass
27+
public static void setUpBeforeClass() {
28+
generatorFactoryInitializer = new GeneratorFactoryInitializer();
29+
generatorFactoryInitializer.contextInitialized(null);
30+
}
31+
32+
@AfterClass
33+
public static void tearDownAfterClass() {
34+
generatorFactoryInitializer.contextDestroyed(null);
35+
}
36+
37+
@Test
38+
public void testGenerate_Number_For_Integer() throws JsonProcessingException {
39+
UiForm ui = UiFormSchemaGenerator.get().generate(IntegerNumberForm.class);
40+
String json = new ObjectMapper().writeValueAsString(ui);
41+
Assert.assertThat(json, hasJsonPath("$.schema.properties.number.title", equalTo("Integer Number")));
42+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')]", hasSize(1)));
43+
Assert.assertThat(json,
44+
hasJsonPath("$.form[?(@.key=='number')].description", hasItem("This is an integer number")));
45+
Assert.assertThat(json,
46+
hasJsonPath("$.form[?(@.key=='number')].placeholder", hasItem("Integer Number PlaceHolder")));
47+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].validationMessage",
48+
hasItem("this is a validation msg for an integer value")));
49+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].type", hasItem("number")));
50+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].notitle", hasItem(true)));
51+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].readonly", hasItem(true)));
52+
53+
}
54+
55+
@Test
56+
public void testGenerate_Number_For_Long() throws JsonProcessingException {
57+
UiForm ui = UiFormSchemaGenerator.get().generate(LongNumberForm.class);
58+
String json = new ObjectMapper().writeValueAsString(ui);
59+
Assert.assertThat(json, hasJsonPath("$.schema.properties.number.title", equalTo("Long Number")));
60+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')]", hasSize(1)));
61+
Assert.assertThat(json,
62+
hasJsonPath("$.form[?(@.key=='number')].description", hasItem("This is a long number")));
63+
Assert.assertThat(json,
64+
hasJsonPath("$.form[?(@.key=='number')].placeholder", hasItem("Long Number PlaceHolder")));
65+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].validationMessage",
66+
hasItem("this is a validation msg for long value")));
67+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].type", hasItem("number")));
68+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].notitle", hasItem(true)));
69+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].readonly", hasItem(true)));
70+
71+
}
72+
73+
74+
@Test
75+
public void testGenerate_Number_For_Double() throws JsonProcessingException {
76+
UiForm ui = UiFormSchemaGenerator.get().generate(DoubleNumberForm.class);
77+
String json = new ObjectMapper().writeValueAsString(ui);
78+
Assert.assertThat(json, hasJsonPath("$.schema.properties.number.title", equalTo("Double Number")));
79+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')]", hasSize(1)));
80+
Assert.assertThat(json,
81+
hasJsonPath("$.form[?(@.key=='number')].description", hasItem("This is a double number")));
82+
Assert.assertThat(json,
83+
hasJsonPath("$.form[?(@.key=='number')].placeholder", hasItem("Double Number PlaceHolder")));
84+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].validationMessage",
85+
hasItem("this is a validation msg for double value")));
86+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].type", hasItem("number")));
87+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].notitle", hasItem(true)));
88+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].readonly", hasItem(true)));
89+
90+
}
91+
92+
@Test
93+
public void testGenerate_Number_WithRightAddon() throws JsonProcessingException {
94+
UiForm ui = UiFormSchemaGenerator.get().generate(NumberFormRight.class);
95+
String json = new ObjectMapper().writeValueAsString(ui);
96+
//Assert.assertThat(json, hasJsonPath("$.schema.properties.number.title",equalTo("Number")));
97+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')]",hasSize(1)));
98+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].description",hasItem("This is a number")));
99+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].placeholder",hasItem("Number of children")));
100+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].validationMessage",hasItem("this is a validation msg")));
101+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].type",hasItem("number")));
102+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].notitle",hasItem(true)));
103+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].readonly",hasItem(true)));
104+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].fieldAddonRight",hasItem("@")));
105+
106+
}
107+
108+
@Test
109+
public void testGenerate_Number_WithLeftAddon() throws JsonProcessingException {
110+
UiForm ui = UiFormSchemaGenerator.get().generate(NumberFormLeft.class);
111+
String json = new ObjectMapper().writeValueAsString(ui);
112+
//Assert.assertThat(json, hasJsonPath("$.schema.properties.number.title",equalTo("Number")));
113+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')]",hasSize(1)));
114+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].description",hasItem("This is a number")));
115+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].placeholder",hasItem("Number of children")));
116+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].validationMessage",hasItem("this is a validation msg")));
117+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].type",hasItem("number")));
118+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].notitle",hasItem(true)));
119+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].readonly",hasItem(true)));
120+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].fieldAddonLeft",hasItem("@")));
121+
122+
}
123+
124+
}
125+
126+
class IntegerNumberForm implements Serializable {
127+
128+
@Number(title = "Integer Number", placeHolder = "Integer Number PlaceHolder", description = "This is an integer number", noTitle = true, validationMessage = "this is a validation msg for an integer value", readOnly = true)
129+
private Integer number;
130+
131+
public Integer getNumber() {
132+
return number;
133+
}
134+
}
135+
136+
class NumberFormRight implements Serializable {
137+
138+
@Number(title = "Number of children", placeHolder = "Number of children", fieldAddonRight = "@", description = "This is a number", noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
139+
private Integer number;
140+
141+
public Integer getNumber() {
142+
return number;
143+
}
144+
}
145+
146+
class LongNumberForm implements Serializable {
147+
148+
@Number(title = "Long Number", placeHolder = "Long Number PlaceHolder", description = "This is a long number", noTitle = true, validationMessage = "this is a validation msg for long value", readOnly = true)
149+
private Long number;
150+
151+
public Long getNumber() {
152+
return number;
153+
}
154+
}
155+
156+
class NumberFormLeft implements Serializable {
157+
158+
@Number(title = "Number of children", placeHolder = "Number of children", fieldAddonLeft = "@", description = "This is a number", noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
159+
private Integer number;
160+
161+
public Integer getNumber() {
162+
return number;
163+
}
164+
}
165+
class DoubleNumberForm implements Serializable {
166+
167+
@Number(title = "Double Number", placeHolder = "Double Number PlaceHolder", description = "This is a double number", noTitle = true, validationMessage = "this is a validation msg for double value", readOnly = true)
168+
private Double number;
169+
170+
public Double getNumber() {
171+
return number;
172+
}
173+
}

0 commit comments

Comments
 (0)