[BAEL-4955] The DTO pattern (#11166)
* [BAEL-4955] The DTO pattern * Fix encrypt logic * Add tests * Add tests * Move packages
This commit is contained in:
parent
9f08b35a58
commit
cf533a0340
5
design-patterns/README.md
Normal file
5
design-patterns/README.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
## Article related
|
||||||
|
|
||||||
|
- [The DTO Pattern (Data Transfer Object)]()
|
||||||
|
|
||||||
|
|
31
design-patterns/dto-pattern/pom.xml
Normal file
31
design-patterns/dto-pattern/pom.xml
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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">
|
||||||
|
|
||||||
|
<groupId>com.baeldung.designpatterns.dtopattern</groupId>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>dto-pattern</artifactId>
|
||||||
|
<name>dto-pattern</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-boot-2</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-boot-2</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Main.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.api;
|
||||||
|
|
||||||
|
import com.baeldung.designpatterns.dtopattern.domain.Role;
|
||||||
|
import com.baeldung.designpatterns.dtopattern.domain.User;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static java.util.stream.Collectors.toList;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class Mapper {
|
||||||
|
public UserDTO toDto(User user) {
|
||||||
|
String name = user.getName();
|
||||||
|
List<String> roles = user
|
||||||
|
.getRoles()
|
||||||
|
.stream()
|
||||||
|
.map(Role::getName)
|
||||||
|
.collect(toList());
|
||||||
|
|
||||||
|
return new UserDTO(name, roles);
|
||||||
|
}
|
||||||
|
|
||||||
|
public User toUser(UserCreationDTO userDTO) {
|
||||||
|
return new User(userDTO.getName(), userDTO.getPassword(), new ArrayList<>());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.api;
|
||||||
|
|
||||||
|
import com.baeldung.designpatterns.dtopattern.domain.RoleService;
|
||||||
|
import com.baeldung.designpatterns.dtopattern.domain.User;
|
||||||
|
import com.baeldung.designpatterns.dtopattern.domain.UserService;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static java.util.stream.Collectors.toList;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/users")
|
||||||
|
class UserController {
|
||||||
|
|
||||||
|
private UserService userService;
|
||||||
|
private RoleService roleService;
|
||||||
|
private Mapper mapper;
|
||||||
|
|
||||||
|
public UserController(UserService userService, RoleService roleService, Mapper mapper) {
|
||||||
|
this.userService = userService;
|
||||||
|
this.roleService = roleService;
|
||||||
|
this.mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
@ResponseBody
|
||||||
|
public List<UserDTO> getUsers() {
|
||||||
|
return userService.getAll()
|
||||||
|
.stream()
|
||||||
|
.map(mapper::toDto)
|
||||||
|
.collect(toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
@ResponseBody
|
||||||
|
public UserIdDTO create(@RequestBody UserCreationDTO userDTO) {
|
||||||
|
User user = mapper.toUser(userDTO);
|
||||||
|
|
||||||
|
userDTO.getRoles()
|
||||||
|
.stream()
|
||||||
|
.map(role -> roleService.getOrCreate(role))
|
||||||
|
.forEach(user::addRole);
|
||||||
|
|
||||||
|
userService.save(user);
|
||||||
|
|
||||||
|
return new UserIdDTO(user.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.api;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class UserCreationDTO {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private String password;
|
||||||
|
private List<String> roles;
|
||||||
|
|
||||||
|
UserCreationDTO() {}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getRoles() {
|
||||||
|
return roles;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setRoles(List<String> roles) {
|
||||||
|
this.roles = roles;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.api;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class UserDTO {
|
||||||
|
private String name;
|
||||||
|
private List<String> roles;
|
||||||
|
|
||||||
|
public UserDTO(String name, List<String> roles) {
|
||||||
|
this.name = name;
|
||||||
|
this.roles = roles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getRoles() {
|
||||||
|
return roles;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.api;
|
||||||
|
|
||||||
|
public class UserIdDTO {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
public UserIdDTO(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.domain;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class InMemoryRepository implements UserRepository, RoleRepository {
|
||||||
|
|
||||||
|
private Map<String, User> users = new LinkedHashMap<>();
|
||||||
|
private Map<String, Role> roles = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<User> getAll() {
|
||||||
|
return new ArrayList<>(users.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(User user) {
|
||||||
|
user.setId(UUID.randomUUID().toString());
|
||||||
|
users.put(user.getId(), user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(Role role) {
|
||||||
|
role.setId(UUID.randomUUID().toString());
|
||||||
|
roles.put(role.getId(), role);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Role getRoleById(String id) {
|
||||||
|
return roles.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Role getRoleByName(String name) {
|
||||||
|
return roles.values()
|
||||||
|
.stream()
|
||||||
|
.filter(role -> role.getName().equalsIgnoreCase(name))
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteAll() {
|
||||||
|
users.clear();
|
||||||
|
roles.clear();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.domain;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Role {
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Role(String name) {
|
||||||
|
this.name = Objects.requireNonNull(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setId(String id) {
|
||||||
|
this.id = Objects.requireNonNull(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = Objects.requireNonNull(name);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.domain;
|
||||||
|
|
||||||
|
public interface RoleRepository {
|
||||||
|
Role getRoleById(String id);
|
||||||
|
Role getRoleByName(String name);
|
||||||
|
void save(Role role);
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.domain;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class RoleService {
|
||||||
|
|
||||||
|
private RoleRepository repository;
|
||||||
|
|
||||||
|
public RoleService(RoleRepository repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Role getOrCreate(String name) {
|
||||||
|
Role role = repository.getRoleByName(name);
|
||||||
|
|
||||||
|
if (role == null) {
|
||||||
|
role = new Role(name);
|
||||||
|
repository.save(role);
|
||||||
|
}
|
||||||
|
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(Role role) {
|
||||||
|
Objects.requireNonNull(role);
|
||||||
|
repository.save(role);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.domain;
|
||||||
|
|
||||||
|
import javax.crypto.*;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
private static SecretKeySpec KEY = initKey();
|
||||||
|
|
||||||
|
static SecretKeySpec initKey(){
|
||||||
|
try {
|
||||||
|
SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
|
||||||
|
return new SecretKeySpec(secretKey.getEncoded(), "AES");
|
||||||
|
} catch (NoSuchAlgorithmException ex) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private String password;
|
||||||
|
private List<Role> roles;
|
||||||
|
|
||||||
|
public User(String name, String password, List<Role> roles) {
|
||||||
|
this.name = Objects.requireNonNull(name);
|
||||||
|
this.password = this.encrypt(password);
|
||||||
|
this.roles = Objects.requireNonNull(roles);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 void addRole(Role role) {
|
||||||
|
roles.add(role);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Role> getRoles() {
|
||||||
|
return Collections.unmodifiableList(roles);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
String encrypt(String password) {
|
||||||
|
Objects.requireNonNull(password);
|
||||||
|
try {
|
||||||
|
Cipher cipher = Cipher.getInstance("AES");
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, KEY);
|
||||||
|
final byte[] encryptedBytes = cipher.doFinal(password.getBytes(StandardCharsets.UTF_8));
|
||||||
|
return new String(encryptedBytes, StandardCharsets.UTF_8);
|
||||||
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
|
||||||
|
// do nothing
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.domain;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface UserRepository {
|
||||||
|
List<User> getAll();
|
||||||
|
void save(User user);
|
||||||
|
void deleteAll();
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.domain;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserService {
|
||||||
|
|
||||||
|
private UserRepository repository;
|
||||||
|
|
||||||
|
public UserService(UserRepository repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<User> getAll() {
|
||||||
|
return repository.getAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(User user) {
|
||||||
|
Objects.requireNonNull(user);
|
||||||
|
repository.save(user);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.api;
|
||||||
|
|
||||||
|
import com.baeldung.designpatterns.dtopattern.domain.Role;
|
||||||
|
import com.baeldung.designpatterns.dtopattern.domain.User;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class MapperUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void toDto_shouldMapFromDomainToDTO() {
|
||||||
|
String name = "Test";
|
||||||
|
String password = "test";
|
||||||
|
Role admin = new Role("admin");
|
||||||
|
List<String> expectedRoles = Collections.singletonList("admin");
|
||||||
|
|
||||||
|
List<Role> roles = new ArrayList<>();
|
||||||
|
roles.add(admin);
|
||||||
|
|
||||||
|
User user = new User(name, password, roles);
|
||||||
|
Mapper mapper = new Mapper();
|
||||||
|
|
||||||
|
UserDTO dto = mapper.toDto(user);
|
||||||
|
|
||||||
|
assertEquals(name, dto.getName());
|
||||||
|
assertEquals(expectedRoles, dto.getRoles());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void toUser_shouldMapFromDTOToDomain() {
|
||||||
|
String name = "Test";
|
||||||
|
String password = "test";
|
||||||
|
String role = "admin";
|
||||||
|
|
||||||
|
UserCreationDTO dto = new UserCreationDTO();
|
||||||
|
dto.setName(name);
|
||||||
|
dto.setPassword(password);
|
||||||
|
dto.setRoles(Collections.singletonList("admin"));
|
||||||
|
|
||||||
|
User expectedUser = new User(name, password, new ArrayList<>());
|
||||||
|
|
||||||
|
Mapper mapper = new Mapper();
|
||||||
|
|
||||||
|
User user = mapper.toUser(dto);
|
||||||
|
|
||||||
|
assertEquals(name, user.getName());
|
||||||
|
assertEquals(expectedUser.getPassword(), user.getPassword());
|
||||||
|
assertEquals(Collections.emptyList(), user.getRoles());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.api;
|
||||||
|
|
||||||
|
import com.baeldung.designpatterns.dtopattern.domain.Role;
|
||||||
|
import com.baeldung.designpatterns.dtopattern.domain.RoleRepository;
|
||||||
|
import com.baeldung.designpatterns.dtopattern.domain.User;
|
||||||
|
import com.baeldung.designpatterns.dtopattern.domain.UserRepository;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import io.restassured.http.ContentType;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.boot.web.server.LocalServerPort;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import static io.restassured.RestAssured.given;
|
||||||
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
|
import static org.springframework.http.HttpStatus.OK;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
class UserControllerIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TestRestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RoleRepository roleRepository;
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
int port;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void create_shouldReturnUseId() throws Exception {
|
||||||
|
UserCreationDTO request = new UserCreationDTO();
|
||||||
|
request.setName("User 1");
|
||||||
|
request.setPassword("Test@123456");
|
||||||
|
request.setRoles(Collections.singletonList("admin"));
|
||||||
|
|
||||||
|
given()
|
||||||
|
.contentType(ContentType.JSON)
|
||||||
|
.body(objectMapper.writeValueAsString(request))
|
||||||
|
.when()
|
||||||
|
.port(port)
|
||||||
|
.post("/users")
|
||||||
|
.then()
|
||||||
|
.statusCode(OK.value())
|
||||||
|
.body("id", notNullValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getAll_shouldReturnUseDTO() {
|
||||||
|
|
||||||
|
userRepository.deleteAll();
|
||||||
|
|
||||||
|
String roleName = "admin";
|
||||||
|
Role admin = new Role(roleName);
|
||||||
|
roleRepository.save(admin);
|
||||||
|
|
||||||
|
String name = "User 1";
|
||||||
|
User user = new User(name, "Test@123456", Collections.singletonList(admin));
|
||||||
|
userRepository.save(user);
|
||||||
|
|
||||||
|
given()
|
||||||
|
.port(port)
|
||||||
|
.when()
|
||||||
|
.get("/users")
|
||||||
|
.then()
|
||||||
|
.statusCode(OK.value())
|
||||||
|
.body("size()", is(1))
|
||||||
|
.body("[0].name", equalTo(name))
|
||||||
|
.body("[0].roles", hasItem(roleName));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,83 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.domain;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
class InMemoryRepositoryUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getAll_shouldReturnAllUsers() {
|
||||||
|
|
||||||
|
String name = "Test";
|
||||||
|
String password = "test123";
|
||||||
|
List<Role> roles = new ArrayList<>();
|
||||||
|
|
||||||
|
User user = new User(name, password, roles);
|
||||||
|
List<User> expectedUsers = Collections.singletonList(user);
|
||||||
|
|
||||||
|
InMemoryRepository repository = new InMemoryRepository();
|
||||||
|
repository.save(user);
|
||||||
|
|
||||||
|
List<User> users = repository.getAll();
|
||||||
|
|
||||||
|
assertEquals(expectedUsers, users);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void save_whenSavingUser_shouldSetId() {
|
||||||
|
String name = "Test";
|
||||||
|
String password = "test123";
|
||||||
|
List<Role> roles = new ArrayList<>();
|
||||||
|
|
||||||
|
User user = new User(name, password, roles);
|
||||||
|
|
||||||
|
InMemoryRepository repository = new InMemoryRepository();
|
||||||
|
repository.save(user);
|
||||||
|
|
||||||
|
assertNotNull(user.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void save_whenSavingRole_shouldSetId() {
|
||||||
|
String name = "Test";
|
||||||
|
Role role = new Role(name);
|
||||||
|
|
||||||
|
InMemoryRepository repository = new InMemoryRepository();
|
||||||
|
repository.save(role);
|
||||||
|
|
||||||
|
assertNotNull(role.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getRoleById_shouldReturnRoleById() {
|
||||||
|
String name = "Test";
|
||||||
|
Role expectedRole = new Role(name);
|
||||||
|
|
||||||
|
InMemoryRepository repository = new InMemoryRepository();
|
||||||
|
repository.save(expectedRole);
|
||||||
|
|
||||||
|
Role role = repository.getRoleById(expectedRole.getId());
|
||||||
|
|
||||||
|
assertEquals(expectedRole, role);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getRoleByName_shouldReturnRoleByName() {
|
||||||
|
String name = "Test";
|
||||||
|
Role expectedRole = new Role(name);
|
||||||
|
|
||||||
|
InMemoryRepository repository = new InMemoryRepository();
|
||||||
|
repository.save(expectedRole);
|
||||||
|
|
||||||
|
Role role = repository.getRoleByName(name);
|
||||||
|
|
||||||
|
assertEquals(expectedRole, role);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.designpatterns.dtopattern.domain;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class UserUnitTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUserIsCreated_shouldEncryptPassword() {
|
||||||
|
User user = new User("Test", "test", new ArrayList<>());
|
||||||
|
|
||||||
|
assertEquals(user.encrypt("test"), user.getPassword());
|
||||||
|
assertNotEquals(user.encrypt("Test"), user.getPassword());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUserIsCreated_shouldFailIfNameIsNull() {
|
||||||
|
assertThrows(NullPointerException.class, () ->
|
||||||
|
new User(null, "test", new ArrayList<>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUserIsCreated_shouldFailIfPasswordIsNull() {
|
||||||
|
assertThrows(NullPointerException.class, () ->
|
||||||
|
new User("Test", null, new ArrayList<>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUserIsCreated_shouldFailIfRolesIsNull() {
|
||||||
|
assertThrows(NullPointerException.class, () ->
|
||||||
|
new User("Test", "Test", null));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
22
design-patterns/pom.xml
Normal file
22
design-patterns/pom.xml
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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>
|
||||||
|
<groupId>com.baeldung.designpatterns</groupId>
|
||||||
|
<artifactId>design-patterns</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<name>design-patterns</name>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>dto-pattern</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
</project>
|
1
pom.xml
1
pom.xml
@ -387,6 +387,7 @@
|
|||||||
<module>core-groovy-strings</module>
|
<module>core-groovy-strings</module>
|
||||||
|
|
||||||
<module>core-java-modules</module>
|
<module>core-java-modules</module>
|
||||||
|
<module>design-patterns</module>
|
||||||
|
|
||||||
<module>couchbase</module>
|
<module>couchbase</module>
|
||||||
<module>custom-pmd</module>
|
<module>custom-pmd</module>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user