Skip to content

Commit

Permalink
refactor: avoid duplication between AbstractResult subclasses (eclips…
Browse files Browse the repository at this point in the history
…e-edc#1050) (eclipse-edc#1097)

This reverts commit 2959abc.
  • Loading branch information
ndr-brt authored Apr 7, 2022
1 parent eadf12f commit 2e679e6
Show file tree
Hide file tree
Showing 75 changed files with 393 additions and 670 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.eclipse.dataspaceconnector.contract.common.ContractId;
import org.eclipse.dataspaceconnector.spi.contract.negotiation.ConsumerContractNegotiationManager;
import org.eclipse.dataspaceconnector.spi.contract.negotiation.observe.ContractNegotiationListener;
import org.eclipse.dataspaceconnector.spi.contract.negotiation.response.NegotiationResult;
import org.eclipse.dataspaceconnector.spi.iam.ClaimToken;
import org.eclipse.dataspaceconnector.spi.response.StatusResult;
import org.eclipse.dataspaceconnector.spi.result.Result;
import org.eclipse.dataspaceconnector.spi.types.domain.contract.agreement.ContractAgreement;
import org.eclipse.dataspaceconnector.spi.types.domain.contract.agreement.ContractAgreementRequest;
Expand All @@ -48,7 +48,7 @@
import static java.lang.String.format;
import static org.eclipse.dataspaceconnector.contract.common.ContractId.DEFINITION_PART;
import static org.eclipse.dataspaceconnector.contract.common.ContractId.parseContractId;
import static org.eclipse.dataspaceconnector.spi.contract.negotiation.response.NegotiationResult.Status.FATAL_ERROR;
import static org.eclipse.dataspaceconnector.spi.response.ResponseStatus.FATAL_ERROR;
import static org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation.Type.CONSUMER;
import static org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiationStates.CONFIRMED;
import static org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiationStates.CONSUMER_APPROVING;
Expand Down Expand Up @@ -95,11 +95,11 @@ public void enqueueCommand(ContractNegotiationCommand command) {
* persisted, which moves it to state REQUESTING.
*
* @param contractOffer Container object containing all relevant request parameters.
* @return a {@link NegotiationResult}: OK
* @return a {@link StatusResult}: OK
*/
@WithSpan
@Override
public NegotiationResult initiate(ContractOfferRequest contractOffer) {
public StatusResult<ContractNegotiation> initiate(ContractOfferRequest contractOffer) {
var negotiation = ContractNegotiation.Builder.newInstance()
.id(UUID.randomUUID().toString())
.protocol(contractOffer.getProtocol())
Expand All @@ -115,7 +115,7 @@ public NegotiationResult initiate(ContractOfferRequest contractOffer) {

monitor.debug(String.format("[Consumer] ContractNegotiation initiated. %s is now in state %s.",
negotiation.getId(), ContractNegotiationStates.from(negotiation.getState())));
return NegotiationResult.success(negotiation);
return StatusResult.success(negotiation);
}

/**
Expand All @@ -128,23 +128,23 @@ public NegotiationResult initiate(ContractOfferRequest contractOffer) {
* @param negotiationId Id of the ContractNegotiation.
* @param contractOffer The contract offer.
* @param hash A hash of all previous contract offers.
* @return a {@link NegotiationResult}: FATAL_ERROR, if no match found for Id or no last
* @return a {@link StatusResult}: FATAL_ERROR, if no match found for Id or no last
* offer found for negotiation; OK otherwise
*/
@WithSpan
@Override
public NegotiationResult offerReceived(ClaimToken token, String negotiationId, ContractOffer contractOffer, String hash) {
public StatusResult<ContractNegotiation> offerReceived(ClaimToken token, String negotiationId, ContractOffer contractOffer, String hash) {
var negotiation = negotiationStore.find(negotiationId);
if (negotiation == null) {
return NegotiationResult.failure(FATAL_ERROR);
return StatusResult.failure(FATAL_ERROR);
}

var latestOffer = negotiation.getLastContractOffer();
try {
Objects.requireNonNull(latestOffer, "latestOffer");
} catch (NullPointerException e) {
monitor.severe("[Consumer] No offer found for validation. Process id: " + negotiation.getId());
return NegotiationResult.failure(FATAL_ERROR);
return StatusResult.failure(FATAL_ERROR);
}

Result<ContractOffer> result = validationService.validate(token, contractOffer, latestOffer);
Expand All @@ -164,7 +164,7 @@ public NegotiationResult offerReceived(ClaimToken token, String negotiationId, C
monitor.debug(String.format("[Consumer] ContractNegotiation %s is now in state %s.",
negotiation.getId(), ContractNegotiationStates.from(negotiation.getState())));

return NegotiationResult.success(negotiation);
return StatusResult.success(negotiation);
}

/**
Expand All @@ -176,23 +176,23 @@ public NegotiationResult offerReceived(ClaimToken token, String negotiationId, C
* @param negotiationId Id of the ContractNegotiation.
* @param agreement Agreement sent by provider.
* @param hash A hash of all previous contract offers.
* @return a {@link NegotiationResult}: FATAL_ERROR, if no match found for Id or no last
* @return a {@link StatusResult}: FATAL_ERROR, if no match found for Id or no last
* offer found for negotiation; OK otherwise
*/
@WithSpan
@Override
public NegotiationResult confirmed(ClaimToken token, String negotiationId, ContractAgreement agreement, String hash) {
public StatusResult<ContractNegotiation> confirmed(ClaimToken token, String negotiationId, ContractAgreement agreement, String hash) {
var negotiation = negotiationStore.find(negotiationId);
if (negotiation == null) {
return NegotiationResult.failure(FATAL_ERROR);
return StatusResult.failure(FATAL_ERROR);
}

var latestOffer = negotiation.getLastContractOffer();
try {
Objects.requireNonNull(latestOffer, "latestOffer");
} catch (NullPointerException e) {
monitor.severe("[Consumer] No offer found for validation. Process id: " + negotiation.getId());
return NegotiationResult.failure(FATAL_ERROR);
return StatusResult.failure(FATAL_ERROR);
}

var result = validationService.validate(token, agreement, latestOffer);
Expand All @@ -204,7 +204,7 @@ public NegotiationResult confirmed(ClaimToken token, String negotiationId, Contr
update(negotiation, l -> l.preDeclining(negotiation));
monitor.debug(String.format("[Consumer] ContractNegotiation %s is now in state %s.",
negotiation.getId(), ContractNegotiationStates.from(negotiation.getState())));
return NegotiationResult.success(negotiation);
return StatusResult.success(negotiation);
}

// Agreement has been approved.
Expand All @@ -218,7 +218,7 @@ public NegotiationResult confirmed(ClaimToken token, String negotiationId, Contr
monitor.debug(String.format("[Consumer] ContractNegotiation %s is now in state %s.",
negotiation.getId(), ContractNegotiationStates.from(negotiation.getState())));

return NegotiationResult.success(negotiation);
return StatusResult.success(negotiation);
}

/**
Expand All @@ -227,23 +227,23 @@ public NegotiationResult confirmed(ClaimToken token, String negotiationId, Contr
*
* @param token Claim token of the consumer that sent the rejection.
* @param negotiationId Id of the ContractNegotiation.
* @return a {@link NegotiationResult}: OK, if successfully transitioned to declined;
* @return a {@link StatusResult}: OK, if successfully transitioned to declined;
* FATAL_ERROR, if no match found for Id.
*/
@WithSpan
@Override
public NegotiationResult declined(ClaimToken token, String negotiationId) {
public StatusResult<ContractNegotiation> declined(ClaimToken token, String negotiationId) {
var negotiation = findContractNegotiationById(negotiationId);
if (negotiation == null) {
return NegotiationResult.failure(FATAL_ERROR);
return StatusResult.failure(FATAL_ERROR);
}

monitor.debug("[Consumer] Contract rejection received. Abort negotiation process.");
negotiation.transitionDeclined();
update(negotiation, l -> l.preDeclined(negotiation));
monitor.debug(String.format("[Consumer] ContractNegotiation %s is now in state %s.",
negotiation.getId(), ContractNegotiationStates.from(negotiation.getState())));
return NegotiationResult.success(negotiation);
return StatusResult.success(negotiation);
}

private ContractNegotiation findContractNegotiationById(String negotiationId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.eclipse.dataspaceconnector.contract.common.ContractId;
import org.eclipse.dataspaceconnector.spi.contract.negotiation.ProviderContractNegotiationManager;
import org.eclipse.dataspaceconnector.spi.contract.negotiation.observe.ContractNegotiationListener;
import org.eclipse.dataspaceconnector.spi.contract.negotiation.response.NegotiationResult;
import org.eclipse.dataspaceconnector.spi.iam.ClaimToken;
import org.eclipse.dataspaceconnector.spi.response.StatusResult;
import org.eclipse.dataspaceconnector.spi.result.Result;
import org.eclipse.dataspaceconnector.spi.types.domain.contract.agreement.ContractAgreement;
import org.eclipse.dataspaceconnector.spi.types.domain.contract.agreement.ContractAgreementRequest;
Expand All @@ -46,7 +46,7 @@
import static java.lang.String.format;
import static org.eclipse.dataspaceconnector.contract.common.ContractId.DEFINITION_PART;
import static org.eclipse.dataspaceconnector.contract.common.ContractId.parseContractId;
import static org.eclipse.dataspaceconnector.spi.contract.negotiation.response.NegotiationResult.Status.FATAL_ERROR;
import static org.eclipse.dataspaceconnector.spi.response.ResponseStatus.FATAL_ERROR;
import static org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation.Type.PROVIDER;
import static org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiationStates.CONFIRMING;
import static org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiationStates.DECLINING;
Expand Down Expand Up @@ -94,15 +94,15 @@ public void enqueueCommand(ContractNegotiationCommand command) {
*
* @param token Claim token of the consumer that sent the rejection.
* @param correlationId Id of the ContractNegotiation on consumer side.
* @return a {@link NegotiationResult}: OK, if successfully transitioned to declined;
* @return a {@link StatusResult}: OK, if successfully transitioned to declined;
* FATAL_ERROR, if no match found for Id.
*/
@WithSpan
@Override
public NegotiationResult declined(ClaimToken token, String correlationId) {
public StatusResult<ContractNegotiation> declined(ClaimToken token, String correlationId) {
var negotiation = findContractNegotiationById(correlationId);
if (negotiation == null) {
return NegotiationResult.failure(FATAL_ERROR);
return StatusResult.failure(FATAL_ERROR);
}

monitor.debug("[Provider] Contract rejection received. Abort negotiation process.");
Expand All @@ -116,7 +116,7 @@ public NegotiationResult declined(ClaimToken token, String correlationId) {
monitor.debug(String.format("[Provider] ContractNegotiation %s is now in state %s.",
negotiation.getId(), ContractNegotiationStates.from(negotiation.getState())));

return NegotiationResult.success(negotiation);
return StatusResult.success(negotiation);
}

private ContractNegotiation findContractNegotiationById(String negotiationId) {
Expand All @@ -135,11 +135,11 @@ private ContractNegotiation findContractNegotiationById(String negotiationId) {
*
* @param token Claim token of the consumer that send the contract request.
* @param request Container object containing all relevant request parameters.
* @return a {@link NegotiationResult}: OK
* @return a {@link StatusResult}: OK
*/
@WithSpan
@Override
public NegotiationResult requested(ClaimToken token, ContractOfferRequest request) {
public StatusResult<ContractNegotiation> requested(ClaimToken token, ContractOfferRequest request) {
var negotiation = ContractNegotiation.Builder.newInstance()
.id(UUID.randomUUID().toString())
.correlationId(request.getCorrelationId())
Expand Down Expand Up @@ -169,14 +169,14 @@ public NegotiationResult requested(ClaimToken token, ContractOfferRequest reques
* @param correlationId Id of the ContractNegotiation on consumer side.
* @param offer The contract offer.
* @param hash A hash of all previous contract offers.
* @return a {@link NegotiationResult}: FATAL_ERROR, if no match found for Id; OK otherwise
* @return a {@link StatusResult}: FATAL_ERROR, if no match found for Id; OK otherwise
*/
@WithSpan
@Override
public NegotiationResult offerReceived(ClaimToken token, String correlationId, ContractOffer offer, String hash) {
public StatusResult<ContractNegotiation> offerReceived(ClaimToken token, String correlationId, ContractOffer offer, String hash) {
var negotiation = negotiationStore.findForCorrelationId(correlationId);
if (negotiation == null) {
return NegotiationResult.failure(FATAL_ERROR);
return StatusResult.failure(FATAL_ERROR);
}

return processIncomingOffer(negotiation, token, offer);
Expand All @@ -190,9 +190,9 @@ public NegotiationResult offerReceived(ClaimToken token, String correlationId, C
* @param negotiation The ContractNegotiation.
* @param token Claim token of the consumer that send the contract request.
* @param offer The contract offer.
* @return a {@link NegotiationResult}: OK
* @return a {@link StatusResult}: OK
*/
private NegotiationResult processIncomingOffer(ContractNegotiation negotiation, ClaimToken token, ContractOffer offer) {
private StatusResult<ContractNegotiation> processIncomingOffer(ContractNegotiation negotiation, ClaimToken token, ContractOffer offer) {
Result<ContractOffer> result;
if (negotiation.getContractOffers().isEmpty()) {
result = validationService.validate(token, offer);
Expand All @@ -211,7 +211,7 @@ private NegotiationResult processIncomingOffer(ContractNegotiation negotiation,

monitor.debug(String.format("[Provider] ContractNegotiation %s is now in state %s.",
negotiation.getId(), ContractNegotiationStates.from(negotiation.getState())));
return NegotiationResult.success(negotiation);
return StatusResult.success(negotiation);
}

monitor.debug("[Provider] Contract offer received. Will be approved.");
Expand All @@ -221,7 +221,7 @@ private NegotiationResult processIncomingOffer(ContractNegotiation negotiation,
monitor.debug(String.format("[Provider] ContractNegotiation %s is now in state %s.",
negotiation.getId(), ContractNegotiationStates.from(negotiation.getState())));

return NegotiationResult.success(negotiation);
return StatusResult.success(negotiation);
}

/**
Expand All @@ -232,21 +232,21 @@ private NegotiationResult processIncomingOffer(ContractNegotiation negotiation,
* @param correlationId Id of the ContractNegotiation on consumer side.
* @param agreement Agreement sent by consumer.
* @param hash A hash of all previous contract offers.
* @return a {@link NegotiationResult}: FATAL_ERROR, if no match found for Id; OK otherwise
* @return a {@link StatusResult}: FATAL_ERROR, if no match found for Id; OK otherwise
*/
@Override
public NegotiationResult consumerApproved(ClaimToken token, String correlationId, ContractAgreement agreement, String hash) {
public StatusResult<ContractNegotiation> consumerApproved(ClaimToken token, String correlationId, ContractAgreement agreement, String hash) {
var negotiation = negotiationStore.findForCorrelationId(correlationId);
if (negotiation == null) {
return NegotiationResult.failure(FATAL_ERROR);
return StatusResult.failure(FATAL_ERROR);
}

monitor.debug("[Provider] Contract offer has been approved by consumer.");
negotiation.transitionConfirming();
update(negotiation, l -> l.preConfirming(negotiation));
monitor.debug(String.format("[Provider] ContractNegotiation %s is now in state %s.",
negotiation.getId(), ContractNegotiationStates.from(negotiation.getState())));
return NegotiationResult.success(negotiation);
return StatusResult.success(negotiation);
}

private StateProcessorImpl<ContractNegotiation> processNegotiationsInState(ContractNegotiationStates state, Function<ContractNegotiation, Boolean> function) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
import org.eclipse.dataspaceconnector.spi.command.CommandRunner;
import org.eclipse.dataspaceconnector.spi.contract.negotiation.observe.ContractNegotiationListener;
import org.eclipse.dataspaceconnector.spi.contract.negotiation.observe.ContractNegotiationObservable;
import org.eclipse.dataspaceconnector.spi.contract.negotiation.response.NegotiationResult;
import org.eclipse.dataspaceconnector.spi.contract.validation.ContractValidationService;
import org.eclipse.dataspaceconnector.spi.iam.ClaimToken;
import org.eclipse.dataspaceconnector.spi.message.MessageContext;
import org.eclipse.dataspaceconnector.spi.message.RemoteMessageDispatcher;
import org.eclipse.dataspaceconnector.spi.message.RemoteMessageDispatcherRegistry;
import org.eclipse.dataspaceconnector.spi.monitor.Monitor;
import org.eclipse.dataspaceconnector.spi.response.StatusResult;
import org.eclipse.dataspaceconnector.spi.types.domain.asset.Asset;
import org.eclipse.dataspaceconnector.spi.types.domain.contract.agreement.ContractAgreementRequest;
import org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation;
Expand All @@ -49,6 +49,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;

import static org.eclipse.dataspaceconnector.spi.response.ResponseStatus.FATAL_ERROR;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -174,7 +175,7 @@ public <T> CompletableFuture<T> send(Class<T> responseType, RemoteMessage messag
}

public CompletableFuture<Object> send(RemoteMessage message) {
NegotiationResult result;
StatusResult<ContractNegotiation> result;
if (message instanceof ContractOfferRequest) {
var request = (ContractOfferRequest) message;
result = consumerManager.offerReceived(token, request.getCorrelationId(), request.getContractOffer(), "hash");
Expand Down Expand Up @@ -216,12 +217,12 @@ public <T> CompletableFuture<T> send(Class<T> responseType, RemoteMessage messag
}

public CompletableFuture<Object> send(RemoteMessage message) {
NegotiationResult result;
StatusResult<ContractNegotiation> result;
if (message instanceof ContractOfferRequest) {
var request = (ContractOfferRequest) message;
consumerNegotiationId = request.getCorrelationId();
result = providerManager.offerReceived(token, request.getCorrelationId(), request.getContractOffer(), "hash");
if (NegotiationResult.Status.FATAL_ERROR.equals(result.getFailure().getStatus())) {
if (result.fatalError()) {
result = providerManager.requested(token, request);
}
} else if (message instanceof ContractAgreementRequest) {
Expand Down
Loading

0 comments on commit 2e679e6

Please sign in to comment.