diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/command/GetCredentialsCommand.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/command/GetCredentialsCommand.java new file mode 100644 index 000000000..e46811489 --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/command/GetCredentialsCommand.java @@ -0,0 +1,22 @@ +package org.eclipse.tractusx.managedidentitywallets.command; + +import java.util.List; + +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +@Builder +@Getter +@Setter +public class GetCredentialsCommand { + private String credentialId; + private String identifier; + private List type; + private String sortColumn; + private String sortType; + private int pageNumber; + private int size; + private boolean asJwt; + private String callerBPN; +} diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/HoldersCredentialController.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/HoldersCredentialController.java index 3d6621702..e1137a13c 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/HoldersCredentialController.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/HoldersCredentialController.java @@ -29,10 +29,11 @@ import org.eclipse.tractusx.managedidentitywallets.apidocs.HoldersCredentialControllerApiDocs.GetCredentialsApiDocs; import org.eclipse.tractusx.managedidentitywallets.apidocs.HoldersCredentialControllerApiDocs.IssueCredentialApiDoc; import org.eclipse.tractusx.managedidentitywallets.apidocs.IssuersCredentialControllerApiDocs.AsJwtParam; +import org.eclipse.tractusx.managedidentitywallets.command.GetCredentialsCommand; import org.eclipse.tractusx.managedidentitywallets.constant.RestURI; +import org.eclipse.tractusx.managedidentitywallets.constant.StringPool; import org.eclipse.tractusx.managedidentitywallets.dto.CredentialsResponse; import org.eclipse.tractusx.managedidentitywallets.service.HoldersCredentialService; -import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; import org.springframework.data.domain.PageImpl; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -71,7 +72,7 @@ public class HoldersCredentialController extends BaseController { */ @GetCredentialsApiDocs @GetMapping(path = RestURI.CREDENTIALS, produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getCredentials(@Parameter(name = "credentialId", description = "Credential Id", examples = {@ExampleObject(name = "Credential Id", value = "did:web:localhost:BPNL000000000000#12528899-160a-48bd-ba15-f396c3959ae9")}) @RequestParam(required = false) String credentialId, + public ResponseEntity> getCredentials(@Parameter(name = "credentialId", description = "Credential Id", examples = {@ExampleObject(name = "Credential Id", value = "did:web:localhost:BPNL000000000000#12528899-160a-48bd-ba15-f396c3959ae9")}) @RequestParam(required = false) String credentialId, @Parameter(name = "issuerIdentifier", description = "Issuer identifier(did of BPN)", examples = {@ExampleObject(name = "bpn", value = "BPNL000000000000", description = "bpn"), @ExampleObject(description = "did", name = "did", value = "did:web:localhost:BPNL000000000000")}) @RequestParam(required = false) String issuerIdentifier, @Parameter(name = "type", description = "Type of VC", examples = {@ExampleObject(name = "SummaryCredential", value = "SummaryCredential", description = "SummaryCredential"), @ExampleObject(description = "BpnCredential", name = "BpnCredential", value = "BpnCredential")}) @RequestParam(required = false) List type, @Parameter(name = "sortColumn", description = "Sort column name", @@ -87,9 +88,23 @@ public ResponseEntity> getCredentials(@Parameter( @Parameter(name = "sortTpe", description = "Sort order", examples = {@ExampleObject(value = "desc", name = "Descending order"), @ExampleObject(value = "asc", name = "Ascending order")}) @RequestParam(required = false, defaultValue = "desc") String sortTpe, @Min(0) @Max(Integer.MAX_VALUE) @Parameter(description = "Page number, Page number start with zero") @RequestParam(required = false, defaultValue = "0") int pageNumber, @Min(0) @Max(Integer.MAX_VALUE) @Parameter(description = "Number of records per page") @RequestParam(required = false, defaultValue = Integer.MAX_VALUE + "") int size, + @AsJwtParam @RequestParam(name = StringPool.AS_JWT, defaultValue = "false") boolean asJwt, + Principal principal) { log.debug("Received request to get credentials. BPN: {}", getBPNFromToken(principal)); - return ResponseEntity.status(HttpStatus.OK).body(holdersCredentialService.getCredentials(credentialId, issuerIdentifier, type, sortColumn, sortTpe, pageNumber, size, getBPNFromToken(principal))); + final GetCredentialsCommand command; + command = GetCredentialsCommand.builder() + .credentialId(credentialId) + .identifier(issuerIdentifier) + .type(type) + .sortColumn(sortColumn) + .sortType(sortTpe) + .pageNumber(pageNumber) + .size(size) + .asJwt(asJwt) + .callerBPN(getBPNFromToken(principal)) + .build(); + return ResponseEntity.status(HttpStatus.OK).body(holdersCredentialService.getCredentials(command)); } diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/IssuersCredentialController.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/IssuersCredentialController.java index 275147b7f..25bb76f81 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/IssuersCredentialController.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/IssuersCredentialController.java @@ -36,6 +36,7 @@ import org.eclipse.tractusx.managedidentitywallets.apidocs.IssuersCredentialControllerApiDocs.IssueFrameworkCredentialApiDocs; import org.eclipse.tractusx.managedidentitywallets.apidocs.IssuersCredentialControllerApiDocs.IssueVerifiableCredentialUsingBaseWalletApiDocs; import org.eclipse.tractusx.managedidentitywallets.apidocs.IssuersCredentialControllerApiDocs.ValidateVerifiableCredentialApiDocs; +import org.eclipse.tractusx.managedidentitywallets.command.GetCredentialsCommand; import org.eclipse.tractusx.managedidentitywallets.constant.RestURI; import org.eclipse.tractusx.managedidentitywallets.constant.StringPool; import org.eclipse.tractusx.managedidentitywallets.dto.CredentialVerificationRequest; @@ -44,7 +45,6 @@ import org.eclipse.tractusx.managedidentitywallets.dto.IssueFrameworkCredentialRequest; import org.eclipse.tractusx.managedidentitywallets.dto.IssueMembershipCredentialRequest; import org.eclipse.tractusx.managedidentitywallets.service.IssuersCredentialService; -import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; import org.springframework.data.domain.PageImpl; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -90,7 +90,7 @@ public class IssuersCredentialController extends BaseController { */ @GetCredentialsApiDocs @GetMapping(path = RestURI.ISSUERS_CREDENTIALS, produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getCredentials(@Parameter(name = "credentialId", description = "Credential Id", examples = {@ExampleObject(name = "Credential Id", value = "did:web:localhost:BPNL000000000000#12528899-160a-48bd-ba15-f396c3959ae9")}) @RequestParam(required = false) String credentialId, + public ResponseEntity> getCredentials(@Parameter(name = "credentialId", description = "Credential Id", examples = {@ExampleObject(name = "Credential Id", value = "did:web:localhost:BPNL000000000000#12528899-160a-48bd-ba15-f396c3959ae9")}) @RequestParam(required = false) String credentialId, @Parameter(name = "holderIdentifier", description = "Holder identifier(did of BPN)", examples = {@ExampleObject(name = "bpn", value = "BPNL000000000001", description = "bpn"), @ExampleObject(description = "did", name = "did", value = "did:web:localhost:BPNL000000000001")}) @RequestParam(required = false) String holderIdentifier, @Parameter(name = "type", description = "Type of VC", examples = {@ExampleObject(name = "SummaryCredential", value = "SummaryCredential", description = "SummaryCredential"), @ExampleObject(description = "BpnCredential", name = "BpnCredential", value = "BpnCredential")}) @RequestParam(required = false) List type, @Min(0) @Max(Integer.MAX_VALUE) @Parameter(description = "Page number, Page number start with zero") @RequestParam(required = false, defaultValue = "0") int pageNumber, @@ -103,9 +103,23 @@ public ResponseEntity> getCredentials(@Parameter( @ExampleObject(value = "credentialId", name = "Credential id") } ) @RequestParam(required = false, defaultValue = "createdAt") String sortColumn, - @Parameter(name = "sortTpe", description = "Sort order", examples = {@ExampleObject(value = "desc", name = "Descending order"), @ExampleObject(value = "asc", name = "Ascending order")}) @RequestParam(required = false, defaultValue = "desc") String sortTpe, Principal principal) { + @Parameter(name = "sortTpe", description = "Sort order", examples = {@ExampleObject(value = "desc", name = "Descending order"), @ExampleObject(value = "asc", name = "Ascending order")}) @RequestParam(required = false, defaultValue = "desc") String sortTpe, + @AsJwtParam @RequestParam(name = StringPool.AS_JWT, defaultValue = "false") boolean asJwt, + Principal principal) { log.debug("Received request to get credentials. BPN: {}", getBPNFromToken(principal)); - return ResponseEntity.status(HttpStatus.OK).body(issuersCredentialService.getCredentials(credentialId, holderIdentifier, type, sortColumn, sortTpe, pageNumber, size, getBPNFromToken(principal))); + final GetCredentialsCommand command; + command = GetCredentialsCommand.builder() + .credentialId(credentialId) + .identifier(holderIdentifier) + .type(type) + .sortColumn(sortColumn) + .sortType(sortTpe) + .pageNumber(pageNumber) + .size(size) + .asJwt(asJwt) + .callerBPN(getBPNFromToken(principal)) + .build(); + return ResponseEntity.status(HttpStatus.OK).body(issuersCredentialService.getCredentials(command)); } /** diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/HoldersCredentialService.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/HoldersCredentialService.java index 825318b04..af31c3e62 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/HoldersCredentialService.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/HoldersCredentialService.java @@ -32,6 +32,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.text.StringEscapeUtils; +import org.eclipse.tractusx.managedidentitywallets.command.GetCredentialsCommand; import org.eclipse.tractusx.managedidentitywallets.constant.StringPool; import org.eclipse.tractusx.managedidentitywallets.dao.entity.HoldersCredential; import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet; @@ -96,42 +97,49 @@ protected SpecificationUtil getSpecificationUtil() { * @param callerBPN the caller bpn * @return the credentials */ - public PageImpl getCredentials(String credentialId, String issuerIdentifier, List type, String sortColumn, String sortType, int pageNumber, int size, String callerBPN) { + public PageImpl getCredentials(GetCredentialsCommand command) { FilterRequest filterRequest = new FilterRequest(); - filterRequest.setPage(pageNumber); - filterRequest.setSize(size); + filterRequest.setPage(command.getPageNumber()); + filterRequest.setSize(command.getSize()); //Holder must be caller of API - Wallet holderWallet = commonService.getWalletByIdentifier(callerBPN); + Wallet holderWallet = commonService.getWalletByIdentifier(command.getCallerBPN()); filterRequest.appendCriteria(StringPool.HOLDER_DID, Operator.EQUALS, holderWallet.getDid()); - if (StringUtils.hasText(issuerIdentifier)) { - Wallet issuerWallet = commonService.getWalletByIdentifier(issuerIdentifier); + if (StringUtils.hasText(command.getIdentifier())) { + Wallet issuerWallet = commonService.getWalletByIdentifier(command.getIdentifier()); filterRequest.appendCriteria(StringPool.ISSUER_DID, Operator.EQUALS, issuerWallet.getDid()); } - if (StringUtils.hasText(credentialId)) { - filterRequest.appendCriteria(StringPool.CREDENTIAL_ID, Operator.EQUALS, credentialId); + if (StringUtils.hasText(command.getCredentialId())) { + filterRequest.appendCriteria(StringPool.CREDENTIAL_ID, Operator.EQUALS, command.getCredentialId()); } FilterRequest request = new FilterRequest(); - if (!CollectionUtils.isEmpty(type)) { + if (!CollectionUtils.isEmpty(command.getType())) { request.setPage(filterRequest.getPage()); request.setSize(filterRequest.getSize()); request.setCriteriaOperator(CriteriaOperator.OR); - for (String str : type) { + for (String str : command.getType()) { request.appendCriteria(StringPool.TYPE, Operator.CONTAIN, str); } } Sort sort = new Sort(); - sort.setColumn(sortColumn); - sort.setSortType(SortType.valueOf(sortType.toUpperCase())); + sort.setColumn(command.getSortColumn()); + sort.setSortType(SortType.valueOf(command.getSortType().toUpperCase())); filterRequest.setSort(sort); Page filter = filter(filterRequest, request, CriteriaOperator.AND); - List list = new ArrayList<>(filter.getContent().size()); + List list = new ArrayList<>(filter.getContent().size()); + for (HoldersCredential credential : filter.getContent()) { - list.add(credential.getData()); + CredentialsResponse cr = new CredentialsResponse(); + if (command.isAsJwt()) { + cr.setJwt(CommonUtils.vcAsJwt(command.getIdentifier() != null ? commonService.getWalletByIdentifier(command.getIdentifier()) : holderWallet , holderWallet, credential.getData(), walletKeyService)); + } else { + cr.setVc(credential.getData()); + } + list.add(cr); } return new PageImpl<>(list, filter.getPageable(), filter.getTotalElements()); diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialService.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialService.java index 9ed9ece40..40193247e 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialService.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialService.java @@ -36,6 +36,7 @@ import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.commons.text.StringEscapeUtils; +import org.eclipse.tractusx.managedidentitywallets.command.GetCredentialsCommand; import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings; import org.eclipse.tractusx.managedidentitywallets.constant.MIWVerifiableCredentialType; import org.eclipse.tractusx.managedidentitywallets.constant.StringPool; @@ -132,42 +133,48 @@ protected SpecificationUtil getSpecificationUtil() { * @param callerBPN the caller bpn * @return the credentials */ - public PageImpl getCredentials(String credentialId, String holderIdentifier, List type, String sortColumn, String sortType, int pageNumber, int size, String callerBPN) { + public PageImpl getCredentials(GetCredentialsCommand command) { FilterRequest filterRequest = new FilterRequest(); - filterRequest.setSize(size); - filterRequest.setPage(pageNumber); + filterRequest.setSize(command.getSize()); + filterRequest.setPage(command.getPageNumber()); //Issuer must be caller of API - Wallet issuerWallet = commonService.getWalletByIdentifier(callerBPN); + Wallet issuerWallet = commonService.getWalletByIdentifier(command.getCallerBPN()); filterRequest.appendCriteria(StringPool.ISSUER_DID, Operator.EQUALS, issuerWallet.getDid()); - if (StringUtils.hasText(holderIdentifier)) { - Wallet holderWallet = commonService.getWalletByIdentifier(holderIdentifier); + if (StringUtils.hasText(command.getIdentifier())) { + Wallet holderWallet = commonService.getWalletByIdentifier(command.getIdentifier()); filterRequest.appendCriteria(StringPool.HOLDER_DID, Operator.EQUALS, holderWallet.getDid()); } - if (StringUtils.hasText(credentialId)) { - filterRequest.appendCriteria(StringPool.CREDENTIAL_ID, Operator.EQUALS, credentialId); + if (StringUtils.hasText(command.getCredentialId())) { + filterRequest.appendCriteria(StringPool.CREDENTIAL_ID, Operator.EQUALS, command.getCredentialId()); } FilterRequest request = new FilterRequest(); - if (!CollectionUtils.isEmpty(type)) { + if (!CollectionUtils.isEmpty(command.getType())) { request.setPage(filterRequest.getPage()); request.setSize(filterRequest.getSize()); request.setCriteriaOperator(CriteriaOperator.OR); - for (String str : type) { + for (String str : command.getType()) { request.appendCriteria(StringPool.TYPE, Operator.CONTAIN, str); } } Sort sort = new Sort(); - sort.setColumn(sortColumn); - sort.setSortType(SortType.valueOf(sortType.toUpperCase())); + sort.setColumn(command.getSortColumn()); + sort.setSortType(SortType.valueOf(command.getSortType().toUpperCase())); filterRequest.setSort(sort); Page filter = filter(filterRequest, request, CriteriaOperator.AND); - List list = new ArrayList<>(filter.getContent().size()); + List list = new ArrayList<>(filter.getContent().size()); for (IssuersCredential credential : filter.getContent()) { - list.add(credential.getData()); + CredentialsResponse cr = new CredentialsResponse(); + if (command.isAsJwt()) { + cr.setJwt(CommonUtils.vcAsJwt(issuerWallet, command.getIdentifier() != null ? commonService.getWalletByIdentifier(command.getIdentifier()) : issuerWallet, credential.getData(), walletKeyService)); + } else { + cr.setVc(credential.getData()); + } + list.add(cr); } return new PageImpl<>(list, filter.getPageable(), filter.getTotalElements()); } diff --git a/src/test/java/org/eclipse/tractusx/managedidentitywallets/command/GetCredentialsCommandTest.java b/src/test/java/org/eclipse/tractusx/managedidentitywallets/command/GetCredentialsCommandTest.java new file mode 100644 index 000000000..bc0c3ec4a --- /dev/null +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/command/GetCredentialsCommandTest.java @@ -0,0 +1,84 @@ +/* + * ******************************************************************************* + * Copyright (c) 2021,2024 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.command; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.Test; + +class GetCredentialsCommandTest { + + @Test + void testGettersAndSetters() { + // Test data + String credentialId = "cred123"; + String identifier = "user123"; + List type = new ArrayList<>(); + String sortColumn = "name"; + String sortType = "asc"; + int pageNumber = 1; + int size = 10; + boolean asJwt = true; + String callerBPN = "callerBPN123"; + + // Create a GetCredentialsCommand object using the builder + GetCredentialsCommand command = GetCredentialsCommand.builder() + .credentialId(credentialId) + .identifier(identifier) + .type(type) + .sortColumn(sortColumn) + .sortType(sortType) + .pageNumber(pageNumber) + .size(size) + .asJwt(asJwt) + .callerBPN(callerBPN) + .build(); + + // Test getter and setter methods + assertEquals(credentialId, command.getCredentialId()); + assertEquals(identifier, command.getIdentifier()); + assertEquals(type, command.getType()); + assertEquals(sortColumn, command.getSortColumn()); + assertEquals(sortType, command.getSortType()); + assertEquals(pageNumber, command.getPageNumber()); + assertEquals(size, command.getSize()); + assertEquals(asJwt, command.isAsJwt()); + assertEquals(callerBPN, command.getCallerBPN()); + + // Modify some fields using setter methods + String newCredentialId = "newCred123"; + String updatedSortColumn = "updatedName"; + int updatedPageNumber = 2; + + command.setCredentialId(newCredentialId); + command.setSortColumn(updatedSortColumn); + command.setPageNumber(updatedPageNumber); + + // Test modified values + assertEquals(newCredentialId, command.getCredentialId()); + assertEquals(updatedSortColumn, command.getSortColumn()); + assertEquals(updatedPageNumber, command.getPageNumber()); + } +} diff --git a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/HoldersCredentialTest.java b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/HoldersCredentialTest.java index 9cf5f37e5..02a1d8aeb 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/HoldersCredentialTest.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/HoldersCredentialTest.java @@ -1,6 +1,6 @@ /* * ******************************************************************************* - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -45,6 +45,7 @@ import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialSubject; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialType; import org.eclipse.tractusx.ssi.lib.proof.LinkedDataProofValidation; +import org.eclipse.tractusx.ssi.lib.serialization.SerializeUtil; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -206,6 +207,56 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti } + @Test + @DisplayName("Get Credentials as JWT") + void getCredentialsAsJWT200() throws com.fasterxml.jackson.core.JsonProcessingException, JSONException { + + String baseDID = miwSettings.authorityWalletDid(); + String bpn = TestUtils.getRandomBpmNumber(); + String did = DidWebFactory.fromHostnameAndPath(miwSettings.host(), bpn).toString(); + HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders(bpn); + // save wallet + TestUtils.createWallet(bpn, did, walletRepository); + TestUtils.issueMembershipVC(restTemplate, bpn, miwSettings.authorityWalletBpn()); + String vcList = """ + [ + {"type":"TraceabilityCredential"}, + {"type":"SustainabilityCredential"}, + {"type":"ResiliencyCredential"}, + {"type":"QualityCredential"}, + {"type":"PcfCredential"} + ] + """; + 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(StringPool.TYPE).toString()); + HttpEntity entity = new HttpEntity<>(request, + AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn())); // ony base wallet + // can issue VC + ResponseEntity exchange = restTemplate.exchange(RestURI.API_CREDENTIALS_ISSUER_FRAMEWORK, + HttpMethod.POST, entity, String.class); + Assertions.assertEquals(exchange.getStatusCode().value(), HttpStatus.CREATED.value()); + } + + HttpEntity entity = new HttpEntity<>(headers); + + ResponseEntity response = restTemplate.exchange(RestURI.CREDENTIALS + "?issuerIdentifier={did}&asJwt=true", + HttpMethod.GET, entity, String.class, baseDID); + + Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); + Map responseMap = SerializeUtil.fromJson(response.getBody()); + List> vcsAsJwt = (ArrayList>) responseMap.get("content"); + // 5 framework + 1 BPN + 1 Summary + Assertions.assertEquals(7 , vcsAsJwt.size()); + vcsAsJwt.forEach(vc -> { + Assertions.assertNotNull(vc.get(StringPool.VC_JWT_KEY)); + }); + } + + @Test void validateCredentialsWithInvalidVC() throws com.fasterxml.jackson.core.JsonProcessingException { //data setup diff --git a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/IssuersCredentialTest.java b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/IssuersCredentialTest.java index d62073fab..ffde1a3ba 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/IssuersCredentialTest.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/IssuersCredentialTest.java @@ -19,6 +19,7 @@ * ****************************************************************************** */ + package org.eclipse.tractusx.managedidentitywallets.vc; import com.fasterxml.jackson.core.JsonProcessingException; @@ -43,9 +44,12 @@ import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialBuilder; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialSubject; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialType; +import org.eclipse.tractusx.ssi.lib.serialization.SerializeUtil; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -79,7 +83,7 @@ class IssuersCredentialTest { @Test - void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingException { + void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingException, JSONException { String baseBPN = miwSettings.authorityWalletBpn(); String holderBpn = TestUtils.getRandomBpmNumber(); String holderDID = "did:web:localhost:" + holderBpn; @@ -148,6 +152,48 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti } } + @Test + @DisplayName("Get Credentials as JWT") + void getCredentialsAsJWT200() throws com.fasterxml.jackson.core.JsonProcessingException, JSONException { + String baseBPN = miwSettings.authorityWalletBpn(); + String holderBpn = TestUtils.getRandomBpmNumber(); + String holderDID = "did:web:localhost:" + holderBpn; + HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders(baseBPN); + //save wallet + TestUtils.createWallet(holderBpn, holderDID, walletRepository); + TestUtils.issueMembershipVC(restTemplate, holderBpn, baseBPN); + String vcList = """ + [ + {"type":"TraceabilityCredential"}, + {"type":"SustainabilityCredential"}, + {"type":"ResiliencyCredential"}, + {"type":"QualityCredential"}, + {"type":"PcfCredential"} + ] + """; + JSONArray jsonArray = new JSONArray(vcList); + + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject jsonObject = jsonArray.getJSONObject(i); + issueFrameworkCredential(holderBpn, jsonObject.get(StringPool.TYPE).toString()); + } + + HttpEntity entity = new HttpEntity<>(headers); + + ResponseEntity response = restTemplate.exchange(RestURI.ISSUERS_CREDENTIALS + "?holderIdentifier={did}&asJwt=true" + , HttpMethod.GET, entity, String.class, holderDID); + + Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); + Map responseMap = SerializeUtil.fromJson(response.getBody()); + List> vcsAsJwt = (ArrayList>) responseMap.get("content"); + //5 framework CV + 1 membership + 6 Summary VC + Assertions.assertEquals(12, vcsAsJwt.size()); + vcsAsJwt.forEach(vc -> { + Assertions.assertNotNull(vc.get(StringPool.VC_JWT_KEY)); + + }); + } + @Test void issueCredentialsTestWithInvalidRole403() { @@ -262,4 +308,12 @@ private ResponseEntity issueVC(String bpn, String holderDid, String issu return restTemplate.exchange(RestURI.ISSUERS_CREDENTIALS + "?holderDid={did}", HttpMethod.POST, entity, String.class, holderDid); } + private void issueFrameworkCredential(String holderBpn, String type) { + IssueFrameworkCredentialRequest request = TestUtils.getIssueFrameworkCredentialRequest(holderBpn, type); + HttpEntity entity = new HttpEntity<>(request, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn())); //ony base wallet can issue VC + ResponseEntity exchange = null; + exchange = restTemplate.exchange(RestURI.API_CREDENTIALS_ISSUER_FRAMEWORK, HttpMethod.POST, entity, String.class); + Assertions.assertEquals(exchange.getStatusCode().value(), HttpStatus.CREATED.value()); + } + }