forked from eclipse-tractusx/managed-identity-wallet
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Resolve the DID document for a given DID or BPN api added
- Loading branch information
1 parent
b13bf28
commit 1368edf
Showing
7 changed files
with
237 additions
and
0 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
43 changes: 43 additions & 0 deletions
43
...in/java/org/eclipse/tractusx/managedidentitywallets/controller/DidDocumentController.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,43 @@ | ||
/* | ||
* ******************************************************************************* | ||
* 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.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.eclipse.tractusx.managedidentitywallets.constant.RestURI; | ||
import org.eclipse.tractusx.managedidentitywallets.service.DidDocumentService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class DidDocumentController { | ||
private final DidDocumentService service; | ||
|
||
@GetMapping(path = RestURI.DID_DOCUMENTS, produces = MediaType.APPLICATION_JSON_VALUE) | ||
public ResponseEntity<String> getDidDocument(@PathVariable(name = "identifier") String identifier) { | ||
return ResponseEntity.status(HttpStatus.OK).body(service.getDidDocument(identifier)); | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
...va/org/eclipse/tractusx/managedidentitywallets/exception/DidDocumentsNotFoundProblem.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,61 @@ | ||
/* | ||
* ******************************************************************************* | ||
* 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.exception; | ||
|
||
/** | ||
* The type Did documents not found problem. | ||
*/ | ||
public class DidDocumentsNotFoundProblem extends RuntimeException { | ||
/** | ||
* Instantiates a new Did documents not found problem. | ||
*/ | ||
public DidDocumentsNotFoundProblem() { | ||
} | ||
|
||
/** | ||
* Instantiates a new Did documents not found problem. | ||
* | ||
* @param message the message | ||
*/ | ||
public DidDocumentsNotFoundProblem(String message) { | ||
super(message); | ||
} | ||
|
||
/** | ||
* Instantiates a new Did documents not found problem. | ||
* | ||
* @param message the message | ||
* @param cause the cause | ||
*/ | ||
public DidDocumentsNotFoundProblem(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
/** | ||
* Instantiates a new Did documents not found problem. | ||
* | ||
* @param cause the cause | ||
*/ | ||
public DidDocumentsNotFoundProblem(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/eclipse/tractusx/managedidentitywallets/service/DidDocumentService.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,46 @@ | ||
/* | ||
* ******************************************************************************* | ||
* 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.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet; | ||
import org.eclipse.tractusx.managedidentitywallets.dao.repository.WalletRepository; | ||
import org.eclipse.tractusx.managedidentitywallets.exception.DidDocumentsNotFoundProblem; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class DidDocumentService { | ||
|
||
private final WalletRepository walletRepository; | ||
|
||
public String getDidDocument(String identifier) { | ||
Wallet wallet = identifier.contains(":") ? walletRepository.getByDid(identifier) : walletRepository.getByBpn(identifier); | ||
if (wallet == null || wallet.getDidDocument() == null) { | ||
throw new DidDocumentsNotFoundProblem("DidDocument not found for identifier " + identifier); | ||
} | ||
return wallet.getDidDocument(); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
src/test/java/org/eclipse/tractusx/managedidentitywallets/DidDocumentsTest.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,74 @@ | ||
/* | ||
* ******************************************************************************* | ||
* 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; | ||
|
||
import org.eclipse.tractusx.managedidentitywallets.config.PostgresSQLContextInitializer; | ||
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.junit.jupiter.api.*; | ||
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.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
import java.util.UUID; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {ManagedIdentityWalletsApplication.class}) | ||
@ActiveProfiles("test") | ||
@ContextConfiguration(initializers = {PostgresSQLContextInitializer.class}) | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
public class DidDocumentsTest { | ||
private final String bpn = "123456789"; | ||
@Autowired | ||
private WalletRepository walletRepository; | ||
@Autowired | ||
private TestRestTemplate restTemplate; | ||
|
||
@Test | ||
@Order(1) | ||
void getDidDocumentInvalidBpn404() { | ||
ResponseEntity<String> response = restTemplate.getForEntity(RestURI.DID_DOCUMENTS, String.class, UUID.randomUUID().toString()); | ||
Assertions.assertEquals(HttpStatus.NOT_FOUND.value(), response.getStatusCode().value()); | ||
} | ||
|
||
@Test | ||
@Order(2) | ||
void getDidDocument200() { | ||
Wallet wallet = Wallet.builder() | ||
.bpn(bpn) | ||
.did("did:web:localhost" + bpn) | ||
.didDocument("{\"@context\":[\"https://w3id.org/did/v1\"],\"id\":\"did:web:localhost%3A8080\",\"verificationMethod\":[{\"id\":\"did:web:localhost%3A8080#key-1\",\"type\":\"Ed25519VerificationKey2020\",\"controller\":\"did:web:localhost%3A8080\",\"publicKeyMultibase\":\"zc37wdjQ4VsLEmX2AoKMHjQNxTtQBdouKbhhwEXLhB1fNJqwFDDaWkqRQ2GTWEhvubaNp8v2ox5dK6B4Efk8n2xMR4so6Ybsgck1BVs3i8WfXNsPNj4Gyz2YSS9rqLpdDv8sfMYdemQa4eq3EmrrkSLNukQrdXvGEtr9cV49CM6cYC6MSXc3S8hxuKh32AbKGURrGDnT9b5j4gj59a7Z4d66EWR1FoLHTZqh8YC96qNboYJyPSCEA6ZgXzRqeKKVwpeSxKXWSD4sa3xEHNeSpLrL4ojXNP3LyNbaHZ2nYqNaQ5pxdnUJCUqKYmiSbyRxbCrZCHSY36xab9XRjpQqQPuDtzrUCJQhpspjg5rd14EABDYzX1x54ZDtiYS9y9j2Pkora3phAyW6EBVBK2u2jo2krLeUgYSWzDTfgsWUJgtT9Pwx6aXvAf68tPQTWJtNoCSsSTBUzAZagosQnGLLTqya3Kgqg9VH1H2KuRfRQVTH29LnfuEa3e9Q2vn8sa6tS74oX2b7tKacndbnbw2CSVmbcuc9qZG8GZEc8ikK1Vvw3ukxTr\"}]}") | ||
.algorithm("ED25519") | ||
.name(bpn) | ||
.build(); | ||
walletRepository.save(wallet); | ||
ResponseEntity<String> response = restTemplate.getForEntity(RestURI.DID_DOCUMENTS, String.class, bpn); | ||
Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value()); | ||
Assertions.assertNotNull(response.getBody()); | ||
System.out.print(response.getBody()); | ||
} | ||
} |