Skip to content

Commit

Permalink
MOSIP-34803 Increased code coverage. (#1309)
Browse files Browse the repository at this point in the history
* MOSIP-34803 Added IdentityUtil test

Signed-off-by: kameshsr <[email protected]>

* MOSIP-34803 Increased sonar coverage

Signed-off-by: kameshsr <[email protected]>

* MOSIP-34803 Increased code coverage

Signed-off-by: kameshsr <[email protected]>

* MOSIP-34803 Added Junit test case for Identity util class

Signed-off-by: kameshsr <[email protected]>

* MOSIP-34803 Added AttributeDisplayTextTest class

Signed-off-by: kameshsr <[email protected]>

* MOSIP-34803 Added unit test case

Signed-off-by: kameshsr <[email protected]>

* MOSIP-34803 Added test case

Signed-off-by: kameshsr <[email protected]>

* MOSIP-34803 Added PerpetualVidUtilTest

Signed-off-by: kameshsr <[email protected]>

* MOSIP-34803 Increased code coverage

Signed-off-by: kameshsr <[email protected]>

* MOSIP-34803 Corrected test case failure for AuditUtil

Signed-off-by: kameshsr <[email protected]>

---------

Signed-off-by: kameshsr <[email protected]>
  • Loading branch information
kameshsr authored Oct 24, 2024
1 parent d939014 commit 9a51ef0
Show file tree
Hide file tree
Showing 4 changed files with 461 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package io.mosip.resident.util;

import io.mosip.resident.constant.RequestType;
import io.mosip.resident.constant.ResidentConstants;
import io.mosip.resident.handler.service.ResidentConfigService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.HashMap;
import java.util.Map;

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

/**
* @author Kamesh
*/

@RunWith(MockitoJUnitRunner.class)
public class AttributeDisplayTextTest {

@InjectMocks
private AttributesDisplayText attributesDisplayText = new AttributesDisplayText();

@Mock
ResidentConfigService residentConfigService;

private Map<String, Map<String, Object>> mockUISchemaDataMap;

@Before
public void setUp() {
// Mock UI Schema Data
mockUISchemaDataMap = new HashMap<>();

Map<String, Object> attributeMap = new HashMap<>();
attributeMap.put(ResidentConstants.LABEL, "Mocked Label");
attributeMap.put("label1", "Mocked Label2");

Map<String, String> formatOptionMap = new HashMap<>();
formatOptionMap.put("format1", "Formatted 1");
formatOptionMap.put("format2", "Formatted 2");

attributeMap.put(ResidentConstants.FORMAT_OPTION, formatOptionMap);

mockUISchemaDataMap.put("attr1", attributeMap);

// Mock ResidentConfigService response
when(residentConfigService.getUISchemaCacheableData(Mockito.anyString()))
.thenReturn(Map.of("en", mockUISchemaDataMap));
}

@Test
public void testGetAttributesDisplayText_withSchemaType() {
String attributesFromDB = "en:format1,format2";
String languageCode = "en";
RequestType requestType = RequestType.UPDATE_MY_UIN;

String result = attributesDisplayText.getAttributesDisplayText(attributesFromDB, languageCode, requestType);

assertEquals("en", result);
}

@Test
public void testGetAttributesDisplayText_withoutSchemaType() {
String attributesFromDB = "attr1:attr2";
String languageCode = "en";
RequestType requestType = RequestType.UPDATE_MY_UIN;
String result = attributesDisplayText.getAttributesDisplayText(attributesFromDB, languageCode, requestType);

assertEquals("Mocked Label", result);
}

@Test
public void testGetAttributesDisplayText_withNullAttributes() {
String attributesFromDB = null;
String languageCode = "en";
RequestType requestType = RequestType.UPDATE_MY_UIN;

String result = attributesDisplayText.getAttributesDisplayText(attributesFromDB, languageCode, requestType);

assertEquals("", result);
}

@Test
public void testGetAttributesDisplayText_withEmptyAttributes() {
String attributesFromDB = "";
String languageCode = "en";
RequestType requestType = RequestType.UPDATE_MY_UIN;

String result = attributesDisplayText.getAttributesDisplayText(attributesFromDB, languageCode, requestType);

assertEquals("", result);
}

@Test
public void testGetAttributesDisplayText(){
Map<String, Map<String, Map<String, Object>>> keyMap = new HashMap<>();
Map<String, Map<String, Object>> attributeMap = new HashMap<>();
Map<String, Object> stringObjectMap = new HashMap<>();
stringObjectMap.put("testKey", "testobj");
attributeMap.put("key", stringObjectMap);
keyMap.put("eng", attributeMap);
when(residentConfigService.getUISchemaCacheableData(Mockito.anyString())).thenReturn(keyMap);
assertEquals("test",
attributesDisplayText.getAttributesDisplayText("test", "eng", RequestType.UPDATE_MY_UIN));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.mosip.resident.util;

import static io.mosip.resident.service.impl.IdentityServiceTest.getAuthUserDetailsFromAuthentication;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.times;
Expand All @@ -11,24 +12,17 @@
import java.time.LocalDateTime;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;

Expand All @@ -51,10 +45,7 @@
/**
* @author Abubacker Siddik
*/
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
@PrepareForTest({SecurityContextHolder.class, InetAddress.class, DateUtils.class})
@Ignore
@RunWith(MockitoJUnitRunner.class)
public class AuditUtilTest {

@InjectMocks
Expand Down Expand Up @@ -103,24 +94,16 @@ public void setUp() throws Exception {
ReflectionTestUtils.setField(auditUtil, "auditUrl", auditUrl);
ReflectionTestUtils.setField(auditUtil, "asyncUtil", asyncUtil);

PowerMockito.mockStatic(SecurityContextHolder.class);
PowerMockito.mockStatic(InetAddress.class);
PowerMockito.mockStatic(DateUtils.class);

UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user1", "password", null);
when(SecurityContextHolder.getContext()).thenReturn(new SecurityContextImpl(token));

host = InetAddress.getLocalHost();
when(InetAddress.getLocalHost()).thenReturn(host);

localDateTime = DateUtils.getUTCCurrentDateTime();
when(DateUtils.getUTCCurrentDateTime()).thenReturn(localDateTime);
ReflectionTestUtils.setField(auditUtil, "hostIpAddress","MOSIP");
when(availableClaimValueUtility.getAvailableClaimValue(Mockito.anyString())).thenReturn("user1");
when(environment.getProperty(Mockito.anyString())).thenReturn("user1");
}

@Test
public void setAuditRequestDtoTest() throws Exception {
getAuthUserDetailsFromAuthentication();
AuditEvent auditEvent = AuditEnum.getAuditEventWithValue(AuditEnum.VALIDATE_REQUEST, "get Rid status API");
AuditResponseDto auditResponseDto = new AuditResponseDto();
auditResponseDto.setStatus(true);
Expand Down Expand Up @@ -161,27 +144,26 @@ public void setAuditRequestDtoTest() throws Exception {
httpEntity.getBody().getRequest().getId());
assertEquals(IdType.UIN.name(), httpEntity.getBody().getRequest().getIdType());

assertEquals(host.getHostName(), httpEntity.getBody().getRequest().getHostName());
assertEquals(host.getHostAddress(), httpEntity.getBody().getRequest().getHostIp());

assertEquals("user1", httpEntity.getBody().getRequest().getSessionUserId());
assertEquals("user1", httpEntity.getBody().getRequest().getSessionUserName());
assertEquals("user1", httpEntity.getBody().getRequest().getCreatedBy());

assertEquals(localDateTime, httpEntity.getBody().getRequest().getActionTimeStamp());
assertEquals(localDateTime.getYear(), httpEntity.getBody().getRequest().getActionTimeStamp().getYear());

assertEquals(auditUrlInput, auditUrl);
}

@Test(expected = RuntimeException.class)
public void testSetAuditRequestDtoWithApisResourceAccessException() throws Exception {
getAuthUserDetailsFromAuthentication();
AuditEvent auditEvent = AuditEnum.getAuditEventWithValue(AuditEnum.VALIDATE_REQUEST, "get Rid status API");
when(availableClaimValueUtility.getAvailableClaimValue(Mockito.anyString())).thenThrow(ApisResourceAccessException.class);
auditUtil.setAuditRequestDto(auditEvent);
}

@Test(expected = ResidentServiceException.class)
public void testGetRefIdHashAndTypeWithApisResourceAccessException() throws Exception {
getAuthUserDetailsFromAuthentication();
AuditEvent auditEvent = AuditEnum.getAuditEventWithValue(AuditEnum.VALIDATE_REQUEST, "get Rid status API");
when(availableClaimValueUtility.getAvailableClaimValue(Mockito.anyString())).thenReturn(null);
Mockito.when(availableClaimUtility.getResidentIndvidualIdFromSession()).thenThrow(ApisResourceAccessException.class);
Expand All @@ -196,6 +178,7 @@ public void testGetRefIdandTypeNoID() {

@Test
public void testGetRefIdandType() throws ApisResourceAccessException, NoSuchAlgorithmException {
getAuthUserDetailsFromAuthentication();
String individualId = "9054257143";
Mockito.when(availableClaimUtility.getResidentIndvidualIdFromSession()).thenReturn(individualId);
Mockito.when(uinVidValidator.getIndividualIdType(individualId)).thenReturn(IdType.UIN);
Expand Down
Loading

0 comments on commit 9a51ef0

Please sign in to comment.