From b643ecb21a2ff9be4a10916227100627335f74a3 Mon Sep 17 00:00:00 2001 From: Olena Chubukina Date: Fri, 20 Dec 2024 14:58:31 +0100 Subject: [PATCH] MAINT-33753 - backport the fix for proper job roles creation --- .../stream/service/AccessGroupService.java | 26 +- ...sGroupServiceUpdateFunctionGroupsTest.java | 290 +++++++++++++++--- 2 files changed, 263 insertions(+), 53 deletions(-) diff --git a/stream-access-control/access-control-core/src/main/java/com/backbase/stream/service/AccessGroupService.java b/stream-access-control/access-control-core/src/main/java/com/backbase/stream/service/AccessGroupService.java index 9bb6045e9..3ee908286 100644 --- a/stream-access-control/access-control-core/src/main/java/com/backbase/stream/service/AccessGroupService.java +++ b/stream-access-control/access-control-core/src/main/java/com/backbase/stream/service/AccessGroupService.java @@ -93,6 +93,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.ObjectUtils; import org.mapstruct.factory.Mappers; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; @@ -1228,11 +1229,26 @@ private Mono createJobRole(StreamTask streamTask, ServiceAgreement serv presentationIngestFunctionGroup.setExternalServiceAgreementId(serviceAgreement.getExternalId()); presentationIngestFunctionGroup.setMetadata(jobRole.getMetadata()); - //since ReferenceJobRole class was removed, now all job roles have the same class, and - //reference job role can be created only for MSA, for CSA it failed - if(BooleanUtils.isTrue(serviceAgreement.getIsMaster())) { - log.debug("Creating a Reference Job Role."); - presentationIngestFunctionGroup.setType(PresentationIngestFunctionGroup.TypeEnum.TEMPLATE); + if (CollectionUtils.isEmpty(jobRole.getFunctionGroups())) { + throw new IllegalArgumentException( + String.format("Unexpected no function groups for job role: %s", jobRole.getName())); + } + final var jobRoleType = jobRole.getFunctionGroups().getFirst().getType(); + if (!ObjectUtils.isEmpty(jobRoleType)) { + switch (jobRoleType) { + case TEMPLATE: + presentationIngestFunctionGroup.setType(PresentationIngestFunctionGroup.TypeEnum.TEMPLATE); + break; + case DEFAULT: + presentationIngestFunctionGroup.setType(PresentationIngestFunctionGroup.TypeEnum.REGULAR); + break; + default: + throw new IllegalArgumentException( + String.format("Unexpected enum constant: %s for job role: %s", jobRoleType, jobRole.getName())); + } + } else { + log.debug("No function group job role type is provided, creating a Local Job Role"); + presentationIngestFunctionGroup.setType(PresentationIngestFunctionGroup.TypeEnum.REGULAR); } // Removing constant from mapper and adding default APS here to avoid issues with apsName. diff --git a/stream-access-control/access-control-core/src/test/java/com/backbase/stream/service/AccessGroupServiceUpdateFunctionGroupsTest.java b/stream-access-control/access-control-core/src/test/java/com/backbase/stream/service/AccessGroupServiceUpdateFunctionGroupsTest.java index c36313d9b..18a22e323 100644 --- a/stream-access-control/access-control-core/src/test/java/com/backbase/stream/service/AccessGroupServiceUpdateFunctionGroupsTest.java +++ b/stream-access-control/access-control-core/src/test/java/com/backbase/stream/service/AccessGroupServiceUpdateFunctionGroupsTest.java @@ -22,6 +22,7 @@ import com.backbase.stream.configuration.DeletionProperties; import com.backbase.stream.legalentity.model.BusinessFunction; import com.backbase.stream.legalentity.model.BusinessFunctionGroup; +import com.backbase.stream.legalentity.model.BusinessFunctionGroup.TypeEnum; import com.backbase.stream.legalentity.model.JobRole; import com.backbase.stream.legalentity.model.LegalEntityParticipant; import com.backbase.stream.legalentity.model.ServiceAgreement; @@ -72,7 +73,7 @@ class AccessGroupServiceUpdateFunctionGroupsTest { private BatchResponseUtils batchResponseUtils; @Test - void setupJobRole() { + void setupJobRoleNoType() { final String saInternalId = "someSaInternalId"; final String saExternalId = "someSaExternalId"; final String description = "someDescription"; @@ -133,6 +134,91 @@ void setupJobRole() { Assertions.assertNotNull(setupJobRole); + Mockito.verify(functionGroupsApi) + .postPresentationIngestFunctionGroup(new PresentationIngestFunctionGroup() + .externalServiceAgreementId(saExternalId) + .apsId(1L) + .name("jobRoleNew") + .description("jobRoleNew") + .type(PresentationIngestFunctionGroup.TypeEnum.REGULAR) + .metadata(Map.of("key1","value1")) + .addPermissionsItem(new PresentationPermission() + .functionId("101") + .addPrivilegesItem("view") + ) + .addPermissionsItem(new PresentationPermission() + .functionId("102") + .addPrivilegesItem("view") + .addPrivilegesItem("edit") + )); + + } + + @Test + void setupJobRoleReferenceType() { + final String saInternalId = "someSaInternalId"; + final String saExternalId = "someSaExternalId"; + final String description = "someDescription"; + final String name = "someName"; + final String validFromDate = "2021-03-08"; + final String validFromTime = "00:00:00"; + final String validUntilDate = "2022-03-08"; + final String validUntilTime = "23:59:59"; + + StreamTask streamTask = Mockito.mock(StreamTask.class); + + ServiceAgreement serviceAgreement = buildInputServiceAgreement(saInternalId, saExternalId, description, name, + LocalDate.parse(validFromDate), validFromTime, LocalDate.parse(validUntilDate), validUntilTime); + + // participants + serviceAgreement + .addParticipantsItem(new LegalEntityParticipant().externalId("p1").sharingAccounts(true) + .sharingUsers(true).action(LegalEntityParticipant.ActionEnum.ADD)) + .addParticipantsItem(new LegalEntityParticipant().externalId("p2").sharingAccounts(false) + .sharingUsers(false).action(LegalEntityParticipant.ActionEnum.REMOVE)) + .addParticipantsItem(new LegalEntityParticipant().externalId("p3").sharingAccounts(false) + .sharingUsers(false).action(LegalEntityParticipant.ActionEnum.ADD)); + + serviceAgreement.setIsMaster(true); + + Mockito.when(functionGroupsApi.getFunctionGroups(saInternalId)) + .thenReturn(Flux.fromIterable(Collections.singletonList(new FunctionGroupItem() + .name("jobRoleOld").id("2") + .addPermissionsItem(new Permission().functionId("102") + .addAssignedPrivilegesItem(new Privilege().privilege("view")) + .addAssignedPrivilegesItem(new Privilege().privilege("edit"))) + ))); + + JobRole jobRole = new JobRole() + .name("jobRoleNew") + .addFunctionGroupsItem(new BusinessFunctionGroup() + .name("fg1") + .type(TypeEnum.TEMPLATE) + .addFunctionsItem(new BusinessFunction() + .name("name1") + .functionId("101") + .addPrivilegesItem(new com.backbase.stream.legalentity.model.Privilege().privilege("view")) + ) + ) + .addFunctionGroupsItem(new BusinessFunctionGroup() + .name("fg2") + .type(TypeEnum.TEMPLATE) + .addFunctionsItem(new BusinessFunction() + .name("name2") + .functionId("102") + .addPrivilegesItem(new com.backbase.stream.legalentity.model.Privilege().privilege("view")) + .addPrivilegesItem(new com.backbase.stream.legalentity.model.Privilege().privilege("edit")) + )) + .metadata(Map.of("key1","value1")); + + Mockito.when(functionGroupsApi.postPresentationIngestFunctionGroup(any())) + .thenReturn(Mono.just(new IdItem().id("1"))); + + Mono listMono = subject.setupJobRole(streamTask, serviceAgreement, jobRole); + JobRole setupJobRole = listMono.block(); + + Assertions.assertNotNull(setupJobRole); + Mockito.verify(functionGroupsApi) .postPresentationIngestFunctionGroup(new PresentationIngestFunctionGroup() .externalServiceAgreementId(saExternalId) @@ -153,6 +239,114 @@ void setupJobRole() { } + @Test + void setupJobRoleInvalidType() { + final String saInternalId = "someSaInternalId"; + final String saExternalId = "someSaExternalId"; + final String description = "someDescription"; + final String name = "someName"; + final String validFromDate = "2021-03-08"; + final String validFromTime = "00:00:00"; + final String validUntilDate = "2022-03-08"; + final String validUntilTime = "23:59:59"; + + StreamTask streamTask = Mockito.mock(StreamTask.class); + + ServiceAgreement serviceAgreement = buildInputServiceAgreement(saInternalId, saExternalId, description, name, + LocalDate.parse(validFromDate), validFromTime, LocalDate.parse(validUntilDate), validUntilTime); + + // participants + serviceAgreement + .addParticipantsItem(new LegalEntityParticipant().externalId("p1").sharingAccounts(true) + .sharingUsers(true).action(LegalEntityParticipant.ActionEnum.ADD)) + .addParticipantsItem(new LegalEntityParticipant().externalId("p2").sharingAccounts(false) + .sharingUsers(false).action(LegalEntityParticipant.ActionEnum.REMOVE)) + .addParticipantsItem(new LegalEntityParticipant().externalId("p3").sharingAccounts(false) + .sharingUsers(false).action(LegalEntityParticipant.ActionEnum.ADD)); + + serviceAgreement.setIsMaster(true); + + Mockito.when(functionGroupsApi.getFunctionGroups(saInternalId)) + .thenReturn(Flux.fromIterable(Collections.singletonList(new FunctionGroupItem() + .name("jobRoleOld").id("2") + .addPermissionsItem(new Permission().functionId("102") + .addAssignedPrivilegesItem(new Privilege().privilege("view")) + .addAssignedPrivilegesItem(new Privilege().privilege("edit"))) + ))); + + JobRole jobRole = new JobRole() + .name("jobRoleNew") + .addFunctionGroupsItem(new BusinessFunctionGroup() + .name("fg1") + .type(TypeEnum.SYSTEM) + .addFunctionsItem(new BusinessFunction() + .name("name1") + .functionId("101") + .addPrivilegesItem(new com.backbase.stream.legalentity.model.Privilege().privilege("view")) + ) + ) + .addFunctionGroupsItem(new BusinessFunctionGroup() + .name("fg2") + .type(TypeEnum.SYSTEM) + .addFunctionsItem(new BusinessFunction() + .name("name2") + .functionId("102") + .addPrivilegesItem(new com.backbase.stream.legalentity.model.Privilege().privilege("view")) + .addPrivilegesItem(new com.backbase.stream.legalentity.model.Privilege().privilege("edit")) + )) + .metadata(Map.of("key1","value1")); + + Mono listMono = subject.setupJobRole(streamTask, serviceAgreement, jobRole); + + Assertions.assertThrows(IllegalArgumentException.class, listMono::block); + + } + + @Test + void setupJobRoleNoFunctionGroups() { + final String saInternalId = "someSaInternalId"; + final String saExternalId = "someSaExternalId"; + final String description = "someDescription"; + final String name = "someName"; + final String validFromDate = "2021-03-08"; + final String validFromTime = "00:00:00"; + final String validUntilDate = "2022-03-08"; + final String validUntilTime = "23:59:59"; + + StreamTask streamTask = Mockito.mock(StreamTask.class); + + ServiceAgreement serviceAgreement = buildInputServiceAgreement(saInternalId, saExternalId, description, name, + LocalDate.parse(validFromDate), validFromTime, LocalDate.parse(validUntilDate), validUntilTime); + + // participants + serviceAgreement + .addParticipantsItem(new LegalEntityParticipant().externalId("p1").sharingAccounts(true) + .sharingUsers(true).action(LegalEntityParticipant.ActionEnum.ADD)) + .addParticipantsItem(new LegalEntityParticipant().externalId("p2").sharingAccounts(false) + .sharingUsers(false).action(LegalEntityParticipant.ActionEnum.REMOVE)) + .addParticipantsItem(new LegalEntityParticipant().externalId("p3").sharingAccounts(false) + .sharingUsers(false).action(LegalEntityParticipant.ActionEnum.ADD)); + + serviceAgreement.setIsMaster(true); + + Mockito.when(functionGroupsApi.getFunctionGroups(saInternalId)) + .thenReturn(Flux.fromIterable(Collections.singletonList(new FunctionGroupItem() + .name("jobRoleOld").id("2") + .addPermissionsItem(new Permission().functionId("102") + .addAssignedPrivilegesItem(new Privilege().privilege("view")) + .addAssignedPrivilegesItem(new Privilege().privilege("edit"))) + ))); + + JobRole jobRole = new JobRole() + .name("jobRoleNew") + .metadata(Map.of("key1","value1")); + + Mono listMono = subject.setupJobRole(streamTask, serviceAgreement, jobRole); + + Assertions.assertThrows(IllegalArgumentException.class, listMono::block); + + } + @Test void updateJobRole() { final String saInternalId = "someSaInternalId"; @@ -219,7 +413,7 @@ void updateJobRole() { .functionGroup(new FunctionGroupUpdate() .name("jobRole") .description("jobRole") - .metadata(Map.of("key1","value1")) + .metadata(Map.of("key1","value1")) .addPermissionsItem(new PresentationPermissionFunctionGroupUpdate() .functionName("name1") .addPrivilegesItem("view")) @@ -244,68 +438,68 @@ void updateJobRoleItemStatusIs400() { StreamTask streamTask = Mockito.mock(StreamTask.class); ServiceAgreement serviceAgreement = buildInputServiceAgreement(saInternalId, saExternalId, description, name, - LocalDate.parse(validFromDate), validFromTime, LocalDate.parse(validUntilDate), validUntilTime); + LocalDate.parse(validFromDate), validFromTime, LocalDate.parse(validUntilDate), validUntilTime); // participants serviceAgreement - .addParticipantsItem(new LegalEntityParticipant().externalId("p1").sharingAccounts(true) - .sharingUsers(true).action(LegalEntityParticipant.ActionEnum.ADD)) - .addParticipantsItem(new LegalEntityParticipant().externalId("p2").sharingAccounts(false) - .sharingUsers(false).action(LegalEntityParticipant.ActionEnum.REMOVE)) - .addParticipantsItem(new LegalEntityParticipant().externalId("p3").sharingAccounts(false) - .sharingUsers(false).action(LegalEntityParticipant.ActionEnum.ADD)); + .addParticipantsItem(new LegalEntityParticipant().externalId("p1").sharingAccounts(true) + .sharingUsers(true).action(LegalEntityParticipant.ActionEnum.ADD)) + .addParticipantsItem(new LegalEntityParticipant().externalId("p2").sharingAccounts(false) + .sharingUsers(false).action(LegalEntityParticipant.ActionEnum.REMOVE)) + .addParticipantsItem(new LegalEntityParticipant().externalId("p3").sharingAccounts(false) + .sharingUsers(false).action(LegalEntityParticipant.ActionEnum.ADD)); Mockito.when(functionGroupsApi.getFunctionGroups(saInternalId)) - .thenReturn(Flux.fromIterable(Collections.singletonList(new FunctionGroupItem() - .name("jobRole").id("1") - .addPermissionsItem(new Permission().functionId("101") - .addAssignedPrivilegesItem(new Privilege().privilege("view")) - .addAssignedPrivilegesItem(new Privilege().privilege("edit"))) - ))); + .thenReturn(Flux.fromIterable(Collections.singletonList(new FunctionGroupItem() + .name("jobRole").id("1") + .addPermissionsItem(new Permission().functionId("101") + .addAssignedPrivilegesItem(new Privilege().privilege("view")) + .addAssignedPrivilegesItem(new Privilege().privilege("edit"))) + ))); JobRole jobRole = new JobRole() - .name("jobRole") - .addFunctionGroupsItem(new BusinessFunctionGroup() - .name("fg1") - .addFunctionsItem(new BusinessFunction() - .name("name1") - .functionId("101") - .addPrivilegesItem(new com.backbase.stream.legalentity.model.Privilege().privilege("view")) - ) + .name("jobRole") + .addFunctionGroupsItem(new BusinessFunctionGroup() + .name("fg1") + .addFunctionsItem(new BusinessFunction() + .name("name1") + .functionId("101") + .addPrivilegesItem(new com.backbase.stream.legalentity.model.Privilege().privilege("view")) ) - .addFunctionGroupsItem(new BusinessFunctionGroup().name("fg2") - .addFunctionsItem(new BusinessFunction() - .name("name2") - .functionId("102") - .addPrivilegesItem(new com.backbase.stream.legalentity.model.Privilege().privilege("view")) - .addPrivilegesItem(new com.backbase.stream.legalentity.model.Privilege().privilege("edit")) - )); + ) + .addFunctionGroupsItem(new BusinessFunctionGroup().name("fg2") + .addFunctionsItem(new BusinessFunction() + .name("name2") + .functionId("102") + .addPrivilegesItem(new com.backbase.stream.legalentity.model.Privilege().privilege("view")) + .addPrivilegesItem(new com.backbase.stream.legalentity.model.Privilege().privilege("edit")) + )); Mockito.when(functionGroupsApi.putFunctionGroupsUpdate(any())) - .thenReturn(Flux.just(new BatchResponseItemExtended() - .resourceId("4028db307522bfbb017523171c9d0007") - .status(BatchResponseItemExtended.StatusEnum.HTTP_STATUS_BAD_REQUEST) - .addErrorsItem("You cannot manage this entity, while the referenced service agreement has a pending change.") - )); + .thenReturn(Flux.just(new BatchResponseItemExtended() + .resourceId("4028db307522bfbb017523171c9d0007") + .status(BatchResponseItemExtended.StatusEnum.HTTP_STATUS_BAD_REQUEST) + .addErrorsItem("You cannot manage this entity, while the referenced service agreement has a pending change.") + )); Mono listMono = subject.setupJobRole(streamTask, serviceAgreement, jobRole); Assertions.assertThrows(StreamTaskException.class, listMono::block); Mockito.verify(functionGroupsApi) - .putFunctionGroupsUpdate(Collections.singletonList(new PresentationFunctionGroupPutRequestBody() - .identifier(new PresentationIdentifier().idIdentifier("1")) - .functionGroup(new FunctionGroupUpdate() - .name("jobRole") - .description("jobRole") - .addPermissionsItem(new PresentationPermissionFunctionGroupUpdate() - .functionName("name1") - .addPrivilegesItem("view")) - .addPermissionsItem(new PresentationPermissionFunctionGroupUpdate() - .functionName("name2") - .addPrivilegesItem("view") - .addPrivilegesItem("edit"))) - )); + .putFunctionGroupsUpdate(Collections.singletonList(new PresentationFunctionGroupPutRequestBody() + .identifier(new PresentationIdentifier().idIdentifier("1")) + .functionGroup(new FunctionGroupUpdate() + .name("jobRole") + .description("jobRole") + .addPermissionsItem(new PresentationPermissionFunctionGroupUpdate() + .functionName("name1") + .addPrivilegesItem("view")) + .addPermissionsItem(new PresentationPermissionFunctionGroupUpdate() + .functionName("name2") + .addPrivilegesItem("view") + .addPrivilegesItem("edit"))) + )); } @Test