Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
fix: test cases due to revocation client
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-vavdiya committed Sep 5, 2024
1 parent b1c5417 commit 02ccd31
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 2 deletions.
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ ssiLibVersion=0.0.19
wiremockVersion=3.4.2
commonsDaoVersion=0.0.5
appGroup=org.eclipse.tractusx.managedidentitywallets
mockInBeanVersion=boot2-v1.5.2
2 changes: 1 addition & 1 deletion miw/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ dependencies {
testImplementation group: 'org.mockito', name: 'mockito-inline', version: '5.2.0'
testImplementation group: 'org.json', name: 'json', version: '20230227'
testImplementation group: 'com.github.curious-odd-man', name: 'rgxgen', version: '1.4'

testImplementation "com.teketik:mock-in-bean:${mockInBeanVersion}"
}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import lombok.SneakyThrows;
import org.apache.commons.lang3.RandomUtils;
import org.eclipse.tractusx.managedidentitywallets.commons.constant.CredentialStatus;
import org.eclipse.tractusx.managedidentitywallets.commons.constant.StringPool;
import org.eclipse.tractusx.managedidentitywallets.commons.exception.ForbiddenException;
import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
Expand All @@ -41,32 +43,41 @@
import org.eclipse.tractusx.managedidentitywallets.dao.repository.WalletRepository;
import org.eclipse.tractusx.managedidentitywallets.domain.SigningServiceType;
import org.eclipse.tractusx.managedidentitywallets.dto.CreateWalletRequest;
import org.eclipse.tractusx.managedidentitywallets.dto.StatusListRequest;
import org.eclipse.tractusx.managedidentitywallets.revocation.RevocationClient;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocument;
import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialBuilder;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialStatusList2021Entry;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialSubject;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialType;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.mockito.Mockito;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.BitSet;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.zip.GZIPOutputStream;

import static org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames.ACCESS_TOKEN;
import static org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames.SCOPE;
Expand Down Expand Up @@ -265,4 +276,135 @@ public static Map<String, Object> getCredentialAsMap(String holderBpn, String ho

return objectMapper.readValue(credentialWithoutProof.toJson(), Map.class);
}


public static VerifiableCredentialStatusList2021Entry getStatusListEntry(int index) {
return new VerifiableCredentialStatusList2021Entry(Map.of(
"id", "https://example.com/credentials/bpn123456789000/revocation/3#" + index,
"type", "BitstringStatusListEntry",
"statusPurpose", "revocation",
"statusListIndex", String.valueOf(index),
"statusListCredential", "https://example.com/credentials/bpn123456789000/revocation/3"
));
}

public static VerifiableCredentialStatusList2021Entry getStatusListEntry() {
int index = RandomUtils.nextInt(1, 100);
return new VerifiableCredentialStatusList2021Entry(Map.of(
"id", "https://example.com/credentials/bpn123456789000/revocation/3#" + index,
"type", "BitstringStatusListEntry",
"statusPurpose", "revocation",
"statusListIndex", String.valueOf(index),
"statusListCredential", "https://example.com/credentials/bpn123456789000/revocation/3"
));
}

public static void mockGetStatusListEntry(RevocationClient revocationClient, int statusIndex) {
//mock revocation service
Mockito.when(revocationClient.getStatusListEntry(Mockito.any(StatusListRequest.class), Mockito.any(String.class))).thenReturn(TestUtils.getStatusListEntry(statusIndex));
}

public static void mockGetStatusListEntry(RevocationClient revocationClient) {
//mock revocation service
Mockito.when(revocationClient.getStatusListEntry(Mockito.any(StatusListRequest.class), Mockito.any(String.class))).thenReturn(TestUtils.getStatusListEntry());
}


public static void mockRevocationVerification(RevocationClient revocationClient, CredentialStatus credentialStatus) {
Mockito.when(revocationClient.verifyCredentialStatus(Mockito.any(), Mockito.anyString())).thenReturn(Map.of("status", credentialStatus.getName().toLowerCase()));
}

