Commit 5f183bd4 authored by Tika Lestari [SLB]'s avatar Tika Lestari [SLB] Committed by Rostislav Vatolin [SLB]
Browse files

Implement long lived cache

parent f71e5dab
...@@ -28,7 +28,7 @@ public class ListGroupOnBehalfOfApi { ...@@ -28,7 +28,7 @@ public class ListGroupOnBehalfOfApi {
private final PartitionHeaderValidationService partitionHeaderValidationService; private final PartitionHeaderValidationService partitionHeaderValidationService;
@GetMapping("/members/{member_email}/groups") @GetMapping("/members/{member_email}/groups")
@PreAuthorize("@authorizationFilter.hasAnyPermission('" + AppProperties.ADMIN + "')") @PreAuthorize("@authorizationFilter.hasAnyPermission('" + AppProperties.OPS + "', '" + AppProperties.ADMIN + "')")
public ResponseEntity<ListGroupResponseDto> listGroupsOnBehalfOf(@PathVariable("member_email") String memberId, public ResponseEntity<ListGroupResponseDto> listGroupsOnBehalfOf(@PathVariable("member_email") String memberId,
@RequestParam(name="type", required = false) String type, @RequestParam(name="type", required = false) String type,
@RequestParam(name="appid", required = false) String appId) { @RequestParam(name="appid", required = false) String appId) {
......
...@@ -27,6 +27,7 @@ public class AddMemberService { ...@@ -27,6 +27,7 @@ public class AddMemberService {
private final AppProperties config; private final AppProperties config;
private final JaxRsDpsLog log; private final JaxRsDpsLog log;
private final PermissionService permissionService; private final PermissionService permissionService;
private final GroupCacheService groupCacheService;
/** /**
* Add Member only allows to create a member node for a new user (first time add a user to a data partition), but not for a group. * Add Member only allows to create a member node for a new user (first time add a user to a data partition), but not for a group.
...@@ -63,7 +64,8 @@ public class AddMemberService { ...@@ -63,7 +64,8 @@ public class AddMemberService {
} }
AddMemberRepoDto addMemberRepoDto = AddMemberRepoDto.builder().memberNode(memberNode).role(addMemberDto.getRole()). AddMemberRepoDto addMemberRepoDto = AddMemberRepoDto.builder().memberNode(memberNode).role(addMemberDto.getRole()).
partitionId(addMemberServiceDto.getPartitionId()).existingParents(allExistingParents).build(); partitionId(addMemberServiceDto.getPartitionId()).existingParents(allExistingParents).build();
addMemberRepo.addMember(existingGroupEntityNode, addMemberRepoDto); Set<String> impactedUsers = addMemberRepo.addMember(existingGroupEntityNode, addMemberRepoDto);
groupCacheService.refreshListGroupCache(impactedUsers, addMemberServiceDto.getPartitionId());
} }
private EntityNode createNewMemberNode(String memberPrimaryId, String memberDesId, String partitionId) { private EntityNode createNewMemberNode(String memberPrimaryId, String memberDesId, String partitionId) {
......
...@@ -24,13 +24,14 @@ public class CreateGroupService { ...@@ -24,13 +24,14 @@ public class CreateGroupService {
private final CreateGroupRepo createGroupRepo; private final CreateGroupRepo createGroupRepo;
private final RetrieveGroupRepo retrieveGroupRepo; private final RetrieveGroupRepo retrieveGroupRepo;
private final GroupCacheService groupCacheService;
private final JaxRsDpsLog log; private final JaxRsDpsLog log;
private final DefaultGroupsService defaultGroupsService; private final DefaultGroupsService defaultGroupsService;
public EntityNode run(EntityNode groupNode, CreateGroupServiceDto createGroupServiceDto) { public EntityNode run(EntityNode groupNode, CreateGroupServiceDto createGroupServiceDto) {
log.info(String.format("requested by %s", createGroupServiceDto.getRequesterId())); log.info(String.format("requested by %s", createGroupServiceDto.getRequesterId()));
EntityNode requesterNode = EntityNode.createMemberNodeForRequester(createGroupServiceDto.getRequesterId(), createGroupServiceDto.getPartitionId()); EntityNode requesterNode = EntityNode.createMemberNodeForRequester(createGroupServiceDto.getRequesterId(), createGroupServiceDto.getPartitionId());
Set<ParentReference> allExistingParents = retrieveGroupRepo.loadAllParents(requesterNode).getParentReferences(); Set<ParentReference> allExistingParents = groupCacheService.getFromPartitionCache(requesterNode.getNodeId(), createGroupServiceDto.getPartitionId());
if (allExistingParents.size() >= EntityNode.MAX_PARENTS) { if (allExistingParents.size() >= EntityNode.MAX_PARENTS) {
log.error(String.format("Identity %s already belong to %d groups", createGroupServiceDto.getRequesterId(), allExistingParents.size())); log.error(String.format("Identity %s already belong to %d groups", createGroupServiceDto.getRequesterId(), allExistingParents.size()));
throw new AppException(HttpStatus.PRECONDITION_FAILED.value(), HttpStatus.PRECONDITION_FAILED.getReasonPhrase(), String.format("%s's group quota hit. Identity can't belong to more than %d groups", createGroupServiceDto.getRequesterId(), EntityNode.MAX_PARENTS)); throw new AppException(HttpStatus.PRECONDITION_FAILED.value(), HttpStatus.PRECONDITION_FAILED.getReasonPhrase(), String.format("%s's group quota hit. Identity can't belong to more than %d groups", createGroupServiceDto.getRequesterId(), EntityNode.MAX_PARENTS));
...@@ -63,6 +64,7 @@ public class CreateGroupService { ...@@ -63,6 +64,7 @@ public class CreateGroupService {
} }
private void createGroup(EntityNode groupNode, CreateGroupRepoDto createGroupRepoDto) { private void createGroup(EntityNode groupNode, CreateGroupRepoDto createGroupRepoDto) {
createGroupRepo.createGroup(groupNode, createGroupRepoDto); Set<String> impactedUsers = createGroupRepo.createGroup(groupNode, createGroupRepoDto);
groupCacheService.refreshListGroupCache(impactedUsers, createGroupRepoDto.getPartitionId());
} }
} }
...@@ -12,6 +12,7 @@ import org.springframework.http.HttpStatus; ...@@ -12,6 +12,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
...@@ -19,6 +20,7 @@ public class DeleteGroupService { ...@@ -19,6 +20,7 @@ public class DeleteGroupService {
private final DeleteGroupRepo deleteGroupRepo; private final DeleteGroupRepo deleteGroupRepo;
private final RetrieveGroupRepo retrieveGroupRepo; private final RetrieveGroupRepo retrieveGroupRepo;
private final GroupCacheService groupCacheService;
private final JaxRsDpsLog log; private final JaxRsDpsLog log;
private final DefaultGroupsService defaultGroupsService; private final DefaultGroupsService defaultGroupsService;
private final RequestInfo requestInfo; private final RequestInfo requestInfo;
...@@ -39,7 +41,7 @@ public class DeleteGroupService { ...@@ -39,7 +41,7 @@ public class DeleteGroupService {
if (defaultGroupsService.isDefaultGroupName(groupNode.getName())) { if (defaultGroupsService.isDefaultGroupName(groupNode.getName())) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), "Invalid group, bootstrap groups are not allowed to be deleted"); throw new AppException(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), "Invalid group, bootstrap groups are not allowed to be deleted");
} }
deleteGroupRepo.deleteGroup(existingGroupEntityNode.get()); Set<String> impactedUsers = deleteGroupRepo.deleteGroup(existingGroupEntityNode.get());
groupCacheService.refreshListGroupCache(impactedUsers, deleteGroupServiceDto.getPartitionId());
} }
} }
...@@ -18,12 +18,14 @@ import java.util.stream.Collectors; ...@@ -18,12 +18,14 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor @RequiredArgsConstructor
public class DeleteMemberService { public class DeleteMemberService {
private final JaxRsDpsLog log; private final JaxRsDpsLog log;
private final RetrieveGroupRepo retrieveGroup; private final RetrieveGroupRepo retrieveGroupRepo;
private final GroupCacheService groupCacheService;
private final RemoveMemberService removeMemberService; private final RemoveMemberService removeMemberService;
public void deleteMember(DeleteMemberDto deleteMemberDto) { public void deleteMember(DeleteMemberDto deleteMemberDto) {
log.info(String.format("Remove member %s from all groups", deleteMemberDto.getMemberEmail())); log.info(String.format("Remove member %s from all groups", deleteMemberDto.getMemberEmail()));
createRemoveMemberServiceDtos(deleteMemberDto).forEach(this::removeMemberFromGroup); createRemoveMemberServiceDtos(deleteMemberDto).forEach(this::removeMemberFromGroup);
groupCacheService.flushListGroupCacheForUser(deleteMemberDto.getMemberEmail(), deleteMemberDto.getPartitionId());
} }
private List<RemoveMemberServiceDto> createRemoveMemberServiceDtos(DeleteMemberDto deleteMemberDto) { private List<RemoveMemberServiceDto> createRemoveMemberServiceDtos(DeleteMemberDto deleteMemberDto) {
...@@ -42,7 +44,7 @@ public class DeleteMemberService { ...@@ -42,7 +44,7 @@ public class DeleteMemberService {
} }
private Set<String> loadDirectParentsEmails(DeleteMemberDto deleteMemberDto) { private Set<String> loadDirectParentsEmails(DeleteMemberDto deleteMemberDto) {
List<ParentReference> directParents = retrieveGroup.loadDirectParents( List<ParentReference> directParents = retrieveGroupRepo.loadDirectParents(
deleteMemberDto.getPartitionId(), deleteMemberDto.getMemberEmail()); deleteMemberDto.getPartitionId(), deleteMemberDto.getMemberEmail());
return directParents.stream() return directParents.stream()
.map(ParentReference::getId) .map(ParentReference::getId)
......
...@@ -7,4 +7,8 @@ import java.util.Set; ...@@ -7,4 +7,8 @@ import java.util.Set;
public interface GroupCacheService { public interface GroupCacheService {
Set<ParentReference> getFromPartitionCache(String requesterId, String partitionId); Set<ParentReference> getFromPartitionCache(String requesterId, String partitionId);
void refreshListGroupCache(final Set<String> userIds, String partitionId);
void flushListGroupCacheForUser(String userId, String partitionId);
} }
...@@ -20,6 +20,7 @@ import java.util.Set; ...@@ -20,6 +20,7 @@ import java.util.Set;
public class RemoveMemberService { public class RemoveMemberService {
private final RemoveMemberRepo removeMemberRepo; private final RemoveMemberRepo removeMemberRepo;
private final RetrieveGroupRepo retrieveGroupRepo; private final RetrieveGroupRepo retrieveGroupRepo;
private final GroupCacheService groupCacheService;
private final JaxRsDpsLog log; private final JaxRsDpsLog log;
private final ServiceAccountsConfigurationService serviceAccountsConfigurationService; private final ServiceAccountsConfigurationService serviceAccountsConfigurationService;
private final BootstrapGroupsConfigurationService bootstrapGroupsConfigurationService; private final BootstrapGroupsConfigurationService bootstrapGroupsConfigurationService;
...@@ -62,7 +63,9 @@ public class RemoveMemberService { ...@@ -62,7 +63,9 @@ public class RemoveMemberService {
memberNode.getName(), existingGroupEntityNode.getName())); memberNode.getName(), existingGroupEntityNode.getName()));
} }
return removeMemberRepo.removeMember(existingGroupEntityNode, memberNode, removeMemberServiceDto); Set<String> impactedUsers = removeMemberRepo.removeMember(existingGroupEntityNode, memberNode, removeMemberServiceDto);
groupCacheService.refreshListGroupCache(impactedUsers, removeMemberServiceDto.getPartitionId());
return impactedUsers;
} }
private EntityNode getEntityNode(String id, String partitionId) { private EntityNode getEntityNode(String id, String partitionId) {
......
package org.opengroup.osdu.entitlements.v2.service; package org.opengroup.osdu.entitlements.v2.service;
import lombok.RequiredArgsConstructor;
import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.entitlements.v2.model.EntityNode; import org.opengroup.osdu.entitlements.v2.model.EntityNode;
import org.opengroup.osdu.entitlements.v2.model.updategroup.UpdateGroupResponseDto; import org.opengroup.osdu.entitlements.v2.model.updategroup.UpdateGroupResponseDto;
...@@ -8,27 +9,24 @@ import org.opengroup.osdu.entitlements.v2.spi.renamegroup.RenameGroupRepo; ...@@ -8,27 +9,24 @@ import org.opengroup.osdu.entitlements.v2.spi.renamegroup.RenameGroupRepo;
import org.opengroup.osdu.entitlements.v2.spi.retrievegroup.RetrieveGroupRepo; import org.opengroup.osdu.entitlements.v2.spi.retrievegroup.RetrieveGroupRepo;
import org.opengroup.osdu.entitlements.v2.spi.updateappids.UpdateAppIdsRepo; import org.opengroup.osdu.entitlements.v2.spi.updateappids.UpdateAppIdsRepo;
import org.opengroup.osdu.entitlements.v2.util.GroupCreationUtil; import org.opengroup.osdu.entitlements.v2.util.GroupCreationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
@Service @Service
@RequiredArgsConstructor
public class UpdateGroupService { public class UpdateGroupService {
@Autowired private final RetrieveGroupRepo retrieveGroupRepo;
private RetrieveGroupRepo retrieveGroupRepo; private final GroupCacheService groupCacheService;
@Autowired private final RenameGroupRepo renameGroupRepo;
private RenameGroupRepo renameGroupRepo; private final UpdateAppIdsRepo updateAppIdsRepo;
@Autowired private final DefaultGroupsService defaultGroupsService;
private UpdateAppIdsRepo updateAppIdsRepo; private final PermissionService permissionService;
@Autowired
private DefaultGroupsService defaultGroupsService;
@Autowired
private PermissionService permissionService;
public UpdateGroupResponseDto updateGroup(UpdateGroupServiceDto updateGroupServiceDto) { public UpdateGroupResponseDto updateGroup(UpdateGroupServiceDto updateGroupServiceDto) {
String existingGroupEmail = updateGroupServiceDto.getExistingGroupEmail(); String existingGroupEmail = updateGroupServiceDto.getExistingGroupEmail();
...@@ -44,6 +42,7 @@ public class UpdateGroupService { ...@@ -44,6 +42,7 @@ public class UpdateGroupService {
existingAppIds.addAll(existingGroupEntityNode.getAppIds()); existingAppIds.addAll(existingGroupEntityNode.getAppIds());
UpdateGroupResponseDto result = new UpdateGroupResponseDto(existingGroupName, existingGroupEmail, existingAppIds); UpdateGroupResponseDto result = new UpdateGroupResponseDto(existingGroupName, existingGroupEmail, existingAppIds);
Set<String> impactedUsers = new HashSet<>();
if (updateGroupServiceDto.getRenameOperation() != null) { if (updateGroupServiceDto.getRenameOperation() != null) {
validateIfGroupIsNotDataGroup(existingGroupEmail, existingGroupEntityNode); validateIfGroupIsNotDataGroup(existingGroupEmail, existingGroupEntityNode);
String newGroupName = updateGroupServiceDto.getRenameOperation().getValue().get(0).toLowerCase(); String newGroupName = updateGroupServiceDto.getRenameOperation().getValue().get(0).toLowerCase();
...@@ -52,17 +51,17 @@ public class UpdateGroupService { ...@@ -52,17 +51,17 @@ public class UpdateGroupService {
String newGroupEmail = GroupCreationUtil.createGroupEmail(newGroupName, partitionDomain); String newGroupEmail = GroupCreationUtil.createGroupEmail(newGroupName, partitionDomain);
validateIfNewGroupNameDoesNotExist(newGroupName, partitionId, newGroupEmail); validateIfNewGroupNameDoesNotExist(newGroupName, partitionId, newGroupEmail);
renameGroupRepo.run(existingGroupEntityNode, newGroupName); impactedUsers.addAll(renameGroupRepo.run(existingGroupEntityNode, newGroupName));
result.setEmail(newGroupEmail); result.setEmail(newGroupEmail);
result.setName(newGroupName); result.setName(newGroupName);
} }
if (updateGroupServiceDto.getAppIdsOperation()!= null) { if (updateGroupServiceDto.getAppIdsOperation() != null) {
List<String> allowedAppIdsList = updateGroupServiceDto.getAppIdsOperation().getValue(); List<String> allowedAppIdsList = updateGroupServiceDto.getAppIdsOperation().getValue();
updateAppIdsRepo.updateAppIds(existingGroupEntityNode, new HashSet<>(allowedAppIdsList)); impactedUsers.addAll(updateAppIdsRepo.updateAppIds(existingGroupEntityNode, new HashSet<>(allowedAppIdsList)));
result.setAppIds(allowedAppIdsList); result.setAppIds(allowedAppIdsList);
} }
groupCacheService.refreshListGroupCache(new HashSet<>(impactedUsers), updateGroupServiceDto.getPartitionId());
return result; return result;
} }
......
...@@ -5,5 +5,5 @@ import org.opengroup.osdu.entitlements.v2.model.EntityNode; ...@@ -5,5 +5,5 @@ import org.opengroup.osdu.entitlements.v2.model.EntityNode;
import java.util.Set; import java.util.Set;
public interface UpdateAppIdsRepo { public interface UpdateAppIdsRepo {
void updateAppIds(EntityNode groupNode, Set<String> allowedAppIds); Set<String> updateAppIds(EntityNode groupNode, Set<String> allowedAppIds);
} }
...@@ -25,9 +25,11 @@ import org.springframework.boot.test.context.SpringBootTest; ...@@ -25,9 +25,11 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
...@@ -47,6 +49,8 @@ public class AddMemberServiceTests { ...@@ -47,6 +49,8 @@ public class AddMemberServiceTests {
@MockBean @MockBean
private RetrieveGroupRepo retrieveGroupRepo; private RetrieveGroupRepo retrieveGroupRepo;
@MockBean @MockBean
private GroupCacheService groupCacheService;
@MockBean
private AddMemberRepo addMemberRepo; private AddMemberRepo addMemberRepo;
@MockBean @MockBean
private JaxRsDpsLog log; private JaxRsDpsLog log;
...@@ -87,6 +91,8 @@ public class AddMemberServiceTests { ...@@ -87,6 +91,8 @@ public class AddMemberServiceTests {
.requesterId("requesterid") .requesterId("requesterid")
.partitionId("common") .partitionId("common")
.build(); .build();
Set<String> allImpactUsers = new HashSet<>(Arrays.asList("member@xxx.com"));
when(addMemberRepo.addMember(any(), any())).thenReturn(allImpactUsers);
addMemberService.run(addMemberDto, addMemberServiceDto); addMemberService.run(addMemberDto, addMemberServiceDto);
...@@ -94,6 +100,7 @@ public class AddMemberServiceTests { ...@@ -94,6 +100,7 @@ public class AddMemberServiceTests {
verify(addMemberRepo).addMember(eq(groupNode), captor.capture()); verify(addMemberRepo).addMember(eq(groupNode), captor.capture());
assertThat(captor.getValue().getRole()).isEqualTo(Role.MEMBER); assertThat(captor.getValue().getRole()).isEqualTo(Role.MEMBER);
assertThat(captor.getValue().getPartitionId()).isEqualTo("common"); assertThat(captor.getValue().getPartitionId()).isEqualTo("common");
verify(groupCacheService).refreshListGroupCache(allImpactUsers, "common");
} }
@Test @Test
...@@ -118,6 +125,8 @@ public class AddMemberServiceTests { ...@@ -118,6 +125,8 @@ public class AddMemberServiceTests {
.requesterId("datafier@test.com") .requesterId("datafier@test.com")
.partitionId("common") .partitionId("common")
.build(); .build();
Set<String> allImpactUsers = new HashSet<>(Arrays.asList("member@xxx.com"));
when(addMemberRepo.addMember(any(), any())).thenReturn(allImpactUsers);
addMemberService.run(addMemberDto, addMemberServiceDto); addMemberService.run(addMemberDto, addMemberServiceDto);
...@@ -125,6 +134,7 @@ public class AddMemberServiceTests { ...@@ -125,6 +134,7 @@ public class AddMemberServiceTests {
verify(addMemberRepo).addMember(eq(groupNode), captor.capture()); verify(addMemberRepo).addMember(eq(groupNode), captor.capture());
assertThat(captor.getValue().getRole()).isEqualTo(Role.MEMBER); assertThat(captor.getValue().getRole()).isEqualTo(Role.MEMBER);
assertThat(captor.getValue().getPartitionId()).isEqualTo("common"); assertThat(captor.getValue().getPartitionId()).isEqualTo("common");
verify(groupCacheService).refreshListGroupCache(allImpactUsers, "common");
} }
@Test @Test
...@@ -178,6 +188,8 @@ public class AddMemberServiceTests { ...@@ -178,6 +188,8 @@ public class AddMemberServiceTests {
.requesterId("requesterid") .requesterId("requesterid")
.partitionId("common") .partitionId("common")
.build(); .build();
Set<String> allImpactUsers = new HashSet<>(Arrays.asList("member@xxx.com"));
when(addMemberRepo.addMember(any(), any())).thenReturn(allImpactUsers);
addMemberService.run(addMemberDto, addMemberServiceDto); addMemberService.run(addMemberDto, addMemberServiceDto);
...@@ -186,6 +198,7 @@ public class AddMemberServiceTests { ...@@ -186,6 +198,7 @@ public class AddMemberServiceTests {
assertThat(captor.getValue().getMemberNode()).isEqualTo(memberNode); assertThat(captor.getValue().getMemberNode()).isEqualTo(memberNode);
assertThat(captor.getValue().getRole()).isEqualTo(Role.MEMBER); assertThat(captor.getValue().getRole()).isEqualTo(Role.MEMBER);
assertThat(captor.getValue().getPartitionId()).isEqualTo("common"); assertThat(captor.getValue().getPartitionId()).isEqualTo("common");
verify(groupCacheService).refreshListGroupCache(allImpactUsers, "common");
} }
@Test @Test
......
...@@ -19,6 +19,7 @@ import org.opengroup.osdu.entitlements.v2.spi.creategroup.CreateGroupRepo; ...@@ -19,6 +19,7 @@ import org.opengroup.osdu.entitlements.v2.spi.creategroup.CreateGroupRepo;
import org.opengroup.osdu.entitlements.v2.spi.retrievegroup.RetrieveGroupRepo; import org.opengroup.osdu.entitlements.v2.spi.retrievegroup.RetrieveGroupRepo;
import org.powermock.reflect.Whitebox; import org.powermock.reflect.Whitebox;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
...@@ -26,6 +27,7 @@ import java.util.Set; ...@@ -26,6 +27,7 @@ import java.util.Set;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.eq; import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
...@@ -40,6 +42,8 @@ public class CreateGroupServiceTests { ...@@ -40,6 +42,8 @@ public class CreateGroupServiceTests {
@Mock @Mock
private RetrieveGroupRepo retrieveGroupRepo; private RetrieveGroupRepo retrieveGroupRepo;
@Mock @Mock
private GroupCacheService groupCacheService;
@Mock
private JaxRsDpsLog logger; private JaxRsDpsLog logger;
@Mock @Mock
private DefaultGroupsService defaultGroupsService; private DefaultGroupsService defaultGroupsService;
...@@ -68,8 +72,7 @@ public class CreateGroupServiceTests { ...@@ -68,8 +72,7 @@ public class CreateGroupServiceTests {
.name("callerdesid") .name("callerdesid")
.dataPartitionId("dp") .dataPartitionId("dp")
.build(); .build();
ParentTreeDto parentTreeDto = ParentTreeDto.builder().parentReferences(parents).maxDepth(2).build(); when(groupCacheService.getFromPartitionCache(requesterNode.getNodeId(), "dp")).thenReturn(parents);
when(retrieveGroupRepo.loadAllParents(requesterNode)).thenReturn(parentTreeDto);
try { try {
CreateGroupServiceDto createGroupServiceDto = CreateGroupServiceDto.builder() CreateGroupServiceDto createGroupServiceDto = CreateGroupServiceDto.builder()
.requesterId("callerdesid") .requesterId("callerdesid")
...@@ -103,9 +106,6 @@ public class CreateGroupServiceTests { ...@@ -103,9 +106,6 @@ public class CreateGroupServiceTests {
when(retrieveGroupRepo.groupExistenceValidation("users.data.root@dp.domain.com", "dp")).thenReturn(dataRootGroupNode); when(retrieveGroupRepo.groupExistenceValidation("users.data.root@dp.domain.com", "dp")).thenReturn(dataRootGroupNode);
when(defaultGroupsService.isNotDefaultGroupName("data.x")).thenReturn(true); when(defaultGroupsService.isNotDefaultGroupName("data.x")).thenReturn(true);
when(retrieveGroupRepo.loadAllParents(dataRootGroupNode)).thenReturn(ParentTreeDto.builder().parentReferences(parents).maxDepth(2).build()); when(retrieveGroupRepo.loadAllParents(dataRootGroupNode)).thenReturn(ParentTreeDto.builder().parentReferences(parents).maxDepth(2).build());
EntityNode requesterNode = EntityNode.createMemberNodeForRequester("callerdesid", "dp");
ParentTreeDto parentTreeDto = ParentTreeDto.builder().parentReferences(Collections.emptySet()).maxDepth(2).build();
when(retrieveGroupRepo.loadAllParents(requesterNode)).thenReturn(parentTreeDto);
try { try {
CreateGroupServiceDto createGroupServiceDto = CreateGroupServiceDto.builder() CreateGroupServiceDto createGroupServiceDto = CreateGroupServiceDto.builder()
.requesterId("callerdesid") .requesterId("callerdesid")
...@@ -143,14 +143,16 @@ public class CreateGroupServiceTests { ...@@ -143,14 +143,16 @@ public class CreateGroupServiceTests {
.partitionDomain("dp.domain.com") .partitionDomain("dp.domain.com")
.partitionId("dp").build(); .partitionId("dp").build();
EntityNode requesterNode = EntityNode.createMemberNodeForRequester("callerdesid", "dp"); EntityNode requesterNode = EntityNode.createMemberNodeForRequester("callerdesid", "dp");
ParentTreeDto parentTreeDto = ParentTreeDto.builder().parentReferences(Collections.emptySet()).maxDepth(2).build(); when(groupCacheService.getFromPartitionCache(requesterNode.getNodeId(), "dp")).thenReturn(Collections.emptySet());
when(retrieveGroupRepo.loadAllParents(requesterNode)).thenReturn(parentTreeDto);
ArgumentCaptor<CreateGroupRepoDto> captor = ArgumentCaptor.forClass(CreateGroupRepoDto.class); ArgumentCaptor<CreateGroupRepoDto> captor = ArgumentCaptor.forClass(CreateGroupRepoDto.class);
when(defaultGroupsService.isNotDefaultGroupName("data.x")).thenReturn(true); when(defaultGroupsService.isNotDefaultGroupName("data.x")).thenReturn(true);
Set<String> impactedUsers = new HashSet<>(Arrays.asList("callerdesid"));
when(createGroupRepo.createGroup(any(), any())).thenReturn(impactedUsers);
createGroupService.run(groupNode, createGroupServiceDto); createGroupService.run(groupNode, createGroupServiceDto);
verify(createGroupRepo, times(1)).createGroup(eq(groupNode), captor.capture()); verify(createGroupRepo, times(1)).createGroup(eq(groupNode), captor.capture());
assertThat(captor.getValue().getDataRootGroupNode()).isNotNull(); assertThat(captor.getValue().getDataRootGroupNode()).isNotNull();
assertThat(captor.getValue().isAddDataRootGroup()).isTrue(); assertThat(captor.getValue().isAddDataRootGroup()).isTrue();
verify(groupCacheService).refreshListGroupCache(impactedUsers, "dp");
} }
@Test @Test
...@@ -166,13 +168,15 @@ public class CreateGroupServiceTests { ...@@ -166,13 +168,15 @@ public class CreateGroupServiceTests {
.partitionDomain("dp.domain.com") .partitionDomain("dp.domain.com")
.partitionId("dp").build(); .partitionId("dp").build();
EntityNode requesterNode = EntityNode.createMemberNodeForRequester("callerdesid", "dp"); EntityNode requesterNode = EntityNode.createMemberNodeForRequester("callerdesid", "dp");
ParentTreeDto parentTreeDto = ParentTreeDto.builder().parentReferences(Collections.emptySet()).maxDepth(2).build(); when(groupCacheService.getFromPartitionCache(requesterNode.getNodeId(), "dp")).thenReturn(Collections.emptySet());
when(retrieveGroupRepo.loadAllParents(requesterNode)).thenReturn(parentTreeDto);
ArgumentCaptor<CreateGroupRepoDto> captor = ArgumentCaptor.forClass(CreateGroupRepoDto.class);