From e87b569f9c7c059202ff13c9ac2376d18341b33b Mon Sep 17 00:00:00 2001 From: ndr_brt Date: Fri, 12 Jan 2024 09:00:40 +0100 Subject: [PATCH] refactor: get rid of DspHttpDispatcherDelegate (#3771) --- .../core/base/EdcHttpClientImpl.java | 9 +- .../core/base/EdcHttpClientImplTest.java | 17 +- .../DspCatalogHttpDispatcherExtension.java | 9 +- .../delegate/ByteArrayBodyExtractor.java | 39 +++++ .../CatalogRequestHttpRawDelegate.java | 54 ------ .../DatasetRequestHttpRawDelegate.java | 55 ------ .../delegate/ByteArrayBodyExtractorTest.java | 58 +++++++ .../CatalogRequestHttpRawDelegateTest.java | 64 ------- .../DatasetRequestHttpRawDelegateTest.java | 64 ------- .../DspHttpRemoteMessageDispatcherImpl.java | 65 ++++++-- ...spHttpRemoteMessageDispatcherImplTest.java | 157 ++++++++++++------ .../dispatcher/DspHttpDispatcherDelegate.java | 77 --------- .../DspHttpRemoteMessageDispatcher.java | 12 +- .../DspHttpResponseBodyExtractor.java | 36 ++++ .../response/NullBodyExtractor.java | 27 +++ .../DspHttpDispatcherDelegateTest.java | 117 ------------- .../DspHttpDispatcherDelegateTestBase.java | 109 ------------ ...DspNegotiationHttpDispatcherExtension.java | 27 +-- .../ContractAgreementMessageHttpDelegate.java | 42 ----- ...eementVerificationMessageHttpDelegate.java | 42 ----- ...ctNegotiationEventMessageHttpDelegate.java | 42 ----- ...tiationTerminationMessageHttpDelegate.java | 42 ----- .../ContractOfferMessageHttpDelegate.java | 35 ---- .../ContractRequestMessageHttpDelegate.java | 59 ------- ...tractAgreementMessageHttpDelegateTest.java | 41 ----- ...ntVerificationMessageHttpDelegateTest.java | 41 ----- ...gotiationEventMessageHttpDelegateTest.java | 41 ----- ...ionTerminationMessageHttpDelegateTest.java | 41 ----- .../ContractOfferMessageHttpDelegateTest.java | 42 ----- ...ontractRequestMessageHttpDelegateTest.java | 97 ----------- ...DspTransferProcessDispatcherExtension.java | 13 +- .../delegate/TransferCompletionDelegate.java | 35 ---- .../delegate/TransferRequestDelegate.java | 34 ---- .../delegate/TransferStartDelegate.java | 34 ---- .../delegate/TransferTerminationDelegate.java | 34 ---- .../TransferCompletionDelegateTest.java | 41 ----- .../delegate/TransferRequestDelegateTest.java | 42 ----- .../delegate/TransferStartDelegateTest.java | 41 ----- .../TransferTerminationDelegateTest.java | 41 ----- .../http/GenericHttpRemoteDispatcherImpl.java | 9 +- .../eclipse/edc/spi/http/EdcHttpClient.java | 16 +- 41 files changed, 385 insertions(+), 1516 deletions(-) create mode 100644 data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/ByteArrayBodyExtractor.java delete mode 100644 data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/CatalogRequestHttpRawDelegate.java delete mode 100644 data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/DatasetRequestHttpRawDelegate.java create mode 100644 data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/ByteArrayBodyExtractorTest.java delete mode 100644 data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/CatalogRequestHttpRawDelegateTest.java delete mode 100644 data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/DatasetRequestHttpRawDelegateTest.java delete mode 100644 data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/DspHttpDispatcherDelegate.java create mode 100644 data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/response/DspHttpResponseBodyExtractor.java create mode 100644 data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/response/NullBodyExtractor.java delete mode 100644 data-protocols/dsp/dsp-http-spi/src/test/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/DspHttpDispatcherDelegateTest.java delete mode 100644 data-protocols/dsp/dsp-http-spi/src/testFixtures/java/org/eclipse/edc/protocol/dsp/spi/testfixtures/dispatcher/DspHttpDispatcherDelegateTestBase.java delete mode 100644 data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementMessageHttpDelegate.java delete mode 100644 data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementVerificationMessageHttpDelegate.java delete mode 100644 data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationEventMessageHttpDelegate.java delete mode 100644 data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationTerminationMessageHttpDelegate.java delete mode 100644 data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractOfferMessageHttpDelegate.java delete mode 100644 data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractRequestMessageHttpDelegate.java delete mode 100644 data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementMessageHttpDelegateTest.java delete mode 100644 data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementVerificationMessageHttpDelegateTest.java delete mode 100644 data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationEventMessageHttpDelegateTest.java delete mode 100644 data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationTerminationMessageHttpDelegateTest.java delete mode 100644 data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractOfferMessageHttpDelegateTest.java delete mode 100644 data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractRequestMessageHttpDelegateTest.java delete mode 100644 data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferCompletionDelegate.java delete mode 100644 data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferRequestDelegate.java delete mode 100644 data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferStartDelegate.java delete mode 100644 data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferTerminationDelegate.java delete mode 100644 data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferCompletionDelegateTest.java delete mode 100644 data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferRequestDelegateTest.java delete mode 100644 data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferStartDelegateTest.java delete mode 100644 data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferTerminationDelegateTest.java diff --git a/core/common/connector-core/src/main/java/org/eclipse/edc/connector/core/base/EdcHttpClientImpl.java b/core/common/connector-core/src/main/java/org/eclipse/edc/connector/core/base/EdcHttpClientImpl.java index 721c97f6cf1..5f8d11f3055 100644 --- a/core/common/connector-core/src/main/java/org/eclipse/edc/connector/core/base/EdcHttpClientImpl.java +++ b/core/common/connector-core/src/main/java/org/eclipse/edc/connector/core/base/EdcHttpClientImpl.java @@ -80,13 +80,18 @@ public CompletableFuture executeAsync(Request request, Function CompletableFuture executeAsync(Request request, List fallbacks, Function mappingFunction) { + public CompletableFuture executeAsync(Request request, List fallbacks) { var call = okHttpClient.newCall(request); var builder = with(retryPolicy); fallbacks.stream().map(it -> it.create(request)).forEach(builder::compose); return builder.compose(call) - .executeAsync() + .executeAsync(); + } + + @Override + public CompletableFuture executeAsync(Request request, List fallbacks, Function mappingFunction) { + return executeAsync(request, fallbacks) .thenApply(response -> { try (response) { return mappingFunction.apply(response); diff --git a/core/common/connector-core/src/test/java/org/eclipse/edc/connector/core/base/EdcHttpClientImplTest.java b/core/common/connector-core/src/test/java/org/eclipse/edc/connector/core/base/EdcHttpClientImplTest.java index 22d46f4f1b3..279a473c713 100644 --- a/core/common/connector-core/src/test/java/org/eclipse/edc/connector/core/base/EdcHttpClientImplTest.java +++ b/core/common/connector-core/src/test/java/org/eclipse/edc/connector/core/base/EdcHttpClientImplTest.java @@ -17,7 +17,7 @@ import dev.failsafe.RetryPolicy; import okhttp3.Request; import okhttp3.Response; -import org.eclipse.edc.spi.monitor.Monitor; +import org.eclipse.edc.spi.http.EdcHttpClient; import org.eclipse.edc.spi.result.Result; import org.eclipse.edc.spi.types.TypeManager; import org.jetbrains.annotations.NotNull; @@ -33,6 +33,7 @@ import java.util.concurrent.TimeUnit; import java.util.function.Function; +import static java.util.Collections.emptyList; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.eclipse.edc.junit.testfixtures.TestUtils.getFreePort; import static org.eclipse.edc.junit.testfixtures.TestUtils.testOkHttpClient; @@ -56,8 +57,8 @@ class EdcHttpClientImplTest { private ClientAndServer server; @NotNull - private static EdcHttpClientImpl clientWith(RetryPolicy retryPolicy) { - return new EdcHttpClientImpl(testOkHttpClient(), retryPolicy, mock(Monitor.class)); + private static EdcHttpClient clientWith(RetryPolicy retryPolicy) { + return new EdcHttpClientImpl(testOkHttpClient(), retryPolicy, mock()); } @BeforeEach @@ -202,7 +203,7 @@ void executeAsync_fallback_shouldRetryIfStatusIsNotSuccessful() { server.when(request(), unlimited()).respond(new HttpResponse().withStatusCode(500)); - var result = client.executeAsync(request, List.of(retryWhenStatusNot2xxOr4xx()), handleResponse()); + var result = client.executeAsync(request, List.of(retryWhenStatusNot2xxOr4xx())).thenApply(handleResponse()); assertThat(result).failsWithin(5, TimeUnit.SECONDS); server.verify(request(), exactly(2)); @@ -218,7 +219,7 @@ void executeAsync_fallback_shouldRetryIfStatusIs4xx() { server.when(request(), unlimited()).respond(new HttpResponse().withStatusCode(500)); - var result = client.executeAsync(request, List.of(retryWhenStatusNot2xxOr4xx()), handleResponse()); + var result = client.executeAsync(request, List.of(retryWhenStatusNot2xxOr4xx())).thenApply(handleResponse()); assertThat(result).failsWithin(5, TimeUnit.SECONDS); server.verify(request(), exactly(2)); @@ -234,7 +235,7 @@ void executeAsync_fallback_shouldNotRetryIfStatusIsExpected() { server.when(request(), unlimited()).respond(new HttpResponse().withStatusCode(404)); - var result = client.executeAsync(request, List.of(retryWhenStatusNot2xxOr4xx()), handleResponse()); + var result = client.executeAsync(request, List.of(retryWhenStatusNot2xxOr4xx())).thenApply(handleResponse()); assertThat(result).succeedsWithin(5, TimeUnit.SECONDS); server.verify(request(), exactly(1)); @@ -249,7 +250,7 @@ void executeAsync_fallback_shouldRetryIfStatusIsNotAsExpected() { .build(); server.when(request(), unlimited()).respond(new HttpResponse().withStatusCode(200)); - var result = client.executeAsync(request, List.of(retryWhenStatusIsNot(204)), handleResponse()); + var result = client.executeAsync(request, List.of(retryWhenStatusIsNot(204))).thenApply(handleResponse()); assertThat(result).failsWithin(5, TimeUnit.SECONDS); server.verify(request(), exactly(2)); @@ -264,7 +265,7 @@ void executeAsync_fallback_shouldFailAfterAttemptsExpired_whenServerIsDown() { .url("http://localhost:" + port) .build(); - var result = client.executeAsync(request, handleResponse()); + var result = client.executeAsync(request, emptyList()).thenApply(handleResponse()); assertThat(result).failsWithin(5, TimeUnit.SECONDS); } diff --git a/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/DspCatalogHttpDispatcherExtension.java b/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/DspCatalogHttpDispatcherExtension.java index e0282861f53..e79622e4190 100644 --- a/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/DspCatalogHttpDispatcherExtension.java +++ b/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/DspCatalogHttpDispatcherExtension.java @@ -16,8 +16,7 @@ import org.eclipse.edc.catalog.spi.CatalogRequestMessage; import org.eclipse.edc.catalog.spi.DatasetRequestMessage; -import org.eclipse.edc.protocol.dsp.catalog.dispatcher.delegate.CatalogRequestHttpRawDelegate; -import org.eclipse.edc.protocol.dsp.catalog.dispatcher.delegate.DatasetRequestHttpRawDelegate; +import org.eclipse.edc.protocol.dsp.catalog.dispatcher.delegate.ByteArrayBodyExtractor; import org.eclipse.edc.protocol.dsp.dispatcher.GetDspHttpRequestFactory; import org.eclipse.edc.protocol.dsp.dispatcher.PostDspHttpRequestFactory; import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpRemoteMessageDispatcher; @@ -52,15 +51,17 @@ public String name() { @Override public void initialize(ServiceExtensionContext context) { + var byteArrayBodyExtractor = new ByteArrayBodyExtractor(); + messageDispatcher.registerMessage( CatalogRequestMessage.class, new PostDspHttpRequestFactory<>(remoteMessageSerializer, m -> BASE_PATH + CATALOG_REQUEST), - new CatalogRequestHttpRawDelegate() + byteArrayBodyExtractor ); messageDispatcher.registerMessage( DatasetRequestMessage.class, new GetDspHttpRequestFactory<>(m -> BASE_PATH + DATASET_REQUEST + "/" + m.getDatasetId()), - new DatasetRequestHttpRawDelegate() + byteArrayBodyExtractor ); } diff --git a/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/ByteArrayBodyExtractor.java b/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/ByteArrayBodyExtractor.java new file mode 100644 index 00000000000..73b4423625a --- /dev/null +++ b/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/ByteArrayBodyExtractor.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + * + * Contributors: + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation + * + */ + +package org.eclipse.edc.protocol.dsp.catalog.dispatcher.delegate; + +import okhttp3.ResponseBody; +import org.eclipse.edc.protocol.dsp.spi.dispatcher.response.DspHttpResponseBodyExtractor; +import org.eclipse.edc.spi.EdcException; + +import java.io.IOException; + +/** + * Extract the body as a byte[] + */ +public class ByteArrayBodyExtractor implements DspHttpResponseBodyExtractor { + @Override + public byte[] extractBody(ResponseBody responseBody) { + try { + if (responseBody == null) { + return null; + } + return responseBody.bytes(); + + } catch (IOException e) { + throw new EdcException("Failed to read response body", e); + } + } +} diff --git a/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/CatalogRequestHttpRawDelegate.java b/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/CatalogRequestHttpRawDelegate.java deleted file mode 100644 index 3931365485b..00000000000 --- a/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/CatalogRequestHttpRawDelegate.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.catalog.dispatcher.delegate; - -import jakarta.json.JsonObject; -import okhttp3.Response; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.spi.EdcException; - -import java.io.IOException; -import java.util.function.Function; - -/** - * Delegate for dispatching catalog requests as defined in the - * dataspace protocol specification - */ -public class CatalogRequestHttpRawDelegate extends DspHttpDispatcherDelegate { - - public CatalogRequestHttpRawDelegate() { - super(); - } - - /** - * Parses the response to a byte[]. It cannot return a {@link java.io.InputStream} because the response gets closed - * by the {@link org.eclipse.edc.spi.http.EdcHttpClient} - * - * @return a function that transforms the response body to a {@link JsonObject}. - */ - @Override - public Function parseResponse() { - return response -> { - try { - //noinspection DataFlowIssue - return response.body().bytes(); - } catch (NullPointerException e) { - throw new EdcException("Failed to read response body, as body was null.", e); - } catch (IOException e) { - throw new EdcException("Failed to read response body", e); - } - }; - } -} diff --git a/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/DatasetRequestHttpRawDelegate.java b/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/DatasetRequestHttpRawDelegate.java deleted file mode 100644 index 15854e611ea..00000000000 --- a/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/DatasetRequestHttpRawDelegate.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.catalog.dispatcher.delegate; - -import jakarta.json.JsonObject; -import okhttp3.Response; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.spi.EdcException; - -import java.io.IOException; -import java.util.function.Function; - -/** - * Delegate for dispatching catalog requests as defined in the - * dataspace protocol specification - */ -public class DatasetRequestHttpRawDelegate extends DspHttpDispatcherDelegate { - - public DatasetRequestHttpRawDelegate() { - super(); - } - - /** - * Parses the response to a byte[]. It cannot return a {@link java.io.InputStream} because the response gets closed - * by the {@link org.eclipse.edc.spi.http.EdcHttpClient} - * - * @return a function that transforms the response body to a {@link JsonObject}. - */ - @Override - public Function parseResponse() { - return response -> { - try { - //noinspection DataFlowIssue - return response.body().bytes(); - - } catch (NullPointerException e) { - throw new EdcException("Failed to read response body, as body was null.", e); - } catch (IOException e) { - throw new EdcException("Failed to read response body", e); - } - }; - } -} diff --git a/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/ByteArrayBodyExtractorTest.java b/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/ByteArrayBodyExtractorTest.java new file mode 100644 index 00000000000..05b3a953375 --- /dev/null +++ b/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/ByteArrayBodyExtractorTest.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + * + * Contributors: + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation + * + */ + +package org.eclipse.edc.protocol.dsp.catalog.dispatcher.delegate; + +import okhttp3.ResponseBody; +import org.eclipse.edc.spi.EdcException; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class ByteArrayBodyExtractorTest { + + private final ByteArrayBodyExtractor extractor = new ByteArrayBodyExtractor(); + + @Test + void shouldReturnBodyAsBytes() throws IOException { + var responseBody = mock(ResponseBody.class); + var bytes = "test".getBytes(); + when(responseBody.bytes()).thenReturn(bytes); + + var result = extractor.extractBody(responseBody); + + assertThat(result).isEqualTo(bytes); + } + + @Test + void shouldReturnNull_whenBodyIsNull() { + var result = extractor.extractBody(null); + + assertThat(result).isNull(); + } + + @Test + void shouldThrowException_whenCannotExtractBytes() throws IOException { + var responseBody = mock(ResponseBody.class); + when(responseBody.bytes()).thenThrow(new IOException()); + + assertThatThrownBy(() -> extractor.extractBody(responseBody)).isInstanceOf(EdcException.class); + } + +} diff --git a/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/CatalogRequestHttpRawDelegateTest.java b/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/CatalogRequestHttpRawDelegateTest.java deleted file mode 100644 index afbd5d30539..00000000000 --- a/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/CatalogRequestHttpRawDelegateTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.catalog.dispatcher.delegate; - -import okhttp3.ResponseBody; -import org.eclipse.edc.catalog.spi.CatalogRequestMessage; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.protocol.dsp.spi.testfixtures.dispatcher.DspHttpDispatcherDelegateTestBase; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.io.ByteArrayInputStream; -import java.io.IOException; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -class CatalogRequestHttpRawDelegateTest extends DspHttpDispatcherDelegateTestBase { - - private CatalogRequestHttpRawDelegate delegate; - - @BeforeEach - void setUp() { - delegate = new CatalogRequestHttpRawDelegate(); - } - - @Test - void parseResponse_returnCatalog() throws IOException { - var responseBody = mock(ResponseBody.class); - var response = dummyResponseBuilder(200).body(responseBody).build(); - var bytes = "test".getBytes(); - - when(responseBody.bytes()).thenReturn(bytes); - when(responseBody.byteStream()).thenReturn(new ByteArrayInputStream(bytes)); - - var result = delegate.parseResponse().apply(response); - - assertThat(result).isEqualTo(bytes); - } - - @Test - void parseResponse_responseBodyNull_throwException() { - testParseResponse_shouldThrowException_whenResponseBodyNull(); - } - - @Override - protected DspHttpDispatcherDelegate delegate() { - return delegate; - } - -} diff --git a/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/DatasetRequestHttpRawDelegateTest.java b/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/DatasetRequestHttpRawDelegateTest.java deleted file mode 100644 index 169dcac818d..00000000000 --- a/data-protocols/dsp/dsp-catalog/dsp-catalog-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/catalog/dispatcher/delegate/DatasetRequestHttpRawDelegateTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.catalog.dispatcher.delegate; - -import okhttp3.ResponseBody; -import org.eclipse.edc.catalog.spi.DatasetRequestMessage; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.protocol.dsp.spi.testfixtures.dispatcher.DspHttpDispatcherDelegateTestBase; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.io.ByteArrayInputStream; -import java.io.IOException; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -class DatasetRequestHttpRawDelegateTest extends DspHttpDispatcherDelegateTestBase { - - private DatasetRequestHttpRawDelegate delegate; - - @BeforeEach - void setUp() { - delegate = new DatasetRequestHttpRawDelegate(); - } - - @Test - void parseResponse_returnDataset() throws IOException { - var responseBody = mock(ResponseBody.class); - var response = dummyResponseBuilder(200).body(responseBody).build(); - var bytes = "test".getBytes(); - - when(responseBody.bytes()).thenReturn(bytes); - when(responseBody.byteStream()).thenReturn(new ByteArrayInputStream(bytes)); - - var result = delegate.parseResponse().apply(response); - - assertThat(result).isEqualTo(bytes); - } - - @Test - void parseResponse_responseBodyNull_throwException() { - testParseResponse_shouldThrowException_whenResponseBodyNull(); - } - - @Override - protected DspHttpDispatcherDelegate delegate() { - return delegate; - } - -} diff --git a/data-protocols/dsp/dsp-http-core/src/main/java/org/eclipse/edc/protocol/dsp/dispatcher/DspHttpRemoteMessageDispatcherImpl.java b/data-protocols/dsp/dsp-http-core/src/main/java/org/eclipse/edc/protocol/dsp/dispatcher/DspHttpRemoteMessageDispatcherImpl.java index 4e732d88969..dad94207f4c 100644 --- a/data-protocols/dsp/dsp-http-core/src/main/java/org/eclipse/edc/protocol/dsp/dispatcher/DspHttpRemoteMessageDispatcherImpl.java +++ b/data-protocols/dsp/dsp-http-core/src/main/java/org/eclipse/edc/protocol/dsp/dispatcher/DspHttpRemoteMessageDispatcherImpl.java @@ -14,12 +14,14 @@ package org.eclipse.edc.protocol.dsp.dispatcher; +import okhttp3.Response; +import okhttp3.ResponseBody; import org.eclipse.edc.policy.engine.spi.PolicyContextImpl; import org.eclipse.edc.policy.engine.spi.PolicyEngine; import org.eclipse.edc.policy.model.Policy; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpRemoteMessageDispatcher; import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpRequestFactory; +import org.eclipse.edc.protocol.dsp.spi.dispatcher.response.DspHttpResponseBodyExtractor; import org.eclipse.edc.protocol.dsp.spi.types.HttpMessageProtocol; import org.eclipse.edc.spi.EdcException; import org.eclipse.edc.spi.http.EdcHttpClient; @@ -28,24 +30,28 @@ import org.eclipse.edc.spi.iam.TokenParameters; import org.eclipse.edc.spi.response.StatusResult; import org.eclipse.edc.spi.types.domain.message.RemoteMessage; +import org.jetbrains.annotations.NotNull; +import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.function.Function; import static java.lang.String.format; import static java.util.concurrent.CompletableFuture.failedFuture; import static org.eclipse.edc.spi.http.FallbackFactories.retryWhenStatusNot2xxOr4xx; +import static org.eclipse.edc.spi.response.ResponseStatus.ERROR_RETRY; +import static org.eclipse.edc.spi.response.ResponseStatus.FATAL_ERROR; /** - * Dispatches remote messages using the dataspace protocol. Uses {@link DspHttpDispatcherDelegate}s - * for creating the requests and parsing the responses for specific message types. + * Dispatches remote messages using the dataspace protocol. */ public class DspHttpRemoteMessageDispatcherImpl implements DspHttpRemoteMessageDispatcher { - private final Map, Handlers> handlers = new HashMap<>(); + private final Map, MessageHandler> handlers = new HashMap<>(); private final Map, PolicyScope> policyScopes = new HashMap<>(); private final EdcHttpClient httpClient; private final IdentityService identityService; @@ -69,12 +75,12 @@ public String protocol() { @Override public CompletableFuture> dispatch(Class responseType, M message) { - var handlers = (Handlers) this.handlers.get(message.getClass()); - if (handlers == null) { + var handler = (MessageHandler) this.handlers.get(message.getClass()); + if (handler == null) { return failedFuture(new EdcException(format("No DSP message dispatcher found for message type %s", message.getClass()))); } - var request = handlers.requestFactory.createRequest(message); + var request = handler.requestFactory.createRequest(message); var tokenParametersBuilder = tokenDecorator.decorate(TokenParameters.Builder.newInstance()); @@ -97,7 +103,8 @@ public CompletableFuture> dispatch( .header("Authorization", token.getToken()) .build(); - return httpClient.executeAsync(requestWithAuth, List.of(retryWhenStatusNot2xxOr4xx()), handlers.delegate.handleResponse()); + return httpClient.executeAsync(requestWithAuth, List.of(retryWhenStatusNot2xxOr4xx())) + .thenApply(response -> handleResponse(response, responseType, handler.bodyExtractor)); }) .orElse(failure -> failedFuture(new EdcException(format("Unable to obtain credentials: %s", failure.getFailureDetail())))); } @@ -108,12 +115,46 @@ public void registerPolicyScope(Class messageClass, } @Override - public void registerMessage(Class clazz, DspHttpRequestFactory requestFactory, DspHttpDispatcherDelegate delegate) { - handlers.put(clazz, new Handlers<>(requestFactory, delegate)); + public void registerMessage(Class clazz, DspHttpRequestFactory requestFactory, + DspHttpResponseBodyExtractor bodyExtractor) { + handlers.put(clazz, new MessageHandler<>(requestFactory, bodyExtractor)); } - private record Handlers(DspHttpRequestFactory requestFactory, DspHttpDispatcherDelegate delegate) { } + @NotNull + private StatusResult handleResponse(Response response, Class responseType, DspHttpResponseBodyExtractor bodyExtractor) { + try (var responseBody = response.body()) { + if (response.isSuccessful()) { + var responsePayload = bodyExtractor.extractBody(responseBody); - private record PolicyScope(Class messageClass, String scope, Function policyProvider) {} + return StatusResult.success(responseType.cast(responsePayload)); + } else { + var stringBody = Optional.ofNullable(responseBody) + .map(this::asString) + .orElse("Response body is null"); + + var status = response.code() >= 400 && response.code() < 500 ? FATAL_ERROR : ERROR_RETRY; + + return StatusResult.failure(status, stringBody); + } + } + } + + private String asString(ResponseBody it) { + try { + return it.string(); + } catch (IOException e) { + return "Cannot read response body: " + e.getMessage(); + } + } + + private record MessageHandler( + DspHttpRequestFactory requestFactory, + DspHttpResponseBodyExtractor bodyExtractor + ) { } + + private record PolicyScope( + Class messageClass, String scope, + Function policyProvider + ) { } } diff --git a/data-protocols/dsp/dsp-http-core/src/test/java/org/eclipse/edc/protocol/dsp/dispatcher/DspHttpRemoteMessageDispatcherImplTest.java b/data-protocols/dsp/dsp-http-core/src/test/java/org/eclipse/edc/protocol/dsp/dispatcher/DspHttpRemoteMessageDispatcherImplTest.java index eac73a9c152..6beec71d525 100644 --- a/data-protocols/dsp/dsp-http-core/src/test/java/org/eclipse/edc/protocol/dsp/dispatcher/DspHttpRemoteMessageDispatcherImplTest.java +++ b/data-protocols/dsp/dsp-http-core/src/test/java/org/eclipse/edc/protocol/dsp/dispatcher/DspHttpRemoteMessageDispatcherImplTest.java @@ -14,41 +14,48 @@ package org.eclipse.edc.protocol.dsp.dispatcher; +import okhttp3.MediaType; +import okhttp3.Protocol; import okhttp3.Request; -import okhttp3.Response; +import okhttp3.ResponseBody; import org.eclipse.edc.policy.engine.spi.PolicyContext; import org.eclipse.edc.policy.engine.spi.PolicyEngine; import org.eclipse.edc.policy.model.Policy; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpRemoteMessageDispatcher; import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpRequestFactory; +import org.eclipse.edc.protocol.dsp.spi.dispatcher.response.DspHttpResponseBodyExtractor; import org.eclipse.edc.spi.EdcException; import org.eclipse.edc.spi.http.EdcHttpClient; import org.eclipse.edc.spi.iam.IdentityService; import org.eclipse.edc.spi.iam.TokenDecorator; import org.eclipse.edc.spi.iam.TokenParameters; import org.eclipse.edc.spi.iam.TokenRepresentation; -import org.eclipse.edc.spi.response.StatusResult; import org.eclipse.edc.spi.result.Result; import org.eclipse.edc.spi.types.domain.message.RemoteMessage; +import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import java.time.Duration; +import java.util.List; import java.util.Map; -import java.util.function.Function; import static java.time.temporal.ChronoUnit.SECONDS; import static java.util.concurrent.CompletableFuture.completedFuture; import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; import static org.eclipse.edc.protocol.dsp.spi.types.HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP; +import static org.eclipse.edc.spi.response.ResponseStatus.ERROR_RETRY; +import static org.eclipse.edc.spi.response.ResponseStatus.FATAL_ERROR; import static org.mockito.AdditionalMatchers.and; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; @@ -60,15 +67,14 @@ class DspHttpRemoteMessageDispatcherImplTest { private final PolicyEngine policyEngine = mock(); private final TokenDecorator tokenDecorator = mock(); private final DspHttpRequestFactory requestFactory = mock(); - private final DspHttpDispatcherDelegate delegate = mock(); private final Duration timeout = Duration.of(5, SECONDS); - private DspHttpRemoteMessageDispatcher dispatcher; + private final DspHttpRemoteMessageDispatcher dispatcher = + new DspHttpRemoteMessageDispatcherImpl(httpClient, identityService, tokenDecorator, policyEngine); @BeforeEach void setUp() { when(tokenDecorator.decorate(any())).thenAnswer(a -> a.getArgument(0)); - dispatcher = new DspHttpRemoteMessageDispatcherImpl(httpClient, identityService, tokenDecorator, policyEngine); } @Test @@ -76,46 +82,17 @@ void protocol_returnDsp() { assertThat(dispatcher.protocol()).isEqualTo(DATASPACE_PROTOCOL_HTTP); } - @Test - void dispatch_sendRequestViaHttpClient() { - var responseBody = "response"; - Function> responseFunction = response -> StatusResult.success(responseBody); - var authToken = "token"; - - when(requestFactory.createRequest(any())).thenReturn(new Request.Builder().url("http://url").build()); - when(delegate.handleResponse()).thenReturn(responseFunction); - when(httpClient.executeAsync(any(), any(), any())).thenReturn(completedFuture(responseBody)); - when(identityService.obtainClientCredentials(any())) - .thenReturn(Result.success(TokenRepresentation.Builder.newInstance().token(authToken).build())); - - dispatcher.registerMessage(TestMessage.class, requestFactory, delegate); - - var message = new TestMessage(); - var result = dispatcher.dispatch(String.class, message); - - assertThat(result).succeedsWithin(timeout).isEqualTo(responseBody); - - verify(requestFactory).createRequest(message); - verify(identityService).obtainClientCredentials(argThat(tr -> tr.getAudience().equals(message.getCounterPartyAddress()))); - verify(httpClient).executeAsync(argThat(r -> authToken.equals(r.headers().get("Authorization"))), any(), eq(responseFunction)); - } - @Test void dispatch_ensureTokenDecoratorScope() { - var responseBody = "response"; - Function> responseFunction = response -> StatusResult.success(responseBody); var authToken = "token"; - Map additional = Map.of("foo", "bar"); - when(tokenDecorator.decorate(any())).thenAnswer(a -> a.getArgument(0, TokenParameters.Builder.class).scope("test-scope").additional(additional)); when(requestFactory.createRequest(any())).thenReturn(new Request.Builder().url("http://url").build()); - when(delegate.handleResponse()).thenReturn(responseFunction); - when(httpClient.executeAsync(any(), any(), any())).thenReturn(completedFuture(responseBody)); + when(httpClient.executeAsync(any(), isA(List.class))).thenReturn(completedFuture(dummyResponse(200))); when(identityService.obtainClientCredentials(any())) .thenReturn(Result.success(TokenRepresentation.Builder.newInstance().token(authToken).build())); - dispatcher.registerMessage(TestMessage.class, requestFactory, delegate); + dispatcher.registerMessage(TestMessage.class, requestFactory, mock()); var message = new TestMessage(); var result = dispatcher.dispatch(String.class, message); @@ -124,8 +101,8 @@ void dispatch_ensureTokenDecoratorScope() { var captor = ArgumentCaptor.forClass(TokenParameters.class); verify(identityService).obtainClientCredentials(captor.capture()); - verify(httpClient).executeAsync(argThat(r -> authToken.equals(r.headers().get("Authorization"))), any(), eq(responseFunction)); - + verify(httpClient).executeAsync(argThat(r -> authToken.equals(r.headers().get("Authorization"))), isA(List.class)); + verify(requestFactory).createRequest(message); assertThat(captor.getValue()).satisfies(tr -> { assertThat(tr.getScope()).isEqualTo("test-scope"); assertThat(tr.getAudience()).isEqualTo(message.getCounterPartyAddress()); @@ -135,7 +112,7 @@ void dispatch_ensureTokenDecoratorScope() { } @Test - void dispatch_noDelegateFound_throwException() { + void dispatch_messageNotRegistered_throwException() { assertThat(dispatcher.dispatch(String.class, new TestMessage())).failsWithin(timeout) .withThrowableThat().withCauseInstanceOf(EdcException.class).withMessageContaining("found"); @@ -144,7 +121,7 @@ void dispatch_noDelegateFound_throwException() { @Test void dispatch_failedToObtainToken_throwException() { - dispatcher.registerMessage(TestMessage.class, requestFactory, delegate); + dispatcher.registerMessage(TestMessage.class, requestFactory, mock()); when(requestFactory.createRequest(any())).thenReturn(new Request.Builder().url("http://url").build()); when(identityService.obtainClientCredentials(any())).thenReturn(Result.failure("error")); @@ -157,11 +134,10 @@ void dispatch_failedToObtainToken_throwException() { @Test void dispatch_shouldNotEvaluatePolicy_whenItIsNotRegistered() { when(requestFactory.createRequest(any())).thenReturn(new Request.Builder().url("http://url").build()); - when(delegate.handleResponse()).thenReturn(response -> null); - when(httpClient.executeAsync(any(), any(), any())).thenReturn(completedFuture(null)); + when(httpClient.executeAsync(any(), isA(List.class))).thenReturn(completedFuture(dummyResponse(200))); when(identityService.obtainClientCredentials(any())) .thenReturn(Result.success(TokenRepresentation.Builder.newInstance().token("any").build())); - dispatcher.registerMessage(TestMessage.class, requestFactory, delegate); + dispatcher.registerMessage(TestMessage.class, requestFactory, mock()); var result = dispatcher.dispatch(String.class, new TestMessage()); @@ -172,11 +148,10 @@ void dispatch_shouldNotEvaluatePolicy_whenItIsNotRegistered() { @Test void dispatch_shouldEvaluatePolicy() { when(requestFactory.createRequest(any())).thenReturn(new Request.Builder().url("http://url").build()); - when(delegate.handleResponse()).thenReturn(response -> null); - when(httpClient.executeAsync(any(), any(), any())).thenReturn(completedFuture(null)); + when(httpClient.executeAsync(any(), isA(List.class))).thenReturn(completedFuture(dummyResponse(200))); when(identityService.obtainClientCredentials(any())) .thenReturn(Result.success(TokenRepresentation.Builder.newInstance().token("any").build())); - dispatcher.registerMessage(TestMessage.class, requestFactory, delegate); + dispatcher.registerMessage(TestMessage.class, requestFactory, mock()); var policy = Policy.Builder.newInstance().build(); dispatcher.registerPolicyScope(TestMessage.class, "test.message", m -> policy); @@ -186,6 +161,92 @@ void dispatch_shouldEvaluatePolicy() { verify(policyEngine).evaluate(eq("test.message"), eq(policy), and(isA(PolicyContext.class), argThat(c -> c.getContextData(TokenParameters.Builder.class) != null))); } + @Nested + class Response { + + private final DspHttpResponseBodyExtractor bodyExtractor = mock(); + + @Test + void shouldShouldReturnSuccess_whenResponseIsSuccessful() { + respondWith(dummyResponse(200), bodyExtractor); + when(bodyExtractor.extractBody(any())).thenReturn("response"); + + var future = dispatcher.dispatch(String.class, new TestMessage()); + + assertThat(future).succeedsWithin(timeout).satisfies(result -> { + assertThat(result).isSucceeded().isEqualTo("response"); + }); + } + + @Test + void shouldReturnFatalError_whenResponseIsClientError() { + var responseBody = ResponseBody.create("expectedValue", MediaType.get("application/json")); + respondWith(dummyResponseBuilder(400).body(responseBody).build(), bodyExtractor); + + var future = dispatcher.dispatch(String.class, new TestMessage()); + + assertThat(future).succeedsWithin(timeout).satisfies(result -> { + assertThat(result).isFailed().satisfies(failure -> { + assertThat(failure.status()).isEqualTo(FATAL_ERROR); + assertThat(failure.getMessages()).containsOnly("expectedValue"); + }); + }); + verify(bodyExtractor, never()).extractBody(any()); + } + + @Test + void shouldReturnFatalError_whenResponseIsClientErrorAndBodyIsNull() { + respondWith(dummyResponseBuilder(400).body(null).build(), bodyExtractor); + + var future = dispatcher.dispatch(String.class, new TestMessage()); + + assertThat(future).succeedsWithin(timeout).satisfies(result -> { + assertThat(result).isFailed().satisfies(failure -> { + assertThat(failure.status()).isEqualTo(FATAL_ERROR); + assertThat(failure.getMessages()).allMatch(it -> it.contains("is null")); + }); + }); + verify(bodyExtractor, never()).extractBody(any()); + } + + @Test + void shouldReturnRetryError_whenResponseIsServerError() { + respondWith(dummyResponse(500), bodyExtractor); + + var future = dispatcher.dispatch(String.class, new TestMessage()); + + assertThat(future).succeedsWithin(timeout).satisfies(result -> { + assertThat(result).isFailed().satisfies(failure -> { + assertThat(failure.status()).isEqualTo(ERROR_RETRY); + }); + }); + verify(bodyExtractor, never()).extractBody(any()); + } + + private void respondWith(okhttp3.Response response, DspHttpResponseBodyExtractor bodyExtractor) { + when(requestFactory.createRequest(any())).thenReturn(new Request.Builder().url("http://url").build()); + when(httpClient.executeAsync(any(), isA(List.class))).thenReturn(completedFuture(response)); + when(identityService.obtainClientCredentials(any())) + .thenReturn(Result.success(TokenRepresentation.Builder.newInstance().token("token").build())); + dispatcher.registerMessage(TestMessage.class, requestFactory, bodyExtractor); + } + } + + private static okhttp3.Response dummyResponse(int code) { + return dummyResponseBuilder(code) + .build(); + } + + @NotNull + private static okhttp3.Response.Builder dummyResponseBuilder(int code) { + return new okhttp3.Response.Builder() + .code(code) + .message("any") + .body(ResponseBody.create("", MediaType.get("application/json"))) + .protocol(Protocol.HTTP_1_1) + .request(new Request.Builder().url("http://any").build()); + } + static class TestMessage implements RemoteMessage { @Override public String getProtocol() { diff --git a/data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/DspHttpDispatcherDelegate.java b/data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/DspHttpDispatcherDelegate.java deleted file mode 100644 index 4309efcd980..00000000000 --- a/data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/DspHttpDispatcherDelegate.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.spi.dispatcher; - -import okhttp3.Response; -import okhttp3.ResponseBody; -import org.eclipse.edc.spi.response.StatusResult; -import org.eclipse.edc.spi.types.domain.message.RemoteMessage; - -import java.io.IOException; -import java.util.Optional; -import java.util.function.Function; - -import static org.eclipse.edc.spi.response.ResponseStatus.ERROR_RETRY; -import static org.eclipse.edc.spi.response.ResponseStatus.FATAL_ERROR; - -/** - * Delegate for sending a specific type of {@link RemoteMessage} using the dataspace protocol. - * - * @param the response type - */ -public abstract class DspHttpDispatcherDelegate { - - protected DspHttpDispatcherDelegate() { - } - - /** - * Handles the response and returns a {@link StatusResult} containing the response object - * - * @return the {@link StatusResult} - */ - public Function> handleResponse() { - return response -> { - if (response.isSuccessful()) { - var responsePayload = parseResponse().apply(response); - return StatusResult.success(responsePayload); - } else { - var responseBody = Optional.ofNullable(response.body()) - .map(this::asString) - .orElse("Response body is null"); - - var status = response.code() >= 400 && response.code() < 500 - ? FATAL_ERROR : ERROR_RETRY; - - return StatusResult.failure(status, responseBody); - } - }; - } - - /** - * Parses the response to return an instance of the expected response type. - * - * @return the parsed response - */ - protected abstract Function parseResponse(); - - private String asString(ResponseBody it) { - try { - return it.string(); - } catch (IOException e) { - return "Cannot read response body: " + e.getMessage(); - } - } - -} diff --git a/data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/DspHttpRemoteMessageDispatcher.java b/data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/DspHttpRemoteMessageDispatcher.java index ce2f1f5aa25..2de8d8c45e0 100644 --- a/data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/DspHttpRemoteMessageDispatcher.java +++ b/data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/DspHttpRemoteMessageDispatcher.java @@ -15,6 +15,7 @@ package org.eclipse.edc.protocol.dsp.spi.dispatcher; import org.eclipse.edc.policy.model.Policy; +import org.eclipse.edc.protocol.dsp.spi.dispatcher.response.DspHttpResponseBodyExtractor; import org.eclipse.edc.spi.message.RemoteMessageDispatcher; import org.eclipse.edc.spi.types.domain.message.RemoteMessage; @@ -28,13 +29,14 @@ public interface DspHttpRemoteMessageDispatcher extends RemoteMessageDispatcher /** * Registers a message request factory and response parser * - * @param clazz the message class. + * @param the type of message + * @param the response type + * @param clazz the message class. * @param requestFactory the request factory. - * @param delegate the response parser delegate. - * @param the type of message - * @param the response type + * @param bodyExtractor the body extractor function. */ - void registerMessage(Class clazz, DspHttpRequestFactory requestFactory, DspHttpDispatcherDelegate delegate); + void registerMessage(Class clazz, DspHttpRequestFactory requestFactory, + DspHttpResponseBodyExtractor bodyExtractor); /** * Registers a {@link Policy} scope to be evaluated for certain types of messages diff --git a/data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/response/DspHttpResponseBodyExtractor.java b/data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/response/DspHttpResponseBodyExtractor.java new file mode 100644 index 00000000000..eb6bf11b645 --- /dev/null +++ b/data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/response/DspHttpResponseBodyExtractor.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + * + * Contributors: + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation + * + */ + +package org.eclipse.edc.protocol.dsp.spi.dispatcher.response; + +import okhttp3.ResponseBody; + +/** + * Extract the body from a http response body into a concrete type + * + * @param the type of the body. + */ +@FunctionalInterface +public interface DspHttpResponseBodyExtractor { + + DspHttpResponseBodyExtractor NOOP = r -> null; + + /** + * Extract the body from the Response + * + * @param responseBody the Response. + * @return the body. + */ + R extractBody(ResponseBody responseBody); +} diff --git a/data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/response/NullBodyExtractor.java b/data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/response/NullBodyExtractor.java new file mode 100644 index 00000000000..f28bc79fd88 --- /dev/null +++ b/data-protocols/dsp/dsp-http-spi/src/main/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/response/NullBodyExtractor.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + * + * Contributors: + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation + * + */ + +package org.eclipse.edc.protocol.dsp.spi.dispatcher.response; + +import okhttp3.ResponseBody; + +/** + * Do not extract the body from response and return null. + */ +public class NullBodyExtractor implements DspHttpResponseBodyExtractor { + @Override + public Object extractBody(ResponseBody response) { + return null; + } +} diff --git a/data-protocols/dsp/dsp-http-spi/src/test/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/DspHttpDispatcherDelegateTest.java b/data-protocols/dsp/dsp-http-spi/src/test/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/DspHttpDispatcherDelegateTest.java deleted file mode 100644 index 63f7378a674..00000000000 --- a/data-protocols/dsp/dsp-http-spi/src/test/java/org/eclipse/edc/protocol/dsp/spi/dispatcher/DspHttpDispatcherDelegateTest.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.spi.dispatcher; - -import okhttp3.MediaType; -import okhttp3.Protocol; -import okhttp3.Request; -import okhttp3.Response; -import okhttp3.ResponseBody; -import org.eclipse.edc.protocol.dsp.spi.serialization.JsonLdRemoteMessageSerializer; -import org.eclipse.edc.spi.response.ResponseFailure; -import org.jetbrains.annotations.NotNull; -import org.junit.jupiter.api.Test; - -import java.util.function.Function; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; -import static org.eclipse.edc.spi.response.ResponseStatus.ERROR_RETRY; -import static org.eclipse.edc.spi.response.ResponseStatus.FATAL_ERROR; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoInteractions; - -class DspHttpDispatcherDelegateTest { - - private final JsonLdRemoteMessageSerializer serializer = mock(); - private final Function parser = mock(); - private final TestDspHttpDispatcherDelegate delegate = new TestDspHttpDispatcherDelegate(); - - @Test - void handleResponse_shouldShouldReturnSuccess_whenResponseIsSuccessful() { - var response = dummyResponse(200); - - var result = delegate.handleResponse().apply(response); - - assertThat(result).isSucceeded(); - verify(parser).apply(response); - } - - @Test - void handleResponse_shouldReturnFatalError_whenResponseIsClientError() { - var responseBody = "{\"any\": \"value\"}"; - var response = dummyResponseBuilder(400).body(ResponseBody.create(responseBody, MediaType.get("application/json"))).build(); - - var result = delegate.handleResponse().apply(response); - - assertThat(result).isFailed().satisfies(failure -> { - assertThat(failure.status()).isEqualTo(FATAL_ERROR); - assertThat(failure.getMessages()).containsOnly(responseBody); - }); - verifyNoInteractions(parser); - } - - @Test - void handleResponse_shouldReturnFatalError_whenResponseIsClientErrorAndBodyIsNull() { - var response = dummyResponseBuilder(400).body(null).build(); - - var result = delegate.handleResponse().apply(response); - - assertThat(result).isFailed().satisfies(failure -> { - assertThat(failure.status()).isEqualTo(FATAL_ERROR); - assertThat(failure.getMessages()).allMatch(it -> it.contains("is null")); - }); - verifyNoInteractions(parser); - } - - @Test - void handleResponse_shouldReturnRetryError_whenResponseIsServerError() { - var response = dummyResponse(500); - - var result = delegate.handleResponse().apply(response); - - assertThat(result).isFailed().extracting(ResponseFailure::status).isEqualTo(ERROR_RETRY); - verifyNoInteractions(parser); - } - - private class TestDspHttpDispatcherDelegate extends DspHttpDispatcherDelegate { - - TestDspHttpDispatcherDelegate() { - super(); - } - - @Override - public Function parseResponse() { - return parser; - } - } - - private static Response dummyResponse(int code) { - return dummyResponseBuilder(code) - .build(); - } - - @NotNull - private static Response.Builder dummyResponseBuilder(int code) { - return new Response.Builder() - .code(code) - .message("any") - .body(ResponseBody.create("", MediaType.get("application/json"))) - .protocol(Protocol.HTTP_1_1) - .request(new Request.Builder().url("http://any").build()); - } - -} diff --git a/data-protocols/dsp/dsp-http-spi/src/testFixtures/java/org/eclipse/edc/protocol/dsp/spi/testfixtures/dispatcher/DspHttpDispatcherDelegateTestBase.java b/data-protocols/dsp/dsp-http-spi/src/testFixtures/java/org/eclipse/edc/protocol/dsp/spi/testfixtures/dispatcher/DspHttpDispatcherDelegateTestBase.java deleted file mode 100644 index ddd4123cb2e..00000000000 --- a/data-protocols/dsp/dsp-http-spi/src/testFixtures/java/org/eclipse/edc/protocol/dsp/spi/testfixtures/dispatcher/DspHttpDispatcherDelegateTestBase.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.spi.testfixtures.dispatcher; - -import com.fasterxml.jackson.databind.ObjectMapper; -import jakarta.json.JsonObject; -import okhttp3.MediaType; -import okhttp3.Protocol; -import okhttp3.Request; -import okhttp3.Response; -import okhttp3.ResponseBody; -import okio.Buffer; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.protocol.dsp.spi.serialization.JsonLdRemoteMessageSerializer; -import org.eclipse.edc.spi.EdcException; -import org.eclipse.edc.spi.response.StatusResult; -import org.eclipse.edc.spi.types.domain.message.RemoteMessage; -import org.eclipse.edc.transform.spi.TypeTransformerRegistry; -import org.jetbrains.annotations.NotNull; - -import java.io.IOException; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -/** - * Test base for implementations of {@link DspHttpDispatcherDelegate}. Provides methods that test - * common behaviour of delegate methods. Each sub-class of this test base should choose the - * respective methods for the delegate implementation it's testing. - */ -public abstract class DspHttpDispatcherDelegateTestBase { - - protected JsonLdRemoteMessageSerializer serializer = mock(JsonLdRemoteMessageSerializer.class); - protected ObjectMapper mapper = mock(ObjectMapper.class); - protected TypeTransformerRegistry registry = mock(TypeTransformerRegistry.class); - - /** - * Returns the delegate to test. - * - * @return the delegate - */ - protected abstract DspHttpDispatcherDelegate delegate(); - - /** - * Checks that a delegate throws an exception when the response body is missing. Only relevant - * for delegates that process the response body. - */ - protected void testParseResponse_shouldThrowException_whenResponseBodyNull() { - var response = dummyResponseBuilder(200).body(null).build(); - - assertThatThrownBy(() -> delegate().handleResponse().apply(response)).isInstanceOf(EdcException.class); - } - - /** - * Checks that a delegate throws an exception when the response body cannot be read. Only - * relevant for delegates that process the response body. - */ - protected void testParseResponse_shouldThrowException_whenReadingResponseBodyFails() throws IOException { - var responseBody = mock(ResponseBody.class); - var response = dummyResponseBuilder(200).body(responseBody).build(); - - when(responseBody.bytes()).thenReturn("test".getBytes()); - when(mapper.readValue(any(byte[].class), eq(JsonObject.class))).thenThrow(IOException.class); - - assertThatThrownBy(() -> delegate().handleResponse().apply(response)).isInstanceOf(EdcException.class); - } - - /** - * Checks that a delegate returns a null function for parsing the response body. Only relevant - * for delegates that do not process the response body. - */ - protected void testParseResponse_shouldReturnNullFunction_whenResponseBodyNotProcessed() { - var response = dummyResponseBuilder(200).body(null).build(); - - assertThat(delegate().handleResponse().apply(response)).extracting(StatusResult::getContent).isNull(); - } - - protected String readRequestBody(Request request) throws IOException { - var buffer = new Buffer(); - request.body().writeTo(buffer); - return buffer.readUtf8(); - } - - @NotNull - protected Response.Builder dummyResponseBuilder(int code) { - return new Response.Builder() - .code(code) - .message("any") - .body(ResponseBody.create("", MediaType.get("application/json"))) - .protocol(Protocol.HTTP_1_1) - .request(new Request.Builder().url("http://any").build()); - } -} diff --git a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/DspNegotiationHttpDispatcherExtension.java b/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/DspNegotiationHttpDispatcherExtension.java index 2f008c52da3..56088f10e7d 100644 --- a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/DspNegotiationHttpDispatcherExtension.java +++ b/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/DspNegotiationHttpDispatcherExtension.java @@ -20,21 +20,13 @@ import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationTerminationMessage; import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractOfferMessage; import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequestMessage; -import org.eclipse.edc.jsonld.spi.JsonLd; import org.eclipse.edc.protocol.dsp.dispatcher.PostDspHttpRequestFactory; -import org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate.ContractAgreementMessageHttpDelegate; -import org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate.ContractAgreementVerificationMessageHttpDelegate; -import org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate.ContractNegotiationEventMessageHttpDelegate; -import org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate.ContractNegotiationTerminationMessageHttpDelegate; -import org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate.ContractOfferMessageHttpDelegate; -import org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate.ContractRequestMessageHttpDelegate; import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpRemoteMessageDispatcher; import org.eclipse.edc.protocol.dsp.spi.serialization.JsonLdRemoteMessageSerializer; import org.eclipse.edc.runtime.metamodel.annotation.Extension; import org.eclipse.edc.runtime.metamodel.annotation.Inject; import org.eclipse.edc.spi.system.ServiceExtension; import org.eclipse.edc.spi.system.ServiceExtensionContext; -import org.eclipse.edc.spi.types.TypeManager; import static org.eclipse.edc.protocol.dsp.negotiation.dispatcher.NegotiationApiPaths.AGREEMENT; import static org.eclipse.edc.protocol.dsp.negotiation.dispatcher.NegotiationApiPaths.BASE_PATH; @@ -44,7 +36,7 @@ import static org.eclipse.edc.protocol.dsp.negotiation.dispatcher.NegotiationApiPaths.INITIAL_CONTRACT_REQUEST; import static org.eclipse.edc.protocol.dsp.negotiation.dispatcher.NegotiationApiPaths.TERMINATION; import static org.eclipse.edc.protocol.dsp.negotiation.dispatcher.NegotiationApiPaths.VERIFICATION; -import static org.eclipse.edc.spi.CoreConstants.JSON_LD; +import static org.eclipse.edc.protocol.dsp.spi.dispatcher.response.DspHttpResponseBodyExtractor.NOOP; @Extension(value = DspNegotiationHttpDispatcherExtension.NAME) public class DspNegotiationHttpDispatcherExtension implements ServiceExtension { @@ -54,11 +46,7 @@ public class DspNegotiationHttpDispatcherExtension implements ServiceExtension { @Inject private DspHttpRemoteMessageDispatcher messageDispatcher; @Inject - private TypeManager typeManager; - @Inject private JsonLdRemoteMessageSerializer remoteMessageSerializer; - @Inject - private JsonLd jsonLdService; @Override public String name() { @@ -67,25 +55,26 @@ public String name() { @Override public void initialize(ServiceExtensionContext context) { + messageDispatcher.registerMessage( ContractAgreementMessage.class, new PostDspHttpRequestFactory<>(remoteMessageSerializer, m -> BASE_PATH + m.getProcessId() + AGREEMENT), - new ContractAgreementMessageHttpDelegate() + NOOP ); messageDispatcher.registerMessage( ContractAgreementVerificationMessage.class, new PostDspHttpRequestFactory<>(remoteMessageSerializer, m -> BASE_PATH + m.getProcessId() + AGREEMENT + VERIFICATION), - new ContractAgreementVerificationMessageHttpDelegate() + NOOP ); messageDispatcher.registerMessage( ContractNegotiationEventMessage.class, new PostDspHttpRequestFactory<>(remoteMessageSerializer, m -> BASE_PATH + m.getProcessId() + EVENT), - new ContractNegotiationEventMessageHttpDelegate() + NOOP ); messageDispatcher.registerMessage( ContractNegotiationTerminationMessage.class, new PostDspHttpRequestFactory<>(remoteMessageSerializer, m -> BASE_PATH + m.getProcessId() + TERMINATION), - new ContractNegotiationTerminationMessageHttpDelegate() + NOOP ); messageDispatcher.registerMessage( ContractRequestMessage.class, @@ -96,12 +85,12 @@ public void initialize(ServiceExtensionContext context) { return BASE_PATH + m.getProcessId() + CONTRACT_REQUEST; } }), - new ContractRequestMessageHttpDelegate(typeManager.getMapper(JSON_LD), jsonLdService) + NOOP ); messageDispatcher.registerMessage( ContractOfferMessage.class, new PostDspHttpRequestFactory<>(remoteMessageSerializer, m -> BASE_PATH + m.getProcessId() + CONTRACT_OFFER), - new ContractOfferMessageHttpDelegate() + NOOP ); } } diff --git a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementMessageHttpDelegate.java b/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementMessageHttpDelegate.java deleted file mode 100644 index 636005537d1..00000000000 --- a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementMessageHttpDelegate.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate; - -import okhttp3.Response; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; - -import java.util.function.Function; - -/** - * Delegate for dispatching contract agreement message as defined in the dataspace protocol specification. - */ -public class ContractAgreementMessageHttpDelegate extends DspHttpDispatcherDelegate { - - public ContractAgreementMessageHttpDelegate() { - super(); - } - - /** - * Parses the response to an agreement message. The JSON-LD structure from the response body is - * expanded and returned. - * - * @return a function that contains the response body or null. - */ - @Override - public Function parseResponse() { - return response -> null; - } - -} diff --git a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementVerificationMessageHttpDelegate.java b/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementVerificationMessageHttpDelegate.java deleted file mode 100644 index 653823f4eee..00000000000 --- a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementVerificationMessageHttpDelegate.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate; - -import okhttp3.Response; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; - -import java.util.function.Function; - -/** - * Delegate for dispatching contract agreement verification message as defined in the dataspace protocol specification. - */ -public class ContractAgreementVerificationMessageHttpDelegate extends DspHttpDispatcherDelegate { - - public ContractAgreementVerificationMessageHttpDelegate() { - super(); - } - - /** - * Parses the response to an agreement verification message. The JSON-LD structure from the response - * body is expanded and returned. - * - * @return a function that contains the response body or null. - */ - @Override - public Function parseResponse() { - return response -> null; - } - -} diff --git a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationEventMessageHttpDelegate.java b/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationEventMessageHttpDelegate.java deleted file mode 100644 index 21d50118648..00000000000 --- a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationEventMessageHttpDelegate.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate; - -import okhttp3.Response; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; - -import java.util.function.Function; - -/** - * Delegate for dispatching contract negotiation event message as defined in the dataspace protocol specification. - */ -public class ContractNegotiationEventMessageHttpDelegate extends DspHttpDispatcherDelegate { - - public ContractNegotiationEventMessageHttpDelegate() { - super(); - } - - /** - * Parses the response to a contract negotiation event message. The JSON-LD structure from the response - * body is expanded and returned. - * - * @return a function that contains the response body or null. - */ - @Override - public Function parseResponse() { - return response -> null; - } - -} diff --git a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationTerminationMessageHttpDelegate.java b/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationTerminationMessageHttpDelegate.java deleted file mode 100644 index ffa0bf8e273..00000000000 --- a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationTerminationMessageHttpDelegate.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate; - -import okhttp3.Response; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; - -import java.util.function.Function; - -/** - * Delegate for dispatching contract negotiation termination message as defined in the dataspace protocol specification. - */ -public class ContractNegotiationTerminationMessageHttpDelegate extends DspHttpDispatcherDelegate { - - public ContractNegotiationTerminationMessageHttpDelegate() { - super(); - } - - /** - * Parses the response to a contract negotiation event message. The JSON-LD structure from the response - * body is expanded and returned. - * - * @return a function that contains the response body or null. - */ - @Override - public Function parseResponse() { - return response -> null; - } - -} diff --git a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractOfferMessageHttpDelegate.java b/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractOfferMessageHttpDelegate.java deleted file mode 100644 index 0e54e3e2dca..00000000000 --- a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractOfferMessageHttpDelegate.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate; - -import okhttp3.Response; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; - -import java.util.function.Function; - -/** - * Delegate for dispatching contract offer message as defined in the dataspace protocol specification. - */ -public class ContractOfferMessageHttpDelegate extends DspHttpDispatcherDelegate { - - public ContractOfferMessageHttpDelegate() { - super(); - } - - @Override - protected Function parseResponse() { - return response -> null; - } -} diff --git a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractRequestMessageHttpDelegate.java b/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractRequestMessageHttpDelegate.java deleted file mode 100644 index 2ee012677df..00000000000 --- a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractRequestMessageHttpDelegate.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate; - -import com.fasterxml.jackson.databind.ObjectMapper; -import jakarta.json.JsonObject; -import okhttp3.Response; -import org.eclipse.edc.jsonld.spi.JsonLd; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.spi.EdcException; - -import java.io.IOException; -import java.util.function.Function; - -/** - * Delegate for dispatching contract request message as defined in the dataspace protocol specification. - */ -public class ContractRequestMessageHttpDelegate extends DspHttpDispatcherDelegate { - - private final ObjectMapper mapper; - private final JsonLd jsonLdService; - - - public ContractRequestMessageHttpDelegate(ObjectMapper mapper, JsonLd jsonLdService) { - super(); - this.mapper = mapper; - this.jsonLdService = jsonLdService; - } - - @Override - public Function parseResponse() { - return response -> { - try { - //noinspection DataFlowIssue - var jsonObject = mapper.readValue(response.body().bytes(), JsonObject.class); - return jsonLdService.expand(jsonObject).map(jo -> jo).orElseThrow(failure -> new EdcException(failure.getFailureDetail())); - } catch (NullPointerException e) { - throw new EdcException("Failed to read response body, as body was null."); - } catch (IndexOutOfBoundsException e) { - throw new EdcException("Failed to expand JSON-LD in response body.", e); - } catch (IOException e) { - throw new EdcException("Failed to read response body.", e); - } - }; - } - -} diff --git a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementMessageHttpDelegateTest.java b/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementMessageHttpDelegateTest.java deleted file mode 100644 index f1c89153ab6..00000000000 --- a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementMessageHttpDelegateTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate; - -import org.eclipse.edc.connector.contract.spi.types.agreement.ContractAgreementMessage; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.protocol.dsp.spi.testfixtures.dispatcher.DspHttpDispatcherDelegateTestBase; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class ContractAgreementMessageHttpDelegateTest extends DspHttpDispatcherDelegateTestBase { - - private ContractAgreementMessageHttpDelegate delegate; - - @BeforeEach - void setUp() { - delegate = new ContractAgreementMessageHttpDelegate(); - } - - @Test - void parseResponse_returnNull() { - testParseResponse_shouldReturnNullFunction_whenResponseBodyNotProcessed(); - } - - @Override - protected DspHttpDispatcherDelegate delegate() { - return delegate; - } -} diff --git a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementVerificationMessageHttpDelegateTest.java b/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementVerificationMessageHttpDelegateTest.java deleted file mode 100644 index 8541f035af9..00000000000 --- a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractAgreementVerificationMessageHttpDelegateTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate; - -import org.eclipse.edc.connector.contract.spi.types.agreement.ContractAgreementVerificationMessage; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.protocol.dsp.spi.testfixtures.dispatcher.DspHttpDispatcherDelegateTestBase; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class ContractAgreementVerificationMessageHttpDelegateTest extends DspHttpDispatcherDelegateTestBase { - - private ContractAgreementVerificationMessageHttpDelegate delegate; - - @BeforeEach - void setUp() { - delegate = new ContractAgreementVerificationMessageHttpDelegate(); - } - - @Test - void parseResponse_returnNull() { - testParseResponse_shouldReturnNullFunction_whenResponseBodyNotProcessed(); - } - - @Override - protected DspHttpDispatcherDelegate delegate() { - return delegate; - } -} diff --git a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationEventMessageHttpDelegateTest.java b/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationEventMessageHttpDelegateTest.java deleted file mode 100644 index 214fbc0d2fc..00000000000 --- a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationEventMessageHttpDelegateTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate; - -import org.eclipse.edc.connector.contract.spi.types.agreement.ContractNegotiationEventMessage; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.protocol.dsp.spi.testfixtures.dispatcher.DspHttpDispatcherDelegateTestBase; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class ContractNegotiationEventMessageHttpDelegateTest extends DspHttpDispatcherDelegateTestBase { - - private ContractNegotiationEventMessageHttpDelegate delegate; - - @BeforeEach - void setUp() { - delegate = new ContractNegotiationEventMessageHttpDelegate(); - } - - @Test - void parseResponse_returnNull() { - testParseResponse_shouldReturnNullFunction_whenResponseBodyNotProcessed(); - } - - @Override - protected DspHttpDispatcherDelegate delegate() { - return delegate; - } -} diff --git a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationTerminationMessageHttpDelegateTest.java b/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationTerminationMessageHttpDelegateTest.java deleted file mode 100644 index 8217d6ed87c..00000000000 --- a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractNegotiationTerminationMessageHttpDelegateTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate; - -import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiationTerminationMessage; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.protocol.dsp.spi.testfixtures.dispatcher.DspHttpDispatcherDelegateTestBase; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class ContractNegotiationTerminationMessageHttpDelegateTest extends DspHttpDispatcherDelegateTestBase { - - private ContractNegotiationTerminationMessageHttpDelegate delegate; - - @BeforeEach - void setUp() { - delegate = new ContractNegotiationTerminationMessageHttpDelegate(); - } - - @Test - void parseResponse_returnNull() { - testParseResponse_shouldReturnNullFunction_whenResponseBodyNotProcessed(); - } - - @Override - protected DspHttpDispatcherDelegate delegate() { - return delegate; - } -} diff --git a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractOfferMessageHttpDelegateTest.java b/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractOfferMessageHttpDelegateTest.java deleted file mode 100644 index a473982dc68..00000000000 --- a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractOfferMessageHttpDelegateTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate; - -import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractOfferMessage; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.protocol.dsp.spi.testfixtures.dispatcher.DspHttpDispatcherDelegateTestBase; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class ContractOfferMessageHttpDelegateTest extends DspHttpDispatcherDelegateTestBase { - - private ContractOfferMessageHttpDelegate delegate; - - @BeforeEach - void setUp() { - delegate = new ContractOfferMessageHttpDelegate(); - } - - @Test - void parseResponse_shouldReturnNullFunction() { - testParseResponse_shouldReturnNullFunction_whenResponseBodyNotProcessed(); - } - - @Override - protected DspHttpDispatcherDelegate delegate() { - return delegate; - } - -} diff --git a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractRequestMessageHttpDelegateTest.java b/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractRequestMessageHttpDelegateTest.java deleted file mode 100644 index 03dbcb99e3f..00000000000 --- a/data-protocols/dsp/dsp-negotiation/dsp-negotiation-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/negotiation/dispatcher/delegate/ContractRequestMessageHttpDelegateTest.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.negotiation.dispatcher.delegate; - -import jakarta.json.Json; -import jakarta.json.JsonObject; -import okhttp3.Response; -import okhttp3.ResponseBody; -import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequestMessage; -import org.eclipse.edc.jsonld.TitaniumJsonLd; -import org.eclipse.edc.jsonld.spi.JsonLdKeywords; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.protocol.dsp.spi.testfixtures.dispatcher.DspHttpDispatcherDelegateTestBase; -import org.eclipse.edc.spi.monitor.Monitor; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.io.IOException; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.edc.protocol.dsp.type.DspNegotiationPropertyAndTypeNames.DSPACE_TYPE_CONTRACT_NEGOTIATION; -import static org.eclipse.edc.protocol.dsp.type.DspNegotiationPropertyAndTypeNames.DSPACE_VALUE_NEGOTIATION_STATE_REQUESTED; -import static org.eclipse.edc.protocol.dsp.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_PROCESS_ID; -import static org.eclipse.edc.protocol.dsp.type.DspPropertyAndTypeNames.DSPACE_PROPERTY_STATE; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -class ContractRequestMessageHttpDelegateTest extends DspHttpDispatcherDelegateTestBase { - private static final String PROCESS_ID = "processId"; - - private ContractRequestMessageHttpDelegate delegate; - private TitaniumJsonLd jsonLdService; - - @BeforeEach - void setUp() { - jsonLdService = new TitaniumJsonLd(mock(Monitor.class)); - delegate = new ContractRequestMessageHttpDelegate(mapper, jsonLdService); - } - - @Test - void parseResponse_returnNegotiation() throws IOException { - var response = mock(Response.class); - var responseBody = mock(ResponseBody.class); - var bytes = "test".getBytes(); - - var expanded = jsonLdService.expand(negotiation()).getContent(); - - when(response.body()).thenReturn(responseBody); - when(responseBody.bytes()).thenReturn(bytes); - when(mapper.readValue(bytes, JsonObject.class)).thenReturn(negotiation()); - - var result = delegate.parseResponse().apply(response); - - assertThat(result).isEqualTo(expanded); - verify(mapper, times(1)).readValue(bytes, JsonObject.class); - } - - @Test - void parseResponse_responseBodyNull_throwException() { - testParseResponse_shouldThrowException_whenResponseBodyNull(); - } - - @Test - void parseResponse_readingResponseBodyFails_throwException() throws IOException { - testParseResponse_shouldThrowException_whenReadingResponseBodyFails(); - } - - @Override - protected DspHttpDispatcherDelegate delegate() { - return delegate; - } - - private JsonObject negotiation() { - var builder = Json.createObjectBuilder(); - builder.add(JsonLdKeywords.ID, "id1"); - builder.add(JsonLdKeywords.TYPE, DSPACE_TYPE_CONTRACT_NEGOTIATION); - - builder.add(DSPACE_PROPERTY_PROCESS_ID, PROCESS_ID); - builder.add(DSPACE_PROPERTY_STATE, DSPACE_VALUE_NEGOTIATION_STATE_REQUESTED); - - return builder.build(); - } -} diff --git a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/DspTransferProcessDispatcherExtension.java b/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/DspTransferProcessDispatcherExtension.java index 88e7f74f9d3..e2e3d9bc8f9 100644 --- a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/DspTransferProcessDispatcherExtension.java +++ b/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/DspTransferProcessDispatcherExtension.java @@ -22,15 +22,12 @@ import org.eclipse.edc.protocol.dsp.dispatcher.PostDspHttpRequestFactory; import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpRemoteMessageDispatcher; import org.eclipse.edc.protocol.dsp.spi.serialization.JsonLdRemoteMessageSerializer; -import org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.delegate.TransferCompletionDelegate; -import org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.delegate.TransferRequestDelegate; -import org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.delegate.TransferStartDelegate; -import org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.delegate.TransferTerminationDelegate; import org.eclipse.edc.runtime.metamodel.annotation.Extension; import org.eclipse.edc.runtime.metamodel.annotation.Inject; import org.eclipse.edc.spi.system.ServiceExtension; import org.eclipse.edc.spi.system.ServiceExtensionContext; +import static org.eclipse.edc.protocol.dsp.spi.dispatcher.response.DspHttpResponseBodyExtractor.NOOP; import static org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.TransferProcessApiPaths.BASE_PATH; import static org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.TransferProcessApiPaths.TRANSFER_COMPLETION; import static org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.TransferProcessApiPaths.TRANSFER_INITIAL_REQUEST; @@ -61,22 +58,22 @@ public void initialize(ServiceExtensionContext context) { messageDispatcher.registerMessage( TransferRequestMessage.class, new PostDspHttpRequestFactory<>(remoteMessageSerializer, m -> BASE_PATH + TRANSFER_INITIAL_REQUEST), - new TransferRequestDelegate() + NOOP ); messageDispatcher.registerMessage( TransferCompletionMessage.class, new PostDspHttpRequestFactory<>(remoteMessageSerializer, m -> BASE_PATH + m.getProcessId() + TRANSFER_COMPLETION), - new TransferCompletionDelegate(remoteMessageSerializer) + NOOP ); messageDispatcher.registerMessage( TransferStartMessage.class, new PostDspHttpRequestFactory<>(remoteMessageSerializer, m -> BASE_PATH + m.getProcessId() + TRANSFER_START), - new TransferStartDelegate() + NOOP ); messageDispatcher.registerMessage( TransferTerminationMessage.class, new PostDspHttpRequestFactory<>(remoteMessageSerializer, m -> BASE_PATH + m.getProcessId() + TRANSFER_TERMINATION), - new TransferTerminationDelegate() + NOOP ); } } diff --git a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferCompletionDelegate.java b/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferCompletionDelegate.java deleted file mode 100644 index 95fa9e479a6..00000000000 --- a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferCompletionDelegate.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.delegate; - -import jakarta.json.JsonObject; -import okhttp3.Response; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.protocol.dsp.spi.serialization.JsonLdRemoteMessageSerializer; - -import java.util.function.Function; - -public class TransferCompletionDelegate extends DspHttpDispatcherDelegate { - - public TransferCompletionDelegate(JsonLdRemoteMessageSerializer serializer) { - super(); - } - - @Override - public Function parseResponse() { - return response -> null; - } - -} diff --git a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferRequestDelegate.java b/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferRequestDelegate.java deleted file mode 100644 index 8fd6df7e23a..00000000000 --- a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferRequestDelegate.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.delegate; - -import jakarta.json.JsonObject; -import okhttp3.Response; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; - -import java.util.function.Function; - -public class TransferRequestDelegate extends DspHttpDispatcherDelegate { - - public TransferRequestDelegate() { - super(); - } - - @Override - public Function parseResponse() { - return response -> null; - } - -} diff --git a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferStartDelegate.java b/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferStartDelegate.java deleted file mode 100644 index f03a2039a41..00000000000 --- a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferStartDelegate.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.delegate; - -import jakarta.json.JsonObject; -import okhttp3.Response; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; - -import java.util.function.Function; - -public class TransferStartDelegate extends DspHttpDispatcherDelegate { - - public TransferStartDelegate() { - super(); - } - - @Override - public Function parseResponse() { - return response -> null; - } - -} diff --git a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferTerminationDelegate.java b/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferTerminationDelegate.java deleted file mode 100644 index 8caa1b5af1f..00000000000 --- a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/main/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferTerminationDelegate.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.delegate; - -import jakarta.json.JsonObject; -import okhttp3.Response; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; - -import java.util.function.Function; - -public class TransferTerminationDelegate extends DspHttpDispatcherDelegate { - - public TransferTerminationDelegate() { - super(); - } - - @Override - public Function parseResponse() { - return response -> null; - } - -} diff --git a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferCompletionDelegateTest.java b/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferCompletionDelegateTest.java deleted file mode 100644 index 826281f02ff..00000000000 --- a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferCompletionDelegateTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.delegate; - -import org.eclipse.edc.connector.transfer.spi.types.protocol.TransferCompletionMessage; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.protocol.dsp.spi.testfixtures.dispatcher.DspHttpDispatcherDelegateTestBase; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class TransferCompletionDelegateTest extends DspHttpDispatcherDelegateTestBase { - - private TransferCompletionDelegate delegate; - - @BeforeEach - void setUp() { - delegate = new TransferCompletionDelegate(serializer); - } - - @Test - void parseResponse_returnNull() { - testParseResponse_shouldReturnNullFunction_whenResponseBodyNotProcessed(); - } - - @Override - protected DspHttpDispatcherDelegate delegate() { - return delegate; - } -} diff --git a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferRequestDelegateTest.java b/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferRequestDelegateTest.java deleted file mode 100644 index 95c7141e9d7..00000000000 --- a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferRequestDelegateTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.delegate; - -import org.eclipse.edc.connector.transfer.spi.types.protocol.TransferRequestMessage; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.protocol.dsp.spi.testfixtures.dispatcher.DspHttpDispatcherDelegateTestBase; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class TransferRequestDelegateTest extends DspHttpDispatcherDelegateTestBase { - - private TransferRequestDelegate delegate; - - @BeforeEach - void setUp() { - delegate = new TransferRequestDelegate(); - } - - @Test - void parseResponse_returnNull() { - testParseResponse_shouldReturnNullFunction_whenResponseBodyNotProcessed(); - } - - @Override - protected DspHttpDispatcherDelegate delegate() { - return delegate; - } - -} diff --git a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferStartDelegateTest.java b/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferStartDelegateTest.java deleted file mode 100644 index 5d49be8e3bb..00000000000 --- a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferStartDelegateTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.delegate; - -import org.eclipse.edc.connector.transfer.spi.types.protocol.TransferStartMessage; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.protocol.dsp.spi.testfixtures.dispatcher.DspHttpDispatcherDelegateTestBase; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class TransferStartDelegateTest extends DspHttpDispatcherDelegateTestBase { - - private TransferStartDelegate delegate; - - @BeforeEach - void setUp() { - delegate = new TransferStartDelegate(); - } - - @Test - void parseResponse_returnNull() { - testParseResponse_shouldReturnNullFunction_whenResponseBodyNotProcessed(); - } - - @Override - protected DspHttpDispatcherDelegate delegate() { - return delegate; - } -} diff --git a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferTerminationDelegateTest.java b/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferTerminationDelegateTest.java deleted file mode 100644 index b4ab667aebb..00000000000 --- a/data-protocols/dsp/dsp-transfer-process/dsp-transfer-process-http-dispatcher/src/test/java/org/eclipse/edc/protocol/dsp/transferprocess/dispatcher/delegate/TransferTerminationDelegateTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0 - * - * SPDX-License-Identifier: Apache-2.0 - * - * Contributors: - * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.dsp.transferprocess.dispatcher.delegate; - -import org.eclipse.edc.connector.transfer.spi.types.protocol.TransferTerminationMessage; -import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate; -import org.eclipse.edc.protocol.dsp.spi.testfixtures.dispatcher.DspHttpDispatcherDelegateTestBase; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class TransferTerminationDelegateTest extends DspHttpDispatcherDelegateTestBase { - - private TransferTerminationDelegate delegate; - - @BeforeEach - void setUp() { - delegate = new TransferTerminationDelegate(); - } - - @Test - void parseResponse_returnNull() { - testParseResponse_shouldReturnNullFunction_whenResponseBodyNotProcessed(); - } - - @Override - protected DspHttpDispatcherDelegate delegate() { - return delegate; - } -} diff --git a/extensions/control-plane/callback/callback-http-dispatcher/src/main/java/org/eclipse/edc/connector/callback/dispatcher/http/GenericHttpRemoteDispatcherImpl.java b/extensions/control-plane/callback/callback-http-dispatcher/src/main/java/org/eclipse/edc/connector/callback/dispatcher/http/GenericHttpRemoteDispatcherImpl.java index 82110e6f6ab..7f73acf0a67 100644 --- a/extensions/control-plane/callback/callback-http-dispatcher/src/main/java/org/eclipse/edc/connector/callback/dispatcher/http/GenericHttpRemoteDispatcherImpl.java +++ b/extensions/control-plane/callback/callback-http-dispatcher/src/main/java/org/eclipse/edc/connector/callback/dispatcher/http/GenericHttpRemoteDispatcherImpl.java @@ -24,6 +24,7 @@ import java.util.concurrent.CompletableFuture; import static java.lang.String.format; +import static java.util.Collections.emptyList; /** * Default implementation for {@link GenericHttpRemoteDispatcher} @@ -50,7 +51,13 @@ public CompletableFuture> dispatch( throw new EdcException(format("No %s message dispatcher found for message type %s", protocol(), message.getClass())); } var request = delegate.buildRequest(message); - return httpClient.executeAsync(request, delegate.parseResponse()).thenApply(StatusResult::success); + return httpClient.executeAsync(request, emptyList()) + .thenApply(response -> { + try (response) { + return delegate.parseResponse().apply(response); + } + }) + .thenApply(StatusResult::success); } @Override diff --git a/spi/common/http-spi/src/main/java/org/eclipse/edc/spi/http/EdcHttpClient.java b/spi/common/http-spi/src/main/java/org/eclipse/edc/spi/http/EdcHttpClient.java index 7d9803749a8..b3e5ec183a2 100644 --- a/spi/common/http-spi/src/main/java/org/eclipse/edc/spi/http/EdcHttpClient.java +++ b/spi/common/http-spi/src/main/java/org/eclipse/edc/spi/http/EdcHttpClient.java @@ -74,10 +74,21 @@ public interface EdcHttpClient { * @param request the {@link Request}. * @param mappingFunction the function that will be applied to the {@link Response}. * @return a {@link CompletableFuture} containing the result value. - * @param the result value. + * @deprecated please use {{@link #executeAsync(Request, List)}} passing an empty list for fallbacks. */ + @Deprecated(since = "0.4.1") CompletableFuture executeAsync(Request request, Function mappingFunction); + /** + * Executes the specified request asynchronously and returns the response. + * Accepts a list of {@link FallbackFactories} that could apply retry in particular occasions. + * + * @param request the {@link Request}. + * @param fallbacks a list of fallbacks to be applied. + * @return a {@link CompletableFuture} containing the {@link Response} instance. + */ + CompletableFuture executeAsync(Request request, List fallbacks); + /** * Executes the specified request asynchronously, maps the response with the mappingFunction. * Accepts a list of {@link FallbackFactories} that could apply retry in particular occasions. @@ -86,8 +97,9 @@ public interface EdcHttpClient { * @param fallbacks a list of fallbacks to be applied. * @param mappingFunction the function that will be applied to the {@link Response}. * @return a {@link CompletableFuture} containing the result value. - * @param the result value. + * @deprecated please use {{@link #executeAsync(Request, List)}}. */ + @Deprecated(since = "0.4.1") CompletableFuture executeAsync(Request request, List fallbacks, Function mappingFunction); /**