Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: #639 rework exception messages for policy handling #742

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,10 +31,10 @@ 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 from " + businessPartnerNumber + " has expired.");
this.policy = policy;
this.businessPartnerNumber = businessPartnerNumber;
}

}
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,15 @@ 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.stream().map(policy -> policy.policy().getPolicyId()).toList()
+ " did not match with policy 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 @@ -37,6 +37,7 @@
import static org.eclipse.tractusx.irs.testing.wiremock.SubmodelFacadeWiremockSupport.TRACEABILITY_1_0;
import static org.eclipse.tractusx.irs.testing.wiremock.WireMockConfig.responseWithStatus;
import static org.eclipse.tractusx.irs.testing.wiremock.WireMockConfig.restTemplateProxy;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -235,13 +236,13 @@ void shouldThrowExceptionWhenPoliciesAreNotAccepted() {
final AcceptedPolicy acceptedPolicy = new AcceptedPolicy(policy("IRS Policy", List.of(permission)),
OffsetDateTime.now().plusYears(1));

when(acceptedPoliciesProvider.getAcceptedPolicies("bpn")).thenReturn(List.of(acceptedPolicy));
when(acceptedPoliciesProvider.getAcceptedPolicies(anyString())).thenReturn(List.of(acceptedPolicy));

prepareNegotiation();
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 [IRS Policy] did not match with policy from BPNL00000000TEST.";
assertThatExceptionOfType(UsagePolicyPermissionException.class).isThrownBy(
() -> edcSubmodelClient.getSubmodelPayload(CONNECTOR_ENDPOINT_URL, SUBMODEL_DATAPLANE_URL, ASSET_ID, "bpn")
.get()).withMessageEndingWith(errorMessage);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
</sonar.coverage.jacoco.xmlReportPaths>

<!-- IRS Registry Client Library -->
<irs-registry-client.version>2.1.0</irs-registry-client.version>
<irs-registry-client.version>2.1.1-SNAPSHOT</irs-registry-client.version>

<!-- Dependencies -->
<springboot.version>3.1.11</springboot.version>
Expand Down
Loading