mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-17 02:23:26 +00:00
feature: add Page Controller pattern (#2202)
* feat: create page controller * add test * add documentation * fix: delete img file * fix: modify App, SignupMode, UserController, pom.xml, and Readme * fix: modify MVC files on page controller * fix: modify readme file
This commit is contained in:
@@ -0,0 +1,162 @@
|
|||||||
|
---
|
||||||
|
title: Page Controller
|
||||||
|
categories: Structural
|
||||||
|
language: en
|
||||||
|
tags:
|
||||||
|
- Decoupling
|
||||||
|
---
|
||||||
|
|
||||||
|
## Name / classification
|
||||||
|
|
||||||
|
Page Controller
|
||||||
|
|
||||||
|
## Intent
|
||||||
|
|
||||||
|
It is an approach of one page leading to one logical file that handles actions or requests on a website.
|
||||||
|
|
||||||
|
## Explanation
|
||||||
|
|
||||||
|
Real-world example
|
||||||
|
|
||||||
|
> In a shopping website, there is a signup page to register a user profile.
|
||||||
|
> After finishing to signup, the signup page will be redirected to a user page to show the user's registered information.
|
||||||
|
|
||||||
|
In plain words
|
||||||
|
|
||||||
|
> Page controller manages HTTP requests and data in a specific page using MVC idea.
|
||||||
|
> The idea is that one page contains one Controller that handles Model and View.
|
||||||
|
|
||||||
|
**Programmatic Example**
|
||||||
|
|
||||||
|
Here's Signup controller when a user signup their information for a website.
|
||||||
|
|
||||||
|
```java
|
||||||
|
@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.
|
||||||
|
*/
|
||||||
|
@GetMapping("/signup")
|
||||||
|
public String getSignup() {
|
||||||
|
return view.display();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle http POST request and access model and view.
|
||||||
|
*/
|
||||||
|
@PostMapping("/signup")
|
||||||
|
public String create(SignupModel form, RedirectAttributes redirectAttributes) {
|
||||||
|
LOGGER.info(form.getName());
|
||||||
|
LOGGER.info(form.getEmail());
|
||||||
|
redirectAttributes.addAttribute("name", form.getName());
|
||||||
|
redirectAttributes.addAttribute("email", form.getEmail());
|
||||||
|
redirectAttributes.addFlashAttribute("userInfo", form);
|
||||||
|
return view.redirect(form);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Here's Signup model and view that are handled by Signup controller.
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Component
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class SignupModel {
|
||||||
|
private String name;
|
||||||
|
private String email;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public SignupModel() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Slf4j
|
||||||
|
public class SignupView {
|
||||||
|
public SignupView() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public String display() {
|
||||||
|
LOGGER.info("display signup front page");
|
||||||
|
return "/signup";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* redirect to user page.
|
||||||
|
*/
|
||||||
|
public String redirect(SignupModel form) {
|
||||||
|
LOGGER.info("Redirect to user page with " + "name " + form.getName() + " email " + form.getEmail());
|
||||||
|
return "redirect:/user";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Here's User Controller to handle Get request in a user page.
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Slf4j
|
||||||
|
@Controller
|
||||||
|
public class UserController {
|
||||||
|
UserView view = new UserView();
|
||||||
|
|
||||||
|
public UserController() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Here's User Model and View that are handled by User controller.
|
||||||
|
```java
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class UserModel {
|
||||||
|
private String name;
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
public UserModel() {}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Slf4j
|
||||||
|
public class UserView {
|
||||||
|
/**
|
||||||
|
* displaying command to generate html.
|
||||||
|
* @param user model content.
|
||||||
|
*/
|
||||||
|
public String display(SignupModel user) {
|
||||||
|
LOGGER.info("display user html" + " name " + user.getName() + " email " + user.getEmail());
|
||||||
|
return "/user";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Class diagram
|
||||||
|

|
||||||
|
|
||||||
|
## Applicability
|
||||||
|
Use the Page Controller pattern when
|
||||||
|
- you implement a site where most controller logic is simple
|
||||||
|
- you implement a site where particular actions are handled with a particular server page
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
- [Page Controller](https://www.martinfowler.com/eaaCatalog/pageController.html)
|
||||||
|
- [Pattern of Enterprise Application Architecture](https://www.martinfowler.com/books/eaa.html)
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 74 KiB |
@@ -0,0 +1,59 @@
|
|||||||
|
@startuml
|
||||||
|
package com.iluwatar.page.controller {
|
||||||
|
class App {
|
||||||
|
- LOGGER : Logger {static}
|
||||||
|
+ App()
|
||||||
|
+ main(args : String[]) {static}
|
||||||
|
}
|
||||||
|
class SignupController {
|
||||||
|
- LOGGER : Logger {static}
|
||||||
|
~ view : SignupView
|
||||||
|
~ SignupController()
|
||||||
|
+ create(form : SignupModel, redirectAttributes : RedirectAttributes) : String
|
||||||
|
+ getSignup() : String
|
||||||
|
}
|
||||||
|
class SignupModel {
|
||||||
|
- email : String
|
||||||
|
- name : String
|
||||||
|
- password : String
|
||||||
|
+ SignupModel()
|
||||||
|
+ SignupModel(name : String, email : String, password : String)
|
||||||
|
+ getEmail() : String
|
||||||
|
+ getName() : String
|
||||||
|
+ getPassword() : String
|
||||||
|
+ setEmail(email : String)
|
||||||
|
+ setName(name : String)
|
||||||
|
+ setPassword(password : String)
|
||||||
|
}
|
||||||
|
class SignupView {
|
||||||
|
- LOGGER : Logger {static}
|
||||||
|
+ SignupView()
|
||||||
|
+ display() : String
|
||||||
|
+ redirect(form : SignupModel) : String
|
||||||
|
}
|
||||||
|
class UserController {
|
||||||
|
- LOGGER : Logger {static}
|
||||||
|
~ view : UserView
|
||||||
|
+ UserController()
|
||||||
|
+ getUserPath(form : SignupModel, model : Model) : String
|
||||||
|
}
|
||||||
|
class UserModel {
|
||||||
|
- email : String
|
||||||
|
- name : String
|
||||||
|
+ UserModel()
|
||||||
|
+ getEmail() : String
|
||||||
|
+ getName() : String
|
||||||
|
+ setEmail(email : String)
|
||||||
|
+ setName(name : String)
|
||||||
|
}
|
||||||
|
class UserView {
|
||||||
|
- LOGGER : Logger {static}
|
||||||
|
+ UserView()
|
||||||
|
+ display(user : SignupModel) : String
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UserController --> "-view" UserView
|
||||||
|
UserController --> "-model" UserModel
|
||||||
|
SignupController --> "-view" SignupView
|
||||||
|
SignupController --> "-model" SignupModel
|
||||||
|
@enduml
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright © 2014-2022 Ilkka Seppälä
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>page-controller</artifactId>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.iluwatar</groupId>
|
||||||
|
<artifactId>java-design-patterns</artifactId>
|
||||||
|
<version>1.26.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-webmvc</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<mainClass>com.iluwatar.page.controller.App</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||||
|
*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2022 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.iluwatar.page.controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@SpringBootApplication
|
||||||
|
public class App {
|
||||||
|
/**
|
||||||
|
* Program entry point.
|
||||||
|
*
|
||||||
|
* @param args command line args
|
||||||
|
*/
|
||||||
|
public static void main(final String[] args) {
|
||||||
|
SpringApplication.run(App.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||||
|
*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2022 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.iluwatar.page.controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
@GetMapping("/signup")
|
||||||
|
public String getSignup() {
|
||||||
|
return view.display();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle http POST request and access model and view.
|
||||||
|
*/
|
||||||
|
@PostMapping("/signup")
|
||||||
|
public String create(SignupModel form, RedirectAttributes redirectAttributes) {
|
||||||
|
LOGGER.info(form.getName());
|
||||||
|
LOGGER.info(form.getEmail());
|
||||||
|
redirectAttributes.addAttribute("name", form.getName());
|
||||||
|
redirectAttributes.addAttribute("email", form.getEmail());
|
||||||
|
redirectAttributes.addFlashAttribute("userInfo", form);
|
||||||
|
return view.redirect(form);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||||
|
*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2022 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.iluwatar.page.controller;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignup model.
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class SignupModel {
|
||||||
|
private String name;
|
||||||
|
private String email;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public SignupModel() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||||
|
*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2022 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.iluwatar.page.controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Signup View.
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class SignupView {
|
||||||
|
public SignupView() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public String display() {
|
||||||
|
LOGGER.info("display signup front page");
|
||||||
|
return "/signup";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* redirect to user page.
|
||||||
|
*/
|
||||||
|
public String redirect(SignupModel form) {
|
||||||
|
LOGGER.info("Redirect to user page with " + "name " + form.getName() + " email " + form.getEmail());
|
||||||
|
return "redirect:/user";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||||
|
*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2022 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.iluwatar.page.controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User Controller.
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Controller
|
||||||
|
public class UserController {
|
||||||
|
private final UserView view = new UserView();
|
||||||
|
|
||||||
|
public UserController() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||||
|
*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2022 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.iluwatar.page.controller;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User model.
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class UserModel {
|
||||||
|
private String name;
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
public UserModel() {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||||
|
*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2022 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.iluwatar.page.controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User view class generating html file.
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class UserView {
|
||||||
|
/**
|
||||||
|
* displaying command to generate html.
|
||||||
|
* @param user model content.
|
||||||
|
*/
|
||||||
|
public String display(SignupModel user) {
|
||||||
|
LOGGER.info("display user html" + " name " + user.getName() + " email " + user.getEmail());
|
||||||
|
return "/user";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#
|
||||||
|
# The MIT License
|
||||||
|
# Copyright © 2014-2021 Ilkka Seppälä
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
# THE SOFTWARE.
|
||||||
|
#
|
||||||
|
#spring.thymeleaf.mode=HTML
|
||||||
|
|
||||||
|
server.port=51515
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<!DOCTYPE HTML>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||||
|
<title> Signup </title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<H1>Sign Up</H1>
|
||||||
|
<form action="#" th:action="@{/signup}" method="post" th:object="${signup}">
|
||||||
|
<div style="margin:20px">
|
||||||
|
<label>Name</label>
|
||||||
|
<input type="text" id="name" name="name"/>
|
||||||
|
</div>
|
||||||
|
<div style="margin:20px">
|
||||||
|
<label>Email</label>
|
||||||
|
<input type="text" id="email" name="email"/>
|
||||||
|
</div>
|
||||||
|
<div style="margin:20px">
|
||||||
|
<label>password</label>
|
||||||
|
<input type="password" id="password" name="password"/>
|
||||||
|
</div>
|
||||||
|
<div style="margin:20px">
|
||||||
|
<input type="submit" value="Signup">
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE HTML>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||||
|
<title> User </title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>Your Information</h1>
|
||||||
|
<p>Name: <span th:text="${name}"></span></p>
|
||||||
|
<p>Email: <span th:text="${email}"></span></p>
|
||||||
|
</body>
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||||
|
*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2022 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.iluwatar.page.controller;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Application test
|
||||||
|
*/
|
||||||
|
public class AppTest {
|
||||||
|
@Test
|
||||||
|
void shouldExecuteApplicationWithoutException() {
|
||||||
|
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||||
|
*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2022 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.iluwatar.page.controller;
|
||||||
|
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
public class SignupControllerTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify if user can sign up and redirect to user page
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testSignup() {
|
||||||
|
var controller = new SignupController();
|
||||||
|
controller.getSignup();
|
||||||
|
|
||||||
|
RedirectAttributes redirectAttributes = new RedirectAttributesModelMap();
|
||||||
|
String redirectPath = controller.create(retrieveSignupData(), redirectAttributes);
|
||||||
|
assertEquals("redirect:/user", redirectPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SignupModel retrieveSignupData() {
|
||||||
|
SignupModel model = new SignupModel();
|
||||||
|
model.setName("Lily");
|
||||||
|
model.setEmail("lily@email.com");
|
||||||
|
model.setPassword("password1234");
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||||
|
*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2022 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.iluwatar.page.controller;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for Signup Model
|
||||||
|
*/
|
||||||
|
public class SignupModelTest {
|
||||||
|
/**
|
||||||
|
* Verify if a user can set a name properly
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testSetName() {
|
||||||
|
SignupModel model = new SignupModel();
|
||||||
|
model.setName("Lily");
|
||||||
|
assertEquals("Lily", model.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify if a user can set an email properly
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testSetEmail() {
|
||||||
|
SignupModel model = new SignupModel();
|
||||||
|
model.setEmail("Lily@email");
|
||||||
|
assertEquals("Lily@email", model.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify if a user can set a password properly
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testSetPassword() {
|
||||||
|
SignupModel model = new SignupModel();
|
||||||
|
model.setPassword("password1234");
|
||||||
|
assertEquals("password1234", model.getPassword());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.iluwatar.page.controller;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
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.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
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
public class UserControllerTest {
|
||||||
|
private UserController userController;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
MockMvc mockMvc;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(model().attribute("name", "Lily"))
|
||||||
|
.andExpect(model().attribute("email", "Lily@email.com"))
|
||||||
|
.andReturn();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.iluwatar.page.controller;
|
||||||
|
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testSetName() {
|
||||||
|
UserModel model = new UserModel();
|
||||||
|
model.setName("Lily");
|
||||||
|
assertEquals("Lily", model.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify if a user can set an email properly
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testSetEmail() {
|
||||||
|
UserModel model = new UserModel();
|
||||||
|
model.setEmail("Lily@email");
|
||||||
|
assertEquals("Lily@email", model.getEmail());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user