diff --git a/CHANGELOG.md b/CHANGELOG.md index cf0bf34018..11d444d1ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ _**For better traceability add the corresponding GitHub issue number in each cha ## [Unreleased] ### Changed -- EdcPolicyDefinitionService, EdcContractDefinitionService and EdcAssetService return existing resource if it exists in EDC +- EdcPolicyDefinitionService, EdcContractDefinitionService and EdcAssetService return throw AlreadyExist exceptions when Conflict is returned from EDC - Added AssetAdministrationShellDescriptor specificAssetIds support for externalSubjectId required for data provisioning - Registering a job - aspects array is now accepting full urn of aspect model instead of name only, eg. 'urn:bamm:io.catenax.single_level_bom_as_built:2.0.0#SingleLevelBomAsBuilt' instead 'SingleLevelBomAsBuilt'. #439 - Changed the version of irs-registry-client from 1.6.0-SNAPSHOT to 1.6.0 diff --git a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/asset/EdcAssetService.java b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/asset/EdcAssetService.java index 5fe20031ff..9652f7b7ca 100644 --- a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/asset/EdcAssetService.java +++ b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/asset/EdcAssetService.java @@ -35,10 +35,12 @@ import org.eclipse.tractusx.irs.edc.client.asset.model.NotificationType; import org.eclipse.tractusx.irs.edc.client.asset.model.exception.CreateEdcAssetException; import org.eclipse.tractusx.irs.edc.client.asset.model.exception.DeleteEdcAssetException; +import org.eclipse.tractusx.irs.edc.client.asset.model.exception.EdcAssetAlreadyExistsException; import org.eclipse.tractusx.irs.edc.client.transformer.EdcTransformer; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; @@ -95,15 +97,13 @@ private String sendRequest(final Asset request) throws CreateEdcAssetException { transformedPayload.toString(), String.class); final HttpStatusCode responseCode = createEdcDataAssetResponse.getStatusCode(); - if (responseCode.value() == HttpStatus.CONFLICT.value()) { - log.info("{} asset already exists in the EDC", request.getId()); - return request.getId(); - } - if (responseCode.value() == HttpStatus.OK.value()) { return request.getId(); } - } catch (RestClientException e) { + } catch (HttpClientErrorException e) { + if (e.getStatusCode().value() == HttpStatus.CONFLICT.value()) { + throw new EdcAssetAlreadyExistsException("asset already exists in the EDC", e); + } throw new CreateEdcAssetException(e); } throw new CreateEdcAssetException("Failed to create asset %s".formatted(request.getId())); @@ -127,11 +127,10 @@ private Asset createNotificationAssetRequest(final String assetName, final Strin final NotificationMethod notificationMethod, final NotificationType notificationType) { final String assetId = UUID.randomUUID().toString(); final Map properties = Map.of(ASSET_CREATION_PROPERTY_DESCRIPTION, assetName, - ASSET_CREATION_PROPERTY_CONTENT_TYPE, DEFAULT_CONTENT_TYPE, - ASSET_CREATION_PROPERTY_POLICY_ID, DEFAULT_POLICY_ID, ASSET_CREATION_PROPERTY_TYPE, - notificationType.getValue(), ASSET_CREATION_PROPERTY_NOTIFICATION_TYPE, - notificationType.getValue(), ASSET_CREATION_PROPERTY_NOTIFICATION_METHOD, - notificationMethod.getValue()); + ASSET_CREATION_PROPERTY_CONTENT_TYPE, DEFAULT_CONTENT_TYPE, ASSET_CREATION_PROPERTY_POLICY_ID, + DEFAULT_POLICY_ID, ASSET_CREATION_PROPERTY_TYPE, notificationType.getValue(), + ASSET_CREATION_PROPERTY_NOTIFICATION_TYPE, notificationType.getValue(), + ASSET_CREATION_PROPERTY_NOTIFICATION_METHOD, notificationMethod.getValue()); final DataAddress dataAddress = DataAddress.Builder.newInstance() .type(HTTP_DATA) @@ -152,8 +151,8 @@ private Asset createNotificationAssetRequest(final String assetName, final Strin } private Asset createDtrAssetRequest(final String assetId, final String baseUrl) { - final Map properties = Map.of(ASSET_CREATION_PROPERTY_DESCRIPTION, "Digital Twin Registry Asset", ASSET_CREATION_PROPERTY_TYPE, - "data.core.digitalTwinRegistry"); + final Map properties = Map.of(ASSET_CREATION_PROPERTY_DESCRIPTION, + "Digital Twin Registry Asset", ASSET_CREATION_PROPERTY_TYPE, "data.core.digitalTwinRegistry"); final DataAddress dataAddress = DataAddress.Builder.newInstance() .type("DataAddress") diff --git a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/asset/model/exception/EdcAssetAlreadyExistsException.java b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/asset/model/exception/EdcAssetAlreadyExistsException.java new file mode 100644 index 0000000000..69a1b3bbe5 --- /dev/null +++ b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/asset/model/exception/EdcAssetAlreadyExistsException.java @@ -0,0 +1,30 @@ +/******************************************************************************** + * 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.irs.edc.client.asset.model.exception; + +/** + * EdcAssetAlreadyExistsException used when asset already exists + */ + +public class EdcAssetAlreadyExistsException extends RuntimeException { + public EdcAssetAlreadyExistsException(final String message, final Throwable exception) { + super(message, exception); + } +} diff --git a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/model/EdcContractAgreementsResponse.java b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/model/EdcContractAgreementsResponse.java index cb22475adc..0f31f088eb 100644 --- a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/model/EdcContractAgreementsResponse.java +++ b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/model/EdcContractAgreementsResponse.java @@ -36,5 +36,6 @@ public record EdcContractAgreementsResponse(@JsonProperty("@id") String contract @JsonProperty("edc:consumerId") String consumerId, @JsonProperty("edc:contractSigningDate") long contractSigningDate, @JsonProperty("edc:assetId") String assetId, - @JsonProperty("@type") String type) { + @JsonProperty("@type") String type, + @JsonProperty("edc:policy") Object policy) { } diff --git a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/model/exception/EdcContractDefinitionAlreadyExists.java b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/model/exception/EdcContractDefinitionAlreadyExists.java new file mode 100644 index 0000000000..e1030cd2bf --- /dev/null +++ b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/model/exception/EdcContractDefinitionAlreadyExists.java @@ -0,0 +1,30 @@ +/******************************************************************************** + * 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.irs.edc.client.contract.model.exception; + +/** + * EdcContractDefinitionAlreadyExists used when contract definition already exists + */ + +public class EdcContractDefinitionAlreadyExists extends RuntimeException { + public EdcContractDefinitionAlreadyExists(final String message, final Throwable exception) { + super(message, exception); + } +} diff --git a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractDefinitionService.java b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractDefinitionService.java index 05893fed03..646df1633a 100644 --- a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractDefinitionService.java +++ b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractDefinitionService.java @@ -21,6 +21,8 @@ import static org.eclipse.tractusx.irs.edc.client.configuration.JsonLdConfiguration.NAMESPACE_EDC; +import java.util.UUID; + import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.eclipse.tractusx.irs.edc.client.EdcConfiguration; @@ -28,10 +30,11 @@ import org.eclipse.tractusx.irs.edc.client.contract.model.EdcContractDefinitionCriteria; import org.eclipse.tractusx.irs.edc.client.contract.model.EdcCreateContractDefinitionRequest; import org.eclipse.tractusx.irs.edc.client.contract.model.exception.CreateEdcContractDefinitionException; +import org.eclipse.tractusx.irs.edc.client.contract.model.exception.EdcContractDefinitionAlreadyExists; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestClientException; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; /** @@ -52,8 +55,9 @@ public class EdcContractDefinitionService { public String createContractDefinition(final String assetId, final String policyId) throws CreateEdcContractDefinitionException { + final String contractId = UUID.randomUUID().toString(); final EdcCreateContractDefinitionRequest createContractDefinitionRequest = createContractDefinitionRequest( - assetId, policyId); + assetId, policyId, contractId); final ResponseEntity createContractDefinitionResponse; try { createContractDefinitionResponse = restTemplate.postForEntity( @@ -62,19 +66,16 @@ public String createContractDefinition(final String assetId, final String policy final HttpStatusCode responseCode = createContractDefinitionResponse.getStatusCode(); - if (responseCode.value() == HttpStatus.CONFLICT.value()) { - log.info("{} contract definition already exists in the EDC", policyId); - - return policyId; - } - if (responseCode.value() == HttpStatus.OK.value()) { return policyId; } throw new CreateEdcContractDefinitionException( - "Failed to create EDC contract definition for %s notification asset id".formatted(assetId)); - } catch (RestClientException e) { + "Failed to create EDC contract definition for %s asset id".formatted(assetId)); + } catch (HttpClientErrorException e) { + if (e.getStatusCode().value() == HttpStatus.CONFLICT.value()) { + throw new EdcContractDefinitionAlreadyExists("Contract definition already exists in the EDC", e); + } log.error( "Failed to create edc contract definition for {} notification asset and {} policy definition id. Reason: ", assetId, policyId, e); @@ -85,7 +86,7 @@ public String createContractDefinition(final String assetId, final String policy } public EdcCreateContractDefinitionRequest createContractDefinitionRequest(final String assetId, - final String accessPolicyId) { + final String accessPolicyId, final String contractId) { final EdcContractDefinitionCriteria edcContractDefinitionCriteria = EdcContractDefinitionCriteria.builder() .type(ASSET_SELECTOR_TYPE) .operandLeft( @@ -102,7 +103,7 @@ public EdcCreateContractDefinitionRequest createContractDefinitionRequest(final .edcContext(edcContext) .type(CONTRACT_DEFINITION_TYPE) .accessPolicyId(accessPolicyId) - .contractDefinitionId(accessPolicyId) + .contractDefinitionId(contractId) .assetsSelector(edcContractDefinitionCriteria) .build(); } diff --git a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/policy/model/exception/EdcPolicyDefinitionAlreadyExists.java b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/policy/model/exception/EdcPolicyDefinitionAlreadyExists.java new file mode 100644 index 0000000000..cd46e0ed70 --- /dev/null +++ b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/policy/model/exception/EdcPolicyDefinitionAlreadyExists.java @@ -0,0 +1,30 @@ +/******************************************************************************** + * 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.irs.edc.client.policy.model.exception; + +/** + * EdcPolicyDefinitionAlreadyExists used when policy already exists + */ + +public class EdcPolicyDefinitionAlreadyExists extends RuntimeException { + public EdcPolicyDefinitionAlreadyExists(final String message, final Throwable exception) { + super(message, exception); + } +} diff --git a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/policy/service/EdcPolicyDefinitionService.java b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/policy/service/EdcPolicyDefinitionService.java index cd4339efaa..1fbe9c9080 100644 --- a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/policy/service/EdcPolicyDefinitionService.java +++ b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/policy/service/EdcPolicyDefinitionService.java @@ -36,9 +36,11 @@ import org.eclipse.tractusx.irs.edc.client.policy.model.EdcPolicyPermissionConstraintExpression; import org.eclipse.tractusx.irs.edc.client.policy.model.exception.CreateEdcPolicyDefinitionException; import org.eclipse.tractusx.irs.edc.client.policy.model.exception.DeleteEdcPolicyDefinitionException; +import org.eclipse.tractusx.irs.edc.client.policy.model.exception.EdcPolicyDefinitionAlreadyExists; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; @@ -80,20 +82,16 @@ public String createAccessPolicy(final EdcCreatePolicyDefinitionRequest policyRe try { createPolicyDefinitionResponse = restTemplate.postForEntity( config.getControlplane().getEndpoint().getPolicyDefinition(), policyRequest, String.class); - } catch (RestClientException e) { - log.error("Failed to create EDC notification asset policy. Reason: ", e); - + } catch (HttpClientErrorException e) { + log.error("Failed to create EDC policy definition. Reason: ", e); + if (e.getStatusCode().value() == HttpStatus.CONFLICT.value()) { + throw new EdcPolicyDefinitionAlreadyExists("Policy definition already exists in the EDC", e); + } throw new CreateEdcPolicyDefinitionException(e); } final HttpStatusCode responseCode = createPolicyDefinitionResponse.getStatusCode(); - if (responseCode.value() == HttpStatus.CONFLICT.value()) { - log.info("Notification asset policy definition already exists in the EDC"); - - return policyRequest.getPolicyDefinitionId(); - } - if (responseCode.value() == HttpStatus.OK.value()) { return policyRequest.getPolicyDefinitionId(); } diff --git a/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/asset/EdcAssetServiceTest.java b/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/asset/EdcAssetServiceTest.java index 73c2ffa079..bf78328eee 100644 --- a/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/asset/EdcAssetServiceTest.java +++ b/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/asset/EdcAssetServiceTest.java @@ -49,6 +49,7 @@ import org.eclipse.tractusx.irs.edc.client.asset.model.NotificationType; import org.eclipse.tractusx.irs.edc.client.asset.model.exception.CreateEdcAssetException; import org.eclipse.tractusx.irs.edc.client.asset.model.exception.DeleteEdcAssetException; +import org.eclipse.tractusx.irs.edc.client.asset.model.exception.EdcAssetAlreadyExistsException; import org.eclipse.tractusx.irs.edc.client.transformer.EdcTransformer; import org.json.JSONException; import org.junit.jupiter.api.BeforeEach; @@ -57,7 +58,9 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.skyscreamer.jsonassert.JSONAssert; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @@ -93,8 +96,10 @@ void setUp() { @Test void testAssetCreateRequestStructure() throws JSONException, JsonProcessingException { - Map properties = Map.of("https://w3id.org/edc/v0.0.1/ns/description", "endpoint to qualityinvestigation receive", - "https://w3id.org/edc/v0.0.1/ns/contenttype", "application/json", "https://w3id.org/edc/v0.0.1/ns/policy-id", "use-eu", "https://w3id.org/edc/v0.0.1/ns/type", "receive", "https://w3id.org/edc/v0.0.1/ns/notificationtype", + Map properties = Map.of("https://w3id.org/edc/v0.0.1/ns/description", + "endpoint to qualityinvestigation receive", "https://w3id.org/edc/v0.0.1/ns/contenttype", + "application/json", "https://w3id.org/edc/v0.0.1/ns/policy-id", "use-eu", + "https://w3id.org/edc/v0.0.1/ns/type", "receive", "https://w3id.org/edc/v0.0.1/ns/notificationtype", "qualityinvestigation", "https://w3id.org/edc/v0.0.1/ns/notificationmethod", "receive"); DataAddress dataAddress = DataAddress.Builder.newInstance() @@ -224,33 +229,33 @@ void givenDeleteAsset_whenOk_ThenReturnCreatedAssetId() throws DeleteEdcAssetExc } @Test - void givenCreateDtrAsset_whenOK_ThenThrowException() { + void givenCreateDtrAsset_whenBadRequest_ThenThrowException() { // given when(edcConfiguration.getControlplane()).thenReturn(controlplaneConfig); when(controlplaneConfig.getEndpoint()).thenReturn(endpointConfig); when(endpointConfig.getAsset()).thenReturn("/management/v2/assets"); String baseUrl = "http://test.test"; String assetName = "asset1"; - doThrow(new RestClientException("Surprise")).when(restTemplate) - .postForEntity(any(String.class), any(String.class), any()); + doThrow(HttpClientErrorException.create("Surprise", HttpStatus.BAD_REQUEST, "", null, null, null)).when( + restTemplate).postForEntity(any(String.class), any(String.class), any()); // when/then assertThrows(CreateEdcAssetException.class, () -> service.createDtrAsset(baseUrl, assetName)); } @Test - void givenCreateDtrAsset_whenTemplateException_ThenThrowException() { + void givenCreateDtrAsset_whenConflict_ThenThrowException() { // given when(edcConfiguration.getControlplane()).thenReturn(controlplaneConfig); when(controlplaneConfig.getEndpoint()).thenReturn(endpointConfig); when(endpointConfig.getAsset()).thenReturn("/management/v2/assets"); String baseUrl = "http://test.test"; String assetName = "asset1"; - doThrow(new RestClientException("Surprise")).when(restTemplate) - .postForEntity(any(String.class), any(String.class), any()); + doThrow(HttpClientErrorException.create("Surprise", HttpStatus.CONFLICT, "", null, null, null)).when( + restTemplate).postForEntity(any(String.class), any(String.class), any()); // when/then - assertThrows(CreateEdcAssetException.class, () -> service.createDtrAsset(baseUrl, assetName)); + assertThrows(EdcAssetAlreadyExistsException.class, () -> service.createDtrAsset(baseUrl, assetName)); } @Test diff --git a/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractAgreementServiceTest.java b/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractAgreementServiceTest.java index 7af50f54a5..4ef37bea78 100644 --- a/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractAgreementServiceTest.java +++ b/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractAgreementServiceTest.java @@ -69,10 +69,12 @@ void shouldReturnContractAgreements() throws ContractAgreementException { List contractAgreementIds = List.of("contractAgreementId"); final EdcContractAgreementsResponse[] edcContractAgreementsResponse = new EdcContractAgreementsResponse[1]; - edcContractAgreementsResponse[0] = EdcContractAgreementsResponse.builder().contractAgreementId("id") + edcContractAgreementsResponse[0] = EdcContractAgreementsResponse.builder() + .contractAgreementId("id") .assetId("assetId") .consumerId("consumerId") .providerId("providerId") + .policy("theProvidedPolicy") .build(); when(restTemplate.exchange(anyString(), eq(HttpMethod.POST), any(), eq(EdcContractAgreementsResponse[].class))).thenReturn( @@ -88,6 +90,8 @@ void shouldReturnContractAgreements() throws ContractAgreementException { eq("https://irs-consumer-controlplane.dev.demo.net/data/management/v2/contractagreements/request"), any(), any(), eq(EdcContractAgreementsResponse[].class)); assertThat(contractAgreements).isNotNull(); + assertThat(contractAgreements.get(0).policy()).isNotNull(); + } @Test diff --git a/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractDefinitionServiceTest.java b/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractDefinitionServiceTest.java index 35af75750a..7fc5545e76 100644 --- a/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractDefinitionServiceTest.java +++ b/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractDefinitionServiceTest.java @@ -27,6 +27,7 @@ import org.eclipse.tractusx.irs.edc.client.EdcConfiguration; import org.eclipse.tractusx.irs.edc.client.contract.model.EdcCreateContractDefinitionRequest; import org.eclipse.tractusx.irs.edc.client.contract.model.exception.CreateEdcContractDefinitionException; +import org.eclipse.tractusx.irs.edc.client.contract.model.exception.EdcContractDefinitionAlreadyExists; import org.json.JSONException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -36,6 +37,7 @@ import org.skyscreamer.jsonassert.JSONAssert; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import org.testcontainers.shaded.com.fasterxml.jackson.core.JsonProcessingException; @@ -66,9 +68,11 @@ void testCreateContractDefinition() throws JsonProcessingException, JSONExceptio // given String assetId = "Asset1"; String policyId = "Policy1"; + String contractId = "ContractId1"; // when - EdcCreateContractDefinitionRequest request = service.createContractDefinitionRequest(assetId, policyId); + EdcCreateContractDefinitionRequest request = service.createContractDefinitionRequest(assetId, policyId, + contractId); // then JSONAssert.assertEquals(""" @@ -77,7 +81,7 @@ void testCreateContractDefinition() throws JsonProcessingException, JSONExceptio "edc": "https://w3id.org/edc/v0.0.1/ns/" }, "@type": "ContractDefinition", - "@id": "Policy1", + "@id": "ContractId1", "accessPolicyId": "Policy1", "contractPolicyId": "Policy1", "assetsSelector": { @@ -115,13 +119,12 @@ void givenCreateContractDefinition_whenConflict_thenThrowException() throws Crea final String assetId = "Asset1"; final String policyId = "Policy1"; when(restTemplate.postForEntity(any(String.class), any(EdcCreateContractDefinitionRequest.class), - any())).thenReturn(ResponseEntity.status(HttpStatus.CONFLICT.value()).build()); + any())).thenThrow( + HttpClientErrorException.create("Surprise", HttpStatus.CONFLICT, "", null, null, null)); - // when - final String result = service.createContractDefinition(assetId, policyId); - - // then - assertThat(result).isEqualTo(policyId); + // when/then + assertThrows(EdcContractDefinitionAlreadyExists.class, + () -> service.createContractDefinition(assetId, policyId)); } @Test @@ -147,7 +150,7 @@ void givenCreateContractDefinition_whenRestClientException_thenThrowException() String assetId = "Asset1"; String policyId = "Policy1"; when(restTemplate.postForEntity(any(String.class), any(EdcCreateContractDefinitionRequest.class), - any())).thenThrow(new RestClientException("Surprise")); + any())).thenThrow( HttpClientErrorException.create("Surprise", HttpStatus.INTERNAL_SERVER_ERROR, "", null, null, null)); assertThrows(CreateEdcContractDefinitionException.class, () -> service.createContractDefinition(assetId, policyId)); diff --git a/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/policy/service/EdcPolicyDefinitionServiceTest.java b/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/policy/service/EdcPolicyDefinitionServiceTest.java index a5a80916dd..8815c33550 100644 --- a/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/policy/service/EdcPolicyDefinitionServiceTest.java +++ b/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/policy/service/EdcPolicyDefinitionServiceTest.java @@ -33,6 +33,7 @@ import org.eclipse.tractusx.irs.edc.client.policy.model.EdcCreatePolicyDefinitionRequest; import org.eclipse.tractusx.irs.edc.client.policy.model.exception.CreateEdcPolicyDefinitionException; import org.eclipse.tractusx.irs.edc.client.policy.model.exception.DeleteEdcPolicyDefinitionException; +import org.eclipse.tractusx.irs.edc.client.policy.model.exception.EdcPolicyDefinitionAlreadyExists; import org.json.JSONException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -42,6 +43,7 @@ import org.skyscreamer.jsonassert.JSONAssert; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @@ -133,13 +135,19 @@ void givenCreatePolicy_whenConflict_thenReturnExstingPolicyId() throws CreateEdc when(endpointConfig.getPolicyDefinition()).thenReturn("/management/v2/policydefinitions"); final String policyName = "policyName"; when(restTemplate.postForEntity(any(String.class), any(EdcCreatePolicyDefinitionRequest.class), - any())).thenReturn(ResponseEntity.status(HttpStatus.CONFLICT.value()).build()); + any())).thenThrow( + HttpClientErrorException.create( + "Surprise", + HttpStatus.CONFLICT, + "", + null, + null, + null + ) + ); - // when - final String result = service.createAccessPolicy(policyName); - - // then - assertThat(result).isNotBlank(); + // when/then + assertThrows(EdcPolicyDefinitionAlreadyExists.class, () -> service.createAccessPolicy(policyName)); } @Test @@ -163,7 +171,14 @@ void givenCreatePolicy_whenRestClientException_thenThrowException() { when(endpointConfig.getPolicyDefinition()).thenReturn("/management/v2/policydefinitions"); String policyName = "policyName"; when(restTemplate.postForEntity(any(String.class), any(EdcCreatePolicyDefinitionRequest.class), - any())).thenThrow(new RestClientException("Surprise")); + any())).thenThrow(HttpClientErrorException.create( + "Surprise", + HttpStatus.EARLY_HINTS, + "", + null, + null, + null + )); assertThrows(CreateEdcPolicyDefinitionException.class, () -> service.createAccessPolicy(policyName)); }