Search in sources :

Example 1 with ContractOfferBuilder

use of de.fraunhofer.iais.eis.ContractOfferBuilder in project IDS-Messaging-Services by International-Data-Spaces-Association.

the class MultipartResponseConverterTest method testConvertResponse.

@Test
void testConvertResponse() throws IOException, MultipartParseException, UnknownResponseException, DeserializeException {
    // setup converter
    final var serializer = new Serializer();
    final var converter = new MultipartResponseConverter();
    // check messages with parsed payloads
    // ContractResponseMessage
    final var contractResponseMessage = buildContractResponseMessage();
    final var offer = new ContractOfferBuilder().build();
    var multipartMap = buildMultipart(contractResponseMessage, offer);
    var convertedMAP = converter.convertResponse(multipartMap);
    assertEquals(ContractResponseMAP.class, convertedMAP.getClass());
    assertEquals(contractResponseMessage, convertedMAP.getMessage());
    assertEquals(offer, convertedMAP.getPayload().get());
    // ContractOfferMessage
    final var contractOfferMessage = buildContractOfferMessage();
    multipartMap = buildMultipart(contractOfferMessage, offer);
    convertedMAP = converter.convertResponse(multipartMap);
    assertEquals(ContractOfferMAP.class, convertedMAP.getClass());
    assertEquals(contractOfferMessage, convertedMAP.getMessage());
    assertEquals(offer, convertedMAP.getPayload().get());
    // ContractAgreementMessage
    final var contractAgreementMessage = buildContractAgreementMessage();
    final var agreement = new ContractAgreementBuilder()._contractStart_(IdsMessageUtils.getGregorianNow()).build();
    multipartMap = buildMultipart(contractAgreementMessage, agreement);
    convertedMAP = converter.convertResponse(multipartMap);
    assertEquals(contractAgreementMessage, convertedMAP.getMessage());
    assertEquals(agreement, convertedMAP.getPayload().get());
    // ContractRequestMessage
    final var contractRequestMessage = buildContractRequestMessage();
    final var request = new ContractRequestBuilder().build();
    multipartMap = buildMultipart(contractRequestMessage, request);
    convertedMAP = converter.convertResponse(multipartMap);
    assertEquals(contractRequestMessage, convertedMAP.getMessage());
    assertEquals(request, convertedMAP.getPayload().get());
    // ParticipantUpdateMessage
    final var participantUpdateMessage = buildParticipantUpdateMessage();
    final var participant = new ParticipantBuilder()._legalForm_("form").build();
    multipartMap = buildMultipart(participantUpdateMessage, participant);
    convertedMAP = converter.convertResponse(multipartMap);
    assertEquals(participantUpdateMessage, convertedMAP.getMessage());
    assertEquals(participant, convertedMAP.getPayload().get());
    // ResourceUpdateMessage
    final var resourceUpdateMessage = buildResourceUpdateMessage();
    final var resource = new ResourceBuilder().build();
    multipartMap = buildMultipart(resourceUpdateMessage, resource);
    convertedMAP = converter.convertResponse(multipartMap);
    assertEquals(resourceUpdateMessage, convertedMAP.getMessage());
    assertEquals(resource, convertedMAP.getPayload().get());
    // ConnectorUpdateMessage
    final var connUpdateMessage = buildConnUpdateMessage();
    final var component = new BaseConnectorBuilder()._securityProfile_(SecurityProfile.BASE_SECURITY_PROFILE)._inboundModelVersion_(List.of("1.0.0"))._outboundModelVersion_("1.0.0")._hasDefaultEndpoint_(new ConnectorEndpointBuilder()._accessURL_(URI.create("http://example.com")).build())._maintainer_(URI.create("http://example.com"))._curator_(URI.create("http://example.com")).build();
    multipartMap = buildMultipart(connUpdateMessage, component);
    convertedMAP = converter.convertResponse(multipartMap);
    assertEquals(connUpdateMessage, convertedMAP.getMessage());
    assertEquals(component, convertedMAP.getPayload().get());
    // messages where payload does not matter for MAP
    final var messages = List.of(buildDescriptionRequestMessage(), buildDescriptionResponseMessage(), buildContractRejectionMessage(), buildRejectionMessage(), buildArtifactRequestMessage(), buildArtifactResponseMessage(), buildConnUnavailableMessage(), buildQueryMessage(), buildResourceUnavailableMessage(), buildParticipantUnavailableMessage(), buildParticipantRequestMessage());
    for (final var msg : messages) {
        final var multiMap = buildMultipart(msg, "payload");
        final var convMap = converter.convertResponse(multiMap);
        assertEquals(msg, convMap.getMessage());
    }
    // check unsupported message (should throw UnknownResponseException)
    final var noteMsg = buildNotificationMessage();
    assertThrows(UnknownResponseException.class, () -> converter.convertResponse(Map.of("header", serializer.serialize(noteMsg), "payload", "a")));
}
Also used : ResourceBuilder(de.fraunhofer.iais.eis.ResourceBuilder) BaseConnectorBuilder(de.fraunhofer.iais.eis.BaseConnectorBuilder) ConnectorEndpointBuilder(de.fraunhofer.iais.eis.ConnectorEndpointBuilder) ContractOfferBuilder(de.fraunhofer.iais.eis.ContractOfferBuilder) ContractAgreementBuilder(de.fraunhofer.iais.eis.ContractAgreementBuilder) ParticipantBuilder(de.fraunhofer.iais.eis.ParticipantBuilder) Serializer(de.fraunhofer.iais.eis.ids.jsonld.Serializer) ContractRequestBuilder(de.fraunhofer.iais.eis.ContractRequestBuilder) Test(org.junit.jupiter.api.Test)

