[JAVA-14174] Renamed paterns to paterns-module (#12718)
* [JAVA-14174] Renamed paterns to paterns-module * [JAVA-14174] naming fixes Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.daopattern.test;
|
||||
|
||||
import com.baeldung.daopattern.daos.UserDao;
|
||||
import com.baeldung.daopattern.entities.User;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UserDaoUnitTest {
|
||||
|
||||
private static UserDao userDao;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpUserDaoInstance() {
|
||||
userDao = new UserDao();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserDaoInstance_whenCalledget_thenOneAssertion() {
|
||||
assertThat(userDao.get(0)).isInstanceOf(Optional.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserDaoInstance_whenCalledgetAll_thenOneAssertion() {
|
||||
assertThat(userDao.getAll()).isInstanceOf(List.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserDaoInstance_whenCalledupdate_thenTwoAssertions() {
|
||||
User user = new User("Julie", "julie@domain.com");
|
||||
userDao.update(user, new String[] {"Julie", "julie@domain.com"});
|
||||
assertThat(userDao.get(2).get().getName()).isEqualTo("Julie");
|
||||
assertThat(userDao.get(2).get().getEmail()).isEqualTo("julie@domain.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserDaoInstance_whenCalledsave_thenTwoAssertions() {
|
||||
User user = new User("Julie", "julie@domain.com");
|
||||
userDao.save(user);
|
||||
assertThat(userDao.get(2).get().getName()).isEqualTo("Julie");
|
||||
assertThat(userDao.get(2).get().getEmail()).isEqualTo("julie@domain.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserDaoInstance_whenCalleddelete_thenOneAssertion() {
|
||||
User user = new User("Julie", "julie@domain.com");
|
||||
userDao.delete(user);
|
||||
assertThat(userDao.getAll().size()).isEqualTo(2);
|
||||
}
|
||||
}
|
||||
+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