Commit a66d20bb authored by Raigo Ukkivi's avatar Raigo Ukkivi
Browse files

TEIS-343. Delete user from group service logic and tests.

parent 00d8f5e4
......@@ -5,12 +5,14 @@ import ee.sm.ti.teis.officegateway.official.dto.OfficialUserDto;
import ee.sm.ti.teis.officegateway.official.dto.OfficialsGroupDto;
import ee.sm.ti.teis.officegateway.official.request.AddOfficialsGroupMembersRequest;
import ee.sm.ti.teis.officegateway.official.request.CreateOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.DeleteOfficialsGroupMemberRequest;
import ee.sm.ti.teis.officegateway.official.request.DeleteOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.OfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.OfficialsGroupsRequest;
import ee.sm.ti.teis.officegateway.official.request.UpdateOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.response.AddOfficialsGroupMembersResponse;
import ee.sm.ti.teis.officegateway.official.response.CreateOfficialsGroupResponse;
import ee.sm.ti.teis.officegateway.official.response.DeleteOfficialsGroupMemberResponse;
import ee.sm.ti.teis.officegateway.official.response.OfficialsGroupResponse;
import ee.sm.ti.teis.officegateway.official.response.OfficialsGroupsResponse;
import ee.sm.ti.teis.officegateway.official.response.UpdateOfficialsGroupResponse;
......@@ -282,6 +284,46 @@ class OfficialsGroupGwListenerTest extends OfficialsAppTestBase {
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.NOT_FOUND);
}
@Test
void deleteOfficialsGroupMember_test() {
UUID currentGroupId = usersRepo.findById(TEST_USER1_ID).get().getOfficialsGroup().getId();
groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(currentGroupId.toString(), TEST_USER1_ID.toString()));
assertThat(usersRepo.findById(TEST_USER1_ID).get().getOfficialsGroup()).isNull();
}
@Test
void deleteOfficialsGroupMember_validates_that_user_is_in_group_test() {
UUID currentGroupId = usersRepo.findById(TEST_USER1_ID).get().getOfficialsGroup().getId();
assertThat(currentGroupId).isNotEqualTo(TEST_GROUP_ID);
groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(TEST_GROUP_ID.toString(), TEST_USER1_ID.toString()));
assertThat(usersRepo.findById(TEST_USER1_ID).get().getOfficialsGroup().getId()).isEqualTo(currentGroupId);
}
@Test
void deleteOfficialsGroupMember_validates_input_test() {
UUID currentGroupId = usersRepo.findById(TEST_USER1_ID).get().getOfficialsGroup().getId();
DeleteOfficialsGroupMemberResponse response = groupListener
.deleteOfficialsGroupMember(composeDeleteMemberRequest("", TEST_USER1_ID.toString()));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.BAD_REQUEST);
response = groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(null, null));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.BAD_REQUEST);
response = groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(currentGroupId.toString(), ""));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.BAD_REQUEST);
response = groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(currentGroupId.toString(), UUID.randomUUID().toString()));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.NOT_FOUND);
response = groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(UUID.randomUUID().toString(), TEST_USER1_ID.toString()));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.NOT_FOUND);
}
@NotNull
private OfficialsGroupsRequest composeOfficialsGroupsRequest(String status) {
OfficialsGroupsRequest request = new OfficialsGroupsRequest();
......@@ -325,4 +367,11 @@ class OfficialsGroupGwListenerTest extends OfficialsAppTestBase {
return request;
}
@NotNull
private DeleteOfficialsGroupMemberRequest composeDeleteMemberRequest(String groupId, String memberId) {
DeleteOfficialsGroupMemberRequest request = new DeleteOfficialsGroupMemberRequest();
request.setPayload(new DeleteOfficialsGroupMemberRequest.Parameters(groupId, memberId), REQUEST_META_DTO);
return request;
}
}
......@@ -35,6 +35,7 @@ public class QueueConfig implements QueueList {
public static final String GW_DELETE_OFFICIALS_GROUP_EMTAK_QUEUE = "officials-service.gw.deleteGroupEmtakRequest";
public static final String GW_OFFICES_QUEUE = "officials-service.gw.findOfficesRequest";
public static final String GW_ADD_OFFICIALS_GROUP_MEMBERS_QUEUE = "officials-service.gw.addGroupMembersRequest";
public static final String GW_DELETE_OFFICIALS_GROUP_MEMBER_QUEUE = "officials-service.gw.deleteGroupMemberRequest";
public static final String MS_OFFICIAL_USER_QUEUE = "officials-service.ms.findUserRequest";
public static final String MS_OFFICIAL_USERS_QUEUE = "officials-service.ms.findUsersRequest";
......@@ -66,6 +67,7 @@ public class QueueConfig implements QueueList {
addGwQueue(queues, "gwDeleteGroupEmtakQueue", DeleteOfficialsGroupEmtakRequest.ROUTING_KEY, GW_DELETE_OFFICIALS_GROUP_EMTAK_QUEUE);
addGwQueue(queues, "gwFindOfficesQueue", OfficesRequest.ROUTING_KEY, GW_OFFICES_QUEUE);
addGwQueue(queues, "gwAddGroupMembersQueue", AddOfficialsGroupMembersRequest.ROUTING_KEY, GW_ADD_OFFICIALS_GROUP_MEMBERS_QUEUE);
addGwQueue(queues, "gwDeleteGroupMemberQueue", DeleteOfficialsGroupMemberRequest.ROUTING_KEY, GW_DELETE_OFFICIALS_GROUP_MEMBER_QUEUE);
addMsQueue(queues, "msFindUserQueue", new OfficialUserDomainRequest().routingKey(), MS_OFFICIAL_USER_QUEUE);
addMsQueue(queues, "msFindUsersQueue", new OfficialUsersDomainRequest().routingKey(), MS_OFFICIAL_USERS_QUEUE);
......
......@@ -4,12 +4,14 @@ import ee.sm.ti.teis.officegateway.official.dto.OfficialUserDto;
import ee.sm.ti.teis.officegateway.official.dto.OfficialsGroupDto;
import ee.sm.ti.teis.officegateway.official.request.AddOfficialsGroupMembersRequest;
import ee.sm.ti.teis.officegateway.official.request.CreateOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.DeleteOfficialsGroupMemberRequest;
import ee.sm.ti.teis.officegateway.official.request.DeleteOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.OfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.OfficialsGroupsRequest;
import ee.sm.ti.teis.officegateway.official.request.UpdateOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.response.AddOfficialsGroupMembersResponse;
import ee.sm.ti.teis.officegateway.official.response.CreateOfficialsGroupResponse;
import ee.sm.ti.teis.officegateway.official.response.DeleteOfficialsGroupMemberResponse;
import ee.sm.ti.teis.officegateway.official.response.DeleteOfficialsGroupResponse;
import ee.sm.ti.teis.officegateway.official.response.OfficialsGroupResponse;
import ee.sm.ti.teis.officegateway.official.response.OfficialsGroupsResponse;
......@@ -95,4 +97,13 @@ public class OfficialsGroupGwListener {
return response;
}
@RabbitListener(queues = {GW_DELETE_OFFICIALS_GROUP_MEMBER_QUEUE})
@PreAuthorize("@accessController.hasOfficialPrivilege({T(ee.sm.ti.teis.types.enums.RolePrivilegeCode).TI_MANAGE_OFFICIALS_GROUPS})")
public DeleteOfficialsGroupMemberResponse deleteOfficialsGroupMember(DeleteOfficialsGroupMemberRequest request) {
service.deleteMemberFromGroup(request.getPayload().getGroupId(), request.getPayload().getMemberId(), request.getRequestMetaDTO());
DeleteOfficialsGroupMemberResponse response = new DeleteOfficialsGroupMemberResponse();
response.setPayload(new OfficialUserDto(), request.getRequestMetaDTO());
return response;
}
}
......@@ -173,7 +173,7 @@ public class OfficialsGroupService {
@Transactional
public List<OfficialUserDto> addMembersToGroup(String groupId, List<String> memberIds, RequestMetaDTO requestMetaDTO) {
final OfficialsGroupEntity group = findGroupById(UUID.fromString(groupId));
final OfficialsGroupEntity group = findGroupById(UUID.fromString(StringUtils.defaultString(groupId)));
validateGroupMembers(memberIds);
List<OfficialUserDto> groupMembers = new ArrayList<>();
for (String memberId : memberIds) {
......@@ -185,6 +185,15 @@ public class OfficialsGroupService {
return groupMembers;
}
@Transactional
public void deleteMemberFromGroup(String groupId, String memberId, RequestMetaDTO requestMetaDTO) {
final OfficialsGroupEntity group = findGroupById(UUID.fromString(StringUtils.defaultString(groupId)));
final OfficialUserEntity officialUser = userService.findUserById(UUID.fromString(StringUtils.defaultString(memberId)));
if (group.getId().equals(officialUser.getOfficialsGroup().getId())) {
userService.updateUserGroupData(officialUser.getId(), null, requestMetaDTO);
}
}
// lets do full validation before performing user group updates, as updates are sending notifications to other micro services
private void validateGroupMembers(List<String> memberIds) {
if (memberIds == null) {
......@@ -197,7 +206,7 @@ public class OfficialsGroupService {
.build());
}
memberIds.forEach(memberId -> {
UUID userId = UUID.fromString(memberId);
UUID userId = UUID.fromString(StringUtils.defaultString(memberId));
userService.findUserById(userId);
});
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment