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")));
}
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);
}
}
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());
}
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);
}
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());
}
Aggregations