mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-19 23:26:07 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -30,10 +30,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Page Controller pattern is utilized when we want to simplify relationship in a dynamic website.
|
||||
* It is an approach of one front page leading to one logical file that handles HTTP requests and actions.
|
||||
* In this example, we build a website with signup page handling an input form with Signup Controller, Signup View, and Signup Model
|
||||
* and after signup, it is redirected to a user page handling with User Controller, User View, and User Model.
|
||||
*/
|
||||
* It is an approach of one front page leading to one logical file that handles HTTP requests and
|
||||
* actions. In this example, we build a website with signup page handling an input form with Signup
|
||||
* Controller, Signup View, and Signup Model and after signup, it is redirected to a user page
|
||||
* handling with User Controller, User View, and User Model.
|
||||
*/
|
||||
@Slf4j
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
|
||||
@@ -31,31 +31,23 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
/**
|
||||
* Signup Controller.
|
||||
*/
|
||||
/** Signup Controller. */
|
||||
@Slf4j
|
||||
@Controller
|
||||
@Component
|
||||
public class SignupController {
|
||||
SignupView view = new SignupView();
|
||||
/**
|
||||
* Signup Controller can handle http request and decide which model and view use.
|
||||
*/
|
||||
SignupController() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle http GET request.
|
||||
*/
|
||||
/** Signup Controller can handle http request and decide which model and view use. */
|
||||
SignupController() {}
|
||||
|
||||
/** Handle http GET request. */
|
||||
@GetMapping("/signup")
|
||||
public String getSignup() {
|
||||
return view.display();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle http POST request and access model and view.
|
||||
*/
|
||||
/** Handle http POST request and access model and view. */
|
||||
@PostMapping("/signup")
|
||||
public String create(SignupModel form, RedirectAttributes redirectAttributes) {
|
||||
LOGGER.info(form.getName());
|
||||
|
||||
@@ -28,9 +28,7 @@ import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* ignup model.
|
||||
*/
|
||||
/** ignup model. */
|
||||
@Component
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@@ -38,5 +36,4 @@ public class SignupModel {
|
||||
private String name;
|
||||
private String email;
|
||||
private String password;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.page.controller;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Signup View.
|
||||
*/
|
||||
/** Signup View. */
|
||||
@Slf4j
|
||||
@NoArgsConstructor
|
||||
public class SignupView {
|
||||
@@ -39,11 +37,10 @@ public class SignupView {
|
||||
return "/signup";
|
||||
}
|
||||
|
||||
/**
|
||||
* redirect to user page.
|
||||
*/
|
||||
/** redirect to user page. */
|
||||
public String redirect(SignupModel form) {
|
||||
LOGGER.info("Redirect to user page with " + "name " + form.getName() + " email " + form.getEmail());
|
||||
LOGGER.info(
|
||||
"Redirect to user page with " + "name " + form.getName() + " email " + form.getEmail());
|
||||
return "redirect:/user";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,22 +30,18 @@ import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* User Controller.
|
||||
*/
|
||||
/** User Controller. */
|
||||
@Slf4j
|
||||
@Controller
|
||||
@NoArgsConstructor
|
||||
public class UserController {
|
||||
private final UserView view = new UserView();
|
||||
|
||||
/**
|
||||
* Handle http GET request and access view and model.
|
||||
*/
|
||||
/** Handle http GET request and access view and model. */
|
||||
@GetMapping("/user")
|
||||
public String getUserPath(SignupModel form, Model model) {
|
||||
model.addAttribute("name", form.getName());
|
||||
model.addAttribute("email", form.getEmail());
|
||||
return view.display(form);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,13 +27,10 @@ package com.iluwatar.page.controller;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* User model.
|
||||
*/
|
||||
/** User model. */
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class UserModel {
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,13 +26,12 @@ package com.iluwatar.page.controller;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* User view class generating html file.
|
||||
*/
|
||||
/** User view class generating html file. */
|
||||
@Slf4j
|
||||
public class UserView {
|
||||
/**
|
||||
* displaying command to generate html.
|
||||
*
|
||||
* @param user model content.
|
||||
*/
|
||||
public String display(SignupModel user) {
|
||||
|
||||
@@ -24,15 +24,14 @@
|
||||
*/
|
||||
package com.iluwatar.page.controller;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Application test */
|
||||
public class AppTest {
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
+4
-7
@@ -24,19 +24,16 @@
|
||||
*/
|
||||
package com.iluwatar.page.controller;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Test for Signup Controller
|
||||
*/
|
||||
/** Test for Signup Controller */
|
||||
public class SignupControllerTest {
|
||||
|
||||
/**
|
||||
* Verify if user can sign up and redirect to user page
|
||||
*/
|
||||
/** Verify if user can sign up and redirect to user page */
|
||||
@Test
|
||||
void testSignup() {
|
||||
var controller = new SignupController();
|
||||
|
||||
@@ -24,16 +24,13 @@
|
||||
*/
|
||||
package com.iluwatar.page.controller;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Test for Signup Model
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Test for Signup Model */
|
||||
public class SignupModelTest {
|
||||
/**
|
||||
* Verify if a user can set a name properly
|
||||
*/
|
||||
/** Verify if a user can set a name properly */
|
||||
@Test
|
||||
void testSetName() {
|
||||
SignupModel model = new SignupModel();
|
||||
@@ -41,9 +38,7 @@ public class SignupModelTest {
|
||||
assertEquals("Lily", model.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if a user can set an email properly
|
||||
*/
|
||||
/** Verify if a user can set an email properly */
|
||||
@Test
|
||||
void testSetEmail() {
|
||||
SignupModel model = new SignupModel();
|
||||
@@ -51,9 +46,7 @@ public class SignupModelTest {
|
||||
assertEquals("Lily@email", model.getEmail());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if a user can set a password properly
|
||||
*/
|
||||
/** Verify if a user can set a password properly */
|
||||
@Test
|
||||
void testSetPassword() {
|
||||
SignupModel model = new SignupModel();
|
||||
|
||||
@@ -24,6 +24,10 @@
|
||||
*/
|
||||
package com.iluwatar.page.controller;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -31,9 +35,6 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest
|
||||
@@ -41,17 +42,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
public class UserControllerTest {
|
||||
private UserController userController;
|
||||
|
||||
@Autowired
|
||||
MockMvc mockMvc;
|
||||
@Autowired MockMvc mockMvc;
|
||||
|
||||
/**
|
||||
* Verify if view and model are directed properly
|
||||
*/
|
||||
/** Verify if view and model are directed properly */
|
||||
@Test
|
||||
void testGetUserPath () throws Exception {
|
||||
this.mockMvc.perform(get("/user")
|
||||
.param("name", "Lily")
|
||||
.param("email", "Lily@email.com"))
|
||||
void testGetUserPath() throws Exception {
|
||||
this.mockMvc
|
||||
.perform(get("/user").param("name", "Lily").param("email", "Lily@email.com"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(model().attribute("name", "Lily"))
|
||||
.andExpect(model().attribute("email", "Lily@email.com"))
|
||||
|
||||
@@ -29,9 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class UserModelTest {
|
||||
/**
|
||||
* Verify if a user can set a name properly
|
||||
*/
|
||||
/** Verify if a user can set a name properly */
|
||||
@Test
|
||||
void testSetName() {
|
||||
UserModel model = new UserModel();
|
||||
@@ -39,9 +37,7 @@ public class UserModelTest {
|
||||
assertEquals("Lily", model.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if a user can set an email properly
|
||||
*/
|
||||
/** Verify if a user can set an email properly */
|
||||
@Test
|
||||
void testSetEmail() {
|
||||
UserModel model = new UserModel();
|
||||
|
||||
Reference in New Issue
Block a user