Skip to content

Commit

Permalink
Merge pull request #836 from eclipse-tractusx/main
Browse files Browse the repository at this point in the history
synch
  • Loading branch information
ds-ext-kmassalski authored Mar 27, 2024
2 parents 4a35ddd + f731e2e commit 1d9952d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String> 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<String> 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
Expand All @@ -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));
Expand All @@ -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));
}
Expand All @@ -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
Expand Down

0 comments on commit 1d9952d

Please sign in to comment.