Search in sources :

Example 1 with Serializer

use of de.fraunhofer.iais.eis.ids.jsonld.Serializer 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 Serializer

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

the class ProcessCreationRequestService method buildMultipartBody.

/**
 * Builds the multipart body for creating a process at the Clearing House. A custom build method
 * is required here, as the Clearing House expects the payload part to have content type
 * application/json.
 *
 * @param header the header.
 * @param payload the payload.
 * @return the multipart body.
 * @throws SerializeException if the multipart message could not be built.
 */
@Override
protected MultipartBody buildMultipartBody(final Message header, final Object payload) throws SerializeException {
    try {
        final var builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        // Add header part
        builder.addFormDataPart(MultipartDatapart.HEADER.toString(), new Serializer().serialize(header));
        // Build and add payload part
        final var payloadPart = RequestBody.create(payload.toString(), MediaType.parse("application/json"));
        builder.addFormDataPart(MultipartDatapart.PAYLOAD.toString(), "payload", payloadPart);
        return builder.build();
    } catch (IOException e) {
        throw new SerializeException(e);
    }
}
Also used : SerializeException(ids.messaging.common.SerializeException) RequestMessageBuilder(de.fraunhofer.iais.eis.RequestMessageBuilder) IOException(java.io.IOException) Serializer(de.fraunhofer.iais.eis.ids.jsonld.Serializer)

Example 3 with Serializer

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

the class ContractRequestHandlerTest method checkContractRequest_invalidRulesInContract_rejectContractWithMalformedMessageResponse.

@Test
@SneakyThrows
public void checkContractRequest_invalidRulesInContract_rejectContractWithMalformedMessageResponse() {
    /* ARRANGE */
    final var contractRequest = new ContractRequestBuilder(URI.create("https://someUri"))._permission_(Util.asList(new PermissionBuilder()._action_(Util.asList(Action.USE))._target_(artifactId).build())).build();
    final var payload = new Serializer().serialize(contractRequest).replace("idsc:USE", "idsc" + ":DONTNOW");
    final var issuerConnector = URI.create("https://localhost:8080");
    final var desc = new ContractDesc();
    desc.setConsumer(issuerConnector);
    desc.setStart(ZonedDateTime.of(2020, 1, 1, 1, 1, 1, 1, ZoneId.systemDefault()));
    desc.setEnd(ZonedDateTime.of(2030, 1, 1, 1, 1, 1, 1, ZoneId.systemDefault()));
    final var contract = new ContractFactory().create(desc);
    final var message = getMessage();
    Mockito.doReturn(Arrays.asList(contract)).when(dependencyResolver).getContractOffersByArtifactId(Mockito.eq(artifactId));
    Mockito.doThrow(IllegalArgumentException.class).when(dependencyResolver).getRulesByContractOffer(Mockito.eq(contract));
    /* ACT */
    final var result = (ErrorResponse) handler.handleMessage((ContractRequestMessageImpl) message, new MessagePayloadInputstream(new ByteArrayInputStream(payload.getBytes()), new ObjectMapper()));
    /* ASSERT */
    assertEquals(RejectionReason.MALFORMED_MESSAGE, result.getRejectionMessage().getRejectionReason());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ContractRequestMessageImpl(de.fraunhofer.iais.eis.ContractRequestMessageImpl) ContractFactory(io.dataspaceconnector.model.contract.ContractFactory) PermissionBuilder(de.fraunhofer.iais.eis.PermissionBuilder) ContractDesc(io.dataspaceconnector.model.contract.ContractDesc) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ContractRequestBuilder(de.fraunhofer.iais.eis.ContractRequestBuilder) Serializer(de.fraunhofer.iais.eis.ids.jsonld.Serializer) ErrorResponse(ids.messaging.response.ErrorResponse) MessagePayloadInputstream(ids.messaging.handler.message.MessagePayloadInputstream) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) SneakyThrows(lombok.SneakyThrows)

Example 4 with Serializer

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

the class ContractRequestHandlerTest method checkContractRequest_validRequest_returnOk.

