From f0d008f08ecd735b7efcceb9fbe5b346db776d2c Mon Sep 17 00:00:00 2001 From: kaifk468 <74772315+kaifk468@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:19:33 +0530 Subject: [PATCH] added test case for VCITransactionHelper.class and AuthTransactionHelper.class (#1114) --- .../helper/AuthTransactionHelperTest.java | 48 ++++++++++++++++++ .../helper/VCITransactionHelperTest.java | 49 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/helper/AuthTransactionHelperTest.java create mode 100644 authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/helper/VCITransactionHelperTest.java diff --git a/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/helper/AuthTransactionHelperTest.java b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/helper/AuthTransactionHelperTest.java new file mode 100644 index 00000000000..1498d5bd388 --- /dev/null +++ b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/helper/AuthTransactionHelperTest.java @@ -0,0 +1,48 @@ +package io.mosip.authentication.esignet.integration.helper; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.mosip.esignet.core.dto.ResponseWrapper; +import org.junit.Assert; +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 org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.RequestEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.client.RestTemplate; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class AuthTransactionHelperTest { + + @Mock + ObjectMapper objectMapper; + + @Mock + RestTemplate restTemplate; + + @InjectMocks + AuthTransactionHelper authTransactionHelper; + + @Test + public void GetAuthTokenWithValidDetails_thenPass() throws Exception { + ReflectionTestUtils.setField(authTransactionHelper, "authTokenUrl", "test"); + ReflectionTestUtils.setField(authTransactionHelper, "clientId", "test"); + ReflectionTestUtils.setField(authTransactionHelper,"secretKey","test"); + ReflectionTestUtils.setField(authTransactionHelper,"appId","test"); String expectedAuthToken = "testAuthToken"; + + ResponseEntity responseEntity = ResponseEntity.ok() + .header("authorization", expectedAuthToken) + .build(); + + when(restTemplate.exchange(Mockito.any(RequestEntity.class), Mockito.any(ParameterizedTypeReference.class))) + .thenReturn(responseEntity); + + String authToken = authTransactionHelper.getAuthToken(); + Assert.assertEquals(expectedAuthToken, authToken); + } +} diff --git a/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/helper/VCITransactionHelperTest.java b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/helper/VCITransactionHelperTest.java new file mode 100644 index 00000000000..28e309785bb --- /dev/null +++ b/authentication/esignet-integration-impl/src/test/java/io/mosip/authentication/esignet/integration/helper/VCITransactionHelperTest.java @@ -0,0 +1,49 @@ +package io.mosip.authentication.esignet.integration.helper; + +import io.mosip.esignet.core.dto.OIDCTransaction; +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 org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.cache.support.NoOpCache; +import org.springframework.test.util.ReflectionTestUtils; + +@RunWith(MockitoJUnitRunner.class) +public class VCITransactionHelperTest { + + @Mock + CacheManager cacheManager; + + @Mock + Cache cache=new NoOpCache("test"); + + @InjectMocks + VCITransactionHelper vciTransactionHelper; + + @Test + public void getOAuthTransactionWithValidDetails_thenPass() throws Exception { + ReflectionTestUtils.setField(vciTransactionHelper, "userinfoCache", "test"); + OIDCTransaction oidcTransaction = new OIDCTransaction(); + oidcTransaction.setTransactionId("test"); + Mockito.when(cacheManager.getCache(Mockito.anyString())).thenReturn(cache); + Mockito.when(cache.get("test",OIDCTransaction.class)).thenReturn(oidcTransaction); + vciTransactionHelper.getOAuthTransaction("test"); + + } + + @Test + public void getOAuthTransactionWithInValidDetails_thenFail() { + try{ + vciTransactionHelper.getOAuthTransaction("test"); + }catch (Exception e){ + assert(e.getMessage().equals("cache_missing")); + } + + + } + +}