forked from eclipse-tractusx/managed-identity-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added test cases for getCredentials api
- Loading branch information
1 parent
32d9587
commit c3e5cd8
Showing
5 changed files
with
194 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/CredentialTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters