Skip to content

Commit

Permalink
MOSIP-30547-improve-sonar-coverage-in-resident-service (#1177)
Browse files Browse the repository at this point in the history
* fixed additional comma issue

Signed-off-by: Ritik Jain <[email protected]>

* fixed error code

Signed-off-by: Ritik Jain <[email protected]>

* updated event enums

Signed-off-by: Ritik Jain <[email protected]>

* updated event enums

Signed-off-by: Ritik Jain <[email protected]>

* updated event enums

Signed-off-by: Ritik Jain <[email protected]>

* updated event enums

Signed-off-by: Ritik Jain <[email protected]>

* removed un necessary file

Signed-off-by: Ritik Jain <[email protected]>

* added async call for audits

Signed-off-by: Ritik Jain <[email protected]>

* updated method names

Signed-off-by: Ritik Jain <[email protected]>

* updated variable names

Signed-off-by: Ritik Jain <[email protected]>

* updated variable names

Signed-off-by: Ritik Jain <[email protected]>

* added default value

Signed-off-by: Ritik Jain <[email protected]>

* added separate value

Signed-off-by: Ritik Jain <[email protected]>

* updated property key

Signed-off-by: Ritik Jain <[email protected]>

* deleted one enum

Signed-off-by: Ritik Jain <[email protected]>

* removed refid and type from event enum

Signed-off-by: Ritik Jain <[email protected]>

* updated event type in event enum

Signed-off-by: Ritik Jain <[email protected]>

* removed generic type

Signed-off-by: Ritik Jain <[email protected]>

* removed generic type

Signed-off-by: Ritik Jain <[email protected]>

* updated constant

Signed-off-by: Ritik Jain <[email protected]>

* called method for hashid

Signed-off-by: Ritik Jain <[email protected]>

* removed unused import

Signed-off-by: Ritik Jain <[email protected]>

* fixed aid issue and test cases

Signed-off-by: Ritik Jain <[email protected]>

* changed method name

Signed-off-by: Ritik Jain <[email protected]>

* fixed otp validation issue

Signed-off-by: Ritik Jain <[email protected]>

* fixed vid issue

Signed-off-by: Ritik Jain <[email protected]>

* updated code to get logo from config

Signed-off-by: Ritik Jain <[email protected]>

* removed constant and used enum

Signed-off-by: Ritik Jain <[email protected]>

* added test cases

Signed-off-by: Ritik Jain <[email protected]>

* coverage

Signed-off-by: Ritik Jain <[email protected]>

* added masked user id in the response

Signed-off-by: Ritik Jain <[email protected]>

* added masked user id in the response

Signed-off-by: Ritik Jain <[email protected]>

* added masked user id in the response

Signed-off-by: Ritik Jain <[email protected]>

* added masked user id in the response

Signed-off-by: Ritik Jain <[email protected]>

* added test cases

Signed-off-by: Ritik Jain <[email protected]>

* added test cases

Signed-off-by: Ritik Jain <[email protected]>

* updated class name

Signed-off-by: Ritik Jain <[email protected]>

* updated class package

Signed-off-by: Ritik Jain <[email protected]>

* added test cases

Signed-off-by: Ritik Jain <[email protected]>

* exclude classes from sonar

Signed-off-by: Ritik Jain <[email protected]>

* sonar coverage

Signed-off-by: Ritik Jain <[email protected]>

* sonar coverage

Signed-off-by: Ritik Jain <[email protected]>

---------

Signed-off-by: Ritik Jain <[email protected]>
Co-authored-by: Ritik Jain <[email protected]>
  • Loading branch information
RitikJain4108 and Ritik Jain authored Dec 8, 2023
1 parent 3ebe548 commit 6fd7903
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ public class TransliterationController {
@Autowired
private AuditUtil audit;

/** The Constant for GET UPDATED DATE TIME application. */
private static final String TRANS = "resident.transliteration.transliterate";

/**
* Post API to transliterate from transliteration application.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -16,6 +18,7 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
Expand All @@ -24,6 +27,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import io.mosip.resident.constant.ResidentConstants;
import io.mosip.resident.dto.IdAuthRequestDto;
import io.mosip.resident.dto.RequestWrapper;
import io.mosip.resident.exception.OtpValidationFailedException;
Expand Down Expand Up @@ -81,19 +85,21 @@ public class IdAuthControllerTest {
Gson gson = new GsonBuilder().serializeNulls().create();

String reqJson;
private RequestWrapper<IdAuthRequestDto> requestWrapper;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(idAuthController).build();
RequestWrapper<IdAuthRequestDto> requestWrapper = new RequestWrapper<IdAuthRequestDto>();
requestWrapper = new RequestWrapper<IdAuthRequestDto>();
IdAuthRequestDto idAuthRequestDto = new IdAuthRequestDto();
idAuthRequestDto.setTransactionId("1234567890");
idAuthRequestDto.setIndividualId("8251649601");
idAuthRequestDto.setOtp("111111");
requestWrapper.setRequest(idAuthRequestDto);
reqJson = gson.toJson(requestWrapper);
Mockito.doNothing().when(auditUtil).setAuditRequestDto(Mockito.any());
ReflectionTestUtils.setField(idAuthController, "validateOtpId", "validate-otp-id");
}

@Test
Expand All @@ -104,12 +110,11 @@ public void testValidateOtp() throws Exception {
.content(reqJson.getBytes())).andExpect(status().isOk());
}

@Test(expected = Exception.class)
@Test(expected = OtpValidationFailedException.class)
public void testValidateOtpFailed() throws Exception {
Mockito.when(idAuthService.validateOtpV1(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenThrow(new OtpValidationFailedException());
mockMvc.perform(MockMvcRequestBuilders.post("/validate-otp").contentType(MediaType.APPLICATION_JSON_VALUE)
.content(reqJson.getBytes())).andExpect(status().isOk());
Mockito.doThrow(new OtpValidationFailedException("otp validation failed", Map.of(ResidentConstants.EVENT_ID, "123456"))).when(idAuthService)
.validateOtpV1(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
idAuthController.validateOtp(requestWrapper);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.mosip.kernel.core.crypto.spi.CryptoCoreSpec;
import io.mosip.kernel.core.http.ResponseWrapper;
import io.mosip.resident.dto.LocationImmediateChildrenResponseDto;
import io.mosip.resident.exception.ResidentServiceCheckedException;
import io.mosip.resident.helper.ObjectStoreHelper;
import io.mosip.resident.service.DocumentService;
Expand Down Expand Up @@ -367,5 +368,14 @@ public void testGetAllDynamicFieldFailure() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/proxy/masterdata/dynamicfields/all/gender"))
.andExpect(status().isOk());
}

@Test
public void testGetImmediateChildrenByLocCode() throws Exception {
ResponseWrapper<LocationImmediateChildrenResponseDto> responseWrapper = new ResponseWrapper<>();
responseWrapper.setResponse(new LocationImmediateChildrenResponseDto());
Mockito.when(proxyMasterdataService.getImmediateChildrenByLocCode(Mockito.anyString(), Mockito.anyList())).thenReturn(responseWrapper.getResponse());
mockMvc.perform(MockMvcRequestBuilders.get("/auth-proxy/masterdata/locations/immediatechildren/KNT?languageCodes=eng"))
.andExpect(status().isOk());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ public class ResidentControllerTest {
/** The mock mvc. */
@Autowired
private MockMvc mockMvc;
private JSONObject idRepoJson;
private String schemaJson;

@Before
Expand Down Expand Up @@ -235,7 +234,6 @@ public void setUp() throws ApisResourceAccessException, IOException {
when(environment.getProperty(anyString())).thenReturn("property");
when(utilities.retrieveIdRepoJsonIdResponseDto(Mockito.any())).thenReturn(new IdResponseDTO1());

idRepoJson = null;
schemaJson = "schema";
}

Expand All @@ -251,6 +249,15 @@ public void testGetRidStatusSuccess() throws Exception {
.andExpect(status().isOk()).andExpect(jsonPath("$.response.ridStatus", is("PROCESSED")));
}

@Test(expected = InvalidInputException.class)
public void testGetRidStatusException() throws Exception {
RequestWrapper<RequestDTO> requestDTO = new RequestWrapper<>();
requestDTO.setRequest(new RequestDTO());
ReflectionTestUtils.setField(residentController, "checkStatusId", "id");
Mockito.doThrow(InvalidInputException.class).when(validator).validateRidCheckStatusRequestDTO(Mockito.any());
residentController.getRidStatus(requestDTO);
}

@Test
@WithUserDetails("resident")
public void testRequestAuthLockSuccess() throws Exception {
Expand Down Expand Up @@ -278,17 +285,11 @@ public void testReqAuthTypeLock() throws Exception {
.andExpect(status().isOk()).andExpect(status().isOk());
}

@Test
@WithUserDetails("resident")
@Test(expected = InvalidInputException.class)
public void testReqAuthTypeLockBadRequest() throws Exception {
ResponseDTO responseDto = new ResponseDTO();
doNothing().when(validator).validateAuthLockOrUnlockRequest(Mockito.any(), Mockito.any());
Mockito.doReturn(Tuples.of(responseDto, "12345")).when(residentService).reqAauthTypeStatusUpdateV2(Mockito.any());

MvcResult result = this.mockMvc
.perform(post("/auth-lock-unlock").contentType(MediaType.APPLICATION_JSON).content(""))
.andExpect(status().isOk()).andReturn();
assertTrue(result.getResponse().getContentAsString().contains("RES-SER-418"));
ReflectionTestUtils.setField(residentController, "authLockStatusUpdateV2Id", "id");
doThrow(InvalidInputException.class).when(validator).validateAuthLockOrUnlockRequestV2(Mockito.any());
residentController.reqAauthTypeStatusUpdateV2(authTypeStatusRequest);
}

@Test
Expand Down Expand Up @@ -394,6 +395,17 @@ public void testGetServiceHistorySuccess() throws Exception {
.andExpect(status().isOk());
}

@Test(expected = InvalidInputException.class)
public void testGetServiceHistoryException() throws Exception {
ReflectionTestUtils.setField(residentController, "serviceHistoryId", "id");
Mockito.doThrow(InvalidInputException.class).when(validator).validateServiceHistoryRequest(Mockito.any(),
Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
Mockito.anyString());
residentController.getServiceHistory("eng", 1, 12, LocalDate.parse("2022-06-10"),
LocalDate.parse("2022-06-10"), SortType.ASC.toString(),
ServiceType.AUTHENTICATION_REQUEST.name(), "success", "123456", 0, LOCALE_EN_US);
}

@Test
@WithUserDetails("reg-admin")
public void testRequestAuthHistorySuccess() throws Exception {
Expand All @@ -418,14 +430,16 @@ public void testRequestAuthHistorySuccess() throws Exception {
.andExpect(status().isOk()).andExpect(jsonPath("$.response.message", is("success")));
}

@Test
@WithUserDetails("reg-admin")
@Test(expected = InvalidInputException.class)
public void testRequestAuthHistoryBadRequest() throws Exception {

MvcResult result = this.mockMvc
.perform(post("/req/auth-history").contentType(MediaType.APPLICATION_JSON).content(""))
.andExpect(status().isOk()).andReturn();
assertTrue(result.getResponse().getContentAsString().contains("RES-SER-418"));
authHistoryRequest = new RequestWrapper<AuthHistoryRequestDTO>();
AuthHistoryRequestDTO hisdto = new AuthHistoryRequestDTO();
hisdto.setIndividualId("1234");
hisdto.setOtp("1234");
hisdto.setTransactionID("1234");
authHistoryRequest.setRequest(hisdto);
doThrow(InvalidInputException.class).when(validator).validateAuthHistoryRequest(Mockito.any());
residentController.reqAuthHistory(authHistoryRequest);
}

@Test
Expand Down Expand Up @@ -484,6 +498,48 @@ public void testUpdateUinDemographics() throws Exception {
assertEquals(new ResidentUpdateResponseDTO(), ((ResponseWrapper<Object>)responseEntity.getBody()).getResponse());
}

@Test(expected = InvalidInputException.class)
public void testUpdateUinDemographicsIdTypeUINException() throws Exception {
Mockito.when(validator.validateUin(Mockito.anyString())).thenReturn(true);
Mockito.doThrow(InvalidInputException.class).when(validator).validateUpdateRequest(Mockito.any(), Mockito.anyBoolean(), Mockito.anyString());
ResidentDemographicUpdateRequestDTO request = new ResidentDemographicUpdateRequestDTO();
request.setTransactionID("12345");
request.setIdentity(JsonUtil.readValue("{\"name\":\"My Name\"}", JSONObject.class));

RequestWrapper<ResidentDemographicUpdateRequestDTO> requestDTO = new RequestWrapper<>();
requestDTO.setRequest(request);
requestDTO.setId("mosip.resident.demographic");
requestDTO.setVersion("v1");
JSONObject jsonObject = new JSONObject();
jsonObject.put("IDSchemaVersion", "0.1");
Tuple3<JSONObject, String, IdResponseDTO1> idRepoJsonSchemaJsonAndIdResponseDtoTuple = Tuples.of(jsonObject, schemaJson, new IdResponseDTO1());
when(utilities.
getIdentityDataFromIndividualID(Mockito.anyString())).thenReturn(idRepoJsonSchemaJsonAndIdResponseDtoTuple);
when(identityServiceImpl.getResidentIndvidualIdFromSession()).thenReturn("9876543210");
residentController.updateUinDemographics(requestDTO);
}

@Test(expected = InvalidInputException.class)
public void testUpdateUinDemographicsIdTypeVIDException() throws Exception {
Mockito.when(validator.validateVid(Mockito.anyString())).thenReturn(true);
Mockito.doThrow(InvalidInputException.class).when(validator).validateUpdateRequest(Mockito.any(), Mockito.anyBoolean(), Mockito.anyString());
ResidentDemographicUpdateRequestDTO request = new ResidentDemographicUpdateRequestDTO();
request.setTransactionID("12345");
request.setIdentity(JsonUtil.readValue("{\"name\":\"My Name\"}", JSONObject.class));

RequestWrapper<ResidentDemographicUpdateRequestDTO> requestDTO = new RequestWrapper<>();
requestDTO.setRequest(request);
requestDTO.setId("mosip.resident.demographic");
requestDTO.setVersion("v1");
JSONObject jsonObject = new JSONObject();
jsonObject.put("IDSchemaVersion", "0.1");
Tuple3<JSONObject, String, IdResponseDTO1> idRepoJsonSchemaJsonAndIdResponseDtoTuple = Tuples.of(jsonObject, schemaJson, new IdResponseDTO1());
when(utilities.
getIdentityDataFromIndividualID(Mockito.anyString())).thenReturn(idRepoJsonSchemaJsonAndIdResponseDtoTuple);
when(identityServiceImpl.getResidentIndvidualIdFromSession()).thenReturn("9876543210");
residentController.updateUinDemographics(requestDTO);
}

@Test
@WithUserDetails("reg-admin")
public void testAuthLockStatus() throws Exception {
Expand Down Expand Up @@ -618,6 +674,14 @@ public void testNotificationCount() throws Exception {
residentController.notificationCount();
}

@Test(expected = ResidentServiceCheckedException.class)
public void testNotificationCountException() throws Exception {
ReflectionTestUtils.setField(residentController, "serviceEventId", "id");
Mockito.when(identityServiceImpl.getResidentIdaToken()).thenReturn("1234567890");
Mockito.when(residentService.getnotificationCount(Mockito.anyString())).thenThrow(ResidentServiceCheckedException.class);
residentController.notificationCount();
}

@Test
@WithUserDetails("reg-admin")
public void testBellupdateClickdttimes() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.mosip.resident.controller;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doThrow;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -20,6 +21,7 @@
import io.mosip.preregistration.application.service.TransliterationService;
import io.mosip.preregistration.core.common.dto.MainRequestDTO;
import io.mosip.preregistration.core.common.dto.MainResponseDTO;
import io.mosip.resident.exception.InvalidInputException;
import io.mosip.resident.helper.ObjectStoreHelper;
import io.mosip.resident.service.IdAuthService;
import io.mosip.resident.service.impl.IdentityServiceImpl;
Expand All @@ -28,8 +30,8 @@
import io.mosip.resident.validator.RequestValidator;

/**
* Acknowledgment Controller Test
* Note: This class is used to test the Acknowledgment Controller
* Transliteration Controller Test
* Note: This class is used to test the Transliteration Controller
* @author Kamesh Shekhar Prasad
*/

Expand Down Expand Up @@ -83,4 +85,15 @@ public void testCreateRequestGenerationSuccess() throws Exception {
assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);
}

@Test(expected = InvalidInputException.class)
public void testWithInvalidInputException() throws Exception {
MainRequestDTO<TransliterationRequestDTO> requestDTO = new MainRequestDTO<>();
TransliterationRequestDTO transliterationRequestDTO = new TransliterationRequestDTO();
transliterationRequestDTO.setFromFieldLang("eng");
transliterationRequestDTO.setFromFieldValue("cat");
transliterationRequestDTO.setToFieldLang("hin");
requestDTO.setRequest(transliterationRequestDTO);
doThrow(InvalidInputException.class).when(requestValidator).validateId(Mockito.any());
transliterationController.translitrator(requestDTO);
}
}

0 comments on commit 6fd7903

Please sign in to comment.