Search in sources :

Example 1 with ContractOffer

use of org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer in project DataSpaceConnector by eclipse-dataspaceconnector.

the class InMemoryFederatedCacheStoreTest method queryCacheAfterInsertingSameAssetTwice_shouldReturnLastInsertedContractOfferOnly.

@Test
void queryCacheAfterInsertingSameAssetTwice_shouldReturnLastInsertedContractOfferOnly() {
    String contractOfferId1 = UUID.randomUUID().toString();
    String contractOfferId2 = UUID.randomUUID().toString();
    String assetId = UUID.randomUUID().toString();
    ContractOffer contractOffer1 = createContractOffer(contractOfferId1, createAsset(assetId));
    ContractOffer contractOffer2 = createContractOffer(contractOfferId2, createAsset(assetId));
    store.save(contractOffer1);
    store.save(contractOffer2);
    Collection<ContractOffer> result = store.query(Collections.emptyList());
    assertThat(result).hasSize(1).allSatisfy(co -> {
        assertThat(co.getId()).isEqualTo(contractOfferId2);
        assertThat(co.getAsset().getId()).isEqualTo(assetId);
    });
}
Also used : ContractOffer(org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer) Test(org.junit.jupiter.api.Test)

Example 2 with ContractOffer

use of org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer in project DataSpaceConnector by eclipse-dataspaceconnector.

the class InMemoryFederatedCacheStoreTest method queryCacheContainingTwoDistinctAssets_shouldReturnBothContractOffers.

@Test
void queryCacheContainingTwoDistinctAssets_shouldReturnBothContractOffers() {
    String contractOfferId1 = UUID.randomUUID().toString();
    String contractOfferId2 = UUID.randomUUID().toString();
    String assetId1 = UUID.randomUUID().toString();
    String assetId2 = UUID.randomUUID().toString();
    ContractOffer contractOffer1 = createContractOffer(contractOfferId1, createAsset(assetId1));
    ContractOffer contractOffer2 = createContractOffer(contractOfferId2, createAsset(assetId2));
    store.save(contractOffer1);
    store.save(contractOffer2);
    Collection<ContractOffer> result = store.query(Collections.emptyList());
    assertThat(result).hasSize(2).anySatisfy(co -> assertThat(co.getAsset().getId()).isEqualTo(assetId1)).anySatisfy(co -> assertThat(co.getAsset().getId()).isEqualTo(assetId2));
}
Also used : ContractOffer(org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer) Test(org.junit.jupiter.api.Test) BeforeEach(org.junit.jupiter.api.BeforeEach) FederatedCacheStore(org.eclipse.dataspaceconnector.catalog.spi.FederatedCacheStore) Policy(org.eclipse.dataspaceconnector.policy.model.Policy) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CriterionConverter(org.eclipse.dataspaceconnector.spi.query.CriterionConverter) Asset(org.eclipse.dataspaceconnector.spi.types.domain.asset.Asset) UUID(java.util.UUID) Collections(java.util.Collections) ContractOffer(org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer) Test(org.junit.jupiter.api.Test)

Example 3 with ContractOffer

use of org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer in project DataSpaceConnector by eclipse-dataspaceconnector.

the class ResourceDescriptionRequestHandler method retrieveObject.

protected OfferedAsset retrieveObject(@NotNull IdsId idsId, @NotNull Result<ClaimToken> verificationResult) {
    String assetId = idsId.getValue();
    Asset asset = assetIndex.findById(assetId);
    if (asset == null) {
        return null;
    }
    ContractOfferQuery contractOfferQuery = ContractOfferQuery.Builder.newInstance().claimToken(verificationResult.getContent()).criterion(new Criterion(Asset.PROPERTY_ID, "=", assetId)).build();
    List<ContractOffer> targetingContractOffers = contractOfferService.queryContractOffers(contractOfferQuery).collect(toList());
    return new OfferedAsset(asset, targetingContractOffers);
}
Also used : ContractOfferQuery(org.eclipse.dataspaceconnector.spi.contract.offer.ContractOfferQuery) Criterion(org.eclipse.dataspaceconnector.spi.query.Criterion) ContractOffer(org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer) OfferedAsset(org.eclipse.dataspaceconnector.ids.spi.types.container.OfferedAsset) Asset(org.eclipse.dataspaceconnector.spi.types.domain.asset.Asset) OfferedAsset(org.eclipse.dataspaceconnector.ids.spi.types.container.OfferedAsset)

Example 4 with ContractOffer

