Search in sources :

Example 1 with RoleEntity

use of org.codenergic.theskeleton.role.RoleEntity in project theskeleton by codenergic.

the class RoleServiceImpl method deleteRole.

@Override
@Transactional
public void deleteRole(String idOrCode) {
    RoleEntity e = findRoleByIdOrCode(idOrCode);
    assertRoleNotNull(e);
    roleRepository.delete(e);
}
Also used : RoleEntity(org.codenergic.theskeleton.role.RoleEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with RoleEntity

use of org.codenergic.theskeleton.role.RoleEntity in project theskeleton by codenergic.

the class RoleServiceImpl method updateRole.

@Override
@Transactional
public RoleEntity updateRole(String id, RoleEntity role) {
    RoleEntity e = findRoleByIdOrCode(id);
    assertRoleNotNull(e);
    e.setCode(role.getCode());
    e.setDescription(role.getDescription());
    return e;
}
Also used : RoleEntity(org.codenergic.theskeleton.role.RoleEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with RoleEntity

use of org.codenergic.theskeleton.role.RoleEntity in project theskeleton by codenergic.

the class UserServiceImpl method addRoleToUser.

@Override
@Transactional
public UserEntity addRoleToUser(String username, String roleCode) {
    UserEntity user = findUserByUsername(username);
    RoleEntity role = roleRepository.findByCode(roleCode);
    return userRoleRepository.save(new UserRoleEntity(user, role)).getUser();
}
Also used : RoleEntity(org.codenergic.theskeleton.role.RoleEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with RoleEntity

use of org.codenergic.theskeleton.role.RoleEntity in project theskeleton by codenergic.

the class UserServiceTest method testAddRoleToUser.

@Test
public void testAddRoleToUser() {
    RoleEntity role = new RoleEntity().setId(UUID.randomUUID().toString()).setCode("role");
    UserEntity user = new UserEntity().setId(UUID.randomUUID().toString()).setUsername("user").setPassword(passwordEncoder.encode("user"));
    UserRoleEntity result = new UserRoleEntity(user, role);
    result.setId(UUID.randomUUID().toString());
    when(roleRepository.findByCode("role")).thenReturn(role);
    when(userRepository.findByUsername("user")).thenReturn(user);
    when(userRoleRepository.save(any(UserRoleEntity.class))).thenReturn(result);
    assertThat(userAdminService.addRoleToUser("user", "role")).isEqualTo(user);
    verify(roleRepository).findByCode("role");
    verify(userRepository).findByUsername("user");
    verify(userRoleRepository).save(any(UserRoleEntity.class));
}
Also used : RoleEntity(org.codenergic.theskeleton.role.RoleEntity) Test(org.junit.Test)

Example 5 with RoleEntity

use of org.codenergic.theskeleton.role.RoleEntity in project theskeleton by codenergic.

the class PrivilegeServiceTest method testAddPrivilegeToRole.

@Test
public void testAddPrivilegeToRole() {
    RoleEntity role = new RoleEntity().setId(UUID.randomUUID().toString()).setCode("role");
    PrivilegeEntity privilege = new PrivilegeEntity().setId(UUID.randomUUID().toString()).setName("privilege");
    RolePrivilegeEntity result = new RolePrivilegeEntity(role, privilege);
    result.setId(UUID.randomUUID().toString());
    result.getAuthority();
    when(roleRepository.findByCode("role")).thenReturn(Optional.of(role));
    when(privilegeRepository.findByName("privilege")).thenReturn(Optional.of(privilege));
    when(rolePrivilegeRepository.save(any(RolePrivilegeEntity.class))).thenReturn(result);
    assertThat(privilegeService.addPrivilegeToRole("role", "privilege")).isEqualTo(role);
    verify(roleRepository).findByCode("role");
    verify(privilegeRepository).findByName("privilege");
    verify(rolePrivilegeRepository).save(any(RolePrivilegeEntity.class));
    when(roleRepository.findByCode("role1")).thenReturn(Optional.empty());
    assertThatThrownBy(() -> {
        privilegeService.addPrivilegeToRole("role1", "privilege");
    }).isInstanceOf(RoleNotFoundException.class);
    verify(roleRepository).findByCode("role1");
    when(privilegeRepository.findByName("privilege1")).thenReturn(Optional.empty());
    assertThatThrownBy(() -> {
        privilegeService.addPrivilegeToRole("role", "privilege1");
    }).isInstanceOf(PrivilegeNotFoundException.class);
    verify(privilegeRepository).findByName("privilege1");
}
Also used : RoleEntity(org.codenergic.theskeleton.role.RoleEntity) Test(org.junit.Test)

Aggregations

RoleEntity (org.codenergic.theskeleton.role.RoleEntity)11 Test (org.junit.Test)6 Transactional (org.springframework.transaction.annotation.Transactional)5 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 ResultActions (org.springframework.test.web.servlet.ResultActions)3 HashSet (java.util.HashSet)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Date (java.util.Date)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 EnableRestDocs (org.codenergic.theskeleton.core.test.EnableRestDocs)1 PrivilegeEntity (org.codenergic.theskeleton.privilege.PrivilegeEntity)1 RoleNotFoundException (org.codenergic.theskeleton.role.RoleNotFoundException)1 RolePrivilegeEntity (org.codenergic.theskeleton.role.RolePrivilegeEntity)1 RoleRestData (org.codenergic.theskeleton.role.RoleRestData)1 UserRoleEntity (org.codenergic.theskeleton.role.UserRoleEntity)1