Skip to content

Commit

Permalink
feature: eclipse-tractusx/traceability-foss#639 rework exception mess…
Browse files Browse the repository at this point in the history
…ages for policy handling
  • Loading branch information
ds-lcapellino committed Jul 1, 2024
1 parent fd3ff5f commit 386093a
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ void shouldCatchUsagePolicyExceptionAndPutTombstone() throws EdcClientException

// when
when(submodelFacade.getSubmodelPayload(any(), any(), any(), any())).thenThrow(
new UsagePolicyPermissionException("itemId", null, businessPartnerNumber));
new UsagePolicyPermissionException(List.of(), null, businessPartnerNumber));
when(connectorEndpointsService.fetchConnectorEndpoints(any())).thenReturn(List.of("connector.endpoint.nl"));
final ItemContainer result = relationshipDelegate.process(itemContainerWithShell, jobParameter(),
new AASTransferProcess(), createKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void shouldCatchUsagePolicyExceptionAndPutTombstone() throws EdcClientException

// when
when(submodelFacade.getSubmodelPayload(any(), any(), any(), any())).thenThrow(
new UsagePolicyPermissionException("itemId", null, businessPartnerNumber));
new UsagePolicyPermissionException(List.of(), null, businessPartnerNumber));
when(connectorEndpointsService.fetchConnectorEndpoints(any())).thenReturn(List.of("connector.endpoint.nl"));
final ItemContainer result = submodelDelegate.process(itemContainerShellWithTwoSubmodels,
jobParameterCollectAspects(), new AASTransferProcess(), createKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ private CompletableFuture<NegotiationResponse> startNewNegotiation(final String

if (!policyCheckerService.isValid(catalogItem.getPolicy(), bpn)) {
log.warn("Policy was not allowed, canceling negotiation.");
throw new UsagePolicyPermissionException(catalogItem.getItemId(), catalogItem.getPolicy(),
throw new UsagePolicyPermissionException(policyCheckerService.getValidStoredPolicies(catalogItem.getConnectorId()), catalogItem.getPolicy(),
catalogItem.getConnectorId());
}

if (policyCheckerService.isExpired(catalogItem.getPolicy(), bpn)) {
log.warn("Policy is expired, canceling negotiation.");
throw new UsagePolicyExpiredException(catalogItem.getItemId(), catalogItem.getPolicy(),
throw new UsagePolicyExpiredException(catalogItem.getPolicy(),
catalogItem.getConnectorId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ public class UsagePolicyExpiredException extends EdcClientException {
private final transient Policy policy;
private final String businessPartnerNumber;

public UsagePolicyExpiredException(final String itemId, final Policy policy, final String businessPartnerNumber) {
super("Consumption of asset '" + itemId
+ "' is not permitted as the required catalog offer policies are expired.");
public UsagePolicyExpiredException(final Policy policy, final String businessPartnerNumber) {
super("Policy " + policy + " from " + businessPartnerNumber + " has expired.");
this.policy = policy;
this.businessPartnerNumber = businessPartnerNumber;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
********************************************************************************/
package org.eclipse.tractusx.irs.edc.client.exceptions;

import java.util.List;

import lombok.Getter;
import org.eclipse.edc.policy.model.Policy;
import org.eclipse.tractusx.irs.edc.client.policy.AcceptedPolicy;

/**
* Usage Policy Permission Exception errors in the contract negotiation.
Expand All @@ -35,11 +38,11 @@ public class UsagePolicyPermissionException extends EdcClientException {
private final transient Policy policy;
private final String businessPartnerNumber;

public UsagePolicyPermissionException(final String itemId, final Policy policy,
final String businessPartnerNumber) {
super("Consumption of asset '" + itemId
+ "' is not permitted as the required catalog offer policies do not comply with defined policies.");
this.policy = policy;
public UsagePolicyPermissionException(final List<AcceptedPolicy> acceptedPolicies,
final Policy providedCatalogItemPolicy, final String businessPartnerNumber) {
super("Policies " + acceptedPolicies + " did not match with policy " + providedCatalogItemPolicy + " from "
+ businessPartnerNumber + ".");
this.policy = providedCatalogItemPolicy;
this.businessPartnerNumber = businessPartnerNumber;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private boolean hasExpiredConstraint(final Permission permission, final List<Acc
acceptedPolicy -> acceptedPolicy.validUntil().isBefore(OffsetDateTime.now()));
}

private List<AcceptedPolicy> getValidStoredPolicies(final String bpn) {
public List<AcceptedPolicy> getValidStoredPolicies(final String bpn) {
return policyStore.getAcceptedPolicies(bpn).stream().toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void shouldThrowExceptionWhenPoliciesAreNotAccepted() {
givenThat(get(urlPathEqualTo(SUBMODEL_DATAPLANE_PATH)).willReturn(responseWithStatus(200).withBody("test")));

// Act & Assert
final String errorMessage = "Consumption of asset '5a7ab616-989f-46ae-bdf2-32027b9f6ee6-31b614f5-ec14-4ed2-a509-e7b7780083e7' is not permitted as the required catalog offer policies do not comply with defined policies.";
final String errorMessage = "Policies [] did not match with policy org.eclipse.edc.policy.model.Policy@c10473a from BPNL00000000TEST.";
assertThatExceptionOfType(UsagePolicyPermissionException.class).isThrownBy(
() -> edcSubmodelClient.getSubmodelPayload(CONNECTOR_ENDPOINT_URL, SUBMODEL_DATAPLANE_URL, ASSET_ID, "bpn")
.get()).withMessageEndingWith(errorMessage);
Expand Down

0 comments on commit 386093a

Please sign in to comment.