@SneakyThrows
public static void mockGetStatusListVC(RevocationClient revocationClient, ObjectMapper objectMapper, String encodedList) {
String vcString = """
{
"type": [
"VerifiableCredential"
],
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://w3id.org/vc/status-list/2021/v1",
"https://w3id.org/security/suites/jws-2020/v1"
],
"id": "http://localhost:8085/api/v1/revocations/credentials/did:web:BPNL01-revocation",
"issuer": "did:key:z6MkhGTzcvb8BXh5aeoaFvb3XJ3MBmfLRamdYdXyV1pxJBce",
"issuanceDate": "2023-11-30T11:29:17Z",
"issued": "2023-11-30T11:29:17Z",
"validFrom": "2023-11-30T11:29:17Z",
"proof": {
"type": "JsonWebSignature2020",
"created": "2023-11-30T11:29:17Z",
"verificationMethod": "did:key:z6MkhGTzcvb8BXh5aeoaFvb3XJ3MBmfLRamdYdXyV1pxJBce#z6MkhGTzcvb8BXh5aeoaFvb3XJ3MBmfLRamdYdXyV1pxJBce",
"jws": "eyJiNjQiOmZhbHNlLCJjcml0IjpbImI2NCJdLCJhbGciOiJFZERTQSJ9..Iv6H_e4kfLj9dr0COsB2D_ZPpkMoFj3BVXW2iyKFC3q5QtvPWraWfzEDJ5fxtfd5bARJQIP6YhaXdfSRgJpACQ"
},
"credentialSubject": {
"id": "did:key:z6MkhGTzcvb8BXh5aeoaFvb3XJ3MBmfLRamdYdXyV1pxJBce",
"type": "BitstringStatusList",
"statusPurpose": "revocation",
"encodedList": "##encodedList"
}
}
""";
vcString = vcString.replace("##encodedList", encodedList);

VerifiableCredential verifiableCredential = new VerifiableCredential(objectMapper.readValue(vcString, Map.class));
Mockito.when(revocationClient.getStatusListCredential(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(String.class), Mockito.any(String.class))).thenReturn(verifiableCredential);
}

@SneakyThrows
public static void mockGetStatusListVC(RevocationClient revocationClient, ObjectMapper objectMapper) {
String vcString = """
{
"type": [
"VerifiableCredential",
"StatusList2021Credential"
],
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://w3id.org/vc/status-list/2021/v1",
"https://w3id.org/security/suites/jws-2020/v1"
],
"id": "http://localhost:8085/api/v1/revocations/credentials/did:web:BPNL01-revocation",
"issuer": "did:key:z6MkhGTzcvb8BXh5aeoaFvb3XJ3MBmfLRamdYdXyV1pxJBce",
"issuanceDate": "2023-11-30T11:29:17Z",
"issued": "2023-11-30T11:29:17Z",
"validFrom": "2023-11-30T11:29:17Z",
"proof": {
"type": "JsonWebSignature2020",
"creator": "did:key:z6MkhGTzcvb8BXh5aeoaFvb3XJ3MBmfLRamdYdXyV1pxJBce",
"created": "2023-11-30T11:29:17Z",
"verificationMethod": "did:key:z6MkhGTzcvb8BXh5aeoaFvb3XJ3MBmfLRamdYdXyV1pxJBce#z6MkhGTzcvb8BXh5aeoaFvb3XJ3MBmfLRamdYdXyV1pxJBce",
"jws": "eyJiNjQiOmZhbHNlLCJjcml0IjpbImI2NCJdLCJhbGciOiJFZERTQSJ9..Iv6H_e4kfLj9dr0COsB2D_ZPpkMoFj3BVXW2iyKFC3q5QtvPWraWfzEDJ5fxtfd5bARJQIP6YhaXdfSRgJpACQ"
},
"credentialSubject": {
"id": "did:key:z6MkhGTzcvb8BXh5aeoaFvb3XJ3MBmfLRamdYdXyV1pxJBce",
"type": "StatusList2021Credential",
"statusPurpose": "revocation",
"encodedList": "H4sIAAAAAAAA/+3BMQEAAAjAoEqzfzk/SwjUmQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIDXFiqoX4AAAAIA"
}
}
""";

VerifiableCredential verifiableCredential = new VerifiableCredential(objectMapper.readValue(vcString, Map.class));
Mockito.when(revocationClient.getStatusListCredential(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(String.class), Mockito.any(String.class))).thenReturn(verifiableCredential);
}

