@Test public void shouldGetAllRolesMapByRightType() throws Exception { List<Role> allRoles = new ArrayList<>(); Role role1 = new Role(); role1.setId(1L); Role role2 = new Role(); role2.setId(2L); Role role3 = new Role(); role3.setId(3L); Role role4 = new Role(); role4.setId(4L); allRoles.add(role1); allRoles.add(role2); allRoles.add(role3); allRoles.add(role4); when(roleRightsRepository.getAllRoles()).thenReturn(allRoles); when(roleRightsRepository.getRightTypeForRoleId(1L)).thenReturn(RightType.REQUISITION); when(roleRightsRepository.getRightTypeForRoleId(2L)).thenReturn(RightType.ADMIN); when(roleRightsRepository.getRightTypeForRoleId(3L)).thenReturn(RightType.ALLOCATION); when(roleRightsRepository.getRightTypeForRoleId(4L)).thenReturn(RightType.ALLOCATION); Map<String, List<Role>> allRolesMap = roleRightsService.getAllRolesMap(); assertThat(allRolesMap.size(), is(3)); assertThat(allRolesMap.get(RightType.ADMIN.name()).size(), is(1)); assertThat(allRolesMap.get(RightType.REQUISITION.name()).size(), is(1)); assertThat(allRolesMap.get(RightType.ALLOCATION.name()).size(), is(2)); }
@Test public void shouldReturnCountOfReportingRightOfUser() throws SQLException { Long userId = 1L; Right right1 = new Right("template1", RightType.REPORTING); Right right2 = new Right("template2", RightType.REPORTING); Right right3 = new Right("template3", RightType.REPORTING); Right right4 = new Right("admin", RightType.REPORTING); rightMapper.insertRight(right1); rightMapper.insertRight(right2); rightMapper.insertRight(right3); rightMapper.insertRight(right4); Role role = new Role(); role.setName("New Role"); roleRightsMapper.insertRole(role); queryExecutor.executeUpdate( "INSERT INTO role_rights(roleId, rightName) VALUES (?,?)", role.getId(), "template1"); queryExecutor.executeUpdate( "INSERT INTO role_rights(roleId, rightName) VALUES (?,?)", role.getId(), "template2"); queryExecutor.executeUpdate( "INSERT INTO role_rights(roleId, rightName) VALUES (?,?)", role.getId(), "template3"); queryExecutor.executeUpdate( "INSERT INTO role_rights(roleId, rightName) VALUES (?,?)", role.getId(), "admin"); queryExecutor.executeUpdate( "INSERT INTO role_assignments(userId, roleId) VALUES (?,?)", userId, role.getId()); Integer count = rightMapper.totalReportingRightsFor(userId); assertThat(count, is(4)); }
@Test public void shouldUpdateRole() { role.setRights(new HashSet<>(asList(CREATE_REQUISITION))); roleRightsService.updateRole(role); verify(roleRightsRepository).updateRole(role); }
@Test public void shouldSaveRole() throws Exception { role.setRights(new HashSet<>(asList(CREATE_REQUISITION, VIEW_REQUISITION))); roleRightsService.saveRole(role); verify(roleRightsRepository).createRole(role); }