@Test
@SneakyThrows
public void checkContractRequest_validRequest_returnOk() {
    /* ARRANGE */
    final var permission = new PermissionBuilder()._action_(Util.asList(Action.USE))._target_(artifactId).build();
    final var contractRequest = new ContractRequestBuilder(URI.create("https://someUri"))._permission_(Util.asList(permission)).build();
    final var payload = new Serializer().serialize(contractRequest);
    final var issuerConnector = URI.create("https://localhost:8080");
    final var contractDesc = new ContractDesc();
    contractDesc.setConsumer(issuerConnector);
    contractDesc.setStart(ZonedDateTime.of(2020, 1, 1, 1, 1, 1, 1, ZoneId.systemDefault()));
    contractDesc.setEnd(ZonedDateTime.of(2030, 1, 1, 1, 1, 1, 1, ZoneId.systemDefault()));
    final var contract = new ContractFactory().create(contractDesc);
    final var ruleDesc = new ContractRuleDesc();
    ruleDesc.setValue(new Serializer().serialize(permission));
    final var rule = new ContractRuleFactory().create(ruleDesc);
    final var message = getMessage();
    Mockito.doReturn(Arrays.asList(contract)).when(dependencyResolver).getContractOffersByArtifactId(Mockito.eq(artifactId));
    Mockito.doReturn(Arrays.asList(rule)).when(dependencyResolver).getRulesByContractOffer(Mockito.eq(contract));
    Mockito.doReturn(getContractAgreement()).when(persistenceService).buildAndSaveContractAgreement(any(), Mockito.eq(Arrays.asList(artifactId)), Mockito.eq(issuerConnector));
    when(connectorService.getCurrentDat()).thenReturn(new DynamicAttributeTokenBuilder()._tokenFormat_(TokenFormat.JWT)._tokenValue_("value").build());
    /* ACT */
    final var result = (BodyResponse<?>) handler.handleMessage((ContractRequestMessageImpl) message, new MessagePayloadInputstream(new ByteArrayInputStream(payload.getBytes()), new ObjectMapper()));
    /* ASSERT */
    assertTrue(result.getHeader() instanceof ContractAgreementMessage);
}
Also used : ContractRuleDesc(io.dataspaceconnector.model.rule.ContractRuleDesc) ContractRequestMessageImpl(de.fraunhofer.iais.eis.ContractRequestMessageImpl) ContractFactory(io.dataspaceconnector.model.contract.ContractFactory) DynamicAttributeTokenBuilder(de.fraunhofer.iais.eis.DynamicAttributeTokenBuilder) ContractDesc(io.dataspaceconnector.model.contract.ContractDesc) BodyResponse(ids.messaging.response.BodyResponse) ContractAgreementMessage(de.fraunhofer.iais.eis.ContractAgreementMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) PermissionBuilder(de.fraunhofer.iais.eis.PermissionBuilder) ContractRuleFactory(io.dataspaceconnector.model.rule.ContractRuleFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ContractRequestBuilder(de.fraunhofer.iais.eis.ContractRequestBuilder) Serializer(de.fraunhofer.iais.eis.ids.jsonld.Serializer) MessagePayloadInputstream(ids.messaging.handler.message.MessagePayloadInputstream) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) SneakyThrows(lombok.SneakyThrows)

Example 5 with Serializer

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

the class ContractRequestHandlerTest method checkContractRequest_notAValidTarget_returnInternalRecipientErrorResponse.

@Test
@SneakyThrows
public void checkContractRequest_notAValidTarget_returnInternalRecipientErrorResponse() {
    /* ARRANGE */
    final var contractRequest = new ContractRequestBuilder(URI.create("https://someUri"))._permission_(Util.asList(new PermissionBuilder()._action_(Util.asList(Action.USE))._target_(URI.create("https://someUri/")).build())).build();
    final var payload = new Serializer().serialize(contractRequest);
    final var message = getMessage();
    /* ACT */
    final var result = (ErrorResponse) handler.handleMessage((ContractRequestMessageImpl) message, new MessagePayloadInputstream(new ByteArrayInputStream(payload.getBytes()), new ObjectMapper()));
    /* ASSERT */
    assertEquals(RejectionReason.INTERNAL_RECIPIENT_ERROR, result.getRejectionMessage().getRejectionReason());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ContractRequestMessageImpl(de.fraunhofer.iais.eis.ContractRequestMessageImpl) PermissionBuilder(de.fraunhofer.iais.eis.PermissionBuilder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ContractRequestBuilder(de.fraunhofer.iais.eis.ContractRequestBuilder) Serializer(de.fraunhofer.iais.eis.ids.jsonld.Serializer) ErrorResponse(ids.messaging.response.ErrorResponse) MessagePayloadInputstream(ids.messaging.handler.message.MessagePayloadInputstream) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) SneakyThrows(lombok.SneakyThrows)

Aggregations

Serializer (de.fraunhofer.iais.eis.ids.jsonld.Serializer)15 Test (org.junit.jupiter.api.Test)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 ContractRequestBuilder (de.fraunhofer.iais.eis.ContractRequestBuilder)9 MessagePayloadInputstream (ids.messaging.handler.message.MessagePayloadInputstream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)9 SneakyThrows (lombok.SneakyThrows)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 ContractRequestMessageImpl (de.fraunhofer.iais.eis.ContractRequestMessageImpl)8 ErrorResponse (ids.messaging.response.ErrorResponse)8 PermissionBuilder (de.fraunhofer.iais.eis.PermissionBuilder)7 ContractDesc (io.dataspaceconnector.model.contract.ContractDesc)4 ContractFactory (io.dataspaceconnector.model.contract.ContractFactory)4 IOException (java.io.IOException)3 BaseConnectorBuilder (de.fraunhofer.iais.eis.BaseConnectorBuilder)2 ConnectorEndpointBuilder (de.fraunhofer.iais.eis.ConnectorEndpointBuilder)2 ResourceBuilder (de.fraunhofer.iais.eis.ResourceBuilder)2 SerializeException (ids.messaging.common.SerializeException)2 AppRouteBuilder (de.fraunhofer.iais.eis.AppRouteBuilder)1 BaseConnector (de.fraunhofer.iais.eis.BaseConnector)1