Example 2 with ContractOfferBuilder

use of de.fraunhofer.iais.eis.ContractOfferBuilder in project DataSpaceConnector by eclipse-dataspaceconnector.

the class OfferedAssetToIdsResourceTransformerTest method testSuccessfulSimple.

@Test
void testSuccessfulSimple() {
    var representation = new RepresentationBuilder().build();
    var id = IdsId.Builder.newInstance().value(RESOURCE_ID).type(IdsType.RESOURCE).build();
    when(context.transform(any(Asset.class), eq(Representation.class))).thenReturn(representation);
    when(context.transform(any(ContractOffer.class), eq(de.fraunhofer.iais.eis.ContractOffer.class))).thenReturn(new ContractOfferBuilder().build());
    when(context.transform(eq(id), eq(URI.class))).thenReturn(RESOURCE_ID_URI);
    var result = transformer.transform(assetAndPolicy(), context);
    Assertions.assertNotNull(result);
    Assertions.assertEquals(RESOURCE_ID_URI, result.getId());
    Assertions.assertEquals(1, result.getRepresentation().size());
    Assertions.assertEquals(representation, result.getRepresentation().get(0));
    verify(context).transform(any(Asset.class), eq(Representation.class));
    verify(context).transform(any(ContractOffer.class), eq(de.fraunhofer.iais.eis.ContractOffer.class));
    verify(context).transform(eq(id), eq(URI.class));
}
Also used : ContractOfferBuilder(de.fraunhofer.iais.eis.ContractOfferBuilder) RepresentationBuilder(de.fraunhofer.iais.eis.RepresentationBuilder) 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) Representation(de.fraunhofer.iais.eis.Representation) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 3 with ContractOfferBuilder

use of de.fraunhofer.iais.eis.ContractOfferBuilder in project DataspaceConnector by International-Data-Spaces-Association.

the class IdsContractBuilder method createInternal.

@Override
protected ContractOffer createInternal(final Contract contract, final int currentDepth, final int maxDepth) throws ConstraintViolationException {
    // Build children.
    final var permissions = create(permBuilder, onlyPermissions(contract.getRules()), currentDepth, maxDepth);
    final var prohibitions = create(prohBuilder, onlyProhibitions(contract.getRules()), currentDepth, maxDepth);
    final var duties = create(dutyBuilder, onlyDuties(contract.getRules()), currentDepth, maxDepth);
    // Build contract only if at least one rule is present.
    if (permissions.isEmpty() && prohibitions.isEmpty() && duties.isEmpty()) {
        return null;
    }
    boolean permissionsEmpty = false;
    boolean prohibitionsEmpty = false;
    boolean dutiesEmpty = false;
    if (permissions.isPresent()) {
        permissionsEmpty = permissions.get().isEmpty();
    }
    if (prohibitions.isPresent()) {
        prohibitionsEmpty = prohibitions.get().isEmpty();
    }
    if (duties.isPresent()) {
        dutiesEmpty = duties.get().isEmpty();
    }
    if (permissionsEmpty && prohibitionsEmpty && dutiesEmpty) {
        return null;
    }
    // Prepare contract attributes.
    final var start = ToIdsObjectMapper.getGregorianOf(contract.getStart());
    final var end = ToIdsObjectMapper.getGregorianOf(contract.getEnd());
    final var consumer = contract.getConsumer();
    final var provider = contract.getProvider();
    final var builder = new ContractOfferBuilder(getAbsoluteSelfLink(contract))._contractStart_(start)._contractEnd_(end)._contractDate_(IdsMessageUtils.getGregorianNow())._consumer_(consumer)._provider_(provider);
    permissions.ifPresent(builder::_permission_);
    prohibitions.ifPresent(builder::_prohibition_);
    duties.ifPresent(builder::_obligation_);
    return builder.build();
}
Also used : ContractOfferBuilder(de.fraunhofer.iais.eis.ContractOfferBuilder)

