Skip to content

Commit

Permalink
feat: added test cases for getCredentials api
Browse files Browse the repository at this point in the history
  • Loading branch information
thackerronak committed May 31, 2023
1 parent 32d9587 commit c3e5cd8
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ protected SpecificationUtil<Credential> getSpecificationUtil() {
* @param id the id
* @param issuerIdentifier the issuer identifier
* @param type the type
* @param pageNumber the page number
* @param size the size
* @param sortColumn the sort column
* @param sortType the sort type
* @return the credentials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@
package org.eclipse.tractusx.managedidentitywallets.utils;

import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
import org.eclipse.tractusx.managedidentitywallets.constant.RestURI;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet;
import org.eclipse.tractusx.managedidentitywallets.dao.repository.WalletRepository;
import org.eclipse.tractusx.managedidentitywallets.dto.IssueFrameworkCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.dto.IssueMembershipCredentialRequest;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocument;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.junit.jupiter.api.Assertions;
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.ResponseEntity;

public class TestUtils {

Expand Down Expand Up @@ -66,4 +74,23 @@ public static void checkVC(VerifiableCredential verifiableCredential, MIWSetting
//check expiry date
Assertions.assertEquals(verifiableCredential.getExpirationDate().compareTo(miwSettings.vcExpiryDate().toInstant()), 0);
}

public static ResponseEntity<String> issueMembershipVC(TestRestTemplate restTemplate, String bpn) {
HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders();
IssueMembershipCredentialRequest request = IssueMembershipCredentialRequest.builder().bpn(bpn).build();
HttpEntity<IssueMembershipCredentialRequest> entity = new HttpEntity<>(request, headers);

return restTemplate.exchange(RestURI.CREDENTIALS_ISSUER_MEMBERSHIP, HttpMethod.POST, entity, String.class);
}

public static IssueFrameworkCredentialRequest getIssueFrameworkCredentialRequest(String bpn, String type, String value) {
IssueFrameworkCredentialRequest twinRequest = IssueFrameworkCredentialRequest.builder()
.contractTemplate("http://localhost")
.contractVersion("v1")
.type(type)
.value(value)
.bpn(bpn)
.build();
return twinRequest;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* *******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.vc;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.tractusx.managedidentitywallets.ManagedIdentityWalletsApplication;
import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
import org.eclipse.tractusx.managedidentitywallets.config.TestContextInitializer;
import org.eclipse.tractusx.managedidentitywallets.constant.MIWVerifiableCredentialType;
import org.eclipse.tractusx.managedidentitywallets.constant.RestURI;
import org.eclipse.tractusx.managedidentitywallets.dao.repository.CredentialRepository;
import org.eclipse.tractusx.managedidentitywallets.dao.repository.WalletRepository;
import org.eclipse.tractusx.managedidentitywallets.dto.CreateWalletRequest;
import org.eclipse.tractusx.managedidentitywallets.dto.IssueFrameworkCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.testcontainers.shaded.com.fasterxml.jackson.core.JsonProcessingException;

import java.util.*;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {ManagedIdentityWalletsApplication.class})
@ActiveProfiles("test")
@ContextConfiguration(initializers = {TestContextInitializer.class})
public class CredentialTest {

@Autowired
private CredentialRepository credentialRepository;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private MIWSettings miwSettings;
@Autowired
private WalletRepository walletRepository;
@Autowired
private TestRestTemplate restTemplate;


@Test
void getCredentialsTest403() {

HttpHeaders headers = AuthenticationUtils.getInvalidUserHttpHeaders();

HttpEntity<CreateWalletRequest> entity = new HttpEntity<>(headers);

ResponseEntity<Map> response = restTemplate.exchange(RestURI.CREDENTIALS, HttpMethod.GET, entity, Map.class);

Assertions.assertEquals(HttpStatus.FORBIDDEN.value(), response.getStatusCode().value());
}

@Test
void getCredentials200() throws JsonProcessingException, com.fasterxml.jackson.core.JsonProcessingException {
HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders();

String baseDID = miwSettings.authorityWalletDid();
String bpn = UUID.randomUUID().toString();
String did = "did:web:localhost:" + bpn;
//save wallet
TestUtils.createWallet(bpn, did, walletRepository);
TestUtils.issueMembershipVC(restTemplate, bpn);
String vcList = """
[
{"type":"cx-traceability","value":"ID_3.0_Trace"},
{"type":"cx-sustainability","value":"Sustainability"},
{"type":"cx-resiliency","value":"Resiliency"},
{"type":"cx-quality","value":"Quality"},
{"type":"cx-pcf","value":"PCF"}
]
""";
JSONArray jsonArray = new JSONArray(vcList);

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
IssueFrameworkCredentialRequest request = TestUtils.getIssueFrameworkCredentialRequest(bpn, jsonObject.get("type").toString(), jsonObject.get("value").toString());
HttpEntity<IssueFrameworkCredentialRequest> entity = new HttpEntity<>(request, headers);
ResponseEntity<String> exchange = restTemplate.exchange(RestURI.API_CREDENTIALS_ISSUER_FRAMEWORK, HttpMethod.POST, entity, String.class);
Assertions.assertEquals(exchange.getStatusCode().value(), HttpStatus.CREATED.value());
}


HttpEntity<Map> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(RestURI.CREDENTIALS + "?bpn={bpn}"
, HttpMethod.GET, entity, String.class, bpn);
List<VerifiableCredential> credentialList = getCredentialsFromString(response.getBody());
Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value());
Assertions.assertTrue(Objects.requireNonNull(credentialList).size() == 7);

response = restTemplate.exchange(RestURI.CREDENTIALS + "?holderIdentifier={did}"
, HttpMethod.GET, entity, String.class, did);
credentialList = getCredentialsFromString(response.getBody());
Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value());
Assertions.assertTrue(Objects.requireNonNull(credentialList).size() == 6);

response = restTemplate.exchange(RestURI.CREDENTIALS + "?issuerIdentifier={did}"
, HttpMethod.GET, entity, String.class, baseDID);
credentialList = getCredentialsFromString(response.getBody());
Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value());
Assertions.assertTrue(Objects.requireNonNull(credentialList).size() == 7);

