From bd9f64cb2a97069d2e2c15bfd58f039830f84ac4 Mon Sep 17 00:00:00 2001 From: Venkata Saidurga Polamraju Date: Thu, 26 Oct 2023 16:22:06 +0530 Subject: [PATCH 1/3] Added test cases for IdaAuditPlugin Signed-off-by: Venkata Saidurga Polamraju --- .../service/IdaAuditPluginImpl.java | 11 +- .../service/IdaAuditPluginImplTest.java | 180 ++++++++++++++++++ .../service/IdaVCIssuancePluginImplTest.java | 2 +- 3 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImplTest.java diff --git a/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java b/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java index 1e2ce7be147..31d5a8368fc 100644 --- a/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java +++ b/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java @@ -72,8 +72,15 @@ private void audit(String username, Action action, ActionStatus status, AuditDTO auditRequest.setHostIp("localhost"); auditRequest.setApplicationId(ESIGNET); auditRequest.setApplicationName(ESIGNET); - auditRequest.setSessionUserId(StringUtils.isEmpty(username)?"no-user":username); - auditRequest.setSessionUserName(StringUtils.isEmpty(username)?"no-user":username); + String sessionUserId = StringUtils.isEmpty(username) ? "" : username; + String sessionUserName = StringUtils.isEmpty(username) ? "" : username; + if (StringUtils.isEmpty(username)) { + throw new IllegalArgumentException("Username cannot be empty"); + } + else { + auditRequest.setSessionUserId(sessionUserId); + auditRequest.setSessionUserName(sessionUserName); + }; auditRequest.setIdType(TRANSACTION); auditRequest.setCreatedBy(this.getClass().getSimpleName()); auditRequest.setModuleName(getModuleByAction(action)); diff --git a/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImplTest.java b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImplTest.java new file mode 100644 index 00000000000..4ce31242f9e --- /dev/null +++ b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImplTest.java @@ -0,0 +1,180 @@ +package io.mosip.authentication.esignet.integration.service; + +import io.mosip.esignet.api.dto.AuditDTO; +import io.mosip.esignet.api.util.Action; +import io.mosip.esignet.api.util.ActionStatus; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.junit.MockitoJUnitRunner; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.mosip.authentication.esignet.integration.dto.AuditResponse; +import io.mosip.authentication.esignet.integration.helper.AuthTransactionHelper; +import io.mosip.kernel.core.http.ResponseWrapper; +import org.mockito.*; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.client.RestTemplate; + +import static org.mockito.ArgumentMatchers.*; + + + +@RunWith(MockitoJUnitRunner.class) +public class IdaAuditPluginImplTest { + + @InjectMocks + private IdaAuditPluginImpl idaAuditPlugin; + + @Mock + private AuthTransactionHelper authTransactionHelper; + + @Mock + private ObjectMapper objectMapper; + + @Mock + private RestTemplate restTemplate; + + @Test + public void logAudit_WithValidDetails_ThenPass() { + Action action = Action.AUTHENTICATE; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + try { + idaAuditPlugin.logAudit(action, status, auditDTO, null); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void logAudit_WithThrowable_ThenPass() { + Action action = Action.GENERATE_TOKEN; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + Throwable throwable = new RuntimeException("Test Exception"); + try { + idaAuditPlugin.logAudit(action, status, auditDTO, throwable); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void logAudit_WithUsername_WithValidDetails_ThenPass() { + String username = "username"; + Action action = Action.OIDC_CLIENT_UPDATE; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + try { + idaAuditPlugin.logAudit(username, action, status, auditDTO, null); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void logAudit_WithUsername_WithThrowable_ThenPass() throws Exception { + String username = "username"; + Action action = Action.GENERATE_TOKEN; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + Throwable throwable = new RuntimeException("Test Exception"); + try { + idaAuditPlugin.logAudit(username,action, status, auditDTO, throwable); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void logAudit_WithValidStatus_ThenPass() throws Exception { + ReflectionTestUtils.setField(idaAuditPlugin, "auditManagerUrl", "auditManagerUrl"); + String username = "username"; + Action action = Action.SAVE_CONSENT; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + Throwable throwable = new RuntimeException("Test Exception"); + ResponseWrapper mockresponseWrapper = new ResponseWrapper<>(); + ResponseEntity responseEntity = ResponseEntity.ok(mockresponseWrapper); + ParameterizedTypeReference responseType = + new ParameterizedTypeReference() { + }; + Mockito.when(authTransactionHelper.getAuthToken()).thenReturn("authToken"); + Mockito.when(objectMapper.writeValueAsString(any())).thenReturn("requestBody"); + Mockito.when(restTemplate.exchange( + Mockito.any(RequestEntity.class), + Mockito.eq(responseType) + )).thenReturn(responseEntity); + try { + idaAuditPlugin.logAudit(username,action, status, auditDTO, throwable); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + } + + @Test + public void logAudit_WithUnauthorizedStatus_ThenPass() throws Exception { + ReflectionTestUtils.setField(idaAuditPlugin, "auditManagerUrl", "auditManagerUrl"); + String username = "username"; + Action action = Action.SAVE_CONSENT; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + Throwable throwable = new RuntimeException("Test Exception"); + ResponseWrapper mockresponseWrapper = new ResponseWrapper<>(); + ResponseEntity responseEntity = ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(mockresponseWrapper); + ParameterizedTypeReference responseType = + new ParameterizedTypeReference() { + }; + Mockito.when(authTransactionHelper.getAuthToken()).thenReturn("authToken"); + Mockito.when(objectMapper.writeValueAsString(any())).thenReturn("requestBody"); + Mockito.when(restTemplate.exchange( + Mockito.any(RequestEntity.class), + Mockito.eq(responseType) + )).thenReturn(responseEntity); + try { + idaAuditPlugin.logAudit(username,action, status, auditDTO, throwable); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + + } + + @Test + public void logAudit_WithForbiddenStatus_ThenPass() throws Exception { + ReflectionTestUtils.setField(idaAuditPlugin, "auditManagerUrl", "auditManagerUrl"); + String username = "username"; + Action action = Action.SAVE_CONSENT; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + Throwable throwable = new RuntimeException("Test Exception"); + ResponseWrapper mockresponseWrapper = new ResponseWrapper<>(); + ResponseEntity responseEntity = ResponseEntity.status(HttpStatus.FORBIDDEN).body(mockresponseWrapper); + ParameterizedTypeReference responseType = + new ParameterizedTypeReference() { + }; + Mockito.when(authTransactionHelper.getAuthToken()).thenReturn("authToken"); + Mockito.when(objectMapper.writeValueAsString(any())).thenReturn("requestBody"); + Mockito.when(restTemplate.exchange( + Mockito.any(RequestEntity.class), + Mockito.eq(responseType) + )).thenReturn(responseEntity); + try { + idaAuditPlugin.logAudit(username,action, status, auditDTO, throwable); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.fail(); + } + + } + +} \ No newline at end of file diff --git a/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaVCIssuancePluginImplTest.java b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaVCIssuancePluginImplTest.java index b37730feed7..25681f8e861 100644 --- a/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaVCIssuancePluginImplTest.java +++ b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaVCIssuancePluginImplTest.java @@ -205,7 +205,7 @@ public void getVerifiableCredentialWithLinkedDataProof_withInValidIndividualId_t } @Test - public void getVerifiableCredentialWithLinkedDataProof_withInVlidResponse_thenFail() throws Exception { + public void getVerifiableCredentialWithLinkedDataProof_withInValidResponse_thenFail() throws Exception { ReflectionTestUtils.setField(idaVCIssuancePlugin,"vciExchangeUrl","http://example.com"); ReflectionTestUtils.setField(idaVCIssuancePlugin,"storeIndividualId",true); From fd6b24758241ef8046dfd78abf57ceae8899a6b5 Mon Sep 17 00:00:00 2001 From: Venkata Saidurga Polamraju Date: Fri, 27 Oct 2023 13:09:54 +0530 Subject: [PATCH 2/3] fixed IdaAuditPluginImpl Signed-off-by: Venkata Saidurga Polamraju --- .../service/IdaAuditPluginImpl.java | 17 +++++++-------- .../service/IdaAuditPluginImplTest.java | 21 ++++++++++++------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java b/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java index 31d5a8368fc..265d834714d 100644 --- a/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java +++ b/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java @@ -54,6 +54,9 @@ public void logAudit(Action action, ActionStatus status, AuditDTO audit, Throwab @Override public void logAudit(String username, Action action, ActionStatus status, AuditDTO audit, Throwable t) { + if (t!=null && StringUtils.isEmpty(username)) { + throw new IllegalArgumentException("Username cannot be empty or null in the audit"); + } audit(username, action, status, audit); } @@ -72,15 +75,11 @@ private void audit(String username, Action action, ActionStatus status, AuditDTO auditRequest.setHostIp("localhost"); auditRequest.setApplicationId(ESIGNET); auditRequest.setApplicationName(ESIGNET); - String sessionUserId = StringUtils.isEmpty(username) ? "" : username; - String sessionUserName = StringUtils.isEmpty(username) ? "" : username; - if (StringUtils.isEmpty(username)) { - throw new IllegalArgumentException("Username cannot be empty"); - } - else { - auditRequest.setSessionUserId(sessionUserId); - auditRequest.setSessionUserName(sessionUserName); - }; + String sessionUserId = username; + String sessionUserName = username; + + auditRequest.setSessionUserId(sessionUserId); + auditRequest.setSessionUserName(sessionUserName); auditRequest.setIdType(TRANSACTION); auditRequest.setCreatedBy(this.getClass().getSimpleName()); auditRequest.setModuleName(getModuleByAction(action)); diff --git a/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImplTest.java b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImplTest.java index 4ce31242f9e..0aa3dceced2 100644 --- a/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImplTest.java +++ b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImplTest.java @@ -80,7 +80,17 @@ public void logAudit_WithUsername_WithValidDetails_ThenPass() { } @Test - public void logAudit_WithUsername_WithThrowable_ThenPass() throws Exception { + public void logAudit_WithEmptyUsername_ThenFail() { + String username = ""; + Action action = Action.OIDC_CLIENT_UPDATE; + ActionStatus status = ActionStatus.SUCCESS; + AuditDTO auditDTO = new AuditDTO(); + Throwable throwable = new RuntimeException("Test Exception"); + Assert.assertThrows(IllegalArgumentException.class,() -> idaAuditPlugin.logAudit(username, action, status, auditDTO, throwable)); + } + + @Test + public void logAudit_WithUsername_WithThrowable() throws Exception { String username = "username"; Action action = Action.GENERATE_TOKEN; ActionStatus status = ActionStatus.SUCCESS; @@ -101,7 +111,6 @@ public void logAudit_WithValidStatus_ThenPass() throws Exception { Action action = Action.SAVE_CONSENT; ActionStatus status = ActionStatus.SUCCESS; AuditDTO auditDTO = new AuditDTO(); - Throwable throwable = new RuntimeException("Test Exception"); ResponseWrapper mockresponseWrapper = new ResponseWrapper<>(); ResponseEntity responseEntity = ResponseEntity.ok(mockresponseWrapper); ParameterizedTypeReference responseType = @@ -114,7 +123,7 @@ public void logAudit_WithValidStatus_ThenPass() throws Exception { Mockito.eq(responseType) )).thenReturn(responseEntity); try { - idaAuditPlugin.logAudit(username,action, status, auditDTO, throwable); + idaAuditPlugin.logAudit(username,action, status, auditDTO, null); Assert.assertTrue(true); } catch (Exception e) { Assert.fail(); @@ -128,7 +137,6 @@ public void logAudit_WithUnauthorizedStatus_ThenPass() throws Exception { Action action = Action.SAVE_CONSENT; ActionStatus status = ActionStatus.SUCCESS; AuditDTO auditDTO = new AuditDTO(); - Throwable throwable = new RuntimeException("Test Exception"); ResponseWrapper mockresponseWrapper = new ResponseWrapper<>(); ResponseEntity responseEntity = ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(mockresponseWrapper); ParameterizedTypeReference responseType = @@ -141,7 +149,7 @@ public void logAudit_WithUnauthorizedStatus_ThenPass() throws Exception { Mockito.eq(responseType) )).thenReturn(responseEntity); try { - idaAuditPlugin.logAudit(username,action, status, auditDTO, throwable); + idaAuditPlugin.logAudit(username,action, status, auditDTO, null); Assert.assertTrue(true); } catch (Exception e) { Assert.fail(); @@ -156,7 +164,6 @@ public void logAudit_WithForbiddenStatus_ThenPass() throws Exception { Action action = Action.SAVE_CONSENT; ActionStatus status = ActionStatus.SUCCESS; AuditDTO auditDTO = new AuditDTO(); - Throwable throwable = new RuntimeException("Test Exception"); ResponseWrapper mockresponseWrapper = new ResponseWrapper<>(); ResponseEntity responseEntity = ResponseEntity.status(HttpStatus.FORBIDDEN).body(mockresponseWrapper); ParameterizedTypeReference responseType = @@ -169,7 +176,7 @@ public void logAudit_WithForbiddenStatus_ThenPass() throws Exception { Mockito.eq(responseType) )).thenReturn(responseEntity); try { - idaAuditPlugin.logAudit(username,action, status, auditDTO, throwable); + idaAuditPlugin.logAudit(username,action, status, auditDTO, null); Assert.assertTrue(true); } catch (Exception e) { Assert.fail(); From fbb5996a3022875df061ca35287b1f87f89eb498 Mon Sep 17 00:00:00 2001 From: Venkata Saidurga Polamraju Date: Fri, 27 Oct 2023 18:38:27 +0530 Subject: [PATCH 3/3] code fix Signed-off-by: Venkata Saidurga Polamraju --- .../esignet/integration/service/IdaAuditPluginImpl.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java b/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java index 265d834714d..5e9c1d45ce2 100644 --- a/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java +++ b/authentication/esignet-integration-impl/src/main/java/io/mosip/authentication/esignet/integration/service/IdaAuditPluginImpl.java @@ -75,11 +75,8 @@ private void audit(String username, Action action, ActionStatus status, AuditDTO auditRequest.setHostIp("localhost"); auditRequest.setApplicationId(ESIGNET); auditRequest.setApplicationName(ESIGNET); - String sessionUserId = username; - String sessionUserName = username; - - auditRequest.setSessionUserId(sessionUserId); - auditRequest.setSessionUserName(sessionUserName); + auditRequest.setSessionUserId(StringUtils.isEmpty(username)?"no-user":username); + auditRequest.setSessionUserName(StringUtils.isEmpty(username)?"no-user":username); auditRequest.setIdType(TRANSACTION); auditRequest.setCreatedBy(this.getClass().getSimpleName()); auditRequest.setModuleName(getModuleByAction(action));