Skip to content

Commit

Permalink
fix: enforce contract negotiation request and transfer request consis…
Browse files Browse the repository at this point in the history
…tency
  • Loading branch information
bscholtes1A committed Jun 11, 2024
1 parent 6034040 commit 770b99c
Show file tree
Hide file tree
Showing 18 changed files with 162 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,11 @@ private interface MethodCall<M extends RemoteMessage> {

interface TestFunctions {
static ContractOffer contractOffer() {
var assetId = "test-asset-id";
return ContractOffer.Builder.newInstance()
.id(ContractOfferId.create("1", "test-asset-id").toString())
.id(ContractOfferId.create("1", assetId).toString())
.policy(createPolicy())
.assetId("assetId")
.assetId(assetId)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package org.eclipse.edc.connector.controlplane.services.contractnegotiation;

import org.eclipse.edc.connector.controlplane.contract.spi.ContractOfferId;
import org.eclipse.edc.connector.controlplane.contract.spi.negotiation.ConsumerContractNegotiationManager;
import org.eclipse.edc.connector.controlplane.contract.spi.negotiation.store.ContractNegotiationStore;
import org.eclipse.edc.connector.controlplane.contract.spi.types.agreement.ContractAgreement;
Expand Down Expand Up @@ -269,10 +270,11 @@ private ContractNegotiation.Builder createContractNegotiationBuilder(String nego
}

private ContractOffer createContractOffer() {
var assetId = "test-asset";
return ContractOffer.Builder.newInstance()
.id(UUID.randomUUID().toString())
.id(ContractOfferId.create("1", assetId).toString())
.policy(Policy.Builder.newInstance().build())
.assetId("test-asset")
.assetId(assetId)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.eclipse.edc.connector.controlplane.contract.negotiation;

import org.eclipse.edc.connector.controlplane.contract.observe.ContractNegotiationObservableImpl;
import org.eclipse.edc.connector.controlplane.contract.spi.ContractOfferId;
import org.eclipse.edc.connector.controlplane.contract.spi.negotiation.ContractNegotiationPendingGuard;
import org.eclipse.edc.connector.controlplane.contract.spi.negotiation.observe.ContractNegotiationListener;
import org.eclipse.edc.connector.controlplane.contract.spi.negotiation.store.ContractNegotiationStore;
Expand Down Expand Up @@ -85,6 +86,7 @@

class ConsumerContractNegotiationManagerImplTest {

private static final String ASSET_ID = "assetId";
private static final String PARTICIPANT_ID = "participantId";
private static final int RETRY_LIMIT = 1;

Expand Down Expand Up @@ -307,7 +309,7 @@ void dispatchException(ContractNegotiationStates starting, ContractNegotiationSt
}

private Criterion[] stateIs(int state) {
return aryEq(new Criterion[]{ hasState(state), isNotPending(), new Criterion("type", "=", "CONSUMER") });
return aryEq(new Criterion[] {hasState(state), isNotPending(), new Criterion("type", "=", "CONSUMER")});
}

private ContractNegotiation.Builder contractNegotiationBuilder() {
Expand All @@ -321,9 +323,10 @@ private ContractNegotiation.Builder contractNegotiationBuilder() {
}

private ContractOffer contractOffer() {
return ContractOffer.Builder.newInstance().id("id:assetId:random")
return ContractOffer.Builder.newInstance()
.id(ContractOfferId.create("1", ASSET_ID).toString())
.policy(Policy.Builder.newInstance().assigner("providerId").build())
.assetId("assetId")
.assetId(ASSET_ID)
.build();
}

Expand All @@ -332,7 +335,7 @@ private ContractAgreement createContractAgreement() {
.id("contractId")
.consumerId("consumerId")
.providerId("providerId")
.assetId("assetId")
.assetId(ASSET_ID)
.policy(Policy.Builder.newInstance().build())
.build();
}
Expand Down Expand Up @@ -368,15 +371,16 @@ private ContractAgreement createContractAgreement() {
.id("contractId")
.consumerId("consumerId")
.providerId("providerId")
.assetId("assetId")
.assetId(ASSET_ID)
.policy(Policy.Builder.newInstance().build())
.build();
}

private ContractOffer contractOffer() {
return ContractOffer.Builder.newInstance().id("id:assetId:random")
return ContractOffer.Builder.newInstance()
.id(ContractOfferId.create("1", ASSET_ID).toString())
.policy(Policy.Builder.newInstance().build())
.assetId("assetId")
.assetId(ASSET_ID)
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@

class ProviderContractNegotiationManagerImplTest {

private static final String ASSET_ID = "assetId";
private static final String PROVIDER_ID = "provider";
private static final int RETRY_LIMIT = 1;
private final ContractNegotiationStore store = mock();
Expand Down Expand Up @@ -301,15 +302,16 @@ private ContractAgreement.Builder contractAgreementBuilder() {
}

private ContractOffer contractOffer() {
var assetId = "test-asset-id";
return ContractOffer.Builder.newInstance()
.id(ContractOfferId.create("1", "test-asset-id").toString())
.id(ContractOfferId.create("1", assetId).toString())
.policy(Policy.Builder.newInstance().build())
.assetId("assetId")
.assetId(assetId)
.build();
}

private Criterion[] stateIs(int state) {
return aryEq(new Criterion[]{ hasState(state), isNotPending(), new Criterion("type", "=", "PROVIDER") });
return aryEq(new Criterion[] {hasState(state), isNotPending(), new Criterion("type", "=", "PROVIDER")});
}

private static class DispatchFailureArguments implements ArgumentsProvider {
Expand Down Expand Up @@ -339,9 +341,10 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext extensionCo
}

private ContractOffer contractOffer() {
return ContractOffer.Builder.newInstance().id("id:assetId:random")
return ContractOffer.Builder.newInstance()
.id(ContractOfferId.create("1", ASSET_ID).toString())
.policy(Policy.Builder.newInstance().build())
.assetId("assetId")
.assetId(ASSET_ID)
.build();
}

Expand All @@ -350,7 +353,7 @@ private ContractAgreement createContractAgreement() {
.id("contractId")
.consumerId("consumerId")
.providerId("providerId")
.assetId("assetId")
.assetId(ASSET_ID)
.policy(Policy.Builder.newInstance().build())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ private boolean processInitial(TransferProcess process) {
return true;
}

if (!process.getAssetId().equals(policy.getTarget())) {
transitionToTerminated(process, "Transfer Process asset id %s does not match asset id in contract %s".formatted(process.getAssetId(), policy.getTarget()));
return true;
}

ResourceManifest manifest;
if (process.getType() == CONSUMER) {
var manifestResult = manifestGenerator.generateConsumerResourceManifest(process, policy);
Expand Down Expand Up @@ -539,17 +544,17 @@ private <T> void handleResult(TransferProcess transferProcess, List<StatusResult
}

private Processor processConsumerTransfersInState(TransferProcessStates state, Function<TransferProcess, Boolean> function) {
var filter = new Criterion[]{ hasState(state.code()), isNotPending(), Criterion.criterion("type", "=", CONSUMER.name()) };
var filter = new Criterion[] {hasState(state.code()), isNotPending(), Criterion.criterion("type", "=", CONSUMER.name())};
return createProcessor(function, filter);
}

private Processor processProviderTransfersInState(TransferProcessStates state, Function<TransferProcess, Boolean> function) {
var filter = new Criterion[]{ hasState(state.code()), isNotPending(), Criterion.criterion("type", "=", PROVIDER.name()) };
var filter = new Criterion[] {hasState(state.code()), isNotPending(), Criterion.criterion("type", "=", PROVIDER.name())};
return createProcessor(function, filter);
}

private Processor processTransfersInState(TransferProcessStates state, Function<TransferProcess, Boolean> function) {
var filter = new Criterion[]{ hasState(state.code()), isNotPending() };
var filter = new Criterion[] {hasState(state.code()), isNotPending()};
return createProcessor(function, filter);
}

Expand Down
Loading

0 comments on commit 770b99c

Please sign in to comment.