[BAEL-4955] Move code example (#11177)
This commit is contained in:
committed by
GitHub
parent
41c9acc0b0
commit
6d5fcbc7ee
@@ -15,6 +15,21 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>rest-assured</artifactId>
|
||||
<version>${rest-assured.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
@@ -38,6 +53,9 @@
|
||||
<assertj-core.version>3.9.1</assertj-core.version>
|
||||
<hibernate-core.version>5.2.16.Final</hibernate-core.version>
|
||||
<mysql-connector.version>6.0.6</mysql-connector.version>
|
||||
<spring-boot.version>2.5.3</spring-boot.version>
|
||||
<rest-assured.version>3.3.0</rest-assured.version>
|
||||
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.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);
|
||||
}
|
||||
}
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.dtopattern.api;
|
||||
|
||||
import com.baeldung.dtopattern.domain.Role;
|
||||
import com.baeldung.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<>());
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.dtopattern.api;
|
||||
|
||||
import com.baeldung.dtopattern.domain.RoleService;
|
||||
import com.baeldung.dtopattern.domain.User;
|
||||
import com.baeldung.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());
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.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;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.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;
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.dtopattern.api;
|
||||
|
||||
public class UserIdDTO {
|
||||
|
||||
private String id;
|
||||
|
||||
public UserIdDTO(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.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();
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.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);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.dtopattern.domain;
|
||||
|
||||
public interface RoleRepository {
|
||||
Role getRoleById(String id);
|
||||
Role getRoleByName(String name);
|
||||
void save(Role role);
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.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);
|
||||
}
|
||||
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.baeldung.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 "";
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.dtopattern.domain;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserRepository {
|
||||
List<User> getAll();
|
||||
void save(User user);
|
||||
void deleteAll();
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.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);
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.dtopattern.api;
|
||||
|
||||
import com.baeldung.dtopattern.domain.Role;
|
||||
import com.baeldung.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());
|
||||
}
|
||||
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.baeldung.dtopattern.api;
|
||||
|
||||
import com.baeldung.dtopattern.domain.Role;
|
||||
import com.baeldung.dtopattern.domain.RoleRepository;
|
||||
import com.baeldung.dtopattern.domain.User;
|
||||
import com.baeldung.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));
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package com.baeldung.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);
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user