-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[INJICERT-331] Added unit tests for validators and certify issuance service #165
Open
Piyush7034
wants to merge
2
commits into
mosip:release-0.10.x
Choose a base branch
from
Infosys:test-coverage
base: release-0.10.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
certify-service/src/test/java/io/mosip/certify/VCICacheServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.mosip.certify; | ||
|
||
import io.mosip.certify.core.dto.VCIssuanceTransaction; | ||
import io.mosip.certify.services.VCICacheService; | ||
import org.junit.Test; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.springframework.cache.Cache; | ||
import org.springframework.cache.CacheManager; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.*; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class VCICacheServiceTest { | ||
@Mock | ||
private CacheManager cacheManager; | ||
|
||
@Mock | ||
private Cache cache; | ||
|
||
@InjectMocks | ||
private VCICacheService vciCacheService = new VCICacheService(); | ||
|
||
private static final String TEST_ACCESS_TOKEN_HASH = "testHash123"; | ||
private static final String VCISSUANCE_CACHE = "vcissuance"; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
when(cacheManager.getCache(VCISSUANCE_CACHE)).thenReturn(cache); | ||
} | ||
|
||
@Test | ||
public void setVCITransaction_ShouldReturnSameTransaction() { | ||
VCIssuanceTransaction transaction = new VCIssuanceTransaction(); | ||
transaction.setCNonce("test-cnonce"); | ||
VCIssuanceTransaction result = vciCacheService.setVCITransaction(TEST_ACCESS_TOKEN_HASH, transaction); | ||
assertNotNull(result); | ||
assertEquals(transaction, result); | ||
} | ||
|
||
@Test | ||
public void getVCITransaction_WhenTransactionExists_ShouldReturnTransaction() { | ||
VCIssuanceTransaction transaction = new VCIssuanceTransaction(); | ||
transaction.setCNonce("test-cnonce"); | ||
when(cacheManager.getCache(VCISSUANCE_CACHE)).thenReturn(cache); | ||
when(cache.get(TEST_ACCESS_TOKEN_HASH, VCIssuanceTransaction.class)).thenReturn(transaction); | ||
VCIssuanceTransaction result = vciCacheService.getVCITransaction(TEST_ACCESS_TOKEN_HASH); | ||
assertNotNull(result); | ||
assertEquals(transaction, result); | ||
verify(cacheManager).getCache(VCISSUANCE_CACHE); | ||
verify(cache).get(eq(TEST_ACCESS_TOKEN_HASH), eq(VCIssuanceTransaction.class)); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...y-service/src/test/java/io/mosip/certify/repository/CredentialTemplateRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.mosip.certify.repository; | ||
|
||
import io.mosip.certify.entity.CredentialTemplate; | ||
import io.mosip.certify.entity.RenderingTemplate; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
|
||
@RunWith(SpringRunner.class) | ||
@DataJpaTest | ||
public class CredentialTemplateRepositoryTest { | ||
@Autowired | ||
private CredentialTemplateRepository credentialTemplateRepository; | ||
|
||
private CredentialTemplate credentialTemplate; | ||
|
||
@Before | ||
public void setup() { | ||
credentialTemplate = new CredentialTemplate(); | ||
String template = "test-template"; | ||
credentialTemplate.setTemplate(template); | ||
credentialTemplate.setCredentialType("MockVerifiableCredential,VerifiableCredential"); | ||
credentialTemplate.setContext("https://www.example.com"); | ||
credentialTemplate.setCreatedTimes(LocalDateTime.now()); | ||
credentialTemplate = credentialTemplateRepository.saveAndFlush(credentialTemplate); | ||
} | ||
|
||
@Test | ||
public void findByValidCredentialTypeAndContext_thenPass() { | ||
Assert.assertNotNull(credentialTemplate); | ||
Optional<CredentialTemplate> optional = credentialTemplateRepository.findByCredentialTypeAndContext(credentialTemplate.getCredentialType(), credentialTemplate.getContext()); | ||
Assert.assertTrue(optional.isPresent()); | ||
Assert.assertEquals(credentialTemplate.getTemplate(), optional.get().getTemplate()); | ||
} | ||
|
||
@Test | ||
public void findByInvalidCredentialTypeAndContext_thenFail() { | ||
Assert.assertNotNull(credentialTemplate); | ||
|
||
String requestedCredentialType = "TestCredential,VerifiableCredential"; | ||
Optional<CredentialTemplate> optional = credentialTemplateRepository.findByCredentialTypeAndContext(requestedCredentialType, credentialTemplate.getContext()); | ||
Assert.assertTrue(optional.isEmpty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...ervice/src/test/java/io/mosip/certify/validators/LdpVcCredentialRequestValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.mosip.certify.validators; | ||
|
||
import io.mosip.certify.core.constants.VCFormats; | ||
import io.mosip.certify.core.dto.CredentialDefinition; | ||
import io.mosip.certify.core.dto.CredentialRequest; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class LdpVcCredentialRequestValidatorTest { | ||
@Test | ||
public void checkLdpVcValidatorWithValidCredentialRequest_thenPass() { | ||
CredentialRequest credentialRequest = new CredentialRequest(); | ||
credentialRequest.setFormat(VCFormats.LDP_VC); | ||
credentialRequest.setCredential_definition(new CredentialDefinition()); | ||
assertTrue(LdpVcCredentialRequestValidator.isValidCheck(credentialRequest)); | ||
} | ||
|
||
@Test | ||
public void checkLdpVcValidatorTestWithInvalidCredentialDefinition_thenFail() { | ||
CredentialRequest credentialRequest = new CredentialRequest(); | ||
credentialRequest.setFormat(VCFormats.LDP_VC); | ||
credentialRequest.setClaims(Map.of("client_id", "test-client-id")); | ||
assertFalse(LdpVcCredentialRequestValidator.isValidCheck(credentialRequest)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...vice/src/test/java/io/mosip/certify/validators/MsoMdocCredentialRequestValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.mosip.certify.validators; | ||
|
||
import io.mosip.certify.core.dto.CredentialRequest; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class MsoMdocCredentialRequestValidatorTest { | ||
@Test | ||
public void checkMsoMdocValidatorWithValidCredentialRequest_thenPass() { | ||
CredentialRequest credentialRequest = new CredentialRequest(); | ||
credentialRequest.setDoctype("mdoc-doctype"); | ||
credentialRequest.setClaims(Map.of("client_id", "test-client-id")); | ||
assertTrue(MsoMdocCredentialRequestValidator.isValidCheck(credentialRequest)); | ||
} | ||
|
||
@Test | ||
public void checkMsoMdocValidatorWithInvalidDoctype_thenFail() { | ||
CredentialRequest credentialRequest = new CredentialRequest(); | ||
credentialRequest.setClaims(Map.of("client_id", "test-client-id")); | ||
assertFalse(MsoMdocCredentialRequestValidator.isValidCheck(credentialRequest)); | ||
} | ||
|
||
@Test | ||
public void checkMsoMdocValidatorWithEmptyClaims_thenFail() { | ||
CredentialRequest credentialRequest = new CredentialRequest(); | ||
credentialRequest.setDoctype("mdoc-doctype"); | ||
assertFalse(MsoMdocCredentialRequestValidator.isValidCheck(credentialRequest)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: is this line redundant as this is already added in
setup()
?