use of org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer in project DataSpaceConnector by eclipse-dataspaceconnector.

the class ConsumerContractNegotiationManagerImplTest method initiate_should_save_a_new_negotiation_in_initial_state.

@Test
void initiate_should_save_a_new_negotiation_in_initial_state() {
    var contractOffer = contractOffer();
    ContractOfferRequest request = ContractOfferRequest.Builder.newInstance().connectorId("connectorId").connectorAddress("connectorAddress").protocol("protocol").contractOffer(contractOffer).build();
    var result = negotiationManager.initiate(request);
    assertThat(result.succeeded()).isTrue();
    verify(store).save(argThat(negotiation -> negotiation.getState() == INITIAL.code() && negotiation.getCounterPartyId().equals(request.getConnectorId()) && negotiation.getCounterPartyAddress().equals(request.getConnectorAddress()) && negotiation.getProtocol().equals(request.getProtocol()) && negotiation.getCorrelationId() == null && negotiation.getContractOffers().size() == 1 && negotiation.getLastContractOffer().equals(contractOffer)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ContractAgreement(org.eclipse.dataspaceconnector.spi.types.domain.contract.agreement.ContractAgreement) ContractNegotiationStore(org.eclipse.dataspaceconnector.spi.contract.negotiation.store.ContractNegotiationStore) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Result(org.eclipse.dataspaceconnector.spi.result.Result) CommandRunner(org.eclipse.dataspaceconnector.spi.command.CommandRunner) ContractValidationService(org.eclipse.dataspaceconnector.spi.contract.validation.ContractValidationService) ClaimToken(org.eclipse.dataspaceconnector.spi.iam.ClaimToken) INITIAL(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiationStates.INITIAL) ArrayList(java.util.ArrayList) ContractOfferRequest(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractOfferRequest) ContractNegotiation(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiation) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) ContractOffer(org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer) Policy(org.eclipse.dataspaceconnector.policy.model.Policy) NegotiationResult(org.eclipse.dataspaceconnector.spi.contract.negotiation.response.NegotiationResult) DECLINING(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiationStates.DECLINING) CONFIRMED(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiationStates.CONFIRMED) CONSUMER_OFFERED(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiationStates.CONSUMER_OFFERED) DECLINED(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiationStates.DECLINED) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) Instant(java.time.Instant) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) CONSUMER_APPROVING(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiationStates.CONSUMER_APPROVING) ContractNegotiationCommand(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.command.ContractNegotiationCommand) RemoteMessageDispatcherRegistry(org.eclipse.dataspaceconnector.spi.message.RemoteMessageDispatcherRegistry) CommandQueue(org.eclipse.dataspaceconnector.spi.command.CommandQueue) REQUESTED(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractNegotiationStates.REQUESTED) Monitor(org.eclipse.dataspaceconnector.spi.monitor.Monitor) ContractNegotiationObservable(org.eclipse.dataspaceconnector.spi.contract.negotiation.observe.ContractNegotiationObservable) Mockito.mock(org.mockito.Mockito.mock) ContractOfferRequest(org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractOfferRequest) Test(org.junit.jupiter.api.Test)

Example 5 with ContractOffer

use of org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer in project DataSpaceConnector by eclipse-dataspaceconnector.

the class ContractRequestHandler method handleRequest.

