Skip to content

Commit

Permalink
[ES-1689] added test case
Browse files Browse the repository at this point in the history
Signed-off-by: Venkata Saidurga Polamraju <[email protected]>
  • Loading branch information
pvsaidurga committed Oct 2, 2024
1 parent 439c7b6 commit 3640c04
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -160,4 +161,18 @@ public void addVerifiedClaim_withInvalidClaim_returnErrorResponse() throws Exce
.andExpect(jsonPath("$.errors[0].errorCode").value(ErrorConstants.INVALID_REQUEST));
}

@Test
public void updateIdentity_withValidIdentity_thenPass() throws Exception {
RequestWrapper<IdentityData> requestWrapper = new RequestWrapper<IdentityData>();
ZonedDateTime requestTime = ZonedDateTime.now(ZoneOffset.UTC);
requestWrapper.setRequestTime(requestTime.format(DateTimeFormatter.ofPattern(UTC_DATETIME_PATTERN)));
requestWrapper.setRequest(identityRequest);

Mockito.doNothing().when(identityService).updateIdentity(identityRequest);

mockMvc.perform(put("/identity").content(objectMapper.writeValueAsString(requestWrapper))
.contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andExpect(jsonPath("$.response.status").value("mock Identity data updated successfully"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,56 @@ public void kycExchange_withValidDetails_thenPass() throws MockIdentityException
Assert.assertEquals("signedData", response.getKyc());
}

@Test
public void kycExchange_withInValidJwe_thenFail() throws MockIdentityException, JsonProcessingException {
ReflectionTestUtils.setField(authenticationService,"transactionTimeoutInSecs",60);
ReflectionTestUtils.setField(authenticationService,"encryptKyc",true);
ReflectionTestUtils.setField(authenticationService,"objectMapper",objectMapper);
String relyingPartyId = "relyingPartyId";
String clientId = "clientId";

KycExchangeRequestDto kycExchangeRequestDto=new KycExchangeRequestDto();
kycExchangeRequestDto.setKycToken("kycToken");
kycExchangeRequestDto.setIndividualId("individualId");
kycExchangeRequestDto.setTransactionId("transactionId");
kycExchangeRequestDto.setClaimLocales(Arrays.asList("en","fr"));
kycExchangeRequestDto.setAcceptedClaims(Arrays.asList("name","gender"));

kycExchangeRequestDto.setRequestDateTime(LocalDateTime.now());

KycAuth kycAuth=new KycAuth();
kycAuth.setResponseTime(LocalDateTime.now().minusSeconds(2));
kycAuth.setPartnerSpecificUserToken("token");

ObjectNode identityData = objectMapper.createObjectNode();
identityData.put("gender", "Male");

ArrayNode arrayNode = objectMapper.createArrayNode();
ObjectNode fullNameEng = objectMapper.createObjectNode();
fullNameEng.put("value", "Test");
fullNameEng.put("language", "eng");
ObjectNode fullNameFra = objectMapper.createObjectNode();
fullNameFra.put("value", "Test_fra");
fullNameFra.put("language", "fra");
arrayNode.add(fullNameEng);
arrayNode.add(fullNameFra);
identityData.put("fullName", arrayNode);
JWTSignatureResponseDto jwtSignatureResponseDto=new JWTSignatureResponseDto();
jwtSignatureResponseDto.setJwtSignedData("signedData");

Mockito.when(authRepository.findByKycTokenAndValidityAndTransactionIdAndIndividualId(
Mockito.anyString(), eq(Valid.ACTIVE), Mockito.anyString(), Mockito.anyString()))
.thenReturn(Optional.of(kycAuth));
Mockito.when(identityService.getIdentityV2(Mockito.anyString())).thenReturn(identityData);
Mockito.when(signatureService.jwtSign(Mockito.any())).thenReturn(jwtSignatureResponseDto);


MockIdentityException exception = Assert.assertThrows(MockIdentityException.class, () -> {
authenticationService.kycExchange(relyingPartyId, clientId, new KycExchangeDto(kycExchangeRequestDto,null));
});
Assert.assertEquals("mock-ida-008", exception.getMessage());
}

@Test
public void kycExchange_invalidToken_thenFail() {
String relyingPartyId = "relyingPartyId";
Expand Down Expand Up @@ -451,9 +501,12 @@ public void kycExchangeV2_withDetail_thenPass() {
verifiedClaim3.put("claims", claims3);

verifiedClaimsList.add(verifiedClaim3);
ObjectNode addressClaim = objectMapper.createObjectNode();
addressClaim.put("locality", NullNode.getInstance());

// Add the list of verified claims to the outer map
acceptedClaims.put("verified_claims", verifiedClaimsList);
acceptedClaims.put("address",addressClaim);

kycExchangeRequestDtoV2.setAcceptedClaimDetail(acceptedClaims);
kycExchangeRequestDtoV2.setClaimLocales(List.of("eng"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class IdentityServiceTest {
Expand Down Expand Up @@ -115,11 +114,25 @@ public void addIdentity_withValidDetails_thenPass() throws MockIdentityException
IdentityData identityData = new IdentityData();
identityData.setEmail("[email protected]");
identityData.setEncodedPhoto("encodedPhoto");
identityData.setPassword("password");
when(identityRepository.findById(identityData.getIndividualId())).thenReturn(Optional.empty());
identityService.addIdentity(identityData);
verify(identityRepository).save(any(MockIdentity.class));
}

@Test
public void addIdentity_withDuplicateDetails_thenFail() throws MockIdentityException {
IdentityData identityData = new IdentityData();
identityData.setEmail("[email protected]");
identityData.setEncodedPhoto("encodedPhoto");
when(identityRepository.findById(identityData.getIndividualId())).thenReturn(Optional.of(new MockIdentity()));
try{
identityService.addIdentity(identityData);
}catch (MockIdentityException e){
Assert.assertEquals(ErrorConstants.DUPLICATE_INDIVIDUAL_ID,e.getErrorCode());
}
}

@Test
public void getIdentity_withValidDetails_thenPass() throws MockIdentityException, JsonProcessingException {
IdentityData identityData = new IdentityData();
Expand All @@ -145,4 +158,30 @@ public void getIdentity_withInvalidId_thenFail() {
});
assertEquals(ErrorConstants.INVALID_INDIVIDUAL_ID, exception.getMessage());
}

@Test
public void updateIdentity_withExistingIndividualId_thenPass() {
IdentityData identityData = new IdentityData();
identityData.setIndividualId("existing-id");
identityData.setPassword("new-password");
MockIdentity mockIdentity = new MockIdentity();
mockIdentity.setIndividualId("existing-id");
mockIdentity.setIdentityJson("{\"existingField\": \"value\"}");
when(identityRepository.findById("existing-id")).thenReturn(Optional.of(mockIdentity));
identityService.updateIdentity(identityData);
verify(identityRepository, times(1)).save(mockIdentity);
Assert.assertNotNull(mockIdentity.getIdentityJson());
}

@Test
public void updateIdentity_withNonExistingIndividualId_thenFail() {
IdentityData identityData = new IdentityData();
identityData.setIndividualId("non-existing-id");
when(identityRepository.findById("non-existing-id")).thenReturn(Optional.empty());
MockIdentityException exception = assertThrows(MockIdentityException.class, () -> {
identityService.updateIdentity(identityData);
});
assertEquals(ErrorConstants.INVALID_INDIVIDUAL_ID, exception.getErrorCode());
}

}

0 comments on commit 3640c04

Please sign in to comment.