diff --git a/CHANGELOG.md b/CHANGELOG.md index 1894a7ca52..f6c42bc754 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 _**For better traceability add the corresponding GitHub issue number in each changelog entry, please.**_ ## [Unreleased] +### Added +- Extended EdcPolicyDefinitionService to check if a policy in the edc exists ## [4.8.0] - 2024-03-18 ### Changed diff --git a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/policy/model/exception/GetEdcPolicyDefinitionException.java b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/policy/model/exception/GetEdcPolicyDefinitionException.java new file mode 100644 index 0000000000..9f93e3a0de --- /dev/null +++ b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/policy/model/exception/GetEdcPolicyDefinitionException.java @@ -0,0 +1,34 @@ +/******************************************************************************** + * 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; + +/** + * GetEdcPolicyDefinitionException used policy creation failed case + */ + +public class GetEdcPolicyDefinitionException extends Exception { + + public GetEdcPolicyDefinitionException(final String message) { + super(message); + } + + public GetEdcPolicyDefinitionException(final Throwable cause) { + super(cause); + } +} 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 1fbe9c9080..80f88f547f 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 @@ -37,6 +37,7 @@ 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.eclipse.tractusx.irs.edc.client.policy.model.exception.GetEdcPolicyDefinitionException; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; @@ -99,6 +100,28 @@ public String createAccessPolicy(final EdcCreatePolicyDefinitionRequest policyRe throw new CreateEdcPolicyDefinitionException("Failed to create EDC policy definition for asset"); } + public boolean policyDefinitionExists(final String policyName) throws GetEdcPolicyDefinitionException { + + try { + final ResponseEntity getPolicyDefinitionRequest = restTemplate.getForEntity( + config.getControlplane().getEndpoint().getPolicyDefinition() + "/" + policyName, String.class); + + final HttpStatusCode responseCode = getPolicyDefinitionRequest.getStatusCode(); + + if (responseCode.value() == HttpStatus.OK.value()) { + return true; + } + } catch (HttpClientErrorException e) { + if (e.getStatusCode().value() == HttpStatus.NOT_FOUND.value()) { + log.info(String.format("Policy with id %s not found within the edc", policyName)); + return false; + } else { + throw new GetEdcPolicyDefinitionException(e); + } + } + return false; + } + public EdcCreatePolicyDefinitionRequest createPolicyDefinition(final String policyName, final String accessPolicyId) { final EdcPolicyPermissionConstraintExpression constraint = EdcPolicyPermissionConstraintExpression.builder() 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 8815c33550..66d59878c9 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 @@ -22,6 +22,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -34,6 +36,7 @@ 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.eclipse.tractusx.irs.edc.client.policy.model.exception.GetEdcPolicyDefinitionException; import org.json.JSONException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -127,6 +130,42 @@ void givenPolicy_WhenCreateAccessPolicy_ThenCreateIt() throws CreateEdcPolicyDef assertThat(result).isNotBlank(); } + @Test + void givenPolicyName_WhenGetPolicy_ThenExists() throws GetEdcPolicyDefinitionException { + // given + when(edcConfiguration.getControlplane()).thenReturn(controlplaneConfig); + when(controlplaneConfig.getEndpoint()).thenReturn(endpointConfig); + String policyName = "policyName"; + when(endpointConfig.getPolicyDefinition()).thenReturn("/management/v2/policydefinitions" + "/" + policyName); + + ResponseEntity responseEntity = ResponseEntity.ok("Mocked response"); + when(restTemplate.getForEntity(anyString(), eq(String.class))).thenReturn(responseEntity); + + // when + boolean result = service.policyDefinitionExists(policyName); + + // then + assertThat(result).isTrue(); + } + + @Test + void givenPolicyName_WhenGetPolicy_ThenNotExists() throws GetEdcPolicyDefinitionException { + // given + when(edcConfiguration.getControlplane()).thenReturn(controlplaneConfig); + when(controlplaneConfig.getEndpoint()).thenReturn(endpointConfig); + String policyName = "policyName"; + when(endpointConfig.getPolicyDefinition()).thenReturn("/management/v2/policydefinitions" + "/" + policyName); + + ResponseEntity responseEntity = ResponseEntity.notFound().build(); + when(restTemplate.getForEntity(anyString(), eq(String.class))).thenReturn(responseEntity); + + // when + boolean result = service.policyDefinitionExists(policyName); + + // then + assertThat(result).isFalse(); + } + @Test void givenCreatePolicy_whenConflict_thenReturnExstingPolicyId() throws CreateEdcPolicyDefinitionException { // given @@ -136,15 +175,7 @@ void givenCreatePolicy_whenConflict_thenReturnExstingPolicyId() throws CreateEdc final String policyName = "policyName"; when(restTemplate.postForEntity(any(String.class), any(EdcCreatePolicyDefinitionRequest.class), any())).thenThrow( - HttpClientErrorException.create( - "Surprise", - HttpStatus.CONFLICT, - "", - null, - null, - null - ) - ); + HttpClientErrorException.create("Surprise", HttpStatus.CONFLICT, "", null, null, null)); // when/then assertThrows(EdcPolicyDefinitionAlreadyExists.class, () -> service.createAccessPolicy(policyName)); @@ -171,14 +202,8 @@ 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(HttpClientErrorException.create( - "Surprise", - HttpStatus.EARLY_HINTS, - "", - null, - null, - null - )); + any())).thenThrow( + HttpClientErrorException.create("Surprise", HttpStatus.EARLY_HINTS, "", null, null, null)); assertThrows(CreateEdcPolicyDefinitionException.class, () -> service.createAccessPolicy(policyName)); } @@ -193,8 +218,7 @@ void givenDeletePolicy_whenRestClientException_thenThrowException() { doThrow(new RestClientException("Surprise")).when(restTemplate).delete(any(String.class)); // when/then - assertThrows(DeleteEdcPolicyDefinitionException.class, - () -> service.deleteAccessPolicy(policyName)); + assertThrows(DeleteEdcPolicyDefinitionException.class, () -> service.deleteAccessPolicy(policyName)); } @Test