Commit 487bb66c authored by Ondřej Pavlica's avatar Ondřej Pavlica
Browse files

Add user role tests

parent a313701d
Pipeline #131690 passed with stages
in 2 minutes and 3 seconds
......@@ -6,9 +6,13 @@ package cz.muni.fi.pa165.winery.persistence.dao;
import cz.muni.fi.pa165.winery.enums.UserRoleType;
import cz.muni.fi.pa165.winery.persistence.PersistenceTestConfig;
import cz.muni.fi.pa165.winery.persistence.dao.user.UserRoleDao;
import cz.muni.fi.pa165.winery.persistence.entities.*;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
......@@ -20,6 +24,20 @@ import java.math.BigDecimal;
public class UserRoleDaoTests extends DaoTestBase<UserRole> {
private User testUser;
@Autowired
private UserRoleDao userRoleDao;
@Test
public void testGetAllForUser() {
var entity = getEntity1();
executeTransaction(() -> entityManager.persist(entity));
var roles = userRoleDao.getAll(testUser.getId());
Assertions.assertThat(roles).containsExactly(entity);
roles = userRoleDao.getAll(-1);
Assertions.assertThat(roles).isEmpty();
}
@Override
protected Class<UserRole> getEntityClass() {
return UserRole.class;
......
/**
* @author Ondřej Pavlica
*/
package cz.muni.fi.pa165.winery.services.persistence.user;
import cz.muni.fi.pa165.winery.dto.user.UserRoleDto;
import cz.muni.fi.pa165.winery.enums.UserRoleType;
import cz.muni.fi.pa165.winery.persistence.dao.DaoBase;
import cz.muni.fi.pa165.winery.persistence.dao.user.UserRoleDao;
import cz.muni.fi.pa165.winery.persistence.entities.User;
import cz.muni.fi.pa165.winery.persistence.entities.UserRole;
import cz.muni.fi.pa165.winery.services.PersistenceServiceImpl;
import cz.muni.fi.pa165.winery.services.mappers.user.UserRoleEntityMapper;
import cz.muni.fi.pa165.winery.services.persistence.PersistenceServiceTestBase;
import cz.muni.fi.pa165.winery.services.user.UserRoleServiceImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(MockitoExtension.class)
public class UserRoleTests extends PersistenceServiceTestBase<UserRoleDto, UserRole> {
@Mock
UserRoleDao userRoleDao;
@Spy
UserRoleEntityMapper userRoleEntityMapper;
UserRoleServiceImpl userRoleService;
@Test
public void testGetAllForUser() {
getPersistenceService();
var entityList = List.of(getTestEntity());
Mockito.when(userRoleDao.getAll(1)).thenReturn(entityList);
var dtos = userRoleService.getAll(1);
assertThat(dtos).hasSize(1);
assertThat(equals(dtos.stream().findFirst().get(), getTestEntity())).isTrue();
Mockito.verify(userRoleDao, Mockito.times(1)).getAll(1);
}
@Override
protected UserRoleDto getTestDto() {
var dto = new UserRoleDto();
dto.setUserId(1);
dto.setRole(UserRoleType.USER);
return dto;
}
@Override
protected UserRole getTestEntity() {
var user = new User();
user.setId(1);
user.setFirstName("Name");
user.setLastName("Surname");
user.setEmail("test@test.com");
user.setPasswordHash("adf");
var entity = new UserRole();
entity.setUser(user);
entity.setRole(UserRoleType.USER);
return entity;
}
@Override
protected boolean equals(UserRoleDto userRoleDto, UserRole userRole) {
return userRoleDto.getRole().equals(userRole.getRole()) &&
userRoleDto.getUserId() == userRole.getUser().getId();
}
@Override
protected PersistenceServiceImpl<UserRoleDto, UserRole> getPersistenceService() {
if (userRoleService != null) {
return userRoleService;
}
userRoleService = new UserRoleServiceImpl(userRoleDao, userRoleEntityMapper);
return userRoleService;
}
@Override
protected DaoBase<UserRole> getMockedDao() {
return userRoleDao;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment