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 c77838efe..8b16fdaff 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/HoldersCredentialController.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/HoldersCredentialController.java @@ -22,13 +22,17 @@ package org.eclipse.tractusx.managedidentitywallets.controller; import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.ExampleObject; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; import lombok.RequiredArgsConstructor; import org.eclipse.tractusx.managedidentitywallets.constant.RestURI; 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; import org.springframework.http.ResponseEntity; @@ -62,12 +66,15 @@ public class HoldersCredentialController extends BaseController { */ @Operation(description = "Permission: **view_wallets** OR **view_wallet** (The BPN of holderIdentifier must equal BPN of caller)\n\n Search verifiable credentials with filter criteria", summary = "Query Verifiable Credentials") @GetMapping(path = RestURI.CREDENTIALS, produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getCredentials(@RequestParam(required = false) String credentialId, - @RequestParam(required = false) String issuerIdentifier, - @RequestParam(required = false) List type, - @RequestParam(required = false, defaultValue = "createdAt") String sortColumn, - @RequestParam(required = false, defaultValue = "desc") String sortTpe, Principal principal) { - return ResponseEntity.status(HttpStatus.OK).body(holdersCredentialService.getCredentials(credentialId, issuerIdentifier, type, sortColumn, sortTpe, getBPNFromToken(principal))); + public ResponseEntity> getCredentials(@RequestParam(required = false) String credentialId, + @RequestParam(required = false) String issuerIdentifier, + @RequestParam(required = false) List type, + @RequestParam(required = false, defaultValue = "createdAt") String sortColumn, + @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, + Principal principal) { + return ResponseEntity.status(HttpStatus.OK).body(holdersCredentialService.getCredentials(credentialId, issuerIdentifier, type, sortColumn, sortTpe, pageNumber, size, getBPNFromToken(principal))); } 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 6d33ad821..61714cf1f 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/IssuersCredentialController.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/IssuersCredentialController.java @@ -22,10 +22,13 @@ package org.eclipse.tractusx.managedidentitywallets.controller; import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.ExampleObject; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; import lombok.RequiredArgsConstructor; import org.eclipse.tractusx.managedidentitywallets.constant.RestURI; import org.eclipse.tractusx.managedidentitywallets.dto.IssueDismantlerCredentialRequest; @@ -33,6 +36,7 @@ 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; import org.springframework.http.ResponseEntity; @@ -69,12 +73,14 @@ public class IssuersCredentialController extends BaseController { @Tag(name = API_TAG_VERIFIABLE_CREDENTIAL_ISSUER) @Operation(description = "Permission: **view_wallets** OR **view_wallet** (The BPN of holderIdentifier must equal BPN of caller)\n\n Search verifiable credentials with filter criteria", summary = "Query Verifiable Credentials") @GetMapping(path = RestURI.ISSUERS_CREDENTIALS, produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getCredentials(@RequestParam(required = false) String credentialId, - @RequestParam(required = false) String holderIdentifier, - @RequestParam(required = false) List type, - @RequestParam(required = false, defaultValue = "createdAt") String sortColumn, - @RequestParam(required = false, defaultValue = "desc") String sortTpe, Principal principal) { - return ResponseEntity.status(HttpStatus.OK).body(issuersCredentialService.getCredentials(credentialId, holderIdentifier, type, sortColumn, sortTpe, getBPNFromToken(principal))); + public ResponseEntity> getCredentials(@RequestParam(required = false) String credentialId, + @RequestParam(required = false) String holderIdentifier, + @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, + @Min(0) @Max(Integer.MAX_VALUE) @Parameter(description = "Number of records per page") @RequestParam(required = false, defaultValue = Integer.MAX_VALUE + "") int size, + @RequestParam(required = false, defaultValue = "createdAt") String sortColumn, + @RequestParam(required = false, defaultValue = "desc") String sortTpe, Principal principal) { + return ResponseEntity.status(HttpStatus.OK).body(issuersCredentialService.getCredentials(credentialId, holderIdentifier, type, sortColumn, sortTpe, pageNumber, size, getBPNFromToken(principal))); } /** diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/CommonService.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/CommonService.java index c3e310253..9f3fb8ad2 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/CommonService.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/CommonService.java @@ -39,7 +39,7 @@ public class CommonService { private final WalletRepository walletRepository; /** - * Gets wallet by identifier. + * Gets wallet by identifier(BPN or did). * * @param identifier the identifier * @return the wallet by identifier diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/DidDocumentService.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/DidDocumentService.java index 8f5763d48..a15f482ee 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/DidDocumentService.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/DidDocumentService.java @@ -37,7 +37,7 @@ public class DidDocumentService { private final CommonService commonService; /** - * Gets did document. + * Gets did document by identifier(BPN or Did). * * @param identifier the identifier * @return the did document 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 288247249..664cff8dc 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/HoldersCredentialService.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/HoldersCredentialService.java @@ -41,6 +41,7 @@ import org.eclipse.tractusx.managedidentitywallets.utils.Validate; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; @@ -86,7 +87,7 @@ protected SpecificationUtil getSpecificationUtil() { /** - * Gets credentials. + * Gets list of holder's credentials * * @param credentialId the credentialId * @param issuerIdentifier the issuer identifier @@ -96,10 +97,10 @@ protected SpecificationUtil getSpecificationUtil() { * @param callerBPN the caller bpn * @return the credentials */ - public List getCredentials(String credentialId, String issuerIdentifier, List type, String sortColumn, String sortType, String callerBPN) { + public PageImpl getCredentials(String credentialId, String issuerIdentifier, List type, String sortColumn, String sortType, int pageNumber, int size, String callerBPN) { FilterRequest filterRequest = new FilterRequest(); - filterRequest.setPage(0); - filterRequest.setSize(1000); + filterRequest.setPage(pageNumber); + filterRequest.setSize(size); //Holder must be caller of API Wallet holderWallet = commonService.getWalletByIdentifier(callerBPN); @@ -134,7 +135,7 @@ public List getCredentials(String credentialId, String iss list.add(credential.getData()); } - return list; + 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 8dc26624e..0b7da81a8 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialService.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialService.java @@ -58,6 +58,7 @@ import org.eclipse.tractusx.ssi.lib.resolver.DidDocumentResolverRegistry; import org.eclipse.tractusx.ssi.lib.resolver.DidDocumentResolverRegistryImpl; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; @@ -143,10 +144,10 @@ protected SpecificationUtil getSpecificationUtil() { * @param callerBPN the caller bpn * @return the credentials */ - public List getCredentials(String credentialId, String holderIdentifier, List type, String sortColumn, String sortType, String callerBPN) { + public PageImpl getCredentials(String credentialId, String holderIdentifier, List type, String sortColumn, String sortType, int pageNumber, int size, String callerBPN) { FilterRequest filterRequest = new FilterRequest(); - filterRequest.setPage(0); - filterRequest.setSize(1000); + filterRequest.setSize(size); + filterRequest.setPage(pageNumber); //Issuer must be caller of API Wallet issuerWallet = commonService.getWalletByIdentifier(callerBPN); @@ -180,8 +181,7 @@ public List getCredentials(String credentialId, String hol for (IssuersCredential credential : filter.getContent()) { list.add(credential.getData()); } - - return list; + return new PageImpl<>(list, filter.getPageable(), filter.getTotalElements()); } diff --git a/src/test/java/org/eclipse/tractusx/managedidentitywallets/utils/TestUtils.java b/src/test/java/org/eclipse/tractusx/managedidentitywallets/utils/TestUtils.java index df9697de8..737b733ed 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/utils/TestUtils.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/utils/TestUtils.java @@ -39,6 +39,7 @@ import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialSubject; +import org.jetbrains.annotations.NotNull; import org.json.JSONArray; import org.json.JSONObject; import org.junit.jupiter.api.Assertions; @@ -200,4 +201,18 @@ public static void checkSummaryCredential(String issuerDID, String holderDID, Ho .orElse(null); Assertions.assertNotNull(previousIssuersCredential); } + + + @NotNull + public static List getVerifiableCredentials(ResponseEntity response, ObjectMapper objectMapper) throws JsonProcessingException { + Map map = objectMapper.readValue(response.getBody(), Map.class); + + List> vcs = (List>) map.get("content"); + + List credentialList = new ArrayList<>(); + for (Map stringObjectMap : vcs) { + credentialList.add(new VerifiableCredential(stringObjectMap)); + } + return credentialList; + } } 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 e64fc4963..afffe1db3 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/HoldersCredentialTest.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/HoldersCredentialTest.java @@ -167,13 +167,13 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti ResponseEntity response = restTemplate.exchange(RestURI.CREDENTIALS + "?issuerIdentifier={did}" , HttpMethod.GET, entity, String.class, baseDID); - List credentialList = TestUtils.getCredentialsFromString(response.getBody()); + List credentialList = TestUtils.getVerifiableCredentials(response, objectMapper); Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); Assertions.assertEquals(7, Objects.requireNonNull(credentialList).size()); //5 framework + 1 BPN + 1 Summary response = restTemplate.exchange(RestURI.CREDENTIALS + "?credentialId={id}" , HttpMethod.GET, entity, String.class, credentialList.get(0).getId()); - credentialList = TestUtils.getCredentialsFromString(response.getBody()); + credentialList = TestUtils.getVerifiableCredentials(response, objectMapper); Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); Assertions.assertEquals(1, Objects.requireNonNull(credentialList).size()); @@ -181,7 +181,7 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti list.add(MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX); response = restTemplate.exchange(RestURI.CREDENTIALS + "?type={list}" , HttpMethod.GET, entity, String.class, String.join(",", list)); - credentialList = TestUtils.getCredentialsFromString(response.getBody()); + credentialList = TestUtils.getVerifiableCredentials(response, objectMapper); Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); Assertions.assertEquals(1, Objects.requireNonNull(credentialList).size()); @@ -189,7 +189,7 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti list.add(MIWVerifiableCredentialType.SUMMARY_CREDENTIAL); response = restTemplate.exchange(RestURI.CREDENTIALS + "?type={list}" , HttpMethod.GET, entity, String.class, String.join(",", list)); - credentialList = TestUtils.getCredentialsFromString(response.getBody()); + credentialList = TestUtils.getVerifiableCredentials(response, objectMapper); Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); Assertions.assertEquals(1, credentialList.size()); VerifiableCredentialSubject subject = credentialList.get(0).getCredentialSubject().get(0); 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 e8b9da0bd..f99f983c2 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/IssuersCredentialTest.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/IssuersCredentialTest.java @@ -110,14 +110,16 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti ResponseEntity response = restTemplate.exchange(RestURI.ISSUERS_CREDENTIALS + "?holderIdentifier={did}" , HttpMethod.GET, entity, String.class, holderDID); - List credentialList = TestUtils.getCredentialsFromString(response.getBody()); + + List credentialList = TestUtils.getVerifiableCredentials(response, objectMapper); + Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); Assertions.assertEquals(12, Objects.requireNonNull(credentialList).size()); //5 framework CV + 1 membership + 6 Summary VC response = restTemplate.exchange(RestURI.ISSUERS_CREDENTIALS + "?credentialId={id}" , HttpMethod.GET, entity, String.class, credentialList.get(0).getId()); - credentialList = TestUtils.getCredentialsFromString(response.getBody()); + credentialList = TestUtils.getVerifiableCredentials(response, objectMapper); Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); Assertions.assertEquals(1, Objects.requireNonNull(credentialList).size()); @@ -125,7 +127,7 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti list.add(MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX); response = restTemplate.exchange(RestURI.ISSUERS_CREDENTIALS + "?type={list}" , HttpMethod.GET, entity, String.class, String.join(",", list)); - credentialList = TestUtils.getCredentialsFromString(response.getBody()); + credentialList = TestUtils.getVerifiableCredentials(response, objectMapper); Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); //all VC must be type of MEMBERSHIP_CREDENTIAL_CX @@ -137,12 +139,12 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti list.add(MIWVerifiableCredentialType.SUMMARY_CREDENTIAL); response = restTemplate.exchange(RestURI.ISSUERS_CREDENTIALS + "?type={list}&holderIdentifier={did}" , HttpMethod.GET, entity, String.class, String.join(",", list), holderDID); - credentialList = TestUtils.getCredentialsFromString(response.getBody()); + credentialList = TestUtils.getVerifiableCredentials(response, objectMapper); Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); Assertions.assertEquals(6, Objects.requireNonNull(credentialList).size()); //5 framework CV + 1 membership - } + @Test void issueCredentialsTestWithInvalidRole403() {