Merge pull request #8649 from kwoyke/BAEL-20862

BAEL-20862: Move spring-boot-crud into spring-boot-modules
This commit is contained in:
Loredana Crusoveanu 2020-02-01 10:03:58 +02:00 committed by GitHub
commit be3f5b4256
14 changed files with 378 additions and 379 deletions

View File

@ -645,7 +645,6 @@
<!-- <module>spring-boot-cli</module> --> <!-- Not a maven project --> <!-- <module>spring-boot-cli</module> --> <!-- Not a maven project -->
<module>spring-boot-client</module> <module>spring-boot-client</module>
<module>spring-boot-config-jpa-error</module> <module>spring-boot-config-jpa-error</module>
<module>spring-boot-crud</module>
<module>spring-boot-ctx-fluent</module> <module>spring-boot-ctx-fluent</module>
<module>spring-boot-deployment</module> <module>spring-boot-deployment</module>
<module>spring-boot-di</module> <module>spring-boot-di</module>
@ -1170,7 +1169,6 @@
<!-- <module>spring-boot-cli</module> --> <!-- Not a maven project --> <!-- <module>spring-boot-cli</module> --> <!-- Not a maven project -->
<module>spring-boot-client</module> <module>spring-boot-client</module>
<module>spring-boot-config-jpa-error</module> <module>spring-boot-config-jpa-error</module>
<module>spring-boot-crud</module>
<module>spring-boot-ctx-fluent</module> <module>spring-boot-ctx-fluent</module>
<module>spring-boot-deployment</module> <module>spring-boot-deployment</module>
<module>spring-boot-di</module> <module>spring-boot-di</module>

View File

@ -19,6 +19,7 @@
<module>spring-boot-autoconfiguration</module> <module>spring-boot-autoconfiguration</module>
<module>spring-boot-camel</module> <module>spring-boot-camel</module>
<module>spring-boot-custom-starter</module> <module>spring-boot-custom-starter</module>
<module>spring-boot-crud</module>
<module>spring-boot-data</module> <module>spring-boot-data</module>
<!-- <module>spring-boot-gradle</module> --> <!-- Not a maven project --> <!-- <module>spring-boot-gradle</module> --> <!-- Not a maven project -->
<module>spring-boot-keycloak</module> <module>spring-boot-keycloak</module>

View File

@ -1,86 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-crud</artifactId> <artifactId>spring-boot-crud</artifactId>
<name>spring-boot-crud</name> <name>spring-boot-crud</name>
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId> <artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath> <relativePath>../../parent-boot-2</relativePath>
</parent> </parent>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId> <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>spring-boot-crud</finalName> <finalName>spring-boot-crud</finalName>
<resources> <resources>
<resource> <resource>
<directory>src/main/resources</directory> <directory>src/main/resources</directory>
<filtering>true</filtering> <filtering>true</filtering>
</resource> </resource>
</resources> </resources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
<configuration> <configuration>
<classifier>exec</classifier> <classifier>exec</classifier>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId> <artifactId>maven-assembly-plugin</artifactId>
<configuration> <configuration>
<descriptorRefs> <descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef> <descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs> </descriptorRefs>
</configuration> </configuration>
<executions> <executions>
<execution> <execution>
<id>make-assembly</id> <id>make-assembly</id>
<phase>package</phase> <phase>package</phase>
<goals> <goals>
<goal>single</goal> <goal>single</goal>
</goals> </goals>
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
</project> </project>

View File

@ -1,23 +1,23 @@
package com.baeldung.crud; package com.baeldung.crud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication @SpringBootApplication
@EnableAutoConfiguration @EnableAutoConfiguration
@ComponentScan(basePackages={"com.baeldung.crud"}) @ComponentScan(basePackages={"com.baeldung.crud"})
@EnableJpaRepositories(basePackages="com.baeldung.crud.repositories") @EnableJpaRepositories(basePackages="com.baeldung.crud.repositories")
@EnableTransactionManagement @EnableTransactionManagement
@EntityScan(basePackages="com.baeldung.crud.entities") @EntityScan(basePackages="com.baeldung.crud.entities")
public class Application { public class Application {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(Application.class, args); SpringApplication.run(Application.class, args);
} }
} }

View File

