From 6cbf1e87c5282415780c95fdb82ca4b8f48ed857 Mon Sep 17 00:00:00 2001
From: Rutuja Joshi <67615932+rutujavjoshi@users.noreply.github.com>
Date: Thu, 17 Sep 2020 22:02:51 +0530
Subject: [PATCH] BAEL-4201 - Initial Commit (#9957)
* BAEL-4201 - Initial commit
* BAEL-4201 - Initial Commit
* Update UserAccountController.java
removed the additional brackets
* Update UserAccountValidationTest.java
Updated the tests
* Bael - 4201 - recommit for github issue fix
For 'This commit does not belong to any branch on this repository' - delete and recommitting controller
* Bael - 4201 - recommit for github issue fix
For 'This commit does not belong to any branch on this repository' - delete and recommitting test
* Bael - 4201 - add for github issue fix
For 'This commit does not belong to any branch on this repository' - adding controller again
* Bael - 4201 - add for github issue fix
For 'This commit does not belong to any branch on this repository' - adding test again
* BAEL-4201: Initial Commit
* Update UserAccountUnitTest.java
* BAEL-4201: updated to fix the commit issue
* BAEL-4201 - temporary deleting
Deleting to fix branch issue
* BAEL-4201 - commiting test again
* BAEL-4201
Fixing build issues
* BAEL-4201
Github commit issue
* BAEL-4201
Github commit issue - webapp folder
* BAEL-4201 - commiting test after view upload
* Bael- 4201 ThymeLeaf related issues
* Bael- 4201 ThymeLeaf related issues
* Bael- 4201 ThymeLeaf related issues
* Bael- 4201 ThymeLeaf related issues - added html
* Bael- 4201 ThymeLeaf issue ; updated suffix- html
* Bael - 4201 white label issue
* Bael - 4201 white label issue
* BAEL-4201 : moving html to templates
* BAEL-4201 - moving to templates
* BAEL-4201 - moving to templates
* BAEL-4201 - review related updates
* BAEL-4201 - review related updates
* BAEL-4201 - review related updates
* BAEL-4201 - review related updates
* BAEL-4201 - review related updates
* BAEL-4201 - review related updates
* BAEL-4201 - review related updates
* BAEL-4201 - added the spring-boot-starter-validation dependency
---
spring-boot-modules/spring-boot-mvc-3/pom.xml | 6 +-
.../springvalidation/ServletInitializer.java | 12 +++
.../SpringValidationApplication.java | 13 +++
.../controller/UserAccountController.java | 44 ++++++++++
.../springvalidation/domain/UserAccount.java | 80 +++++++++++++++++++
.../springvalidation/domain/UserAddress.java | 17 ++++
.../interfaces/AdvanceInfo.java | 6 ++
.../interfaces/BasicInfo.java | 6 ++
.../src/main/resources/application.properties | 2 +
.../src/main/resources/templates/error.html | 10 +++
.../src/main/resources/templates/success.html | 10 +++
.../springvalidation/UserAccountUnitTest.java | 57 +++++++++++++
12 files changed, 262 insertions(+), 1 deletion(-)
create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/ServletInitializer.java
create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/SpringValidationApplication.java
create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/controller/UserAccountController.java
create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/domain/UserAccount.java
create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/domain/UserAddress.java
create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/interfaces/AdvanceInfo.java
create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/interfaces/BasicInfo.java
create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/resources/application.properties
create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/resources/templates/error.html
create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/resources/templates/success.html
create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/springvalidation/UserAccountUnitTest.java
diff --git a/spring-boot-modules/spring-boot-mvc-3/pom.xml b/spring-boot-modules/spring-boot-mvc-3/pom.xml
index 31c43461d2..1290b0432f 100644
--- a/spring-boot-modules/spring-boot-mvc-3/pom.xml
+++ b/spring-boot-modules/spring-boot-mvc-3/pom.xml
@@ -22,6 +22,10 @@
org.springframework.boot
spring-boot-starter-web
+
+ org.springframework.boot
+ spring-boot-starter-validation
+
org.springframework.boot
spring-boot-starter-thymeleaf
@@ -37,4 +41,4 @@
2.7
-
\ No newline at end of file
+
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/ServletInitializer.java b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/ServletInitializer.java
new file mode 100644
index 0000000000..f365582999
--- /dev/null
+++ b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/ServletInitializer.java
@@ -0,0 +1,12 @@
+package com.baeldung.springvalidation;
+
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
+
+public class ServletInitializer extends SpringBootServletInitializer {
+
+ @Override
+ protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
+ return application.sources(SpringValidationApplication.class);
+ }
+}
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/SpringValidationApplication.java b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/SpringValidationApplication.java
new file mode 100644
index 0000000000..ccfe990ce7
--- /dev/null
+++ b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/SpringValidationApplication.java
@@ -0,0 +1,13 @@
+package com.baeldung.springvalidation;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class SpringValidationApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(SpringValidationApplication.class, args);
+ }
+
+}
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/controller/UserAccountController.java b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/controller/UserAccountController.java
new file mode 100644
index 0000000000..48de7b35d0
--- /dev/null
+++ b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/controller/UserAccountController.java
@@ -0,0 +1,44 @@
+package com.baeldung.springvalidation.controller;
+
+import javax.validation.Valid;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.ui.ModelMap;
+import org.springframework.validation.BindingResult;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+import com.baeldung.springvalidation.domain.UserAccount;
+import com.baeldung.springvalidation.interfaces.BasicInfo;
+
+@Controller
+public class UserAccountController {
+
+ @GetMapping("/getUserForm")
+ public String showUserForm(Model theModel) {
+ UserAccount theUser = new UserAccount();
+ theModel.addAttribute("useraccount", theUser);
+ return "userHome";
+ }
+
+ @RequestMapping(value = "/saveBasicInfo", method = RequestMethod.POST)
+ public String saveBasicInfo(@Valid @ModelAttribute("useraccount") UserAccount useraccount, BindingResult result, ModelMap model) {
+ if (result.hasErrors()) {
+ return "error";
+ }
+ return "success";
+ }
+
+ @RequestMapping(value = "/saveBasicInfoStep1", method = RequestMethod.POST)
+ public String saveBasicInfoStep1(@Validated(BasicInfo.class) @ModelAttribute("useraccount") UserAccount useraccount, BindingResult result, ModelMap model) {
+ if (result.hasErrors()) {
+ return "error";
+ }
+ return "success";
+ }
+
+}
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/domain/UserAccount.java b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/domain/UserAccount.java
new file mode 100644
index 0000000000..fd5437fe5e
--- /dev/null
+++ b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/domain/UserAccount.java
@@ -0,0 +1,80 @@
+package com.baeldung.springvalidation.domain;
+
+import javax.validation.Valid;
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+import com.baeldung.springvalidation.interfaces.AdvanceInfo;
+import com.baeldung.springvalidation.interfaces.BasicInfo;
+
+public class UserAccount {
+
+ @NotNull(groups = BasicInfo.class)
+ @Size(min = 4, max = 15, groups = BasicInfo.class)
+ private String password;
+
+ @NotBlank(groups = BasicInfo.class)
+ private String name;
+
+ @Min(value = 18, message = "Age should not be less than 18", groups = AdvanceInfo.class)
+ private int age;
+
+ @NotBlank(groups = AdvanceInfo.class)
+ private String phone;
+
+ @Valid
+ @NotNull(groups = AdvanceInfo.class)
+ private UserAddress useraddress;
+
+ public UserAddress getUseraddress() {
+ return useraddress;
+ }
+
+ public void setUseraddress(UserAddress useraddress) {
+ this.useraddress = useraddress;
+ }
+
+ public UserAccount() {
+
+ }
+
+ public UserAccount(String email, String password, String name, int age) {
+ this.password = password;
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public String getPhone() {
+ return phone;
+ }
+
+ public void setPhone(String phone) {
+ this.phone = phone;
+ }
+
+}
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/domain/UserAddress.java b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/domain/UserAddress.java
new file mode 100644
index 0000000000..9aa9656eed
--- /dev/null
+++ b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/domain/UserAddress.java
@@ -0,0 +1,17 @@
+package com.baeldung.springvalidation.domain;
+
+import javax.validation.constraints.NotBlank;
+public class UserAddress {
+
+ @NotBlank
+ private String countryCode;
+
+ public String getCountryCode() {
+ return countryCode;
+ }
+
+ public void setCountryCode(String countryCode) {
+ this.countryCode = countryCode;
+ }
+
+}
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/interfaces/AdvanceInfo.java b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/interfaces/AdvanceInfo.java
new file mode 100644
index 0000000000..dd0c7c69d0
--- /dev/null
+++ b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/interfaces/AdvanceInfo.java
@@ -0,0 +1,6 @@
+package com.baeldung.springvalidation.interfaces;
+
+public interface AdvanceInfo {
+ // validation group marker interface
+
+}
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/interfaces/BasicInfo.java b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/interfaces/BasicInfo.java
new file mode 100644
index 0000000000..95d2b5e6a2
--- /dev/null
+++ b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/springvalidation/interfaces/BasicInfo.java
@@ -0,0 +1,6 @@
+package com.baeldung.springvalidation.interfaces;
+
+public interface BasicInfo {
+ // validation group marker interface
+
+}
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/resources/application.properties b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/application.properties
new file mode 100644
index 0000000000..89390eaace
--- /dev/null
+++ b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/application.properties
@@ -0,0 +1,2 @@
+spring.mvc.view.prefix=/WEB-INF/views/
+spring.mvc.view.suffix=.html
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/resources/templates/error.html b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/templates/error.html
new file mode 100644
index 0000000000..341cc73603
--- /dev/null
+++ b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/templates/error.html
@@ -0,0 +1,10 @@
+
+
+
+SpringValidation
+
+
+
+ Error!!!
+
+
\ No newline at end of file
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/resources/templates/success.html b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/templates/success.html
new file mode 100644
index 0000000000..b422152153
--- /dev/null
+++ b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/templates/success.html
@@ -0,0 +1,10 @@
+
+
+
+SpringValidation
+
+
+
+ SUCCESS!!!
+
+
\ No newline at end of file
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/springvalidation/UserAccountUnitTest.java b/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/springvalidation/UserAccountUnitTest.java
new file mode 100644
index 0000000000..507321bf8e
--- /dev/null
+++ b/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/springvalidation/UserAccountUnitTest.java
@@ -0,0 +1,57 @@
+package com.baeldung.springvalidation;
+
+import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
+
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@AutoConfigureMockMvc
+public class UserAccountUnitTest {
+
+ @Autowired
+ private MockMvc mockMvc;
+
+ @Test
+ public void givenSaveBasicInfo_whenCorrectInput_thenSuccess() throws Exception {
+ this.mockMvc.perform(MockMvcRequestBuilders.post("/saveBasicInfo")
+ .accept(MediaType.TEXT_HTML)
+ .param("name", "test123")
+ .param("password", "pass"))
+ .andExpect(view().name("success"))
+ .andExpect(status().isOk())
+ .andDo(print());
+ }
+
+ @Test
+ public void givenSaveBasicInfoStep1_whenCorrectInput_thenSuccess() throws Exception {
+ this.mockMvc.perform(MockMvcRequestBuilders.post("/saveBasicInfoStep1")
+ .accept(MediaType.TEXT_HTML)
+ .param("name", "test123")
+ .param("password", "pass"))
+ .andExpect(view().name("success"))
+ .andExpect(status().isOk())
+ .andDo(print());
+ }
+
+ @Test
+ public void givenSaveBasicInfoStep1_whenIncorrectInput_thenError() throws Exception {
+ this.mockMvc.perform(MockMvcRequestBuilders.post("/saveBasicInfoStep1")
+ .accept(MediaType.TEXT_HTML))
+ // .param("name", "test123")
+ // .param("password", "pass"))
+ .andExpect(model().errorCount(2))
+ // .andExpect(view().name("error"))
+ .andExpect(status().isOk())
+ .andDo(print());
+ }
+
+}