Skip to content

Commit

Permalink
added test case for VCITransactionHelper.class and AuthTransactionHel…
Browse files Browse the repository at this point in the history
…per.class (#1114)
  • Loading branch information
kaifk468 authored Oct 25, 2023
1 parent 2eaf408 commit f0d008f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<ResponseWrapper> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}


}

}

0 comments on commit f0d008f

Please sign in to comment.