diff --git a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/constant/ErrorCode.java b/partner/partner-management-service/src/main/java/io/mosip/pms/partner/constant/ErrorCode.java index 5c56f0aadb..c6b2b973f6 100644 --- a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/constant/ErrorCode.java +++ b/partner/partner-management-service/src/main/java/io/mosip/pms/partner/constant/ErrorCode.java @@ -146,7 +146,11 @@ public enum ErrorCode { DOWNLOAD_CERTIFICATE_FTM_DEACTIVATED_ERROR("PMS_FTM_ERROR_009", "Cannot download the certificate for a deactivated FTM"), UNABLE_TO_DOWNLOAD_ORIGINAL_FTM_CERTIFICATE("PMS_FTM_ERROR_009", "Unable to download original FTM certificate"), FTM_NOT_APPROVED("PMS_FTM_ERROR_010", "The selected FTM is not in an approved status."), - PARTNERS_FETCH_ERROR("PMS_PS_ERROR_001", "Error while fetching partners list"); + PARTNERS_FETCH_ERROR("PMS_PS_ERROR_001", "Error while fetching partners list"), + PARTNER_DOES_NOT_EXIST_ERROR("PMS_PS_ERROR_002", "Partner does not exists"), + PARTNER_POLICIES_FETCH_ERROR("PMS_PS_ERROR_003", "Error while fetching partner policies"), + PARTNER_NOT_APPROVED_ERROR("PMS_PS_ERROR_004", "The Given partner is not approved"), + NOT_AUTH_PARTNER_TYPE_ERROR("PMS_PS_ERROR_004", "The specified partner is not of type Authentication Partner");; /** * The error code. */ diff --git a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/controller/MultiPartnerServiceController.java b/partner/partner-management-service/src/main/java/io/mosip/pms/partner/controller/MultiPartnerServiceController.java deleted file mode 100644 index f64e53675c..0000000000 --- a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/controller/MultiPartnerServiceController.java +++ /dev/null @@ -1,39 +0,0 @@ -package io.mosip.pms.partner.controller; - -import io.mosip.pms.common.response.dto.ResponseWrapperV2; -import io.mosip.pms.partner.dto.*; -import io.mosip.pms.partner.service.MultiPartnerService; -import io.mosip.pms.partner.util.RequestValidator; -import io.swagger.annotations.Api; -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.media.Content; -import io.swagger.v3.oas.annotations.media.Schema; -import io.swagger.v3.oas.annotations.responses.ApiResponse; -import io.swagger.v3.oas.annotations.responses.ApiResponses; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.web.bind.annotation.*; - -import java.util.*; - -@RestController -@RequestMapping(value = "/partners") -@Api(tags = {"Multi Partner Service Controller"}) -public class MultiPartnerServiceController { - - @Autowired - MultiPartnerService multiPartnerService; - - @Autowired - RequestValidator requestValidator; - - @PreAuthorize("hasAnyRole(@authorizedRoles.getGetauthpartnerspolicies())") - @GetMapping(value = "/auth-partners-policies") - @Operation(summary = "Get all approved auth partner policies", description = "fetch all approved auth partner policies") - @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), - @ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(schema = @Schema(hidden = true))), - @ApiResponse(responseCode = "403", description = "Forbidden", content = @Content(schema = @Schema(hidden = true)))}) - public ResponseWrapperV2> getAuthPartnersPolicies() { - return multiPartnerService.getAuthPartnersPolicies(); - } -} \ No newline at end of file diff --git a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/controller/PartnerServiceController.java b/partner/partner-management-service/src/main/java/io/mosip/pms/partner/controller/PartnerServiceController.java index 6a5908b021..63c9d09a5d 100644 --- a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/controller/PartnerServiceController.java +++ b/partner/partner-management-service/src/main/java/io/mosip/pms/partner/controller/PartnerServiceController.java @@ -8,11 +8,7 @@ import javax.validation.constraints.NotNull; import io.mosip.pms.common.response.dto.ResponseWrapperV2; -import io.mosip.pms.partner.dto.ApiKeyResponseDto; -import io.mosip.pms.partner.dto.CertificateDto; -import io.mosip.pms.partner.dto.PolicyDto; -import io.mosip.pms.partner.dto.PartnerDtoV4; -import io.mosip.pms.partner.dto.PartnerPolicyMappingResponseDto; +import io.mosip.pms.partner.dto.*; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; @@ -415,6 +411,18 @@ public ResponseWrapperV2> getAuthPartnerApiKeys() { return partnerService.getAuthPartnerApiKeys(); } + @PreAuthorize("hasAnyRole(@authorizedRoles.getGetauthpartnerspolicies())") + @GetMapping(value = "/{partnerId}/policies") + @Operation(summary = "Get all approved auth partner policies", description = "Fetch all approved auth partner policies") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "OK"), + @ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(schema = @Schema(hidden = true))), + @ApiResponse(responseCode = "403", description = "Forbidden", content = @Content(schema = @Schema(hidden = true))) + }) + public ResponseWrapperV2> getAuthPartnersPolicies(@PathVariable("partnerId") String partnerId) { + return partnerService.getAuthPartnersPolicies(partnerId); + } + @ResponseFilter @PostMapping("/search") @PreAuthorize("hasAnyRole(@authorizedRoles.getPostpartnerssearch())") diff --git a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/dto/ApprovedPolicyDto.java b/partner/partner-management-service/src/main/java/io/mosip/pms/partner/dto/ApprovedPolicyDto.java deleted file mode 100644 index e9ebe78f03..0000000000 --- a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/dto/ApprovedPolicyDto.java +++ /dev/null @@ -1,28 +0,0 @@ -package io.mosip.pms.partner.dto; - -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; -import lombok.Getter; -import lombok.Setter; - -import java.util.List; - -@Data -@Getter -@Setter -public class ApprovedPolicyDto { - @Schema(description = "Unique identifier for the partner", example = "partner123") - private String partnerId; - - @Schema(description = "Unique identifier for the policy group", example = "policyGroup456") - private String policyGroupId; - - @Schema(description = "Description of the policy group", example = "Group of policies related to compliance and regulations") - private String policyGroupDescription; - - @Schema(description = "Name of the policy group", example = "Compliance Policies") - private String policyGroupName; - - @Schema(description = "List of active policies associated with the partner") - private List activePolicies; -} \ No newline at end of file diff --git a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/service/MultiPartnerService.java b/partner/partner-management-service/src/main/java/io/mosip/pms/partner/service/MultiPartnerService.java deleted file mode 100644 index 808762d6ae..0000000000 --- a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/service/MultiPartnerService.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.mosip.pms.partner.service; - -import io.mosip.pms.common.response.dto.ResponseWrapperV2; -import io.mosip.pms.partner.dto.*; - -import java.util.List; - -public interface MultiPartnerService { - - public ResponseWrapperV2> getAuthPartnersPolicies(); - -} diff --git a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/service/PartnerService.java b/partner/partner-management-service/src/main/java/io/mosip/pms/partner/service/PartnerService.java index 163a96b4a9..5a52ffd794 100644 --- a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/service/PartnerService.java +++ b/partner/partner-management-service/src/main/java/io/mosip/pms/partner/service/PartnerService.java @@ -239,4 +239,6 @@ public interface PartnerService { public ResponseWrapperV2> getAuthPartnerApiKeys(); ResponseWrapperV2> getPartnersV4(String status, Boolean policyGroupAvailable, String partnerType); + + ResponseWrapperV2> getAuthPartnersPolicies(String PartnerId); } diff --git a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/service/impl/MultiPartnerServiceImpl.java b/partner/partner-management-service/src/main/java/io/mosip/pms/partner/service/impl/MultiPartnerServiceImpl.java deleted file mode 100644 index d69ca0e1d7..0000000000 --- a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/service/impl/MultiPartnerServiceImpl.java +++ /dev/null @@ -1,119 +0,0 @@ -package io.mosip.pms.partner.service.impl; - -import io.mosip.kernel.core.authmanager.authadapter.model.AuthUserDetails; -import io.mosip.kernel.core.logger.spi.Logger; -import io.mosip.pms.common.entity.*; -import io.mosip.pms.common.repository.AuthPolicyRepository; -import io.mosip.pms.common.repository.PartnerServiceRepository; -import io.mosip.pms.common.response.dto.ResponseWrapperV2; -import io.mosip.pms.common.util.PMSLogger; -import io.mosip.pms.partner.constant.ErrorCode; -import io.mosip.pms.partner.dto.*; -import io.mosip.pms.partner.exception.PartnerServiceException; -import io.mosip.pms.partner.service.MultiPartnerService; -import io.mosip.pms.partner.util.PartnerHelper; -import io.mosip.pms.partner.util.MultiPartnerUtil; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.stereotype.Service; - -import java.util.*; - -@Service -public class MultiPartnerServiceImpl implements MultiPartnerService { - - private static final Logger LOGGER = PMSLogger.getLogger(MultiPartnerServiceImpl.class); - public static final String APPROVED = "approved"; - public static final String ACTIVE = "ACTIVE"; - public static final String VERSION = "1.0"; - - @Value("${mosip.pms.api.id.auth.partners.policies.get}") - private String getAuthPartnersPoliciesId; - - @Autowired - PartnerServiceRepository partnerRepository; - - @Autowired - AuthPolicyRepository authPolicyRepository; - - @Autowired - PartnerHelper partnerHelper; - - @Override - public ResponseWrapperV2> getAuthPartnersPolicies() { - ResponseWrapperV2> responseWrapper = new ResponseWrapperV2<>(); - try { - String userId = getUserId(); - List partnerList = partnerRepository.findByUserId(userId); - if (!partnerList.isEmpty()) { - List approvedPolicyList = new ArrayList<>(); - for (Partner partner : partnerList) { - if (partnerHelper.checkIfPartnerIsApprovedAuthPartner(partner)) { - partnerHelper.validatePartnerId(partner, userId); - partnerHelper.validatePolicyGroupId(partner, userId); - PolicyGroup policyGroup = partnerHelper.validatePolicyGroup(partner); - ApprovedPolicyDto approvedPolicyDto = new ApprovedPolicyDto(); - approvedPolicyDto.setPartnerId(partner.getId()); - approvedPolicyDto.setPolicyGroupId(policyGroup.getId()); - approvedPolicyDto.setPolicyGroupDescription(policyGroup.getDesc()); - approvedPolicyDto.setPolicyGroupName(policyGroup.getName()); - List partnerPolicyRequestList = partner.getPartnerPolicyRequests(); - List activePolicyDtoList = new ArrayList<>(); - if (!partnerPolicyRequestList.isEmpty()) { - for (PartnerPolicyRequest partnerPolicyRequest : partnerPolicyRequestList) { - if (partnerPolicyRequest.getStatusCode().equals(APPROVED)) { - AuthPolicy policyDetails = authPolicyRepository.findActivePoliciesByPolicyGroupId(partner.getPolicyGroupId(), partnerPolicyRequest.getPolicyId()); - if (Objects.nonNull(policyDetails)) { - ActivePolicyDto activePolicyDto = new ActivePolicyDto(); - activePolicyDto.setPolicyId(policyDetails.getId()); - activePolicyDto.setPolicyDescription(policyDetails.getDescr()); - activePolicyDto.setPolicyName(policyDetails.getName()); - activePolicyDtoList.add(activePolicyDto); - } else { - LOGGER.info("No matching policy not found for policy group ID :" + partner.getPolicyGroupId() + "and Policy ID :" + partnerPolicyRequest.getPolicyId()); - throw new PartnerServiceException(ErrorCode.MATCHING_POLICY_NOT_FOUND.getErrorCode(), - ErrorCode.MATCHING_POLICY_NOT_FOUND.getErrorMessage()); - } - } - } - approvedPolicyDto.setActivePolicies(activePolicyDtoList); - approvedPolicyList.add(approvedPolicyDto); - } else { - approvedPolicyDto.setActivePolicies(activePolicyDtoList); - approvedPolicyList.add(approvedPolicyDto); - } - } - } - responseWrapper.setResponse(approvedPolicyList); - } else { - LOGGER.info("sessionId", "idType", "id", "User id does not exists."); - throw new PartnerServiceException(ErrorCode.USER_ID_NOT_EXISTS.getErrorCode(), - ErrorCode.USER_ID_NOT_EXISTS.getErrorMessage()); - } - } catch (PartnerServiceException ex) { - LOGGER.info("sessionId", "idType", "id", "In getAuthPartnersPolicies method of MultiPartnerServiceImpl - " + ex.getMessage()); - responseWrapper.setErrors(MultiPartnerUtil.setErrorResponse(ex.getErrorCode(), ex.getErrorText())); - } catch (Exception ex) { - LOGGER.debug("sessionId", "idType", "id", ex.getStackTrace()); - LOGGER.error("sessionId", "idType", "id", - "In getAuthPartnersPolicies method of MultiPartnerServiceImpl - " + ex.getMessage()); - String errorCode = ErrorCode.PARTNER_POLICY_FETCH_ERROR.getErrorCode(); - String errorMessage = ErrorCode.PARTNER_POLICY_FETCH_ERROR.getErrorMessage(); - responseWrapper.setErrors(MultiPartnerUtil.setErrorResponse(errorCode, errorMessage)); - } - responseWrapper.setId(getAuthPartnersPoliciesId); - responseWrapper.setVersion(VERSION); - return responseWrapper; - } - - private AuthUserDetails authUserDetails() { - return (AuthUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); - } - - private String getUserId() { - String userId = authUserDetails().getUserId(); - return userId; - } - -} diff --git a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/service/impl/PartnerServiceImpl.java b/partner/partner-management-service/src/main/java/io/mosip/pms/partner/service/impl/PartnerServiceImpl.java index 6f7b56e031..8ad2d71740 100644 --- a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/service/impl/PartnerServiceImpl.java +++ b/partner/partner-management-service/src/main/java/io/mosip/pms/partner/service/impl/PartnerServiceImpl.java @@ -261,6 +261,9 @@ public class PartnerServiceImpl implements PartnerService { @Value("${mosip.pms.api.id.partners.v4.get}") private String getPartnersV4Id; + @Value("${mosip.pms.api.id.auth.partners.policies.get}") + private String getAuthPartnersPoliciesId; + @Autowired AuditUtil auditUtil; @@ -1795,41 +1798,40 @@ public ResponseWrapperV2> getAuthPartnerApiKeys() { if (!partnerList.isEmpty()) { List apiKeyResponseDtoList = new ArrayList<>(); for (Partner partner : partnerList) { - if (partnerHelper.checkIfPartnerIsApprovedAuthPartner(partner)) { - partnerHelper.validatePartnerId(partner, userId); - partnerHelper.validatePolicyGroupId(partner, userId); - List apiKeyRequestsList = partnerPolicyRepository.findAPIKeysByPartnerId(partner.getId()); - if (!apiKeyRequestsList.isEmpty()) { - for (PartnerPolicy partnerPolicy : apiKeyRequestsList) { - Optional authPolicy = authPolicyRepository.findById(partnerPolicy.getPolicyId()); - if (!authPolicy.isPresent()) { - LOGGER.info("Policy does not exists."); - throw new PartnerServiceException(ErrorCode.POLICY_NOT_EXIST.getErrorCode(), - ErrorCode.POLICY_NOT_EXIST.getErrorMessage()); - } - PolicyGroup policyGroup = authPolicy.get().getPolicyGroup(); - if (Objects.isNull(policyGroup)) { - LOGGER.info("Policy Group is null or empty"); - throw new PartnerServiceException(ErrorCode.POLICY_GROUP_NOT_EXISTS.getErrorCode(), - ErrorCode.POLICY_GROUP_NOT_EXISTS.getErrorMessage()); - } - ApiKeyResponseDto apiKeyResponseDto = new ApiKeyResponseDto(); - apiKeyResponseDto.setApiKeyLabel(partnerPolicy.getLabel()); - if (partnerPolicy.getIsActive()) { - apiKeyResponseDto.setStatus(ACTIVE); - } else { - apiKeyResponseDto.setStatus(INACTIVE); - } - apiKeyResponseDto.setPartnerId(partner.getId()); - apiKeyResponseDto.setPolicyGroupId(policyGroup.getId()); - apiKeyResponseDto.setPolicyGroupName(policyGroup.getName()); - apiKeyResponseDto.setPolicyGroupDescription(policyGroup.getDesc()); - apiKeyResponseDto.setPolicyId(authPolicy.get().getId()); - apiKeyResponseDto.setPolicyName(authPolicy.get().getName()); - apiKeyResponseDto.setPolicyDescription(authPolicy.get().getDescr()); - apiKeyResponseDto.setCreatedDateTime(partnerPolicy.getCrDtimes().toLocalDateTime()); - apiKeyResponseDtoList.add(apiKeyResponseDto); + partnerHelper.validateIfPartnerIsApprovedAuthPartner(partner); + partnerHelper.validatePartnerId(partner, userId); + partnerHelper.validatePolicyGroupId(partner, userId); + List apiKeyRequestsList = partnerPolicyRepository.findAPIKeysByPartnerId(partner.getId()); + if (!apiKeyRequestsList.isEmpty()) { + for (PartnerPolicy partnerPolicy : apiKeyRequestsList) { + Optional authPolicy = authPolicyRepository.findById(partnerPolicy.getPolicyId()); + if (!authPolicy.isPresent()) { + LOGGER.info("Policy does not exists."); + throw new PartnerServiceException(ErrorCode.POLICY_NOT_EXIST.getErrorCode(), + ErrorCode.POLICY_NOT_EXIST.getErrorMessage()); + } + PolicyGroup policyGroup = authPolicy.get().getPolicyGroup(); + if (Objects.isNull(policyGroup)) { + LOGGER.info("Policy Group is null or empty"); + throw new PartnerServiceException(ErrorCode.POLICY_GROUP_NOT_EXISTS.getErrorCode(), + ErrorCode.POLICY_GROUP_NOT_EXISTS.getErrorMessage()); } + ApiKeyResponseDto apiKeyResponseDto = new ApiKeyResponseDto(); + apiKeyResponseDto.setApiKeyLabel(partnerPolicy.getLabel()); + if (partnerPolicy.getIsActive()) { + apiKeyResponseDto.setStatus(ACTIVE); + } else { + apiKeyResponseDto.setStatus(INACTIVE); + } + apiKeyResponseDto.setPartnerId(partner.getId()); + apiKeyResponseDto.setPolicyGroupId(policyGroup.getId()); + apiKeyResponseDto.setPolicyGroupName(policyGroup.getName()); + apiKeyResponseDto.setPolicyGroupDescription(policyGroup.getDesc()); + apiKeyResponseDto.setPolicyId(authPolicy.get().getId()); + apiKeyResponseDto.setPolicyName(authPolicy.get().getName()); + apiKeyResponseDto.setPolicyDescription(authPolicy.get().getDescr()); + apiKeyResponseDto.setCreatedDateTime(partnerPolicy.getCrDtimes().toLocalDateTime()); + apiKeyResponseDtoList.add(apiKeyResponseDto); } } } @@ -1898,6 +1900,79 @@ public ResponseWrapperV2> getPartnersV4(String status, Boolea return responseWrapper; } + @Override + public ResponseWrapperV2> getAuthPartnersPolicies(String partnerId) { + ResponseWrapperV2> responseWrapper = new ResponseWrapperV2<>(); + try { + String userId = getUserId(); + List partnerList = partnerRepository.findByUserId(userId); + if (partnerList.isEmpty()) { + LOGGER.info("sessionId", "idType", "id", "User id does not exists."); + throw new PartnerServiceException(ErrorCode.USER_ID_NOT_EXISTS.getErrorCode(), + ErrorCode.USER_ID_NOT_EXISTS.getErrorMessage()); + } + + //validate whether partner Id exists + Optional optionalPartner = partnerRepository.findById(partnerId); + if (optionalPartner.isEmpty()) { + LOGGER.info("sessionId", "idType", "id", "User id does not exists."); + throw new PartnerServiceException(ErrorCode.PARTNER_DOES_NOT_EXIST_ERROR.getErrorCode(), + ErrorCode.PARTNER_DOES_NOT_EXIST_ERROR.getErrorMessage()); + } + Partner partner = optionalPartner.get(); + //check if partner Id belongs to user + boolean partnerBelongsToUser = false; + for (Partner partnerData : partnerList) { + if (partnerData.getUserId().equals(partner.getUserId())) { + partnerBelongsToUser = true; + break; + } + } + if (!partnerBelongsToUser) { + LOGGER.info("sessionId", "idType", "id", "Partner is not associated with user."); + throw new PartnerServiceException(ErrorCode.PARTNER_ID_NOT_ASSOCIATED_WITH_USER.getErrorCode(), + ErrorCode.PARTNER_ID_NOT_ASSOCIATED_WITH_USER.getErrorMessage()); + } + partnerHelper.validateIfPartnerIsApprovedAuthPartner(partner); + partnerHelper.validatePolicyGroupId(partner, userId); + PolicyGroup policyGroup = partnerHelper.validatePolicyGroup(partner); + List partnerPolicyRequestList = partner.getPartnerPolicyRequests(); + List activePolicyDtoList = new ArrayList<>(); + if (!partnerPolicyRequestList.isEmpty()) { + for (PartnerPolicyRequest partnerPolicyRequest : partnerPolicyRequestList) { + if (partnerPolicyRequest.getStatusCode().equals(APPROVED)) { + AuthPolicy policyDetails = authPolicyRepository.findActivePoliciesByPolicyGroupId(policyGroup.getId(), partnerPolicyRequest.getPolicyId()); + if (Objects.nonNull(policyDetails)) { + ActivePolicyDto activePolicyDto = new ActivePolicyDto(); + activePolicyDto.setPolicyId(policyDetails.getId()); + activePolicyDto.setPolicyDescription(policyDetails.getDescr()); + activePolicyDto.setPolicyName(policyDetails.getName()); + activePolicyDtoList.add(activePolicyDto); + } else { + LOGGER.info("No matching policy not found for policy group ID :" + policyGroup.getId() + "and Policy ID :" + partnerPolicyRequest.getPolicyId()); + throw new PartnerServiceException(ErrorCode.MATCHING_POLICY_NOT_FOUND.getErrorCode(), + ErrorCode.MATCHING_POLICY_NOT_FOUND.getErrorMessage()); + } + } + } + } + responseWrapper.setResponse(activePolicyDtoList); + } catch (PartnerServiceException ex) { + LOGGER.info("sessionId", "idType", "id", "In getAuthPartnersPolicies method of PartnerServiceImpl - " + ex.getMessage()); + responseWrapper.setErrors(MultiPartnerUtil.setErrorResponse(ex.getErrorCode(), ex.getErrorText())); + } catch (Exception ex) { + LOGGER.debug("sessionId", "idType", "id", ex.getStackTrace()); + LOGGER.error("sessionId", "idType", "id", + "In getAuthPartnersPolicies method of PartnerServiceImpl - " + ex.getMessage()); + String errorCode = ErrorCode.PARTNER_POLICIES_FETCH_ERROR.getErrorCode(); + String errorMessage = ErrorCode.PARTNER_POLICIES_FETCH_ERROR.getErrorMessage(); + responseWrapper.setErrors(MultiPartnerUtil.setErrorResponse(errorCode, errorMessage)); + } + responseWrapper.setId(getAuthPartnersPoliciesId); + responseWrapper.setVersion(VERSION); + return responseWrapper; + } + /** * validates the loggedInUser authorization * @param loggedInUserId diff --git a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/util/PartnerHelper.java b/partner/partner-management-service/src/main/java/io/mosip/pms/partner/util/PartnerHelper.java index b511a5d902..6dc1dba51f 100644 --- a/partner/partner-management-service/src/main/java/io/mosip/pms/partner/util/PartnerHelper.java +++ b/partner/partner-management-service/src/main/java/io/mosip/pms/partner/util/PartnerHelper.java @@ -420,7 +420,7 @@ public void validatePartnerId(Partner partner, String userId) { } } - public boolean checkIfPartnerIsApprovedAuthPartner(Partner partner) { + public void validateIfPartnerIsApprovedAuthPartner(Partner partner) { String partnerType = partner.getPartnerTypeCode(); String approvalStatus = partner.getApprovalStatus(); if (Objects.isNull(partnerType) || partnerType.equals(BLANK_STRING)) { @@ -433,6 +433,15 @@ public boolean checkIfPartnerIsApprovedAuthPartner(Partner partner) { throw new PartnerServiceException(ErrorCode.APPROVAL_STATUS_NOT_EXISTS.getErrorCode(), ErrorCode.APPROVAL_STATUS_NOT_EXISTS.getErrorMessage()); } - return partnerType.equals(AUTH_PARTNER) && approvalStatus.equals(APPROVED); + if (!partnerType.equals(AUTH_PARTNER)) { + LOGGER.info("The specified partner is not of type Authentication Partner " + partner.getId()); + throw new PartnerServiceException(ErrorCode.NOT_AUTH_PARTNER_TYPE_ERROR.getErrorCode(), + ErrorCode.NOT_AUTH_PARTNER_TYPE_ERROR.getErrorMessage()); + } + if (!approvalStatus.equals(APPROVED)) { + LOGGER.info("The specified partner is not of type Authentication Partner " + partner.getId()); + throw new PartnerServiceException(ErrorCode.PARTNER_NOT_APPROVED_ERROR.getErrorCode(), + ErrorCode.PARTNER_NOT_APPROVED_ERROR.getErrorMessage()); + } } } diff --git a/partner/partner-management-service/src/test/java/io/mosip/pms/test/partner/controller/MultiPartnerServiceControllerTest.java b/partner/partner-management-service/src/test/java/io/mosip/pms/test/partner/controller/MultiPartnerServiceControllerTest.java deleted file mode 100644 index f3d9d1bf3c..0000000000 --- a/partner/partner-management-service/src/test/java/io/mosip/pms/test/partner/controller/MultiPartnerServiceControllerTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package io.mosip.pms.test.partner.controller; - -import io.mosip.pms.common.response.dto.ResponseWrapperV2; -import io.mosip.pms.partner.controller.MultiPartnerServiceController; -import io.mosip.pms.partner.dto.*; -import io.mosip.pms.partner.service.MultiPartnerService; -import io.mosip.pms.partner.util.RequestValidator; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestContext; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.web.context.WebApplicationContext; - -import java.util.ArrayList; -import java.util.List; - -@ContextConfiguration(classes = {TestContext.class, WebApplicationContext.class}) -@RunWith(SpringRunner.class) -@SpringBootTest(classes = {MultiPartnerServiceController.class}) -public class MultiPartnerServiceControllerTest { - - @Autowired - private MultiPartnerServiceController multiPartnerServiceController; - - @MockBean - private MultiPartnerService multiPartnerService; - - @MockBean - RequestValidator requestValidator; - - public static final String VERSION = "1.0"; - - @Test - @WithMockUser(roles = {"PARTNER"}) - public void getAuthPartnersPoliciesTest() throws Exception { - ResponseWrapperV2> responseWrapper = new ResponseWrapperV2<>(); - ApprovedPolicyDto policyDto = new ApprovedPolicyDto(); - policyDto.setPartnerId("abc"); - policyDto.setPolicyGroupName("123"); - List policyDtoList = new ArrayList<>(); - policyDtoList.add(policyDto); - responseWrapper.setResponse(policyDtoList); - Mockito.when(multiPartnerService.getAuthPartnersPolicies()).thenReturn(responseWrapper); - ResponseWrapperV2> response = multiPartnerServiceController.getAuthPartnersPolicies(); - } -} diff --git a/partner/partner-management-service/src/test/java/io/mosip/pms/test/partner/service/impl/MultiPartnerServiceImplTest.java b/partner/partner-management-service/src/test/java/io/mosip/pms/test/partner/service/impl/MultiPartnerServiceImplTest.java deleted file mode 100644 index 7add00eac4..0000000000 --- a/partner/partner-management-service/src/test/java/io/mosip/pms/test/partner/service/impl/MultiPartnerServiceImplTest.java +++ /dev/null @@ -1,207 +0,0 @@ -package io.mosip.pms.test.partner.service.impl; - -import io.mosip.kernel.openid.bridge.model.AuthUserDetails; -import io.mosip.pms.common.entity.*; -import io.mosip.pms.common.repository.*; -import io.mosip.pms.common.util.RestUtil; -import io.mosip.pms.device.authdevice.entity.FTPChipDetail; -import io.mosip.pms.device.authdevice.entity.SecureBiometricInterface; -import io.mosip.pms.device.authdevice.repository.FTPChipDetailRepository; -import io.mosip.pms.device.authdevice.repository.SecureBiometricInterfaceRepository; -import io.mosip.pms.partner.service.impl.MultiPartnerServiceImpl; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.core.env.Environment; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContext; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.test.context.junit4.SpringRunner; - -import java.sql.Timestamp; -import java.time.LocalDateTime; -import java.util.*; - -import static org.mockito.ArgumentMatchers.*; -import static org.mockito.Mockito.when; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class MultiPartnerServiceImplTest { - - @Autowired - MultiPartnerServiceImpl multiPartnerServiceImpl; - - @MockBean - RestUtil restUtil; - - @MockBean - PartnerServiceRepository partnerRepository; - - @MockBean - PolicyGroupRepository policyGroupRepository; - - @MockBean - AuthPolicyRepository authPolicyRepository; - - @MockBean - PartnerPolicyRepository partnerPolicyRepository; - - @MockBean - SecureBiometricInterfaceRepository secureBiometricInterfaceRepository; - - @MockBean - DeviceDetailSbiRepository deviceDetailSbiRepository; - - @MockBean - FTPChipDetailRepository ftpChipDetailRepository; - - @Mock - Authentication authentication; - @Mock - SecurityContext securityContext; - - @Test - public void getAuthPartnersPolicies() throws Exception { - io.mosip.kernel.openid.bridge.model.MosipUserDto mosipUserDto = getMosipUserDto(); - AuthUserDetails authUserDetails = new AuthUserDetails(mosipUserDto, "123"); - SecurityContextHolder.setContext(securityContext); - when(authentication.getPrincipal()).thenReturn(authUserDetails); - when(securityContext.getAuthentication()).thenReturn(authentication); - - List partnerList = new ArrayList<>(); - Partner partner = new Partner(); - partner.setId("123"); - partner.setPartnerTypeCode("Auth_Partner"); - partner.setPolicyGroupId("abc"); - partner.setApprovalStatus("approved"); - List partnerPolicyRequestList = new ArrayList<>(); - PartnerPolicyRequest partnerPolicyRequest = new PartnerPolicyRequest(); - partnerPolicyRequest.setPolicyId("xyz"); - partnerPolicyRequest.setCrDtimes(Timestamp.valueOf(LocalDateTime.now())); - partnerPolicyRequest.setStatusCode("approved"); - partnerPolicyRequestList.add(partnerPolicyRequest); - partner.setPartnerPolicyRequests(partnerPolicyRequestList); - partnerList.add(partner); - when(partnerRepository.findByUserId(anyString())).thenReturn(partnerList); - when(partnerRepository.findById(anyString())).thenReturn(Optional.of(partner)); - PolicyGroup policyGroup = new PolicyGroup(); - policyGroup.setId("abc"); - policyGroup.setName("group1"); - policyGroup.setDesc("dgvhsd"); - when(policyGroupRepository.findPolicyGroupById(anyString())).thenReturn(policyGroup); - - AuthPolicy authPolicy = new AuthPolicy(); - authPolicy.setName("policy123"); - when(authPolicyRepository.findActivePoliciesByPolicyGroupId(anyString(), anyString())).thenReturn(authPolicy); - multiPartnerServiceImpl.getAuthPartnersPolicies(); - - when(authPolicyRepository.findActivePoliciesByPolicyGroupId(anyString(), anyString())).thenReturn(null); - multiPartnerServiceImpl.getAuthPartnersPolicies(); - } - - @Test - public void getAuthPartnersPoliciesTest1() throws Exception { - io.mosip.kernel.openid.bridge.model.MosipUserDto mosipUserDto = getMosipUserDto(); - AuthUserDetails authUserDetails = new AuthUserDetails(mosipUserDto, "123"); - SecurityContextHolder.setContext(securityContext); - when(authentication.getPrincipal()).thenReturn(authUserDetails); - when(securityContext.getAuthentication()).thenReturn(authentication); - - List partnerList = new ArrayList<>(); - Partner partner = new Partner(); - partner.setId("123"); - partner.setPartnerTypeCode("Auth_Partner"); - partner.setPolicyGroupId("abc"); - partner.setApprovalStatus("approved"); - List partnerPolicyRequestList = new ArrayList<>(); - partner.setPartnerPolicyRequests(partnerPolicyRequestList); - partnerList.add(partner); - when(partnerRepository.findByUserId(anyString())).thenReturn(partnerList); - when(partnerRepository.findById(anyString())).thenReturn(Optional.of(partner)); - PolicyGroup policyGroup = new PolicyGroup(); - policyGroup.setId("abc"); - policyGroup.setName("group1"); - policyGroup.setDesc("dgvhsd"); - when(policyGroupRepository.findPolicyGroupById(anyString())).thenReturn(policyGroup); - - AuthPolicy authPolicy = new AuthPolicy(); - authPolicy.setName("policy123"); - when(authPolicyRepository.findActivePoliciesByPolicyGroupId(anyString(), anyString())).thenReturn(authPolicy); - multiPartnerServiceImpl.getAuthPartnersPolicies(); - } - - @Test - public void getAuthPartnersPoliciesTest2() throws Exception { - io.mosip.kernel.openid.bridge.model.MosipUserDto mosipUserDto = getMosipUserDto(); - AuthUserDetails authUserDetails = new AuthUserDetails(mosipUserDto, "123"); - SecurityContextHolder.setContext(securityContext); - when(authentication.getPrincipal()).thenReturn(authUserDetails); - when(securityContext.getAuthentication()).thenReturn(authentication); - - List partnerList = new ArrayList<>(); - Partner partner = new Partner(); - partner.setId("123"); - partner.setPartnerTypeCode(""); - partner.setPolicyGroupId("abc"); - partner.setApprovalStatus("approved"); - List partnerPolicyRequestList = new ArrayList<>(); - partner.setPartnerPolicyRequests(partnerPolicyRequestList); - partnerList.add(partner); - when(partnerRepository.findByUserId(anyString())).thenReturn(partnerList); - when(partnerRepository.findById(anyString())).thenReturn(Optional.of(partner)); - multiPartnerServiceImpl.getAuthPartnersPolicies(); - } - - @Test - public void getAuthPartnersPoliciesTest3() throws Exception { - io.mosip.kernel.openid.bridge.model.MosipUserDto mosipUserDto = getMosipUserDto(); - AuthUserDetails authUserDetails = new AuthUserDetails(mosipUserDto, "123"); - SecurityContextHolder.setContext(securityContext); - when(authentication.getPrincipal()).thenReturn(authUserDetails); - when(securityContext.getAuthentication()).thenReturn(authentication); - - List partnerList = new ArrayList<>(); - Partner partner = new Partner(); - partner.setId("123"); - partner.setPartnerTypeCode("Auth_Partner"); - partner.setPolicyGroupId("abc"); - partner.setApprovalStatus(""); - List partnerPolicyRequestList = new ArrayList<>(); - partner.setPartnerPolicyRequests(partnerPolicyRequestList); - partnerList.add(partner); - when(partnerRepository.findByUserId(anyString())).thenReturn(partnerList); - when(partnerRepository.findById(anyString())).thenReturn(Optional.of(partner)); - multiPartnerServiceImpl.getAuthPartnersPolicies(); - } - - @Test - public void getAuthPartnersPoliciesException() throws Exception { - multiPartnerServiceImpl.getAuthPartnersPolicies(); - } - - @Test - public void getAuthPartnersPoliciesException1() throws Exception { - io.mosip.kernel.openid.bridge.model.MosipUserDto mosipUserDto = getMosipUserDto(); - AuthUserDetails authUserDetails = new AuthUserDetails(mosipUserDto, "123"); - SecurityContextHolder.setContext(securityContext); - when(authentication.getPrincipal()).thenReturn(authUserDetails); - when(securityContext.getAuthentication()).thenReturn(authentication); - - List partnerList = new ArrayList<>(); - Partner partner = new Partner(); - when(partnerRepository.findByUserId(anyString())).thenReturn(partnerList); - when(partnerRepository.findById(anyString())).thenReturn(Optional.of(partner)); - multiPartnerServiceImpl.getAuthPartnersPolicies(); - } - - private io.mosip.kernel.openid.bridge.model.MosipUserDto getMosipUserDto() { - io.mosip.kernel.openid.bridge.model.MosipUserDto mosipUserDto = new io.mosip.kernel.openid.bridge.model.MosipUserDto(); - mosipUserDto.setUserId("123"); - mosipUserDto.setMail("abc@gmail.com"); - return mosipUserDto; - } -} diff --git a/partner/pms-common/src/main/java/io/mosip/pms/common/repository/PartnerServiceRepository.java b/partner/pms-common/src/main/java/io/mosip/pms/common/repository/PartnerServiceRepository.java index e96d7c31e1..b76aab248e 100644 --- a/partner/pms-common/src/main/java/io/mosip/pms/common/repository/PartnerServiceRepository.java +++ b/partner/pms-common/src/main/java/io/mosip/pms/common/repository/PartnerServiceRepository.java @@ -39,7 +39,7 @@ public interface PartnerServiceRepository extends JpaRepository public List findByUserId(String userId); @Query("SELECT p FROM Partner p " + - "WHERE p.id = :userId " + + "WHERE p.userId = :userId " + "AND (p.approvalStatus = :status) " + "AND (:partnerType IS NULL OR p.partnerTypeCode = :partnerType) " + "AND ((:policyGroupAvailable IS NULL) " +