RoleService { @Transactional public Role save(RoleDTO roleDTO) { Role role = GenericConverter.mapper(roleDTO, Role.class); Set<Role> nameRole = roleRepository.findByName(roleDTO.getName()); HeimdallException.checkThrow(nameRole.size() > 0, ExceptionMessage.ROLE_ALREADY_EXIST); List<Long> invalidPrivileges = new ArrayList<>(); role.getPrivileges().forEach(p -> { Privilege privilege = privilegeRepository.findOne(p.getId()); if (privilege == null) { invalidPrivileges.add(p.getId()); } }); HeimdallException.checkThrow(!invalidPrivileges.isEmpty(), PRIVILEGES_NOT_EXIST, invalidPrivileges.toString()); role = roleRepository.save(role); return role; }  @Transactional Role save(RoleDTO roleDTO); Role find(Long id); @Transactional(readOnly = false) RolePage list(RoleDTO roleDTO, PageableDTO pageableDTO); @Transactional(readOnly = false) List<Role> list(RoleDTO roleDTO); @Transactional void delete(Long roleId); @Transactional Role update(Long roleId, RoleDTO roleDTO);  }
@Test public void rejectNewRoleWithInvalidPrivileges() { thrown.expect(BadRequestException.class); thrown.expectMessage("Privileges [9, 10] defined to attach in role not exist"); RoleDTO roleDTO = new RoleDTO(); roleDTO.setName("name"); List<ReferenceIdDTO> privileges = Arrays.asList(new ReferenceIdDTO(10L), new ReferenceIdDTO(9L)); roleDTO.setPrivileges(privileges); Mockito.when(privilegeRepository.findOne(Mockito.anyLong())).thenReturn(null); service.save(roleDTO); }