List<String> list = new ArrayList<>();
list.add(MIWVerifiableCredentialType.BPN_CREDENTIAL_CX);
response = restTemplate.exchange(RestURI.CREDENTIALS + "?type={list}"
, HttpMethod.GET, entity, String.class, String.join(",", list));
credentialList = getCredentialsFromString(response.getBody());
Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value());
Assertions.assertTrue(Objects.requireNonNull(credentialList).size() == 1);
}


private List<VerifiableCredential> getCredentialsFromString(String body) throws com.fasterxml.jackson.core.JsonProcessingException {
List<VerifiableCredential> credentialList = new ArrayList<>();

JSONArray array = new JSONArray(body);
if (array.length() == 0) {
return credentialList;
}

for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
ObjectMapper objectMapper = new ObjectMapper();
credentialList.add(new VerifiableCredential(objectMapper.readValue(jsonObject.toString(), Map.class)));
}
return credentialList;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void issueBehaviorTwinCredentialTest201() throws JsonProcessingException, JSONEx

HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders();

IssueFrameworkCredentialRequest twinRequest = getIssueFrameworkCredentialRequest(bpn, type, value);
IssueFrameworkCredentialRequest twinRequest = TestUtils.getIssueFrameworkCredentialRequest(bpn, type, value);

HttpEntity<IssueFrameworkCredentialRequest> entity = new HttpEntity<>(twinRequest, headers);

