Skip to content

Commit

Permalink
[MOSIP-29600]Update PartnerManagementServiceImpl.java (#605)
Browse files Browse the repository at this point in the history
* [MOSIP-29600]Update PartnerManagementServiceImpl.java

Signed-off-by: Balaji Alluru <[email protected]>

* Update PartnerManagementServiceImplTest.java

* Update AuthPolicyRepository.java

* Update PolicyManagementService.java

* Update PolicyServiceTest.java

* Update PartnerServiceImplTest.java

* Update PartnerManagementServiceImplTest.java

* Update PartnerManagementServiceImpl.java

* Update PartnerServiceImpl.java

---------

Signed-off-by: Balaji Alluru <[email protected]>
  • Loading branch information
balaji-alluru authored Oct 12, 2023
1 parent 3106a98 commit 7a696df
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,18 @@ public APIKeyGenerateResponseDto generateAPIKey(String partnerId, APIKeyGenerate
throw new PartnerManagerServiceException(ErrorCode.LOGGEDIN_USER_NOT_AUTHORIZED.getErrorCode(),
ErrorCode.LOGGEDIN_USER_NOT_AUTHORIZED.getErrorMessage());
}
AuthPolicy validPolicy = authPolicyRepository.findByPolicyName(requestDto.getPolicyName());
Optional<Partner> partnerFromDb = partnerRepository.findById(partnerId);
if (partnerFromDb.isEmpty()) {
auditUtil.setAuditRequestDto(PartnerManageEnum.GENERATE_API_KEY_FAILURE, partnerId, "partnerId");
throw new PartnerManagerServiceException(ErrorCode.PARTNER_ID_DOES_NOT_EXIST_EXCEPTION.getErrorCode(),
ErrorCode.PARTNER_ID_DOES_NOT_EXIST_EXCEPTION.getErrorMessage());
}
if (!partnerFromDb.get().getIsActive()) {
auditUtil.setAuditRequestDto(PartnerManageEnum.GENERATE_API_KEY_FAILURE, partnerId, "partnerId");
throw new PartnerManagerServiceException(ErrorCode.PARTNER_NOT_ACTIVE_EXCEPTION.getErrorCode(),
ErrorCode.PARTNER_NOT_ACTIVE_EXCEPTION.getErrorMessage());
}
AuthPolicy validPolicy = authPolicyRepository.findByPolicyGroupIdAndName(partnerFromDb.get().getPolicyGroupId(),requestDto.getPolicyName());
if(validPolicy == null) {
auditUtil.setAuditRequestDto(PartnerManageEnum.GENERATE_API_KEY_FAILURE, partnerId, "partnerId");
throw new PartnerManagerServiceException(ErrorCode.POLICY_NOT_EXIST_EXCEPTION.getErrorCode(),
Expand All @@ -597,17 +608,6 @@ public APIKeyGenerateResponseDto generateAPIKey(String partnerId, APIKeyGenerate
throw new PartnerManagerServiceException(ErrorCode.PARTNER_POLICY_MAPPING_NOT_EXISTS.getErrorCode(),
ErrorCode.PARTNER_POLICY_MAPPING_NOT_EXISTS.getErrorMessage());
}
Optional<Partner> partnerFromDb = partnerRepository.findById(partnerId);
if (partnerFromDb.isEmpty()) {
auditUtil.setAuditRequestDto(PartnerManageEnum.GENERATE_API_KEY_FAILURE, partnerId, "partnerId");
throw new PartnerManagerServiceException(ErrorCode.PARTNER_ID_DOES_NOT_EXIST_EXCEPTION.getErrorCode(),
ErrorCode.PARTNER_ID_DOES_NOT_EXIST_EXCEPTION.getErrorMessage());
}
if (!partnerFromDb.get().getIsActive()) {
auditUtil.setAuditRequestDto(PartnerManageEnum.GENERATE_API_KEY_FAILURE, partnerId, "partnerId");
throw new PartnerManagerServiceException(ErrorCode.PARTNER_NOT_ACTIVE_EXCEPTION.getErrorCode(),
ErrorCode.PARTNER_NOT_ACTIVE_EXCEPTION.getErrorMessage());
}
PartnerPolicy policyByLabel = partnerPolicyRepository.findByPartnerIdPolicyIdAndLabel(
partnerFromDb.get().getId(), validPolicy.getId(), requestDto.getLabel());
if(policyByLabel != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ public PartnerResponse updatePartnerDetails(PartnerUpdateDto partnerUpdateReques
}

private AuthPolicy validatePolicyGroupAndPolicy(String policyGroupId, String policyName) {
AuthPolicy authPolicyFromDb = authPolicyRepository.findByPolicyGroupAndName(policyGroupId, policyName);
AuthPolicy authPolicyFromDb = authPolicyRepository.findByPolicyGroupIdAndName(policyGroupId, policyName);
if (authPolicyFromDb == null) {
auditUtil.setAuditRequestDto(PartnerServiceAuditEnum.SUBMIT_API_REQUEST_FAILURE, policyName, "policyName");
throw new PartnerServiceException(ErrorCode.POLICY_GROUP_POLICY_NOT_EXISTS.getErrorCode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,10 +948,11 @@ public void approveRejectPartnerPolicyMappingTest05() {
public void generateAPIKeyTest() {
APIKeyGenerateRequestDto request = new APIKeyGenerateRequestDto();
request.setLabel("unique");
request.setPolicyName("policyName");
Mockito.when(authPolicyRepository.findByPolicyName(request.getPolicyName())).thenReturn(getAuthPolicies().get(0));
request.setPolicyName("policyName");
Optional<Partner> newPartner = Optional.of(getPartner());
Mockito.when(partnerRepository.findById("partner")).thenReturn(newPartner);
Mockito.when(authPolicyRepository.findByPolicyGroupIdAndName(newPartner.get().getPolicyGroupId(),request.getPolicyName())).thenReturn(getAuthPolicies().get(0));
Mockito.when((partnerPolicyRequestRepository.findByPartnerIdAndPolicyIdAndStatusCode(Mockito.any(),Mockito.any(),Mockito.any()))).thenReturn(List.of(getPartnerPolicyRequestData()));
Mockito.when(partnerRepository.findById("partner")).thenReturn(Optional.of(getPartner()));
Mockito.when(partnerPolicyRepository.findByPartnerIdPolicyIdAndLabel("partner","234","unique")).thenReturn(getPartnerPolicy());
Map<String, Object> response = new HashMap<>();
response.put("response", getCertResponse());
Expand All @@ -964,39 +965,36 @@ public void generateAPIKeyTest() {
}catch (PartnerManagerServiceException e) {
assertTrue(e.getErrorCode().equals(ErrorCode.LOGGEDIN_USER_NOT_AUTHORIZED.getErrorCode()));
}

Mockito.when(authPolicyRepository.findByPolicyName(request.getPolicyName())).thenReturn(null);
Mockito.when(partnerRepository.findById("partner")).thenReturn(Optional.empty());
try {
partnerManagementImpl.generateAPIKey("partner", request);
}catch (PartnerManagerServiceException e) {
assertTrue(e.getErrorCode().equals(ErrorCode.POLICY_NOT_EXIST_EXCEPTION.getErrorCode()));
assertTrue(e.getErrorCode().equals(ErrorCode.PARTNER_ID_DOES_NOT_EXIST_EXCEPTION.getErrorCode()));
}
Mockito.when(authPolicyRepository.findByPolicyName(request.getPolicyName())).thenReturn(getAuthPolicies().get(0));
Mockito.when((partnerPolicyRequestRepository.findByPartnerIdAndPolicyIdAndStatusCode(Mockito.any(),Mockito.any(),Mockito.any()))).thenReturn(Collections.emptyList());
Optional<Partner> partner = Optional.of(getPartner());
partner.get().setIsActive(false);
Mockito.when(partnerRepository.findById("partner")).thenReturn(partner);
try {
partnerManagementImpl.generateAPIKey("partner", request);
}catch (PartnerManagerServiceException e) {
assertTrue(e.getErrorCode().equals(ErrorCode.PARTNER_POLICY_MAPPING_NOT_EXISTS.getErrorCode()));
assertTrue(e.getErrorCode().equals(ErrorCode.PARTNER_NOT_ACTIVE_EXCEPTION.getErrorCode()));
}
Mockito.when(authPolicyRepository.findByPolicyName(request.getPolicyName())).thenReturn(getAuthPolicies().get(0));
Mockito.when((partnerPolicyRequestRepository.findByPartnerIdAndPolicyIdAndStatusCode(Mockito.any(),Mockito.any(),Mockito.any()))).thenReturn(List.of(getPartnerPolicyRequestData()));
Mockito.when(partnerRepository.findById("partner")).thenReturn(Optional.empty());
Mockito.when(partnerRepository.findById("partner")).thenReturn(newPartner);
Mockito.when(authPolicyRepository.findByPolicyGroupIdAndName(request.getPolicyName(),newPartner.get().getPolicyGroupId())).thenReturn(null);
try {
partnerManagementImpl.generateAPIKey("partner", request);
}catch (PartnerManagerServiceException e) {
assertTrue(e.getErrorCode().equals(ErrorCode.PARTNER_ID_DOES_NOT_EXIST_EXCEPTION.getErrorCode()));
}
Mockito.when(authPolicyRepository.findByPolicyName(request.getPolicyName())).thenReturn(getAuthPolicies().get(0));
Mockito.when((partnerPolicyRequestRepository.findByPartnerIdAndPolicyIdAndStatusCode(Mockito.any(),Mockito.any(),Mockito.any()))).thenReturn(List.of(getPartnerPolicyRequestData()));
Optional<Partner> partner = Optional.of(getPartner());
partner.get().setIsActive(false);
Mockito.when(partnerRepository.findById("partner")).thenReturn(partner);
assertTrue(e.getErrorCode().equals(ErrorCode.POLICY_NOT_EXIST_EXCEPTION.getErrorCode()));
}
Mockito.when(partnerRepository.findById("partner")).thenReturn(newPartner);
Mockito.when(authPolicyRepository.findByPolicyGroupIdAndName(newPartner.get().getPolicyGroupId(),request.getPolicyName())).thenReturn(getAuthPolicies().get(0));
Mockito.when((partnerPolicyRequestRepository.findByPartnerIdAndPolicyIdAndStatusCode(Mockito.any(),Mockito.any(),Mockito.any()))).thenReturn(Collections.emptyList());
try {
partnerManagementImpl.generateAPIKey("partner", request);
}catch (PartnerManagerServiceException e) {
assertTrue(e.getErrorCode().equals(ErrorCode.PARTNER_NOT_ACTIVE_EXCEPTION.getErrorCode()));
assertTrue(e.getErrorCode().equals(ErrorCode.PARTNER_POLICY_MAPPING_NOT_EXISTS.getErrorCode()));
}
Mockito.when(authPolicyRepository.findByPolicyName(request.getPolicyName())).thenReturn(getAuthPolicies().get(0));
Mockito.when(authPolicyRepository.findByPolicyGroupIdAndName(request.getPolicyName(),newPartner.get().getPolicyGroupId())).thenReturn(getAuthPolicies().get(0));
Mockito.when((partnerPolicyRequestRepository.findByPartnerIdAndPolicyIdAndStatusCode(Mockito.any(),Mockito.any(),Mockito.any()))).thenReturn(List.of(getPartnerPolicyRequestData()));
Mockito.when(partnerRepository.findById("partner")).thenReturn(Optional.of(getPartner()));
Mockito.when(partnerPolicyRepository.findByPartnerIdPolicyIdAndLabel("123456","234","unique")).thenReturn(getPartnerPolicy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ public void mapPartnerPolicyCredentialType_001() {
Mockito.when(partnerRepository.findById("12345")).thenReturn(partner);
Mockito.when(authPolicyRepository.findById("12345")).thenReturn(Optional.of(createAuthPolicy()));
Mockito.when(authPolicyRepository
.findByPolicyGroupAndName(Mockito.anyString(), Mockito.anyString())).thenReturn(createAuthPolicy());
.findByPolicyGroupIdAndName(Mockito.anyString(), Mockito.anyString())).thenReturn(createAuthPolicy());
pserviceImpl.mapPartnerPolicyCredentialType("euin", "12345", "12345");
}

Expand Down Expand Up @@ -879,7 +879,7 @@ public void requestForPolicyMappingTest() {
Optional<Partner> partner = Optional.of(createPartner(true));
partner.get().setPartnerTypeCode("Auth_Partner");
Mockito.when(partnerRepository.findById("12345")).thenReturn(partner);
Mockito.when(authPolicyRepository.findByPolicyGroupAndName("12345","policyName")).thenReturn(createAuthPolicy());
Mockito.when(authPolicyRepository.findByPolicyGroupIdAndName("12345","policyName")).thenReturn(createAuthPolicy());
Mockito.when(partnerPolicyRequestRepository.findByPartnerIdAndPolicyId("12345","123")).thenReturn(List.of(createPartnerPolicyRequest("approved")));
pserviceImpl.requestForPolicyMapping(request, "12345");
partner.get().setPolicyGroupId(null);
Expand Down Expand Up @@ -921,15 +921,15 @@ public void requestForPolicyMappingTest() {
}
partner.get().setIsActive(true);
Mockito.when(partnerRepository.findById("12345")).thenReturn(partner);
Mockito.when(authPolicyRepository.findByPolicyGroupAndName("12345","policyName")).thenReturn(null);
Mockito.when(authPolicyRepository.findByPolicyGroupIdAndName("12345","policyName")).thenReturn(null);
try {
pserviceImpl.requestForPolicyMapping(request, "12345");
}catch (PartnerServiceException e) {
assertTrue(e.getErrorCode().equals(ErrorCode.POLICY_GROUP_POLICY_NOT_EXISTS.getErrorCode()));
}
AuthPolicy authPolicy = createAuthPolicy();
authPolicy.setIsActive(false);
Mockito.when(authPolicyRepository.findByPolicyGroupAndName("12345","policyName")).thenReturn(authPolicy);
Mockito.when(authPolicyRepository.findByPolicyGroupIdAndName("12345","policyName")).thenReturn(authPolicy);
try {
pserviceImpl.requestForPolicyMapping(request, "12345");
}catch (PartnerServiceException e) {
Expand All @@ -938,7 +938,7 @@ public void requestForPolicyMappingTest() {
authPolicy = createAuthPolicy();
authPolicy.setIsActive(true);
authPolicy.setValidToDate(LocalDateTime.now().minusMonths(3));
Mockito.when(authPolicyRepository.findByPolicyGroupAndName("12345","policyName")).thenReturn(authPolicy);
Mockito.when(authPolicyRepository.findByPolicyGroupIdAndName("12345","policyName")).thenReturn(authPolicy);
try {
pserviceImpl.requestForPolicyMapping(request, "12345");
}catch (PartnerServiceException e) {
Expand All @@ -947,7 +947,7 @@ public void requestForPolicyMappingTest() {
authPolicy = createAuthPolicy();
authPolicy.setIsActive(true);
authPolicy.getPolicyGroup().setIsActive(false);
Mockito.when(authPolicyRepository.findByPolicyGroupAndName("12345","policyName")).thenReturn(authPolicy);
Mockito.when(authPolicyRepository.findByPolicyGroupIdAndName("12345","policyName")).thenReturn(authPolicy);
try {
pserviceImpl.requestForPolicyMapping(request, "12345");
}catch (PartnerServiceException e) {
Expand Down Expand Up @@ -1264,4 +1264,4 @@ private String caCertResponse() {
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public interface AuthPolicyRepository extends JpaRepository<AuthPolicy, String>{

@Query(value = "select * from auth_policy ap where ap.policy_group_id=?1 and ap.name = ?2",nativeQuery = true)
AuthPolicy findByPolicyGroupAndName(String policyGroupId, String name);
AuthPolicy findByPolicyGroupIdAndName(String policyGroupId, String name);

@Query(value = "select * from auth_policy ap where ap.policy_group_id=?1 and ap.id = ?2",nativeQuery = true)
AuthPolicy findByPolicyGroupAndId(String policyGroupId, String policyId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public PolicyCreateResponseDto updatePolicies(PolicyUpdateRequestDto requestDto,
throw new PolicyManagementServiceException(ErrorMessages.PUBLISHED_POLICY_NOT_UPDATED.getErrorCode(),
ErrorMessages.PUBLISHED_POLICY_NOT_UPDATED.getErrorMessage());
}
AuthPolicy mappedPolicy = authPolicyRepository.findByPolicyGroupAndName(policyGroup.getId(),
AuthPolicy mappedPolicy = authPolicyRepository.findByPolicyGroupIdAndName(policyGroup.getId(),
requestDto.getName());
if (mappedPolicy != null && !mappedPolicy.getName().equals(authPolicy.getName())) {
auditUtil.setAuditRequestDto(PolicyManageEnum.UPDATE_POLICY_FAILURE, requestDto.getName(), "policyName");
Expand Down Expand Up @@ -354,7 +354,7 @@ private PolicyGroupCreateResponseDto savePolicyGroup(PolicyGroup policyGroup) {
* @return
*/
private void validateAuthPolicyName(String policyGroupId, String auth_policy_name) {
AuthPolicy auth_policy_by_name = authPolicyRepository.findByPolicyGroupAndName(policyGroupId, auth_policy_name);
AuthPolicy auth_policy_by_name = authPolicyRepository.findByPolicyGroupIdAndName(policyGroupId, auth_policy_name);
if (auth_policy_by_name != null) {
auditUtil.setAuditRequestDto(PolicyManageEnum.CREATE_POLICY_FAILURE, auth_policy_name, "policyName");
throw new PolicyManagementServiceException(
Expand Down Expand Up @@ -445,7 +445,7 @@ private PolicyResponseDto mapPolicyAndPolicyGroup(AuthPolicy authPolicy)
private PolicyCreateResponseDto savePolicy(JSONObject policyJson, String oldPolicyName, String newPolicyName,
String policyDesc, String policyGroupId, String policyType, String policyGroupName, String version,
String authPolicyId) throws PolicyManagementServiceException, Exception {
AuthPolicy authPolicy = authPolicyRepository.findByPolicyGroupAndName(policyGroupId, oldPolicyName);
AuthPolicy authPolicy = authPolicyRepository.findByPolicyGroupIdAndName(policyGroupId, oldPolicyName);
if (authPolicy != null) {
authPolicy.setId(authPolicy.getId());
authPolicy.setDescr(policyDesc);
Expand Down
Loading

0 comments on commit 7a696df

Please sign in to comment.