public static String createEncodedList() throws IOException {
BitSet bitSet = new BitSet(16 * 1024 * 8);

byte[] bitstringBytes = bitSet.toByteArray();
// Perform GZIP compression
ByteArrayOutputStream gzipOutput = new ByteArrayOutputStream();
try (GZIPOutputStream gzipStream = new GZIPOutputStream(gzipOutput)) {
gzipStream.write(bitstringBytes);
}


// Base64 encode the compressed byte array
byte[] compressedBytes = gzipOutput.toByteArray();
String encodedList = Base64.getEncoder().encodeToString(compressedBytes);


return encodedList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.teketik.test.mockinbean.MockInBean;
import lombok.SneakyThrows;
import org.eclipse.tractusx.managedidentitywallets.ManagedIdentityWalletsApplication;
import org.eclipse.tractusx.managedidentitywallets.commons.constant.CredentialStatus;
import org.eclipse.tractusx.managedidentitywallets.commons.constant.StringPool;
import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
import org.eclipse.tractusx.managedidentitywallets.config.TestContextInitializer;
Expand All @@ -36,6 +38,8 @@
import org.eclipse.tractusx.managedidentitywallets.dao.repository.WalletRepository;
import org.eclipse.tractusx.managedidentitywallets.dto.CreateWalletRequest;
import org.eclipse.tractusx.managedidentitywallets.dto.CredentialVerificationRequest;
import org.eclipse.tractusx.managedidentitywallets.revocation.RevocationClient;
import org.eclipse.tractusx.managedidentitywallets.service.revocation.RevocationService;
import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolver;
Expand All @@ -54,7 +58,9 @@
import org.eclipse.tractusx.ssi.lib.proof.LinkedDataProofValidation;
import org.eclipse.tractusx.ssi.lib.serialization.SerializeUtil;
import org.json.JSONException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -95,9 +101,24 @@ class HoldersCredentialTest {
@Autowired
private TestRestTemplate restTemplate;

@MockInBean(RevocationService.class)
private RevocationClient revocationClient;

@Autowired
private IssuersCredentialController credentialController;

@SneakyThrows
@BeforeEach
void beforeEach() {
TestUtils.mockGetStatusListEntry(revocationClient);
TestUtils.mockGetStatusListVC(revocationClient, objectMapper, TestUtils.createEncodedList());
TestUtils.mockRevocationVerification(revocationClient, CredentialStatus.ACTIVE);
}

@AfterEach
void afterEach() {
Mockito.reset(revocationClient);
}

@Test
void issueCredentialTestWithInvalidBPNAccess403() throws JsonProcessingException {
Expand All @@ -122,7 +143,6 @@ void issueCredentialTest200() throws JsonProcessingException {

ResponseEntity<String> response = issueVC(bpn, did, type, headers);


Assertions.assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value());
VerifiableCredential verifiableCredential = new VerifiableCredential(new ObjectMapper().readValue(response.getBody(), Map.class));
Assertions.assertNotNull(verifiableCredential.getProof());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.teketik.test.mockinbean.MockInBean;
import lombok.SneakyThrows;
import org.eclipse.tractusx.managedidentitywallets.ManagedIdentityWalletsApplication;
import org.eclipse.tractusx.managedidentitywallets.commons.constant.CredentialStatus;
import org.eclipse.tractusx.managedidentitywallets.commons.constant.StringPool;
import org.eclipse.tractusx.managedidentitywallets.commons.exception.ForbiddenException;
import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
Expand All @@ -37,15 +40,20 @@
import org.eclipse.tractusx.managedidentitywallets.dao.repository.IssuersCredentialRepository;
import org.eclipse.tractusx.managedidentitywallets.dao.repository.WalletRepository;
import org.eclipse.tractusx.managedidentitywallets.dto.CreateWalletRequest;
import org.eclipse.tractusx.managedidentitywallets.revocation.RevocationClient;
import org.eclipse.tractusx.managedidentitywallets.service.revocation.RevocationService;
import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.eclipse.tractusx.ssi.lib.serialization.SerializeUtil;
import org.json.JSONException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
Expand Down Expand Up @@ -82,6 +90,22 @@ class IssuersCredentialTest {
private IssuersCredentialRepository issuersCredentialRepository;


@MockInBean(RevocationService.class)
private RevocationClient revocationClient;

@SneakyThrows
@BeforeEach
void beforeEach() {
TestUtils.mockGetStatusListEntry(revocationClient);
TestUtils.mockGetStatusListVC(revocationClient, objectMapper, TestUtils.createEncodedList());
TestUtils.mockRevocationVerification(revocationClient, CredentialStatus.ACTIVE);
}

@AfterEach
void afterEach() {
Mockito.reset(revocationClient);
}

@Test
void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingException, JSONException {
String baseBPN = miwSettings.authorityWalletBpn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.JWTParser;
import com.nimbusds.jwt.SignedJWT;
import com.teketik.test.mockinbean.MockInBean;
import lombok.SneakyThrows;
import org.eclipse.tractusx.managedidentitywallets.ManagedIdentityWalletsApplication;
import org.eclipse.tractusx.managedidentitywallets.commons.constant.CredentialStatus;
import org.eclipse.tractusx.managedidentitywallets.commons.constant.StringPool;
import org.eclipse.tractusx.managedidentitywallets.commons.exception.BadDataException;
import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
Expand All @@ -38,17 +40,22 @@
import org.eclipse.tractusx.managedidentitywallets.dao.repository.WalletRepository;
import org.eclipse.tractusx.managedidentitywallets.exception.MissingVcTypesException;
import org.eclipse.tractusx.managedidentitywallets.exception.PermissionViolationException;
import org.eclipse.tractusx.managedidentitywallets.revocation.RevocationClient;
import org.eclipse.tractusx.managedidentitywallets.service.IssuersCredentialService;
import org.eclipse.tractusx.managedidentitywallets.service.PresentationService;
import org.eclipse.tractusx.managedidentitywallets.service.revocation.RevocationService;
import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.TestConstants;
import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils;
import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialSubject;
import org.eclipse.tractusx.ssi.lib.model.verifiable.presentation.VerifiablePresentation;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
Expand Down Expand Up @@ -98,6 +105,23 @@ class PresentationServiceTest {
@Autowired
private WalletRepository walletRepository;

@MockInBean(RevocationService.class)
private RevocationClient revocationClient;

@SneakyThrows
@BeforeEach
void beforeEach() {
TestUtils.mockGetStatusListEntry(revocationClient);
TestUtils.mockGetStatusListVC(revocationClient, objectMapper, TestUtils.createEncodedList());
TestUtils.mockRevocationVerification(revocationClient, CredentialStatus.ACTIVE);
}

@AfterEach
void afterEach() {
Mockito.reset(revocationClient);
}


@SneakyThrows
@Test
void createPresentation200ResponseAsJWT() {
Expand Down
Loading

0 comments on commit 02ccd31

Please sign in to comment.