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

feat: separate multiple state transitions #2970

Merged
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 @@ -41,6 +41,7 @@
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.AGREEING;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.FINALIZING;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.OFFERING;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.REQUESTED;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.TERMINATING;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.VERIFIED;
import static org.eclipse.edc.spi.persistence.StateEntityStore.hasState;
Expand All @@ -57,6 +58,7 @@ private ProviderContractNegotiationManagerImpl() {
public void start() {
stateMachineManager = StateMachineManager.Builder.newInstance("provider-contract-negotiation", monitor, executorInstrumentation, waitStrategy)
.processor(processNegotiationsInState(OFFERING, this::processOffering))
.processor(processNegotiationsInState(REQUESTED, this::processRequested))
.processor(processNegotiationsInState(AGREEING, this::processAgreeing))
.processor(processNegotiationsInState(VERIFIED, this::processVerified))
.processor(processNegotiationsInState(FINALIZING, this::processFinalizing))
Expand Down Expand Up @@ -145,6 +147,17 @@ private boolean processTerminating(ContractNegotiation negotiation) {
.execute("[Provider] send rejection");
}

/**
* Processes {@link ContractNegotiation} in state REQUESTED. It transitions to AGREEING, because the automatic agreement.
*
* @return true if processed, false otherwise
*/
@WithSpan
private boolean processRequested(ContractNegotiation negotiation) {
transitionToAgreeing(negotiation);
return true;
}

/**
* Processes {@link ContractNegotiation} in state CONFIRMING. Tries to send a contract agreement to the respective
* consumer. If this succeeds, the ContractNegotiation is transitioned to state CONFIRMED. Else, it is transitioned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.FINALIZING;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.OFFERED;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.OFFERING;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.REQUESTED;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.TERMINATED;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.TERMINATING;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.VERIFIED;
Expand Down Expand Up @@ -128,6 +129,20 @@ void offering_shouldSendOfferAndTransitionToOffered() {
});
}

@Test
void requested_shouldTransitionToAgreeing() {
var negotiation = contractNegotiationBuilder().state(REQUESTED.code()).build();
when(store.nextNotLeased(anyInt(), stateIs(REQUESTED.code()))).thenReturn(List.of(negotiation)).thenReturn(emptyList());
when(store.findById(negotiation.getId())).thenReturn(negotiation);

negotiationManager.start();

await().untilAsserted(() -> {
verify(store).save(argThat(p -> p.getState() == AGREEING.code()));
verifyNoInteractions(dispatcherRegistry);
});
}

@Test
void verified_shouldTransitionToFinalizing() {
var negotiation = contractNegotiationBuilder().state(VERIFIED.code()).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@ public ContractNegotiationProtocolServiceImpl(ContractNegotiationStore store,
@WithSpan
@NotNull
public ServiceResult<ContractNegotiation> notifyRequested(ContractRequestMessage message, ClaimToken claimToken) {
return transactionContext.execute(() ->
validateOffer(message, claimToken)
.compose(validatedOffer -> createNegotiation(message, validatedOffer)).onSuccess(negotiation -> {
negotiation.transitionRequested();
monitor.debug(() -> "[Provider] Contract offer received. Will be approved automatically.");
negotiation.transitionAgreeing(); // automatic agree
update(negotiation);
observable.invokeForEach(l -> l.requested(negotiation));
}));
return transactionContext.execute(() -> validateOffer(message, claimToken)
.compose(validatedOffer -> createNegotiation(message, validatedOffer))
.onSuccess(negotiation -> {
monitor.debug(() -> "[Provider] Contract offer received.");
negotiation.transitionRequested();
update(negotiation);
observable.invokeForEach(l -> l.requested(negotiation));
}));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiation.Type.PROVIDER;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.AGREED;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.AGREEING;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.FINALIZED;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.REQUESTED;
import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationStates.TERMINATED;
Expand Down Expand Up @@ -113,7 +112,7 @@ void notifyRequested_shouldInitiateNegotiation_whenOfferIsValid() {
var calls = ArgumentCaptor.forClass(ContractNegotiation.class);
verify(store).save(calls.capture());
assertThat(calls.getAllValues()).anySatisfy(n -> {
assertThat(n.getState()).isEqualTo(AGREEING.code());
assertThat(n.getState()).isEqualTo(REQUESTED.code());
assertThat(n.getCounterPartyId()).isEqualTo(message.getConnectorId());
assertThat(n.getCounterPartyAddress()).isEqualTo(message.getCallbackAddress());
assertThat(n.getProtocol()).isEqualTo(message.getProtocol());
Expand Down