Example 4 with ContractOfferBuilder

use of de.fraunhofer.iais.eis.ContractOfferBuilder in project DataSpaceConnector by eclipse-dataspaceconnector.

the class ContractOfferToIdsContractOfferTransformer method transform.

@Override
@Nullable
public de.fraunhofer.iais.eis.ContractOffer transform(ContractOffer object, @NotNull TransformerContext context) {
    Objects.requireNonNull(context);
    if (object == null) {
        return null;
    }
    var idsPermissions = new ArrayList<Permission>();
    var idsProhibitions = new ArrayList<Prohibition>();
    var idsObligations = new ArrayList<Duty>();
    var policy = object.getPolicy();
    if (policy.getPermissions() != null) {
        for (var edcPermission : policy.getPermissions()) {
            var idsPermission = context.transform(edcPermission, Permission.class);
            idsPermissions.add(idsPermission);
        }
    }
    if (policy.getProhibitions() != null) {
        for (var edcProhibition : policy.getProhibitions()) {
            var idsProhibition = context.transform(edcProhibition, Prohibition.class);
            idsProhibitions.add(idsProhibition);
        }
    }
    if (policy.getObligations() != null) {
        for (var edcObligation : policy.getObligations()) {
            var idsObligation = context.transform(edcObligation, Duty.class);
            idsObligations.add(idsObligation);
        }
    }
    var idsId = IdsId.Builder.newInstance().value(object.getId()).type(IdsType.CONTRACT_OFFER).build();
    var id = context.transform(idsId, URI.class);
    ContractOfferBuilder contractOfferBuilder = new ContractOfferBuilder(id);
    contractOfferBuilder._obligation_(idsObligations);
    contractOfferBuilder._prohibition_(idsProhibitions);
    contractOfferBuilder._permission_(idsPermissions);
    contractOfferBuilder._consumer_(object.getConsumer());
    contractOfferBuilder._provider_(object.getProvider());
    if (object.getContractStart() != null) {
        try {
            contractOfferBuilder._contractStart_(DatatypeFactory.newInstance().newXMLGregorianCalendar((GregorianCalendar.from(object.getContractStart()))));
        } catch (DatatypeConfigurationException e) {
            context.reportProblem("cannot convert contract start time to XMLGregorian");
        }
    }
    if (object.getContractEnd() != null) {
        try {
            contractOfferBuilder._contractEnd_(DatatypeFactory.newInstance().newXMLGregorianCalendar(((GregorianCalendar.from(object.getContractEnd())))));
        } catch (DatatypeConfigurationException e) {
            context.reportProblem("cannot convert contract end time to XMLGregorian");
        }
    }
    return contractOfferBuilder.build();
}
Also used : DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) ContractOfferBuilder(de.fraunhofer.iais.eis.ContractOfferBuilder) ArrayList(java.util.ArrayList) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ContractOfferBuilder (de.fraunhofer.iais.eis.ContractOfferBuilder)4 Test (org.junit.jupiter.api.Test)2 BaseConnectorBuilder (de.fraunhofer.iais.eis.BaseConnectorBuilder)1 ConnectorEndpointBuilder (de.fraunhofer.iais.eis.ConnectorEndpointBuilder)1 ContractAgreementBuilder (de.fraunhofer.iais.eis.ContractAgreementBuilder)1 ContractRequestBuilder (de.fraunhofer.iais.eis.ContractRequestBuilder)1 ParticipantBuilder (de.fraunhofer.iais.eis.ParticipantBuilder)1 Representation (de.fraunhofer.iais.eis.Representation)1 RepresentationBuilder (de.fraunhofer.iais.eis.RepresentationBuilder)1 ResourceBuilder (de.fraunhofer.iais.eis.ResourceBuilder)1 Serializer (de.fraunhofer.iais.eis.ids.jsonld.Serializer)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 DatatypeConfigurationException (javax.xml.datatype.DatatypeConfigurationException)1 OfferedAsset (org.eclipse.dataspaceconnector.ids.spi.types.container.OfferedAsset)1 Asset (org.eclipse.dataspaceconnector.spi.types.domain.asset.Asset)1 ContractOffer (org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer)1 Nullable (org.jetbrains.annotations.Nullable)1