diff --git a/pom.xml b/pom.xml index 2a02de4..ef1fe52 100644 --- a/pom.xml +++ b/pom.xml @@ -67,6 +67,12 @@ slf4j-api 1.7.25 + + com.jayway.jsonpath + json-path-assert + 2.2.0 + test + diff --git a/src/main/java/io/asfjava/ui/core/GeneratorFactoryInitializer.java b/src/main/java/io/asfjava/ui/core/GeneratorFactoryInitializer.java index 6acd17e..d6bd94b 100644 --- a/src/main/java/io/asfjava/ui/core/GeneratorFactoryInitializer.java +++ b/src/main/java/io/asfjava/ui/core/GeneratorFactoryInitializer.java @@ -5,7 +5,7 @@ import javax.servlet.annotation.WebListener; @WebListener -class GeneratorFactoryInitializer implements ServletContextListener { +public class GeneratorFactoryInitializer implements ServletContextListener { @Override public final void contextInitialized(final ServletContextEvent sce) { diff --git a/src/test/java/io/asfjava/ui/AppTest.java b/src/test/java/io/asfjava/ui/AppTest.java deleted file mode 100644 index 24100c4..0000000 --- a/src/test/java/io/asfjava/ui/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package io.asfjava.ui; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/src/test/java/io/asfjava/ui/core/schema/CivilStateValues.java b/src/test/java/io/asfjava/ui/core/schema/CivilStateValues.java new file mode 100644 index 0000000..e54aff0 --- /dev/null +++ b/src/test/java/io/asfjava/ui/core/schema/CivilStateValues.java @@ -0,0 +1,21 @@ +package io.asfjava.ui.core.schema; + +import java.util.HashMap; +import java.util.Map; + +import io.asfjava.ui.core.form.ValuesContainer; + +public class CivilStateValues implements ValuesContainer { + + @Override + public Map getValues() { + + HashMap myMap = new HashMap<>(); + + myMap.put("Maried", "COMMITTED"); + myMap.put("Single", "HAPPY"); + myMap.put("Divorced", "RELEASED"); + + return myMap; + } +} diff --git a/src/test/java/io/asfjava/ui/core/schema/GenderTitleMap.java b/src/test/java/io/asfjava/ui/core/schema/GenderTitleMap.java new file mode 100644 index 0000000..8a2bea3 --- /dev/null +++ b/src/test/java/io/asfjava/ui/core/schema/GenderTitleMap.java @@ -0,0 +1,18 @@ +package io.asfjava.ui.core.schema; + +import java.util.HashMap; +import java.util.Map; + +import io.asfjava.ui.core.form.ValuesContainer; + +public class GenderTitleMap implements ValuesContainer { + + @Override + public Map getValues() { + HashMap values = new HashMap<>(); + values.put("Male", "male"); + values.put("Female", "female"); + return values; + } + +} diff --git a/src/test/java/io/asfjava/ui/core/schema/MyCheckBoxValues.java b/src/test/java/io/asfjava/ui/core/schema/MyCheckBoxValues.java new file mode 100644 index 0000000..8eff4b2 --- /dev/null +++ b/src/test/java/io/asfjava/ui/core/schema/MyCheckBoxValues.java @@ -0,0 +1,18 @@ +package io.asfjava.ui.core.schema; + +import java.util.HashMap; +import java.util.Map; + +import io.asfjava.ui.core.form.ValuesContainer; + +public class MyCheckBoxValues implements ValuesContainer { + + @Override + public Map getValues() { + Map values = new HashMap<>(); + values.put("Red", "red"); + values.put("Green", "green"); + values.put("Blue", "blue"); + return values; + } +} diff --git a/src/test/java/io/asfjava/ui/core/schema/UiFormSchemaGeneratorTest.java b/src/test/java/io/asfjava/ui/core/schema/UiFormSchemaGeneratorTest.java new file mode 100644 index 0000000..22f9be6 --- /dev/null +++ b/src/test/java/io/asfjava/ui/core/schema/UiFormSchemaGeneratorTest.java @@ -0,0 +1,258 @@ +package io.asfjava.ui.core.schema; + +import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasSize; + +import java.io.Serializable; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import io.asfjava.ui.core.GeneratorFactoryInitializer; +import io.asfjava.ui.core.form.CheckBox; +import io.asfjava.ui.core.form.ComboBox; +import io.asfjava.ui.core.form.Number; +import io.asfjava.ui.core.form.Password; +import io.asfjava.ui.core.form.RadioBox; +import io.asfjava.ui.core.form.TextArea; +import io.asfjava.ui.core.form.TextField; +import io.asfjava.ui.dto.UiForm; + +public class UiFormSchemaGeneratorTest { + + @BeforeClass + public static void init() { + new GeneratorFactoryInitializer().contextInitialized(null); + } + + @Test + public void testGenerate_textField() throws JsonProcessingException { + UiForm ui = UiFormSchemaGenerator.get().generate(TextFieldForm.class); + String json = new ObjectMapper().writeValueAsString(ui); + + + Assert.assertThat(json, hasJsonPath("$.schema.properties.firstName.title",equalTo("First Name"))); + Assert.assertThat(json, hasJsonPath("$.schema.properties.firstName.pattern",equalTo("[a-z]"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='firstName')]",hasSize(1))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='firstName')].description",hasItem("This is a description for your first name field"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='firstName')].placeholder",hasItem("Your first name"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='firstName')].validationMessage",hasItem("this is a validation msg"))); + //Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='firstName')].type",hasItem("textField"))); + + } + + @Test + public void testGenerate_Number() throws JsonProcessingException { + UiForm ui = UiFormSchemaGenerator.get().generate(NumberForm.class); + String json = new ObjectMapper().writeValueAsString(ui); + //Assert.assertThat(json, hasJsonPath("$.schema.properties.number.title",equalTo("Number"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')]",hasSize(1))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].description",hasItem("This is a number"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].placeholder",hasItem("Number of children"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].validationMessage",hasItem("this is a validation msg"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].type",hasItem("number"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].notitle",hasItem(true))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='number')].readonly",hasItem(true))); + + } + + @Test + public void testGenerate_Password() throws JsonProcessingException { + UiForm ui = UiFormSchemaGenerator.get().generate(PasswordForm.class); + + String json = new ObjectMapper().writeValueAsString(ui); + Assert.assertThat(json, hasJsonPath("$.schema.properties.password.title",equalTo("Password"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')]",hasSize(1))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].description",hasItem("This is password"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].placeholder",hasItem("Please set you password"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].validationMessage",hasItem("this is a validation msg"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].type",hasItem("password"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].notitle",hasItem(true))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].readonly",hasItem(true))); + } + + @Test + public void testGenerate_TextArea() throws JsonProcessingException { + UiForm ui = UiFormSchemaGenerator.get().generate(TextAreaForm.class); + + String json = new ObjectMapper().writeValueAsString(ui); + Assert.assertThat(json, hasJsonPath("$.schema.properties.address.title",equalTo("Address"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')]",hasSize(1))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].description",hasItem("This is textarea"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].placeholder",hasItem("Fill your address please"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].validationMessage",hasItem("this is a validation msg"))); + //Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].type",hasItem("textArea"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].notitle",hasItem(true))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].readonly",hasItem(true))); + + } + + @Test + public void testGenerate_CheckBox() throws JsonProcessingException { + UiForm ui = UiFormSchemaGenerator.get().generate(CheckBoxForm.class); + String json = new ObjectMapper().writeValueAsString(ui); + Assert.assertThat(json, hasJsonPath("$.schema.properties.color.title",equalTo("Color"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')]",hasSize(1))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].multiple",hasItem(false))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].required",hasItem(true))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Red')].value",hasItem("red"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Blue')].value",hasItem("blue"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Green')].value",hasItem("green"))); + } + + @Test + public void testGenerate_CheckBox_WithCustomValuesContainer() throws JsonProcessingException { + UiForm ui = UiFormSchemaGenerator.get().generate(CheckBoxForm2.class); + String json = new ObjectMapper().writeValueAsString(ui); + Assert.assertThat(json, hasJsonPath("$.schema.properties.color.title",equalTo("Color"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')]",hasSize(1))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].multiple",hasItem(true))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].required",hasItem(false))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Red')].value",hasItem("red"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Blue')].value",hasItem("blue"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='color')].titleMap[?(@.name=='Green')].value",hasItem("green"))); + } + + @Test + public void testGenerate_RadioBox() throws JsonProcessingException { + UiForm ui = UiFormSchemaGenerator.get().generate(RadioBoxForm.class); + String json = new ObjectMapper().writeValueAsString(ui); + Assert.assertThat(json, hasJsonPath("$.schema.properties.civilState.title",equalTo("Civil State"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='civilState')]",hasSize(1))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='civilState')].readOnly",hasItem(false))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='civilState')].titleMap[?(@.name=='Maried')].value",hasItem("COMMITTED"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='civilState')].titleMap[?(@.name=='Single')].value",hasItem("HAPPY"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='civilState')].titleMap[?(@.name=='Divorced')].value",hasItem("RELEASED"))); + + } + + @Test + public void testGenerate_ComboBox() throws JsonProcessingException { + UiForm ui = UiFormSchemaGenerator.get().generate(ComboBoxForm.class); + + String json = new ObjectMapper().writeValueAsString(ui); + Assert.assertThat(json, hasJsonPath("$.schema.properties.currency.title",equalTo("Currency"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')]",hasSize(1))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].disabled",hasItem(false))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].multiple",hasItem(false))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].required",hasItem(true))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].autofocus",hasItem(false))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].titleMap[?(@.name=='Euro')].value",hasItem("euro"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='currency')].titleMap[?(@.name=='Dollar')].value",hasItem("dollar"))); + + } + + @Test + public void testGenerate_ComboBox_WithCustomValuesContainer() throws JsonProcessingException { + UiForm ui = UiFormSchemaGenerator.get().generate(ComboBoxForm2.class); + + String json = new ObjectMapper().writeValueAsString(ui); + Assert.assertThat(json, hasJsonPath("$.schema.properties.gender.title",equalTo("Gender"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')]",hasSize(1))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].disabled",hasItem(false))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].multiple",hasItem(false))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].required",hasItem(false))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].autofocus",hasItem(false))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].titleMap[?(@.name=='Male')].value",hasItem("male"))); + Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='gender')].titleMap[?(@.name=='Female')].value",hasItem("female"))); + + } + +} + +class TextFieldForm implements Serializable { + + @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") + private String firstName; + + public String getFirstName() { + return firstName; + } + +} + +class NumberForm implements Serializable { + + @Number(title = "Number of children", placeHolder = "Number of children", description = "This is a number", noTitle = true, validationMessage = "this is a validation msg", readOnly = true) + private Integer number; + + public Integer getNumber() { + return number; + } +} + +class PasswordForm implements Serializable { + + @Password(title = "Password", placeHolder = "Please set you password", description = "This is password", noTitle = true, validationMessage = "this is a validation msg", readOnly = true) + private String password; + + public String getPassword() { + return password; + } +} + +class TextAreaForm implements Serializable { + + @TextArea(title = "Address", placeHolder = "Fill your address please", description = "This is textarea", noTitle = true, validationMessage = "this is a validation msg", readOnly = true) + private String address; + + public String getAddress() { + return address; + } +} + +class CheckBoxForm implements Serializable { + + @CheckBox(title = "Color", values = { "red", "blue", "green" }, defaultvalue = "red", required = true) + private String color; + + public String getColor() { + return color; + } +} + +class CheckBoxForm2 implements Serializable { + + @CheckBox(title = "Color", titleMap = MyCheckBoxValues.class, defaultvalue = "red", multiple = true) + private String color; + + public String getColor() { + return color; + } +} + +class RadioBoxForm implements Serializable { + + @RadioBox(title = "Civil State", titleMap = CivilStateValues.class) + private String civilState; + + public String getCivilState() { + return civilState; + } +} + +class ComboBoxForm implements Serializable { + + @ComboBox(title = "Currency", values = { "euro", "dollar" }, required = true) + private String currency; + + public String getCurrency() { + return currency; + } +} + +class ComboBoxForm2 implements Serializable { + + @ComboBox(title = "Gender", titleMap = GenderTitleMap.class) + private String gender; + + public String getGender() { + return gender; + } +}