Skip to content
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
wants to merge 2 commits into
base: release-0.10.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import jakarta.persistence.Entity;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import jakarta.validation.constraints.NotBlank;
import java.time.LocalDateTime;
Expand All @@ -11,6 +12,7 @@
@NoArgsConstructor
@Table(name = "credential_template")
@IdClass(TemplateId.class)
@Data
public class CredentialTemplate {
@NotBlank(message = "Template is mandatory")
@Getter
Expand All @@ -25,7 +27,7 @@ public class CredentialTemplate {
@Setter
private String credentialType;

@NotBlank
@NotNull
@Column(name = "cr_dtimes")
private LocalDateTime createdTimes;

Expand Down
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
when(cacheManager.getCache(VCISSUANCE_CACHE)).thenReturn(cache);

nitpick: is this line redundant as this is already added in setup() ?

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));
}

}
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,34 @@ public void getCredentialIssuerMetadata_valid() {
assertNotNull(actual);
}

@Test
public void getCredentialIssuerMetadataVD11_valid() {
Mockito.when(issuerMetadata.containsKey("vd11")).thenReturn(false);
LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("credential_issuer", "https://localhost:9090");
linkedHashMap.put("credential_endpoint", "https://localhost:9090/v1/certify/issuance/credential");
Mockito.when(issuerMetadata.get("latest")).thenReturn(linkedHashMap);
Map<String, Object> actual = issuanceService.getCredentialIssuerMetadata("vd11");
assertNotNull(actual);
assertTrue(actual.containsKey("credential_issuer"));
assertTrue(actual.containsKey("credential_endpoint"));
assertEquals("https://localhost:9090/v1/certify/issuance/vd11/credential", actual.get("credential_endpoint"));
}

@Test
public void getCredentialIssuerMetadataVD12_valid() {
Mockito.when(issuerMetadata.containsKey("vd12")).thenReturn(false);
LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("credential_issuer", "https://localhost:9090");
linkedHashMap.put("credential_endpoint", "https://localhost:9090/v1/certify/issuance/credential");
Mockito.when(issuerMetadata.get("latest")).thenReturn(linkedHashMap);
Map<String, Object> actual = issuanceService.getCredentialIssuerMetadata("vd12");
assertNotNull(actual);
assertTrue(actual.containsKey("credential_issuer"));
assertTrue(actual.containsKey("credential_endpoint"));
assertEquals("https://localhost:9090/v1/certify/issuance/vd12/credential", actual.get("credential_endpoint"));
}

@Test
public void getCredentialIssuerMetadata_invalid() {
Mockito.when(issuerMetadata.containsKey("latest")).thenReturn(false);
Expand Down
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));
}
}
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ public class VelocityTemplatingEngineImplTest extends TestCase {
@Mock
CredentialTemplateRepository credentialTemplateRepository;

private CredentialTemplate vc1;
private CredentialTemplate vc2;
private CredentialTemplate vc3;

@SneakyThrows
@Before
public void setUp() {
CredentialTemplate vc1 = initTemplate("""
vc1 = initTemplate("""
{
"@context": [
"https://www.w3.org/2018/credentials/v1"]
Expand All @@ -58,7 +62,7 @@ public void setUp() {
""",
"MockVerifiableCredential,VerifiableCredential",
"https://schema.org,https://www.w3.org/2018/credentials/v1");
CredentialTemplate vc2 = initTemplate("""
vc2 = initTemplate("""
{
"@context": [
"https://www.w3.org/ns/credentials/v2"],
Expand All @@ -85,7 +89,7 @@ public void setUp() {
"MockVerifiableCredential,VerifiableCredential",
"https://example.org/Person.json,https://www.w3.org/ns/credentials/v2"
);
CredentialTemplate vc3 = initTemplate("""
vc3 = initTemplate("""
{
"@context": [
"https://www.w3.org/2018/credentials/v1",
Expand Down Expand Up @@ -184,4 +188,20 @@ public void testTemplating_localOnly() {
Assert.fail(e.getMessage());
}
}

@Test
public void getTemplateNameWithValidKey_thenPass() {
String key = "MockVerifiableCredential,VerifiableCredential:https://example.org/Person.json,https://www.w3.org/ns/credentials/v2";
String template = formatter.getTemplate(key);
Assert.assertNotNull(template);
Assert.assertEquals(vc2.getTemplate(), template);
}

@Test
public void getTemplateNameWithInvalidKey_thenFail() {
String key = "MockVerifiableCredential,VerifiableCredential;example.org/Person.json,www.w3.org/ns/credentials/v2";
String template = formatter.getTemplate(key);
Assert.assertNull(template);
// Assert.assertEquals(vc2.getTemplate(), template);
}
}
Loading