@Override
@Nullable
public MultipartResponse handleRequest(@NotNull MultipartRequest multipartRequest, @NotNull ClaimToken claimToken) {
    Objects.requireNonNull(multipartRequest);
    Objects.requireNonNull(claimToken);
    var message = (ContractRequestMessage) multipartRequest.getHeader();
    ContractRequest contractRequest;
    try {
        contractRequest = objectMapper.readValue(multipartRequest.getPayload(), ContractRequest.class);
    } catch (IOException e) {
        monitor.severe("ContractRequestHandler: Contract Request is invalid", e);
        return createBadParametersErrorMultipartResponse(message);
    }
    var idsWebhookAddress = message.getProperties().get(IDS_WEBHOOK_ADDRESS_PROPERTY);
    if (idsWebhookAddress == null || idsWebhookAddress.toString().isBlank()) {
        var msg = "Ids webhook address is invalid";
        monitor.debug(String.format("ContractRequestHandler: %s", msg));
        return createBadParametersErrorMultipartResponse(message, msg);
    }
    // extract target from contract request
    var permission = contractRequest.getPermission().stream().findFirst().orElse(null);
    if (permission == null) {
        monitor.debug("ContractRequestHandler: Contract Request is invalid");
        return createBadParametersErrorMultipartResponse(message);
    }
    var target = permission.getTarget();
    if (target == null || String.valueOf(target).isBlank()) {
        monitor.debug("ContractRequestHandler: Contract Request is invalid");
        return createBadParametersErrorMultipartResponse(message);
    }
    // search for matching asset
    var assetId = IdsIdParser.parse(String.valueOf(target));
    var asset = assetIndex.findById(assetId.getValue());
    if (asset == null) {
        var msg = "Target id is invalid";
        monitor.debug(String.format("ContractRequestHandler: %s", msg));
        return createBadParametersErrorMultipartResponse(message, msg);
    }
    // Create contract offer request
    var input = ContractTransformerInput.Builder.newInstance().contract(contractRequest).asset(asset).build();
    Result<ContractOffer> result = transformerRegistry.transform(input, ContractOffer.class);
    if (result.failed()) {
        monitor.debug(String.format("Could not transform contract request: [%s]", String.join(", ", result.getFailureMessages())));
        return createBadParametersErrorMultipartResponse(message);
    }
    var contractOffer = result.getContent();
    var requestObj = ContractOfferRequest.Builder.newInstance().protocol(Protocols.IDS_MULTIPART).connectorAddress(idsWebhookAddress.toString()).type(ContractOfferRequest.Type.INITIAL).connectorId(String.valueOf(message.getIssuerConnector())).correlationId(String.valueOf(message.getTransferContract())).contractOffer(contractOffer).build();
    Message response;
    try {
        response = responseMessageFactory.createRequestInProcessMessage(message);
    } catch (Exception e) {
        if (e instanceof InvalidCorrelationMessageException) {
            monitor.debug(String.format("Rejecting invalid IDS contract request message [Msg-ID: %s]", message.getId()), e);
        } else {
            monitor.severe(String.format("Exception while creating IDS RequestInProcessMessage to answer contract request [Msg-ID: %s]", message.getId()), e);
        }
        response = responseMessageFactory.createRejectionMessage(message, e);
    }
    if (response instanceof RequestInProcessMessage) {
        // Start negotiation process
        negotiationManager.requested(claimToken, requestObj);
    }
    return MultipartResponse.Builder.newInstance().header(response).build();
}
Also used : RequestInProcessMessage(de.fraunhofer.iais.eis.RequestInProcessMessage) ContractRequestMessage(de.fraunhofer.iais.eis.ContractRequestMessage) InvalidCorrelationMessageException(org.eclipse.dataspaceconnector.ids.api.multipart.message.ids.exceptions.InvalidCorrelationMessageException) ContractRequestMessage(de.fraunhofer.iais.eis.ContractRequestMessage) RequestInProcessMessage(de.fraunhofer.iais.eis.RequestInProcessMessage) Message(de.fraunhofer.iais.eis.Message) ContractOffer(org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer) ContractRequest(de.fraunhofer.iais.eis.ContractRequest) IOException(java.io.IOException) InvalidCorrelationMessageException(org.eclipse.dataspaceconnector.ids.api.multipart.message.ids.exceptions.InvalidCorrelationMessageException) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ContractOffer (org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer)29 Test (org.junit.jupiter.api.Test)22 ContractOfferRequest (org.eclipse.dataspaceconnector.spi.types.domain.contract.negotiation.ContractOfferRequest)12 Asset (org.eclipse.dataspaceconnector.spi.types.domain.asset.Asset)11 ContractAgreement (org.eclipse.dataspaceconnector.spi.types.domain.contract.agreement.ContractAgreement)10 UUID (java.util.UUID)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Policy (org.eclipse.dataspaceconnector.policy.model.Policy)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 ComponentTest (org.eclipse.dataspaceconnector.common.annotations.ComponentTest)6 Disabled (org.junit.jupiter.api.Disabled)6 Instant (java.time.Instant)5 CommandQueue (org.eclipse.dataspaceconnector.spi.command.CommandQueue)5 CommandRunner (org.eclipse.dataspaceconnector.spi.command.CommandRunner)5 ContractNegotiationObservable (org.eclipse.dataspaceconnector.spi.contract.negotiation.observe.ContractNegotiationObservable)5 ContractNegotiationStore (org.eclipse.dataspaceconnector.spi.contract.negotiation.store.ContractNegotiationStore)5 ContractValidationService (org.eclipse.dataspaceconnector.spi.contract.validation.ContractValidationService)5 ClaimToken (org.eclipse.dataspaceconnector.spi.iam.ClaimToken)5