Expand All @@ -117,7 +117,7 @@ void issuePCFCredentialTest201() throws JsonProcessingException, JSONException {

HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders();

IssueFrameworkCredentialRequest twinRequest = getIssueFrameworkCredentialRequest(bpn, type, value);
IssueFrameworkCredentialRequest twinRequest = TestUtils.getIssueFrameworkCredentialRequest(bpn, type, value);

HttpEntity<IssueFrameworkCredentialRequest> entity = new HttpEntity<>(twinRequest, headers);

Expand All @@ -138,7 +138,7 @@ void issueQualityCredentialTest201() throws JsonProcessingException, JSONExcepti

HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders();

IssueFrameworkCredentialRequest twinRequest = getIssueFrameworkCredentialRequest(bpn, type, value);
IssueFrameworkCredentialRequest twinRequest = TestUtils.getIssueFrameworkCredentialRequest(bpn, type, value);

HttpEntity<IssueFrameworkCredentialRequest> entity = new HttpEntity<>(twinRequest, headers);

Expand All @@ -159,7 +159,7 @@ void issueResiliencyCredentialTest201() throws JsonProcessingException, JSONExce

HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders();

IssueFrameworkCredentialRequest twinRequest = getIssueFrameworkCredentialRequest(bpn, type, value);
IssueFrameworkCredentialRequest twinRequest = TestUtils.getIssueFrameworkCredentialRequest(bpn, type, value);

HttpEntity<IssueFrameworkCredentialRequest> entity = new HttpEntity<>(twinRequest, headers);

Expand All @@ -180,7 +180,7 @@ void issueSustainabilityCredentialTest201() throws JsonProcessingException, JSON

HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders();

IssueFrameworkCredentialRequest twinRequest = getIssueFrameworkCredentialRequest(bpn, type, value);
IssueFrameworkCredentialRequest twinRequest = TestUtils.getIssueFrameworkCredentialRequest(bpn, type, value);

HttpEntity<IssueFrameworkCredentialRequest> entity = new HttpEntity<>(twinRequest, headers);

Expand All @@ -202,7 +202,7 @@ void issueTraceabilityCredentialTest201() throws JsonProcessingException, JSONEx

HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders();

IssueFrameworkCredentialRequest twinRequest = getIssueFrameworkCredentialRequest(bpn, type, value);
IssueFrameworkCredentialRequest twinRequest = TestUtils.getIssueFrameworkCredentialRequest(bpn, type, value);

HttpEntity<IssueFrameworkCredentialRequest> entity = new HttpEntity<>(twinRequest, headers);

Expand All @@ -224,7 +224,7 @@ void issueFrameworkCredentialTest400() throws JsonProcessingException, JSONExcep

HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders();

IssueFrameworkCredentialRequest twinRequest = getIssueFrameworkCredentialRequest(bpn, type, value);
IssueFrameworkCredentialRequest twinRequest = TestUtils.getIssueFrameworkCredentialRequest(bpn, type, value);

HttpEntity<IssueFrameworkCredentialRequest> entity = new HttpEntity<>(twinRequest, headers);

Expand Down Expand Up @@ -256,14 +256,5 @@ private void validate(Wallet wallet, String type, String value, ResponseEntity<S
Assertions.assertTrue(vcFromDB.getCredentialSubject().get(0).get("id").equals(wallet.getDid()));
}

private static IssueFrameworkCredentialRequest getIssueFrameworkCredentialRequest(String bpn, String type, String value) {
IssueFrameworkCredentialRequest twinRequest = IssueFrameworkCredentialRequest.builder()
.contractTemplate("http://localhost")
.contractVersion("v1")
.type(type)
.value(value)
.bpn(bpn)
.build();
return twinRequest;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void issueMembershipCredentialTest201() throws JsonProcessingException, JSONExce
//save wallet
Wallet wallet = TestUtils.createWallet(bpn, did, walletRepository);

ResponseEntity<String> response = issueMembershipVC(bpn, did);
ResponseEntity<String> response = TestUtils.issueMembershipVC(restTemplate, bpn);
Assertions.assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value());

ObjectMapper objectMapper = new ObjectMapper();
Expand Down Expand Up @@ -123,21 +123,13 @@ void issueMembershipCredentialWithDuplicateBpn409() {
//save wallet
Wallet wallet = TestUtils.createWallet(bpn, did, walletRepository);

ResponseEntity<String> response = issueMembershipVC(bpn, did);
ResponseEntity<String> response = TestUtils.issueMembershipVC(restTemplate, bpn);
Assertions.assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value());

ResponseEntity<String> duplicateResponse = issueMembershipVC(bpn, did);
ResponseEntity<String> duplicateResponse = TestUtils.issueMembershipVC(restTemplate, bpn);

Assertions.assertEquals(HttpStatus.CONFLICT.value(), duplicateResponse.getStatusCode().value());
}

private ResponseEntity<String> issueMembershipVC(String bpn, String did) {
HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders();
IssueMembershipCredentialRequest request = IssueMembershipCredentialRequest.builder().bpn(bpn).build();
HttpEntity<IssueMembershipCredentialRequest> entity = new HttpEntity<>(request, headers);

return restTemplate.exchange(RestURI.CREDENTIALS_ISSUER_MEMBERSHIP, HttpMethod.POST, entity, String.class);
}


}

0 comments on commit c3e5cd8

Please sign in to comment.