### Question:
UserGroupServiceImpl implements UserGroupService { @Override public void removeGroup(String groupId) { if (projectDAO.getProjectsWithEndpointStatusNotIn(UserInstanceStatus.TERMINATED, UserInstanceStatus.TERMINATING) .stream() .map(ProjectDTO::getGroups) .noneMatch(groups -> groups.contains(groupId))) { userRoleDao.removeGroup(groupId); userGroupDao.removeGroup(groupId); } else { throw new ResourceConflictException("Group can not be removed because it is used in some project"); } }  @Override void createGroup(String group, Set<String> roleIds, Set<String> users); @Override void updateGroup(UserInfo user, String group, Set<String> roleIds, Set<String> users); @Override void removeGroup(String groupId); @Override List<UserGroupDto> getAggregatedRolesByGroup(UserInfo user);  }### Answer:
@Test public void removeGroup() { when(userRoleDao.removeGroup(anyString())).thenReturn(true); final ProjectDTO projectDTO = new ProjectDTO( "name", Collections.emptySet(), "", "", null, Collections.emptyList(), true); when(projectDAO.getProjectsWithEndpointStatusNotIn(UserInstanceStatus.TERMINATED, UserInstanceStatus.TERMINATING)).thenReturn(Collections.singletonList(projectDTO)); doNothing().when(userGroupDao).removeGroup(anyString()); userGroupService.removeGroup(GROUP); verify(userRoleDao).removeGroup(GROUP); verify(userGroupDao).removeGroup(GROUP); verifyNoMoreInteractions(userGroupDao, userRoleDao); }
@Test public void removeGroupWhenItIsUsedInProject() { when(userRoleDao.removeGroup(anyString())).thenReturn(true); when(projectDAO.getProjectsWithEndpointStatusNotIn(UserInstanceStatus.TERMINATED, UserInstanceStatus.TERMINATING)).thenReturn(Collections.singletonList( new ProjectDTO( "name", Collections.singleton(GROUP), "", "", null, Collections.emptyList(), true))); doNothing().when(userGroupDao).removeGroup(anyString()); try { userGroupService.removeGroup(GROUP); } catch (Exception e){ assertEquals("Group can not be removed because it is used in some project", e.getMessage()); } verify(userRoleDao, never()).removeGroup(GROUP); verify(userGroupDao, never()).removeGroup(GROUP); verifyNoMoreInteractions(userGroupDao, userRoleDao); }
@Test public void removeGroupWhenGroupNotExist() { final ProjectDTO projectDTO = new ProjectDTO( "name", Collections.emptySet(), "", "", null, Collections.emptyList(), true); when(projectDAO.getProjectsWithEndpointStatusNotIn(UserInstanceStatus.TERMINATED, UserInstanceStatus.TERMINATING)).thenReturn(Collections.singletonList(projectDTO)); when(userRoleDao.removeGroup(anyString())).thenReturn(false); doNothing().when(userGroupDao).removeGroup(anyString()); userGroupService.removeGroup(GROUP); verify(userRoleDao).removeGroup(GROUP); verify(userGroupDao).removeGroup(GROUP); verifyNoMoreInteractions(userGroupDao, userRoleDao); }
@Test public void removeGroupWithException() { final ProjectDTO projectDTO = new ProjectDTO( "name", Collections.emptySet(), "", "", null, Collections.emptyList(), true); when(projectDAO.getProjectsWithEndpointStatusNotIn(UserInstanceStatus.TERMINATED, UserInstanceStatus.TERMINATING)).thenReturn(Collections.singletonList(projectDTO)); when(userRoleDao.removeGroup(anyString())).thenThrow(new DlabException("Exception")); expectedException.expectMessage("Exception"); expectedException.expect(DlabException.class); userGroupService.removeGroup(GROUP); }