@ -1,68 +1,68 @@
package com.baeldung.crud.controllers; package com.baeldung.crud.controllers;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import com.baeldung.crud.entities.User; import com.baeldung.crud.entities.User;
import com.baeldung.crud.repositories.UserRepository; import com.baeldung.crud.repositories.UserRepository;
@Controller @Controller
public class UserController { public class UserController {
private final UserRepository userRepository; private final UserRepository userRepository;
@Autowired @Autowired
public UserController(UserRepository userRepository) { public UserController(UserRepository userRepository) {
this.userRepository = userRepository; this.userRepository = userRepository;
} }
@GetMapping("/signup") @GetMapping("/signup")
public String showSignUpForm(User user) { public String showSignUpForm(User user) {
return "add-user"; return "add-user";
} }
@PostMapping("/adduser") @PostMapping("/adduser")
public String addUser(@Valid User user, BindingResult result, Model model) { public String addUser(@Valid User user, BindingResult result, Model model) {
if (result.hasErrors()) { if (result.hasErrors()) {
return "add-user"; return "add-user";
} }
userRepository.save(user); userRepository.save(user);
model.addAttribute("users", userRepository.findAll()); model.addAttribute("users", userRepository.findAll());
return "index"; return "index";
} }
@GetMapping("/edit/{id}") @GetMapping("/edit/{id}")
public String showUpdateForm(@PathVariable("id") long id, Model model) { public String showUpdateForm(@PathVariable("id") long id, Model model) {
User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id)); User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
model.addAttribute("user", user); model.addAttribute("user", user);
return "update-user"; return "update-user";
} }
@PostMapping("/update/{id}") @PostMapping("/update/{id}")
public String updateUser(@PathVariable("id") long id, @Valid User user, BindingResult result, Model model) { public String updateUser(@PathVariable("id") long id, @Valid User user, BindingResult result, Model model) {
if (result.hasErrors()) { if (result.hasErrors()) {
user.setId(id); user.setId(id);
return "update-user"; return "update-user";
} }
userRepository.save(user); userRepository.save(user);
model.addAttribute("users", userRepository.findAll()); model.addAttribute("users", userRepository.findAll());
return "index"; return "index";
} }
@GetMapping("/delete/{id}") @GetMapping("/delete/{id}")
public String deleteUser(@PathVariable("id") long id, Model model) { public String deleteUser(@PathVariable("id") long id, Model model) {
User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id)); User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
userRepository.delete(user); userRepository.delete(user);
model.addAttribute("users", userRepository.findAll()); model.addAttribute("users", userRepository.findAll());
return "index"; return "index";
} }
} }

View File

