mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-23 07:45:49 +00:00
add CredentialServiceTest
This commit is contained in:
parent
459e115b1d
commit
f37360e8ab
5 changed files with 319 additions and 8 deletions
|
@ -36,6 +36,11 @@ public class CredentialDTO extends BaseDTO {
|
||||||
@Pattern(regexp = CommonParameter.EMAIL_REGEXP, message = CommonError.EMAIL_FORMAT)
|
@Pattern(regexp = CommonParameter.EMAIL_REGEXP, message = CommonError.EMAIL_FORMAT)
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Size(min = CommonParameter.PASSWORD_MIN, max = CommonParameter.PASSWORD_MAX, message = CommonError.FORMAT_BETWEEN
|
||||||
|
+ CommonParameter.PASSWORD_MIN + " AND " + CommonParameter.PASSWORD_MAX + " !")
|
||||||
|
private String password;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private Boolean verified;
|
private Boolean verified;
|
||||||
|
|
||||||
|
@ -43,18 +48,22 @@ public class CredentialDTO extends BaseDTO {
|
||||||
|
|
||||||
private Date expiration;
|
private Date expiration;
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Size(min = CommonParameter.PASSWORD_MIN, max = CommonParameter.PASSWORD_MAX, message = CommonError.FORMAT_BETWEEN
|
|
||||||
+ CommonParameter.PASSWORD_MIN + " AND " + CommonParameter.PASSWORD_MAX + " !")
|
|
||||||
private String password;
|
|
||||||
|
|
||||||
public CredentialDTO(UserDTO user) {
|
public CredentialDTO(UserDTO user) {
|
||||||
this.verified = false;
|
|
||||||
this.setToken();
|
|
||||||
this.setExpiration();
|
|
||||||
this.setProvider(CommonParameter.DEFAULT_PROVIDER);
|
this.setProvider(CommonParameter.DEFAULT_PROVIDER);
|
||||||
this.email = user.getEmail();
|
this.email = user.getEmail();
|
||||||
this.password = user.getId().toString();
|
this.password = user.getId().toString();
|
||||||
|
this.verified = false;
|
||||||
|
this.setToken();
|
||||||
|
this.setExpiration();
|
||||||
|
}
|
||||||
|
|
||||||
|
public CredentialDTO(@NotNull String provider, @NotNull @Size(min = CommonParameter.EMAIL_MIN, max = CommonParameter.EMAIL_MAX, message = CommonError.FORMAT_BETWEEN
|
||||||
|
+ CommonParameter.EMAIL_MIN + " AND " + CommonParameter.EMAIL_MAX + " !") @Pattern(regexp = CommonParameter.EMAIL_REGEXP, message = CommonError.EMAIL_FORMAT) String email, @NotNull @Size(min = CommonParameter.PASSWORD_MIN, max = CommonParameter.PASSWORD_MAX, message = CommonError.FORMAT_BETWEEN
|
||||||
|
+ CommonParameter.PASSWORD_MIN + " AND " + CommonParameter.PASSWORD_MAX + " !") String password, @NotNull Boolean verified) {
|
||||||
|
this.provider = provider;
|
||||||
|
this.email = email;
|
||||||
|
this.password = password;
|
||||||
|
this.verified = verified;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDefaultProvider() {
|
public void setDefaultProvider() {
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
package org.springframework.samples.petclinic.model.common;
|
package org.springframework.samples.petclinic.model.common;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
@ -10,6 +14,13 @@ import javax.persistence.Table;
|
||||||
*/
|
*/
|
||||||
@Entity(name = "AuthProvider")
|
@Entity(name = "AuthProvider")
|
||||||
@Table(name = "auth_providers")
|
@Table(name = "auth_providers")
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
public class AuthProvider extends NamedEntity {
|
public class AuthProvider extends NamedEntity {
|
||||||
|
|
||||||
|
public AuthProvider(Integer id, String name) {
|
||||||
|
this.setId(id);
|
||||||
|
this.setName(name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package org.springframework.samples.petclinic.model.common;
|
package org.springframework.samples.petclinic.model.common;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import org.springframework.samples.petclinic.common.CommonError;
|
import org.springframework.samples.petclinic.common.CommonError;
|
||||||
import org.springframework.samples.petclinic.common.CommonParameter;
|
import org.springframework.samples.petclinic.common.CommonParameter;
|
||||||
|
@ -23,6 +25,8 @@ import java.util.UUID;
|
||||||
@Table(name = "credentials")
|
@Table(name = "credentials")
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
public class Credential extends BaseEntity {
|
public class Credential extends BaseEntity {
|
||||||
|
|
||||||
private static final int TOKEN_EXPIRATION = 60 * 24;
|
private static final int TOKEN_EXPIRATION = 60 * 24;
|
||||||
|
@ -54,6 +58,15 @@ public class Credential extends BaseEntity {
|
||||||
@Column(name = "expiration")
|
@Column(name = "expiration")
|
||||||
private Date expiration;
|
private Date expiration;
|
||||||
|
|
||||||
|
public Credential(@NotNull Integer providerId, @NotNull @Size(min = CommonParameter.EMAIL_MIN, max = CommonParameter.EMAIL_MAX, message = CommonError.FORMAT_BETWEEN
|
||||||
|
+ CommonParameter.EMAIL_MIN + " AND " + CommonParameter.EMAIL_MAX + " !") @Pattern(regexp = CommonParameter.EMAIL_REGEXP, message = CommonError.EMAIL_FORMAT) String email, @NotNull @Size(min = CommonParameter.PASSWORD_MIN, max = CommonParameter.PASSWORD_MAX, message = CommonError.FORMAT_BETWEEN
|
||||||
|
+ CommonParameter.PASSWORD_MIN + " AND " + CommonParameter.PASSWORD_MAX + " !") String password, @NotNull Boolean verified) {
|
||||||
|
this.providerId = providerId;
|
||||||
|
this.email = email;
|
||||||
|
this.password = password;
|
||||||
|
this.verified = verified;
|
||||||
|
}
|
||||||
|
|
||||||
public Boolean isVerified() {
|
public Boolean isVerified() {
|
||||||
return verified;
|
return verified;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,102 @@
|
||||||
|
package org.springframework.samples.petclinic.service.common;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Tag;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.samples.petclinic.dto.common.CredentialDTO;
|
||||||
|
import org.springframework.samples.petclinic.model.common.AuthProvider;
|
||||||
|
import org.springframework.samples.petclinic.model.common.Credential;
|
||||||
|
import org.springframework.samples.petclinic.repository.AuthProviderRepository;
|
||||||
|
import org.springframework.samples.petclinic.repository.CredentialRepository;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.BDDMockito.given;
|
||||||
|
|
||||||
|
@AutoConfigureTestDatabase
|
||||||
|
@SpringBootTest
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
class CredentialServiceTest {
|
||||||
|
|
||||||
|
private static String PROVIDER_TEST_NAME = "Provider Test";
|
||||||
|
private static Integer PROVIDER_TEST_ID = 11;
|
||||||
|
private static final String EMAIL_TEST = "eduardo.rodriguez@petclinic.com";
|
||||||
|
private static final String PASSWORD_TEST = "$2a$10$8KypNYtPopFo8Sk5jbKJ4.lCKeBhdApsrkmFfhwjB8nCls8qpzjZG";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BCryptPasswordEncoder bCryptPasswordEncoder;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private AuthProviderRepository authProviderRepository;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private CredentialRepository credentialRepository;
|
||||||
|
|
||||||
|
private CredentialService credentialService;
|
||||||
|
|
||||||
|
private Credential credential;
|
||||||
|
|
||||||
|
private CredentialDTO credentialDTO;
|
||||||
|
|
||||||
|
private AuthProvider authProvider;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void beforeEach() {
|
||||||
|
credentialService = new CredentialService(credentialRepository, bCryptPasswordEncoder, authProviderRepository);
|
||||||
|
authProvider = new AuthProvider(PROVIDER_TEST_ID,PROVIDER_TEST_NAME);
|
||||||
|
credential = new Credential(PROVIDER_TEST_ID, EMAIL_TEST, PASSWORD_TEST,true);
|
||||||
|
credentialDTO = new CredentialDTO(PROVIDER_TEST_NAME, EMAIL_TEST, PASSWORD_TEST,true);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("dtoToEntity")
|
||||||
|
@DisplayName("Verify the convertion from DTO to Entity")
|
||||||
|
void dtoToEntity() {
|
||||||
|
given(authProviderRepository.findByName(PROVIDER_TEST_NAME)).willReturn(authProvider);
|
||||||
|
Credential found = credentialService.dtoToEntity(credentialDTO);
|
||||||
|
|
||||||
|
assertThat(found).isEqualToComparingFieldByField(credential);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("entityToDTO")
|
||||||
|
@DisplayName("Verify the convertion from Entity to DTO")
|
||||||
|
void entityToDTO() {
|
||||||
|
given(authProviderRepository.findById(PROVIDER_TEST_ID)).willReturn(authProvider);
|
||||||
|
|
||||||
|
CredentialDTO found = credentialService.entityToDTO(credential);
|
||||||
|
|
||||||
|
assertThat(found).isEqualToComparingFieldByField(credentialDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("findByEmailAndProvider")
|
||||||
|
@DisplayName("Verify that we call right method to get Credential by Email and Provider")
|
||||||
|
void findByEmailAndProvider() {
|
||||||
|
given(credentialRepository.findByEmailAndProvider(EMAIL_TEST,PROVIDER_TEST_ID)).willReturn(credential);
|
||||||
|
given(authProviderRepository.findByName(PROVIDER_TEST_NAME)).willReturn(authProvider);
|
||||||
|
given(authProviderRepository.findById(PROVIDER_TEST_ID)).willReturn(authProvider);
|
||||||
|
|
||||||
|
CredentialDTO found = credentialService.findByEmailAndProvider(EMAIL_TEST,PROVIDER_TEST_NAME);
|
||||||
|
|
||||||
|
assertThat(found).isEqualToComparingFieldByField(credentialDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,176 @@
|
||||||
|
package org.springframework.samples.petclinic.service.common;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Tag;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.samples.petclinic.dto.common.PrivilegeDTO;
|
||||||
|
import org.springframework.samples.petclinic.dto.common.RoleDTO;
|
||||||
|
import org.springframework.samples.petclinic.dto.common.UserDTO;
|
||||||
|
import org.springframework.samples.petclinic.model.common.Privilege;
|
||||||
|
import org.springframework.samples.petclinic.model.common.Role;
|
||||||
|
import org.springframework.samples.petclinic.model.common.User;
|
||||||
|
import org.springframework.samples.petclinic.repository.RoleRepository;
|
||||||
|
import org.springframework.samples.petclinic.repository.UserRepository;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@AutoConfigureTestDatabase
|
||||||
|
@SpringBootTest
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
class UserServiceIntegrationTest {
|
||||||
|
|
||||||
|
private final static Integer USER_ID = 11;
|
||||||
|
|
||||||
|
private final static String USER_FIRST_NAME = "Sam";
|
||||||
|
|
||||||
|
private final static String USER_LAST_NAME = "Schultz";
|
||||||
|
|
||||||
|
private final static String USER_EMAIL = "Sam.Schultz@petclinic.com";
|
||||||
|
|
||||||
|
private final static String USER_PASSWORD = "PASSWORD_TEST9879879$^m$*ùm*^$*ù";
|
||||||
|
|
||||||
|
private final static String USER_ADDRESS = "4, Evans Street";
|
||||||
|
|
||||||
|
private final static String USER_CITY = "Wollongong";
|
||||||
|
|
||||||
|
private final static String USER_ZIPCODE = "65879";
|
||||||
|
|
||||||
|
private final static String USER_COUNTRY = "USA";
|
||||||
|
|
||||||
|
private final static String USER_PHONE = "1234567890";
|
||||||
|
|
||||||
|
private final static Integer ROLE_ID = 4;
|
||||||
|
|
||||||
|
private final static Integer PRIVILEGE_ID = 3;
|
||||||
|
|
||||||
|
private final static String ROLE_NAME = "ROLE_TEST";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RoleRepository roleRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PrivilegeService privilegeService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RoleService roleService;
|
||||||
|
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
private User user;
|
||||||
|
|
||||||
|
private UserDTO userDTO;
|
||||||
|
|
||||||
|
private Role role;
|
||||||
|
|
||||||
|
private RoleDTO roleDTO;
|
||||||
|
|
||||||
|
private Privilege privilege;
|
||||||
|
|
||||||
|
private PrivilegeDTO privilegeDTO;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void beforeEach() {
|
||||||
|
userService = new UserService(userRepository, roleRepository);
|
||||||
|
user = new User();
|
||||||
|
userDTO = new UserDTO();
|
||||||
|
|
||||||
|
roleDTO = roleService.findById(2);
|
||||||
|
role = roleService.dtoToEntity(roleDTO);
|
||||||
|
|
||||||
|
user.setId(USER_ID);
|
||||||
|
user.setFirstName(USER_FIRST_NAME);
|
||||||
|
user.setLastName(USER_LAST_NAME);
|
||||||
|
user.setEmail(USER_EMAIL);
|
||||||
|
user.setPassword(USER_PASSWORD);
|
||||||
|
user.setAccountNonExpired(true);
|
||||||
|
user.setAccountNonLocked(true);
|
||||||
|
user.setCredentialsNonExpired(true);
|
||||||
|
user.setEnabled(true);
|
||||||
|
user.setTelephone(USER_PHONE);
|
||||||
|
user.setStreet1(USER_ADDRESS);
|
||||||
|
user.setCity(USER_CITY);
|
||||||
|
user.setZipCode(USER_ZIPCODE);
|
||||||
|
user.setCountry(USER_COUNTRY);
|
||||||
|
user.setRoles(Collections.singleton(role));
|
||||||
|
role.addUser(user);
|
||||||
|
|
||||||
|
userDTO.setId(USER_ID);
|
||||||
|
userDTO.setFirstName(USER_FIRST_NAME);
|
||||||
|
userDTO.setLastName(USER_LAST_NAME);
|
||||||
|
userDTO.setEmail(USER_EMAIL);
|
||||||
|
userDTO.setPassword(USER_PASSWORD);
|
||||||
|
userDTO.setMatchingPassword(USER_PASSWORD);
|
||||||
|
userDTO.setAccountNonExpired(true);
|
||||||
|
userDTO.setAccountNonLocked(true);
|
||||||
|
userDTO.setCredentialsNonExpired(true);
|
||||||
|
userDTO.setEnabled(true);
|
||||||
|
userDTO.setTelephone(USER_PHONE);
|
||||||
|
userDTO.setStreet1(USER_ADDRESS);
|
||||||
|
userDTO.setCity(USER_CITY);
|
||||||
|
userDTO.setZipCode(USER_ZIPCODE);
|
||||||
|
userDTO.setCountry(USER_COUNTRY);
|
||||||
|
userDTO.setRoles(Collections.singleton(roleDTO));
|
||||||
|
roleDTO.addUser(userDTO);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("findById")
|
||||||
|
@DisplayName("Verify that we get UserDTO by his ID")
|
||||||
|
void findById() {
|
||||||
|
List<UserDTO> userDTOS = userService.findAll();
|
||||||
|
UserDTO expected = userDTOS.get(2);
|
||||||
|
|
||||||
|
UserDTO found = userService.findById(expected.getId());
|
||||||
|
|
||||||
|
assertThat(found).isEqualToIgnoringGivenFields(expected, "roles");
|
||||||
|
assertThat(found.getRoles()).usingElementComparatorIgnoringFields("users", "privileges")
|
||||||
|
.contains(expected.getRoles().toArray(new RoleDTO[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("findAll")
|
||||||
|
@DisplayName("Verify that the UserDTO list contain all previous elements and the new saved one")
|
||||||
|
void findAll() {
|
||||||
|
List<UserDTO> expected = userService.findAll();
|
||||||
|
userDTO.setRoles(new HashSet<>());
|
||||||
|
|
||||||
|
assertThat(expected).doesNotContain(userDTO);
|
||||||
|
|
||||||
|
UserDTO saved = userService.save(userDTO);
|
||||||
|
expected.add(saved);
|
||||||
|
List<UserDTO> found = userService.findAll();
|
||||||
|
|
||||||
|
assertThat(found).usingElementComparatorIgnoringFields("roles").containsOnlyOnceElementsOf(expected);
|
||||||
|
|
||||||
|
userService.delete(saved);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("save")
|
||||||
|
@DisplayName("Verify that all UserDTO list contain the new saved one")
|
||||||
|
void save() {
|
||||||
|
Collection<UserDTO> expected = userService.findAll();
|
||||||
|
assertThat(expected).doesNotContain(userDTO);
|
||||||
|
|
||||||
|
UserDTO saved = userService.save(userDTO);
|
||||||
|
|
||||||
|
assertThat(saved).isEqualToIgnoringGivenFields(userDTO, "id", "roles");
|
||||||
|
assertThat(saved.getRoles()).usingElementComparatorIgnoringFields("users", "privileges")
|
||||||
|
.contains(userDTO.getRoles().toArray(new RoleDTO[0]));
|
||||||
|
|
||||||
|
userService.delete(saved);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue