Skip to content

Commit

Permalink
feat: Paggination support added in list VC API
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-vavdiya committed Jun 9, 2023
1 parent 0340efb commit b74c41c
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<List<VerifiableCredential>> getCredentials(@RequestParam(required = false) String credentialId,
@RequestParam(required = false) String issuerIdentifier,
@RequestParam(required = false) List<String> 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<PageImpl<VerifiableCredential>> getCredentials(@RequestParam(required = false) String credentialId,
@RequestParam(required = false) String issuerIdentifier,
@RequestParam(required = false) List<String> 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)));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@
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;
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;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -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<List<VerifiableCredential>> getCredentials(@RequestParam(required = false) String credentialId,
@RequestParam(required = false) String holderIdentifier,
@RequestParam(required = false) List<String> 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<PageImpl<VerifiableCredential>> getCredentials(@RequestParam(required = false) String credentialId,
@RequestParam(required = false) String holderIdentifier,
@RequestParam(required = false) List<String> 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)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,7 +87,7 @@ protected SpecificationUtil<HoldersCredential> getSpecificationUtil() {


/**
* Gets credentials.
* Gets list of holder's credentials
*
* @param credentialId the credentialId
* @param issuerIdentifier the issuer identifier
Expand All @@ -96,10 +97,10 @@ protected SpecificationUtil<HoldersCredential> getSpecificationUtil() {
* @param callerBPN the caller bpn
* @return the credentials
*/
public List<VerifiableCredential> getCredentials(String credentialId, String issuerIdentifier, List<String> type, String sortColumn, String sortType, String callerBPN) {
public PageImpl<VerifiableCredential> getCredentials(String credentialId, String issuerIdentifier, List<String> 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);
Expand Down Expand Up @@ -134,7 +135,7 @@ public List<VerifiableCredential> getCredentials(String credentialId, String iss
list.add(credential.getData());
}

return list;
return new PageImpl<>(list, filter.getPageable(), filter.getTotalElements());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -143,10 +144,10 @@ protected SpecificationUtil<IssuersCredential> getSpecificationUtil() {
* @param callerBPN the caller bpn
* @return the credentials
*/
public List<VerifiableCredential> getCredentials(String credentialId, String holderIdentifier, List<String> type, String sortColumn, String sortType, String callerBPN) {
public PageImpl<VerifiableCredential> getCredentials(String credentialId, String holderIdentifier, List<String> 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);
Expand Down Expand Up @@ -180,8 +181,7 @@ public List<VerifiableCredential> getCredentials(String credentialId, String hol
for (IssuersCredential credential : filter.getContent()) {
list.add(credential.getData());
}

return list;
return new PageImpl<>(list, filter.getPageable(), filter.getTotalElements());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -200,4 +201,18 @@ public static void checkSummaryCredential(String issuerDID, String holderDID, Ho
.orElse(null);
Assertions.assertNotNull(previousIssuersCredential);
}


@NotNull
public static List<VerifiableCredential> getVerifiableCredentials(ResponseEntity<String> response, ObjectMapper objectMapper) throws JsonProcessingException {
Map<String, Object> map = objectMapper.readValue(response.getBody(), Map.class);

List<Map<String, Object>> vcs = (List<Map<String, Object>>) map.get("content");

List<VerifiableCredential> credentialList = new ArrayList<>();
for (Map<String, Object> stringObjectMap : vcs) {
credentialList.add(new VerifiableCredential(stringObjectMap));
}
return credentialList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,29 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti

ResponseEntity<String> response = restTemplate.exchange(RestURI.CREDENTIALS + "?issuerIdentifier={did}"
, HttpMethod.GET, entity, String.class, baseDID);
List<VerifiableCredential> credentialList = TestUtils.getCredentialsFromString(response.getBody());
List<VerifiableCredential> 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());

List<String> list = new ArrayList<>();
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());

list = new ArrayList<>();
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,24 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti

ResponseEntity<String> response = restTemplate.exchange(RestURI.ISSUERS_CREDENTIALS + "?holderIdentifier={did}"
, HttpMethod.GET, entity, String.class, holderDID);
List<VerifiableCredential> credentialList = TestUtils.getCredentialsFromString(response.getBody());

List<VerifiableCredential> 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());

List<String> list = new ArrayList<>();
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
Expand All @@ -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() {

Expand Down

0 comments on commit b74c41c

Please sign in to comment.