@ -1,56 +1,56 @@
package com.baeldung.crud.entities; package com.baeldung.crud.entities;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
@Entity @Entity
public class User { public class User {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private long id; private long id;
@NotBlank(message = "Name is mandatory") @NotBlank(message = "Name is mandatory")
private String name; private String name;
@NotBlank(message = "Email is mandatory") @NotBlank(message = "Email is mandatory")
private String email; private String email;
public User() {} public User() {}
public User(String name, String email) { public User(String name, String email) {
this.name = name; this.name = name;
this.email = email; this.email = email;
} }
public void setId(long id) { public void setId(long id) {
this.id = id; this.id = id;
} }
public long getId() { public long getId() {
return id; return id;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public void setEmail(String email) { public void setEmail(String email) {
this.email = email; this.email = email;
} }
public String getName() { public String getName() {
return name; return name;
} }
public String getEmail() { public String getEmail() {
return email; return email;
} }
@Override @Override
public String toString() { public String toString() {
return "User{" + "id=" + id + ", name=" + name + ", email=" + email + '}'; return "User{" + "id=" + id + ", name=" + name + ", email=" + email + '}';
} }
} }

View File

@ -1,13 +1,13 @@
package com.baeldung.crud.repositories; package com.baeldung.crud.repositories;
import com.baeldung.crud.entities.User; import com.baeldung.crud.entities.User;
import java.util.List; import java.util.List;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface UserRepository extends CrudRepository<User, Long> { public interface UserRepository extends CrudRepository<User, Long> {
List<User> findByName(String name); List<User> findByName(String name);
} }

View File

@ -1,83 +1,83 @@
package com.baeldung.crud; package com.baeldung.crud;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import com.baeldung.crud.controllers.UserController; import com.baeldung.crud.controllers.UserController;
import com.baeldung.crud.entities.User; import com.baeldung.crud.entities.User;
import com.baeldung.crud.repositories.UserRepository; import com.baeldung.crud.repositories.UserRepository;
public class UserControllerUnitTest { public class UserControllerUnitTest {
private static UserController userController; private static UserController userController;
private static UserRepository mockedUserRepository; private static UserRepository mockedUserRepository;
private static BindingResult mockedBindingResult; private static BindingResult mockedBindingResult;
private static Model mockedModel; private static Model mockedModel;
@BeforeClass @BeforeClass
public static void setUpUserControllerInstance() { public static void setUpUserControllerInstance() {
mockedUserRepository = mock(UserRepository.class); mockedUserRepository = mock(UserRepository.class);
mockedBindingResult = mock(BindingResult.class); mockedBindingResult = mock(BindingResult.class);
mockedModel = mock(Model.class); mockedModel = mock(Model.class);
userController = new UserController(mockedUserRepository); userController = new UserController(mockedUserRepository);
} }
@Test @Test
public void whenCalledshowSignUpForm_thenCorrect() { public void whenCalledshowSignUpForm_thenCorrect() {
User user = new User("John", "john@domain.com"); User user = new User("John", "john@domain.com");
assertThat(userController.showSignUpForm(user)).isEqualTo("add-user"); assertThat(userController.showSignUpForm(user)).isEqualTo("add-user");
} }
@Test @Test
public void whenCalledaddUserAndValidUser_thenCorrect() { public void whenCalledaddUserAndValidUser_thenCorrect() {
User user = new User("John", "john@domain.com"); User user = new User("John", "john@domain.com");
when(mockedBindingResult.hasErrors()).thenReturn(false); when(mockedBindingResult.hasErrors()).thenReturn(false);
assertThat(userController.addUser(user, mockedBindingResult, mockedModel)).isEqualTo("index"); assertThat(userController.addUser(user, mockedBindingResult, mockedModel)).isEqualTo("index");
} }
@Test @Test
public void whenCalledaddUserAndInValidUser_thenCorrect() { public void whenCalledaddUserAndInValidUser_thenCorrect() {
User user = new User("John", "john@domain.com"); User user = new User("John", "john@domain.com");
when(mockedBindingResult.hasErrors()).thenReturn(true); when(mockedBindingResult.hasErrors()).thenReturn(true);
assertThat(userController.addUser(user, mockedBindingResult, mockedModel)).isEqualTo("add-user"); assertThat(userController.addUser(user, mockedBindingResult, mockedModel)).isEqualTo("add-user");
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void whenCalledshowUpdateForm_thenIllegalArgumentException() { public void whenCalledshowUpdateForm_thenIllegalArgumentException() {
assertThat(userController.showUpdateForm(0, mockedModel)).isEqualTo("update-user"); assertThat(userController.showUpdateForm(0, mockedModel)).isEqualTo("update-user");
} }
@Test @Test
public void whenCalledupdateUserAndValidUser_thenCorrect() { public void whenCalledupdateUserAndValidUser_thenCorrect() {
User user = new User("John", "john@domain.com"); User user = new User("John", "john@domain.com");
when(mockedBindingResult.hasErrors()).thenReturn(false); when(mockedBindingResult.hasErrors()).thenReturn(false);
assertThat(userController.updateUser(1l, user, mockedBindingResult, mockedModel)).isEqualTo("index"); assertThat(userController.updateUser(1l, user, mockedBindingResult, mockedModel)).isEqualTo("index");
} }
@Test @Test
public void whenCalledupdateUserAndInValidUser_thenCorrect() { public void whenCalledupdateUserAndInValidUser_thenCorrect() {
User user = new User("John", "john@domain.com"); User user = new User("John", "john@domain.com");
when(mockedBindingResult.hasErrors()).thenReturn(true); when(mockedBindingResult.hasErrors()).thenReturn(true);
assertThat(userController.updateUser(1l, user, mockedBindingResult, mockedModel)).isEqualTo("update-user"); assertThat(userController.updateUser(1l, user, mockedBindingResult, mockedModel)).isEqualTo("update-user");
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void whenCalleddeleteUser_thenIllegalArgumentException() { public void whenCalleddeleteUser_thenIllegalArgumentException() {
assertThat(userController.deleteUser(1l, mockedModel)).isEqualTo("index"); assertThat(userController.deleteUser(1l, mockedModel)).isEqualTo("index");
} }
} }

View File

@ -1,48 +1,48 @@
package com.baeldung.crud; package com.baeldung.crud;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test; import org.junit.Test;
import com.baeldung.crud.entities.User; import com.baeldung.crud.entities.User;
public class UserUnitTest { public class UserUnitTest {
@Test @Test
public void whenCalledGetName_thenCorrect() { public void whenCalledGetName_thenCorrect() {
User user = new User("Julie", "julie@domain.com"); User user = new User("Julie", "julie@domain.com");
assertThat(user.getName()).isEqualTo("Julie"); assertThat(user.getName()).isEqualTo("Julie");
} }
@Test @Test
public void whenCalledGetEmail_thenCorrect() { public void whenCalledGetEmail_thenCorrect() {
User user = new User("Julie", "julie@domain.com"); User user = new User("Julie", "julie@domain.com");
assertThat(user.getEmail()).isEqualTo("julie@domain.com"); assertThat(user.getEmail()).isEqualTo("julie@domain.com");
} }
@Test @Test
public void whenCalledSetName_thenCorrect() { public void whenCalledSetName_thenCorrect() {
User user = new User("Julie", "julie@domain.com"); User user = new User("Julie", "julie@domain.com");
user.setName("John"); user.setName("John");
assertThat(user.getName()).isEqualTo("John"); assertThat(user.getName()).isEqualTo("John");
} }
@Test @Test
public void whenCalledSetEmail_thenCorrect() { public void whenCalledSetEmail_thenCorrect() {
User user = new User("Julie", "julie@domain.com"); User user = new User("Julie", "julie@domain.com");
user.setEmail("john@domain.com"); user.setEmail("john@domain.com");
assertThat(user.getEmail()).isEqualTo("john@domain.com"); assertThat(user.getEmail()).isEqualTo("john@domain.com");
} }
@Test @Test
public void whenCalledtoString_thenCorrect() { public void whenCalledtoString_thenCorrect() {
User user = new User("Julie", "julie@domain.com"); User user = new User("Julie", "julie@domain.com");
assertThat(user.toString()).isEqualTo("User{id=0, name=Julie, email=julie@domain.com}"); assertThat(user.toString()).isEqualTo("User{id=0, name=Julie, email=julie@domain.com}");
} }
} }