diff --git a/data-protocols/ids/ids-core/src/main/java/org/eclipse/edc/protocol/ids/transform/IdsTransformerRegistryImpl.java b/data-protocols/ids/ids-core/src/main/java/org/eclipse/edc/protocol/ids/transform/IdsTransformerRegistryImpl.java index 5847cf7b96e..d13f008f2bb 100644 --- a/data-protocols/ids/ids-core/src/main/java/org/eclipse/edc/protocol/ids/transform/IdsTransformerRegistryImpl.java +++ b/data-protocols/ids/ids-core/src/main/java/org/eclipse/edc/protocol/ids/transform/IdsTransformerRegistryImpl.java @@ -16,104 +16,11 @@ import org.eclipse.edc.protocol.ids.spi.transform.IdsTransformerRegistry; import org.eclipse.edc.protocol.ids.spi.transform.IdsTypeTransformer; -import org.eclipse.edc.spi.EdcException; -import org.eclipse.edc.spi.result.Result; -import org.eclipse.edc.transform.spi.TransformerContext; -import org.eclipse.edc.transform.spi.TransformerContextImpl; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; +import org.eclipse.edc.transform.spi.TypeTransformerRegistryImpl; /** - * Implements a {@link IdsTransformerRegistry} that recursively dispatches to transformers for type conversion. + * Marker class to allow for a type-safe registry that only holds implementations of the {@link IdsTypeTransformer} interface. */ -public class IdsTransformerRegistryImpl implements IdsTransformerRegistry { - private final Map> transformers = new HashMap<>(); - - @Override - public void register(IdsTypeTransformer transformer) { - Objects.requireNonNull(transformer); - transformers.put(new TransformKey(transformer.getInputType(), transformer.getOutputType()), transformer); - } - - @Override - public Result transform(@NotNull INPUT object, @NotNull Class outputType) { - var context = new TransformerContextImpl(this); - var output = transform(object, outputType, context); - return context.hasProblems() ? Result.failure(context.getProblems()) : Result.success(output); - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - private @Nullable OUTPUT transform(INPUT object, Class outputType, TransformerContext context) { - Objects.requireNonNull(object); - - IdsTypeTransformer idsTypeTransformer = findEligibleTransformer(object, outputType); - if (idsTypeTransformer == null) { - throw new EdcException("Transformer not found for pair:" + new TransformKey(object.getClass(), outputType)); // this is a programming error - } - return outputType.cast(idsTypeTransformer.transform(object, context)); - } - - @SuppressWarnings({ "unchecked" }) - private IdsTypeTransformer findEligibleTransformer(INPUT object, Class outputType) { - IdsTypeTransformer idsTypeTransformer; - - Class inputClass = object.getClass(); - do { - idsTypeTransformer = (IdsTypeTransformer) transformers.get(new TransformKey(inputClass, outputType)); - if (idsTypeTransformer == null) { - for (Class anInterface : inputClass.getInterfaces()) { - idsTypeTransformer = (IdsTypeTransformer) transformers.get(new TransformKey(anInterface, outputType)); - if (idsTypeTransformer != null) { - break; - } - } - } - - inputClass = inputClass.getSuperclass(); - } while (inputClass != null && idsTypeTransformer == null); - - return idsTypeTransformer; - } - - private static class TransformKey { - private final Class input; - private final Class output; - - TransformKey(Class input, Class output) { - Objects.requireNonNull(input); - Objects.requireNonNull(output); - this.input = input; - this.output = output; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - TransformKey transformKey = (TransformKey) o; - return input.equals(transformKey.input) && output.equals(transformKey.output); - } - - @Override - public int hashCode() { - return Objects.hash(input, output); - } - - @Override - public String toString() { - return "TKey{" + - "input=" + input + - ", output=" + output + - '}'; - } - } +public class IdsTransformerRegistryImpl extends TypeTransformerRegistryImpl> implements IdsTransformerRegistry { } diff --git a/data-protocols/ids/ids-core/src/test/java/org/eclipse/edc/protocol/ids/transform/IdsTransformerRegistryImplTest.java b/data-protocols/ids/ids-core/src/test/java/org/eclipse/edc/protocol/ids/transform/IdsTransformerRegistryImplTest.java deleted file mode 100644 index e0b9dd3e766..00000000000 --- a/data-protocols/ids/ids-core/src/test/java/org/eclipse/edc/protocol/ids/transform/IdsTransformerRegistryImplTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (c) 2021 Microsoft Corporation - * - * 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: - * Microsoft Corporation - initial API and implementation - * - */ - -package org.eclipse.edc.protocol.ids.transform; - -import org.eclipse.edc.protocol.ids.spi.transform.IdsTypeTransformer; -import org.eclipse.edc.transform.spi.TransformerContext; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.isA; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -/** - * Verifies registry dispatching and problem reporting. - */ -class IdsTransformerRegistryImplTest { - private IdsTransformerRegistryImpl registry; - - @Test - void verifyDispatch() { - var fooBarTransformer = createMock(Foo.class, Bar.class); - when(fooBarTransformer.transform(isA(Foo.class), isA(TransformerContext.class))).thenReturn(new Bar()); - - var fooBazTransformer = createMock(Foo.class, Baz.class); - - registry.register(fooBarTransformer); - registry.register(fooBazTransformer); - - assertNotNull(registry.transform(new Foo(), Bar.class).getContent()); - verify(fooBarTransformer).transform(isA(Foo.class), isA(TransformerContext.class)); - } - - @Test - void verifyProblems() { - var fooBarTransformer = createMock(Foo.class, Bar.class); - when(fooBarTransformer.transform(isA(Foo.class), isA(TransformerContext.class))).thenAnswer(invocation -> { - TransformerContext context = invocation.getArgument(1); - context.reportProblem("problem"); - return null; - }); - - registry.register(fooBarTransformer); - - var result = registry.transform(new Foo(), Bar.class); - - assertTrue(result.failed()); - verify(fooBarTransformer).transform(isA(Foo.class), isA(TransformerContext.class)); - } - - @BeforeEach - void setUp() { - registry = new IdsTransformerRegistryImpl(); - } - - private IdsTypeTransformer createMock(Class input, Class output) { - IdsTypeTransformer transformer = mock(IdsTypeTransformer.class); - when(transformer.getInputType()).thenReturn(input); - when(transformer.getOutputType()).thenReturn(output); - return transformer; - } - - private static class Foo { - - } - - private static class Bar { - - } - - private static class Baz { - - } - -} diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetFromIdsArtifactTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetFromIdsArtifactTransformer.java index 36ce11c7462..6438d418dfd 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetFromIdsArtifactTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetFromIdsArtifactTransformer.java @@ -24,7 +24,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; import java.util.UUID; public class AssetFromIdsArtifactTransformer implements IdsTypeTransformer { @@ -40,12 +39,7 @@ public Class getOutputType() { } @Override - public @Nullable Asset transform(Artifact object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable Asset transform(@NotNull Artifact object, @NotNull TransformerContext context) { var result = IdsId.from(object.getId()); if (result.failed()) { context.reportProblem("id of incoming IDS artifact expected to be not null"); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetFromIdsRepresentationTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetFromIdsRepresentationTransformer.java index fc7cfb1fc16..edf09ede523 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetFromIdsRepresentationTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetFromIdsRepresentationTransformer.java @@ -25,7 +25,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; import java.util.UUID; /** @@ -46,12 +45,7 @@ public Class getOutputType() { } @Override - public @Nullable Asset transform(Representation object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable Asset transform(@NotNull Representation object, @NotNull TransformerContext context) { var result = IdsId.from(object.getId()); if (result.failed()) { context.reportProblem("id of incoming IDS representation expected to be not null"); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetFromIdsResourceTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetFromIdsResourceTransformer.java index 5e0c792aae1..6e1cbccbe42 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetFromIdsResourceTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetFromIdsResourceTransformer.java @@ -27,7 +27,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; import java.util.UUID; /** @@ -48,12 +47,7 @@ public Class getOutputType() { } @Override - public @Nullable Asset transform(Resource object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable Asset transform(@NotNull Resource object, @NotNull TransformerContext context) { var result = IdsId.from(object.getId()); if (result.failed()) { context.reportProblem("id of incoming IDS resource expected to be not null"); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetToIdsArtifactTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetToIdsArtifactTransformer.java index 8ef12d3829c..ba622cd3052 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetToIdsArtifactTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetToIdsArtifactTransformer.java @@ -26,7 +26,6 @@ import org.jetbrains.annotations.Nullable; import java.math.BigInteger; -import java.util.Objects; public class AssetToIdsArtifactTransformer implements IdsTypeTransformer { @@ -41,12 +40,7 @@ public Class getOutputType() { } @Override - public @Nullable Artifact transform(Asset object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable Artifact transform(@NotNull Asset object, @NotNull TransformerContext context) { var id = IdsId.Builder.newInstance().value(object.getId()).type(IdsType.ARTIFACT).build().toUri(); var builder = new ArtifactBuilder(id); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetToIdsRepresentationTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetToIdsRepresentationTransformer.java index 6ad5424992e..4007cf89944 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetToIdsRepresentationTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetToIdsRepresentationTransformer.java @@ -29,7 +29,6 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.Objects; public class AssetToIdsRepresentationTransformer implements IdsTypeTransformer { @@ -44,12 +43,7 @@ public Class getOutputType() { } @Override - public @Nullable Representation transform(Asset object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable Representation transform(@NotNull Asset object, @NotNull TransformerContext context) { var artifact = context.transform(object, Artifact.class); var id = IdsId.Builder.newInstance().value(object.getId()).type(IdsType.REPRESENTATION).build().toUri(); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetToIdsResourceTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetToIdsResourceTransformer.java index af406c1cb1e..857c57dc799 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetToIdsResourceTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/AssetToIdsResourceTransformer.java @@ -29,7 +29,6 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.Objects; public class AssetToIdsResourceTransformer implements IdsTypeTransformer { @@ -47,12 +46,7 @@ public Class getOutputType() { } @Override - public @Nullable Resource transform(Asset object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable Resource transform(@NotNull Asset object, @NotNull TransformerContext context) { var result = context.transform(object, Representation.class); var id = IdsId.Builder.newInstance().value(object.getId()).type(IdsType.RESOURCE).build().toUri(); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/OfferedAssetToIdsResourceTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/OfferedAssetToIdsResourceTransformer.java index 02e17b9cc9e..119dd183129 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/OfferedAssetToIdsResourceTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/asset/OfferedAssetToIdsResourceTransformer.java @@ -46,11 +46,7 @@ public Class getOutputType() { } @Override - public @Nullable Resource transform(OfferedAsset object, @NotNull TransformerContext context) { - if (object == null) { - return null; - } - + public @Nullable Resource transform(@NotNull OfferedAsset object, @NotNull TransformerContext context) { var asset = object.getAsset(); var id = IdsId.Builder.newInstance().value(asset.getId()).type(IdsType.RESOURCE).build().toUri(); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/CatalogFromIdsResourceCatalogTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/CatalogFromIdsResourceCatalogTransformer.java index f799761d466..f7d9310f8a6 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/CatalogFromIdsResourceCatalogTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/CatalogFromIdsResourceCatalogTransformer.java @@ -28,7 +28,6 @@ import org.jetbrains.annotations.Nullable; import java.util.LinkedList; -import java.util.Objects; public class CatalogFromIdsResourceCatalogTransformer implements IdsTypeTransformer { @Override @@ -42,12 +41,7 @@ public Class getOutputType() { } @Override - public @Nullable Catalog transform(@Nullable ResourceCatalog object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable Catalog transform(@NotNull ResourceCatalog object, @NotNull TransformerContext context) { var builder = Catalog.Builder.newInstance(); var result = IdsId.from(object.getId().toString()); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/CatalogToIdsResourceCatalogTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/CatalogToIdsResourceCatalogTransformer.java index 4bd8a4670eb..2f3624c0281 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/CatalogToIdsResourceCatalogTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/CatalogToIdsResourceCatalogTransformer.java @@ -29,7 +29,6 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; -import java.util.Objects; import java.util.stream.Collectors; public class CatalogToIdsResourceCatalogTransformer implements IdsTypeTransformer { @@ -45,12 +44,7 @@ public Class getOutputType() { } @Override - public @Nullable ResourceCatalog transform(Catalog object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable ResourceCatalog transform(@NotNull Catalog object, @NotNull TransformerContext context) { var id = IdsId.Builder.newInstance().value(object.getId()).type(IdsType.CATALOG).build().toUri(); var builder = new ResourceCatalogBuilder(id); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/ConnectorToIdsConnectorTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/ConnectorToIdsConnectorTransformer.java index 54791a1c504..301bb2e3341 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/ConnectorToIdsConnectorTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/ConnectorToIdsConnectorTransformer.java @@ -30,7 +30,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Objects; public class ConnectorToIdsConnectorTransformer implements IdsTypeTransformer { @@ -46,12 +45,7 @@ public Class getOutputType() { @Nullable @Override - public de.fraunhofer.iais.eis.Connector transform(Connector object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public de.fraunhofer.iais.eis.Connector transform(@NotNull Connector object, @NotNull TransformerContext context) { var builder = new BaseConnectorBuilder(object.getId().toUri()); if (object.getDataCatalogs() != null) { diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/SecurityProfileToIdsSecurityProfileTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/SecurityProfileToIdsSecurityProfileTransformer.java index 92a4443a11f..c521f310c44 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/SecurityProfileToIdsSecurityProfileTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/connector/SecurityProfileToIdsSecurityProfileTransformer.java @@ -23,7 +23,6 @@ import java.util.HashMap; import java.util.Map; -import java.util.Objects; public class SecurityProfileToIdsSecurityProfileTransformer implements IdsTypeTransformer { private static final Map MAPPING = new HashMap<>() { @@ -45,12 +44,7 @@ public Class getOutputType() { } @Override - public @Nullable de.fraunhofer.iais.eis.SecurityProfile transform(SecurityProfile object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable de.fraunhofer.iais.eis.SecurityProfile transform(@NotNull SecurityProfile object, @NotNull TransformerContext context) { return MAPPING.get(object); } } diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractAgreementFromIdsContractAgreementTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractAgreementFromIdsContractAgreementTransformer.java index 0924151e7cf..1f3808832e3 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractAgreementFromIdsContractAgreementTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractAgreementFromIdsContractAgreementTransformer.java @@ -51,7 +51,7 @@ public Class getOutputType() { } @Override - public @Nullable ContractAgreementTransformerOutput transform(de.fraunhofer.iais.eis.ContractAgreement contractAgreement, @NotNull TransformerContext context) { + public @Nullable ContractAgreementTransformerOutput transform(de.fraunhofer.iais.eis.@NotNull ContractAgreement contractAgreement, @NotNull TransformerContext context) { Objects.requireNonNull(context); if (contractAgreement == null) { return null; diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractAgreementToIdsContractAgreementTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractAgreementToIdsContractAgreementTransformer.java index b2c4edde7f7..3b1d25ea088 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractAgreementToIdsContractAgreementTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractAgreementToIdsContractAgreementTransformer.java @@ -50,7 +50,7 @@ public Class getOutputType() { } @Override - public @Nullable ContractAgreement transform(ContractAgreementRequest request, @NotNull TransformerContext context) { + public @Nullable ContractAgreement transform(@NotNull ContractAgreementRequest request, @NotNull TransformerContext context) { Objects.requireNonNull(context); if (request == null) { return null; diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractOfferFromIdsContractOfferOrRequestTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractOfferFromIdsContractOfferOrRequestTransformer.java index f158cf4ff4a..6225ffe1832 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractOfferFromIdsContractOfferOrRequestTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractOfferFromIdsContractOfferOrRequestTransformer.java @@ -30,7 +30,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.Objects; /** * Transforms an IDS ContractRequest into an {@link ContractOffer}. @@ -48,12 +47,7 @@ public Class getOutputType() { } @Override - public @Nullable ContractOffer transform(ContractTransformerInput object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable ContractOffer transform(@NotNull ContractTransformerInput object, @NotNull TransformerContext context) { var contract = object.getContract(); var asset = object.getAsset(); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractOfferToIdsContractOfferTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractOfferToIdsContractOfferTransformer.java index be5e13a65ca..b5d078195e6 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractOfferToIdsContractOfferTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/contract/ContractOfferToIdsContractOfferTransformer.java @@ -49,7 +49,7 @@ public Class getOutputType() { } @Override - public @Nullable de.fraunhofer.iais.eis.ContractOffer transform(ContractOffer object, @NotNull TransformerContext context) { + public @Nullable de.fraunhofer.iais.eis.ContractOffer transform(@NotNull ContractOffer object, @NotNull TransformerContext context) { Objects.requireNonNull(context); if (object == null || object.getPolicy() == null) { return null; diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ActionFromIdsActionTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ActionFromIdsActionTransformer.java index ce81cfa565c..5158d0163df 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ActionFromIdsActionTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ActionFromIdsActionTransformer.java @@ -21,8 +21,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; - public class ActionFromIdsActionTransformer implements IdsTypeTransformer { @Override @@ -36,12 +34,7 @@ public Class getOutputType() { } @Override - public @Nullable Action transform(de.fraunhofer.iais.eis.Action object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable Action transform(de.fraunhofer.iais.eis.@NotNull Action object, @NotNull TransformerContext context) { return Action.Builder.newInstance().type(object.name()).build(); } } diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ActionToIdsActionTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ActionToIdsActionTransformer.java index 6884f9415a0..4e768a05ff5 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ActionToIdsActionTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ActionToIdsActionTransformer.java @@ -20,8 +20,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; - public class ActionToIdsActionTransformer implements IdsTypeTransformer { @Override @@ -35,12 +33,7 @@ public Class getOutputType() { } @Override - public @Nullable de.fraunhofer.iais.eis.Action transform(Action object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable de.fraunhofer.iais.eis.Action transform(@NotNull Action object, @NotNull TransformerContext context) { var type = object.getType(); de.fraunhofer.iais.eis.Action idsAction; diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintFromIdsConstraintTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintFromIdsConstraintTransformer.java index f0cb636e183..e13c5ce4ef0 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintFromIdsConstraintTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintFromIdsConstraintTransformer.java @@ -40,7 +40,7 @@ public Class getOutputType() { } @Override - public @Nullable Constraint transform(de.fraunhofer.iais.eis.Constraint constraint, @NotNull TransformerContext context) { + public @Nullable Constraint transform(de.fraunhofer.iais.eis.@NotNull Constraint constraint, @NotNull TransformerContext context) { Objects.requireNonNull(context); if (constraint == null) { return null; diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintFromIdsLogicalConstraintTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintFromIdsLogicalConstraintTransformer.java index b48cdf0ba9d..447d9e0ad45 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintFromIdsLogicalConstraintTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintFromIdsLogicalConstraintTransformer.java @@ -42,7 +42,7 @@ public Class getOutputType() { } @Override - public @Nullable Constraint transform(LogicalConstraint constraint, @NotNull TransformerContext context) { + public @Nullable Constraint transform(@NotNull LogicalConstraint constraint, @NotNull TransformerContext context) { Objects.requireNonNull(context); if (constraint == null) { return null; diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintToIdsConstraintTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintToIdsConstraintTransformer.java index 58a802c204d..e0dc7a1ff28 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintToIdsConstraintTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintToIdsConstraintTransformer.java @@ -42,7 +42,7 @@ public Class getOutputType() { } @Override - public @Nullable de.fraunhofer.iais.eis.Constraint transform(Constraint constraint, @NotNull TransformerContext context) { + public @Nullable de.fraunhofer.iais.eis.Constraint transform(@NotNull Constraint constraint, @NotNull TransformerContext context) { Objects.requireNonNull(context); if (constraint == null) { return null; diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintToIdsLogicalConstraintTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintToIdsLogicalConstraintTransformer.java index d65adf0b000..43e6c6c24b1 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintToIdsLogicalConstraintTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ConstraintToIdsLogicalConstraintTransformer.java @@ -29,8 +29,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; - /** * Transforms an EDC constraint to its corresponding IDS type. Specifically, an EDC {@link MultiplicityConstraint} is transformed to a * {@link de.fraunhofer.iais.eis.LogicalConstraint}; an EDC {@link AtomicConstraint} is transformed to a {@link de.fraunhofer.iais.eis.Constraint}. @@ -48,12 +46,7 @@ public Class getOutputType() { } @Override - public @Nullable de.fraunhofer.iais.eis.LogicalConstraint transform(Constraint object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable de.fraunhofer.iais.eis.LogicalConstraint transform(@NotNull Constraint object, @NotNull TransformerContext context) { if (object instanceof MultiplicityConstraint) { return transformMultiplicityConstraint((MultiplicityConstraint) object, context); } else { diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/DutyToIdsDutyTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/DutyToIdsDutyTransformer.java index 50d37b0f2c8..78dd3b0d692 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/DutyToIdsDutyTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/DutyToIdsDutyTransformer.java @@ -27,7 +27,6 @@ import org.jetbrains.annotations.Nullable; import java.net.URI; -import java.util.Objects; public class DutyToIdsDutyTransformer implements IdsTypeTransformer { @@ -42,12 +41,7 @@ public Class getOutputType() { } @Override - public @Nullable de.fraunhofer.iais.eis.Duty transform(Duty object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable de.fraunhofer.iais.eis.Duty transform(@NotNull Duty object, @NotNull TransformerContext context) { var id = IdsId.Builder.newInstance().value(object.hashCode()).type(IdsType.OBLIGATION).build().toUri(); var builder = new DutyBuilder(id); for (var edcConstraint : object.getConstraints()) { diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionFromIdsLeftOperandTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionFromIdsLeftOperandTransformer.java index 195e96d3856..2cf14537172 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionFromIdsLeftOperandTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionFromIdsLeftOperandTransformer.java @@ -22,8 +22,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; - public class ExpressionFromIdsLeftOperandTransformer implements IdsTypeTransformer { @Override @@ -37,12 +35,7 @@ public Class getOutputType() { } @Override - public @Nullable Expression transform(@Nullable String object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable Expression transform(@NotNull String object, @NotNull TransformerContext context) { return new LiteralExpression(object); } } diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionFromIdsRdfResourceTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionFromIdsRdfResourceTransformer.java index 70e05b25391..7612206fc67 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionFromIdsRdfResourceTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionFromIdsRdfResourceTransformer.java @@ -22,8 +22,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; - public class ExpressionFromIdsRdfResourceTransformer implements IdsTypeTransformer { @Override @@ -37,12 +35,7 @@ public Class getOutputType() { } @Override - public @Nullable Expression transform(@Nullable RdfResource object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable Expression transform(@NotNull RdfResource object, @NotNull TransformerContext context) { return new LiteralExpression(object.getValue()); } } diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionToIdsLeftOperandTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionToIdsLeftOperandTransformer.java index f35c2f68115..f7e98a859c5 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionToIdsLeftOperandTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionToIdsLeftOperandTransformer.java @@ -21,8 +21,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; - public class ExpressionToIdsLeftOperandTransformer implements IdsTypeTransformer { @Override @@ -36,12 +34,7 @@ public Class getOutputType() { } @Override - public @Nullable String transform(Expression object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable String transform(@NotNull Expression object, @NotNull TransformerContext context) { if (!(object instanceof LiteralExpression)) { context.reportProblem(String.format("Cannot transform %s. Supported type: LiteralExpression", object.getClass().getName())); return null; diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionToIdsRdfResourceTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionToIdsRdfResourceTransformer.java index 6a139906db3..894b20ce5f3 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionToIdsRdfResourceTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ExpressionToIdsRdfResourceTransformer.java @@ -22,8 +22,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; - public class ExpressionToIdsRdfResourceTransformer implements IdsTypeTransformer { @Override @@ -37,12 +35,7 @@ public Class getOutputType() { } @Override - public @Nullable RdfResource transform(Expression object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable RdfResource transform(@NotNull Expression object, @NotNull TransformerContext context) { String value = null; if (object instanceof LiteralExpression) { value = ((LiteralExpression) object).asString(); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/OperatorFromIdsBinaryOperatorTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/OperatorFromIdsBinaryOperatorTransformer.java index 78999009476..9a2589815ce 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/OperatorFromIdsBinaryOperatorTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/OperatorFromIdsBinaryOperatorTransformer.java @@ -24,7 +24,6 @@ import java.util.HashMap; import java.util.Map; -import java.util.Objects; public class OperatorFromIdsBinaryOperatorTransformer implements IdsTypeTransformer { private static final Map MAPPING = new HashMap<>() { @@ -50,12 +49,7 @@ public Class getOutputType() { } @Override - public @Nullable Operator transform(@Nullable BinaryOperator object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable Operator transform(@NotNull BinaryOperator object, @NotNull TransformerContext context) { var operator = MAPPING.get(object); if (operator == null) { context.reportProblem(String.format("cannot transform IDS operator %s", object)); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/OperatorToIdsBinaryOperatorTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/OperatorToIdsBinaryOperatorTransformer.java index f9d8ad1e5df..0cbc8553b33 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/OperatorToIdsBinaryOperatorTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/OperatorToIdsBinaryOperatorTransformer.java @@ -24,7 +24,6 @@ import java.util.HashMap; import java.util.Map; -import java.util.Objects; public class OperatorToIdsBinaryOperatorTransformer implements IdsTypeTransformer { private static final Map MAPPING = new HashMap<>() { @@ -49,12 +48,7 @@ public Class getOutputType() { } @Override - public @Nullable BinaryOperator transform(Operator object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable BinaryOperator transform(@NotNull Operator object, @NotNull TransformerContext context) { var binaryOperator = MAPPING.get(object); if (binaryOperator == null) { context.reportProblem(String.format("Can not transform %s to IDS BinaryOperator", object.name())); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/PermissionFromIdsPermissionTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/PermissionFromIdsPermissionTransformer.java index 6c3f180e14d..0c14e39853d 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/PermissionFromIdsPermissionTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/PermissionFromIdsPermissionTransformer.java @@ -25,8 +25,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; - public class PermissionFromIdsPermissionTransformer implements IdsTypeTransformer { @Override @@ -40,12 +38,7 @@ public Class getOutputType() { } @Override - public @Nullable Permission transform(de.fraunhofer.iais.eis.Permission object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable Permission transform(de.fraunhofer.iais.eis.@NotNull Permission object, @NotNull TransformerContext context) { var builder = Permission.Builder.newInstance(); if (object.getPostDuty() != null && !object.getPostDuty().isEmpty()) { context.reportProblem("Cannot map IDS permission post duty to EDC (ODRL)"); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/PermissionToIdsPermissionTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/PermissionToIdsPermissionTransformer.java index f372b36a688..6f5a6fcf83e 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/PermissionToIdsPermissionTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/PermissionToIdsPermissionTransformer.java @@ -30,7 +30,6 @@ import java.net.URI; import java.util.ArrayList; import java.util.List; -import java.util.Objects; public class PermissionToIdsPermissionTransformer implements IdsTypeTransformer { @@ -45,12 +44,7 @@ public Class getOutputType() { } @Override - public @Nullable de.fraunhofer.iais.eis.Permission transform(Permission object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable de.fraunhofer.iais.eis.Permission transform(@NotNull Permission object, @NotNull TransformerContext context) { var permissionId = IdsId.Builder.newInstance().value(object.hashCode()).type(IdsType.PERMISSION).build().toUri(); var builder = new PermissionBuilder(permissionId); for (var edcConstraint : object.getConstraints()) { diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ProhibitionFromIdsProhibitionTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ProhibitionFromIdsProhibitionTransformer.java index 3166f83a7d1..41435d30a46 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ProhibitionFromIdsProhibitionTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ProhibitionFromIdsProhibitionTransformer.java @@ -22,8 +22,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; - public class ProhibitionFromIdsProhibitionTransformer implements IdsTypeTransformer { @Override @@ -37,12 +35,7 @@ public Class getOutputType() { } @Override - public @Nullable Prohibition transform(de.fraunhofer.iais.eis.Prohibition object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable Prohibition transform(de.fraunhofer.iais.eis.@NotNull Prohibition object, @NotNull TransformerContext context) { var builder = Prohibition.Builder.newInstance(); for (var idsConstraint : object.getConstraint()) { var edcConstraint = context.transform(idsConstraint, Constraint.class); diff --git a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ProhibitionToIdsProhibitionTransformer.java b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ProhibitionToIdsProhibitionTransformer.java index deaaca1d1b5..771ec4b5627 100644 --- a/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ProhibitionToIdsProhibitionTransformer.java +++ b/data-protocols/ids/ids-transform-v1/src/main/java/org/eclipse/edc/protocol/ids/transform/type/policy/ProhibitionToIdsProhibitionTransformer.java @@ -28,7 +28,6 @@ import org.jetbrains.annotations.Nullable; import java.net.URI; -import java.util.Objects; public class ProhibitionToIdsProhibitionTransformer implements IdsTypeTransformer { @@ -43,12 +42,7 @@ public Class getOutputType() { } @Override - public @Nullable de.fraunhofer.iais.eis.Prohibition transform(Prohibition object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable de.fraunhofer.iais.eis.Prohibition transform(@NotNull Prohibition object, @NotNull TransformerContext context) { var id = IdsId.Builder.newInstance().value(object.hashCode()).type(IdsType.PROHIBITION).build().toUri(); var builder = new ProhibitionBuilder(id); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ActionFromIdsActionTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ActionFromIdsActionTransformerTest.java index 392480b21a5..c9d4e929f80 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ActionFromIdsActionTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ActionFromIdsActionTransformerTest.java @@ -35,27 +35,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(Action.USE, null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulMap() { var result = transformer.transform(Action.USE, context); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ActionToIdsActionTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ActionToIdsActionTransformerTest.java index 5b65b0a1d64..22a88aabc64 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ActionToIdsActionTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ActionToIdsActionTransformerTest.java @@ -35,27 +35,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(Action.Builder.newInstance().type("USE").build(), null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulMap() { var action = Action.Builder.newInstance().type("USE").build(); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetFromIdsArtifactTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetFromIdsArtifactTransformerTest.java index 41fab9039e5..bdd02521d3f 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetFromIdsArtifactTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetFromIdsArtifactTransformerTest.java @@ -26,7 +26,6 @@ import java.net.URI; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.mock; /** @@ -50,25 +49,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - assertThatExceptionOfType(NullPointerException.class) - .isThrownBy(() -> transformer.transform(null, null)); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - assertThatExceptionOfType(NullPointerException.class) - .isThrownBy(() -> transformer.transform(new ArtifactBuilder().build(), null)); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - assertThat(result).isNull(); - } - @Test void testSuccessfulMap() { var artifact = new ArtifactBuilder(ARTIFACT_URI) diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetFromIdsRepresentationTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetFromIdsRepresentationTransformerTest.java index 0a833a454b0..08f6e0909d4 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetFromIdsRepresentationTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetFromIdsRepresentationTransformerTest.java @@ -57,27 +57,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(representation, null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulMap() { var result = transformer.transform(representation, context); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetFromIdsResourceTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetFromIdsResourceTransformerTest.java index 74ac4b519a2..215d8fe613d 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetFromIdsResourceTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetFromIdsResourceTransformerTest.java @@ -34,7 +34,6 @@ import java.util.Collections; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.mock; class AssetFromIdsResourceTransformerTest { @@ -75,25 +74,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - assertThatExceptionOfType(NullPointerException.class) - .isThrownBy(() -> transformer.transform(null, null)); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - assertThatExceptionOfType(NullPointerException.class) - .isThrownBy(() -> transformer.transform(resource, null)); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - assertThat(result).isNull(); - } - @Test void testSuccessfulMap() { var result = transformer.transform(resource, context); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetToIdsArtifactTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetToIdsArtifactTransformerTest.java index 517afe7f34d..1ddcadab50f 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetToIdsArtifactTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetToIdsArtifactTransformerTest.java @@ -27,7 +27,6 @@ import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.mock; class AssetToIdsArtifactTransformerTest { @@ -63,25 +62,6 @@ void transformAssetWithProperties() { .containsEntry("somekey", "somevalue"); } - @Test - void testThrowsNullPointerExceptionForAll() { - assertThatExceptionOfType(NullPointerException.class) - .isThrownBy(() -> transformer.transform(null, null)); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - assertThatExceptionOfType(NullPointerException.class) - .isThrownBy(() -> transformer.transform(Asset.Builder.newInstance().build(), null)); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - assertThat(result).isNull(); - } - @Test void testSuccessfulSimple() { var asset = Asset.Builder.newInstance().id(ASSET_ID).build(); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetToIdsRepresentationTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetToIdsRepresentationTransformerTest.java index 7e4d4d875a5..b1e0b2abc5c 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetToIdsRepresentationTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetToIdsRepresentationTransformerTest.java @@ -50,27 +50,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(Asset.Builder.newInstance().build(), null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulSimple() { var asset = Asset.Builder.newInstance().id(REPRESENTATION_ID).build(); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetToIdsResourceTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetToIdsResourceTransformerTest.java index c67db150dad..27c6c2a6f9c 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetToIdsResourceTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/AssetToIdsResourceTransformerTest.java @@ -20,7 +20,6 @@ import org.eclipse.edc.protocol.ids.transform.type.asset.AssetToIdsResourceTransformer; import org.eclipse.edc.spi.types.domain.asset.Asset; import org.eclipse.edc.transform.spi.TransformerContext; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -50,27 +49,6 @@ void setUp() { transformer = new AssetToIdsResourceTransformer(); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(Asset.Builder.newInstance().build(), null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulSimple() { String description = "foo bar"; diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/CatalogToIdsResourceCatalogTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/CatalogToIdsResourceCatalogTransformerTest.java index 45090e0f1c0..d76b75c62f5 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/CatalogToIdsResourceCatalogTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/CatalogToIdsResourceCatalogTransformerTest.java @@ -31,7 +31,6 @@ import java.util.List; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.mock; @@ -53,27 +52,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - assertThrows(NullPointerException.class, () -> { - transformer.transform(Catalog.Builder.newInstance().build(), null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - assertThat(result).isNull(); - } - @Test void testSuccessfulSimple() { var a1 = Asset.Builder.newInstance().id("a1").build(); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ConstraintFromIdsConstraintTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ConstraintFromIdsConstraintTransformerTest.java index f2331c84c50..5ab4cc172bf 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ConstraintFromIdsConstraintTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ConstraintFromIdsConstraintTransformerTest.java @@ -56,27 +56,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(idsConstraint, null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testTransform() { var expectedLeftExpression = new LiteralExpression("left"); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ConstraintToIdsConstraintTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ConstraintToIdsConstraintTransformerTest.java index fcefee0b831..7a02f1fc14b 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ConstraintToIdsConstraintTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ConstraintToIdsConstraintTransformerTest.java @@ -48,27 +48,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(AtomicConstraint.Builder.newInstance().build(), null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testNonAtomicConstraint() { Constraint nonAtomicConstraint = mock(Constraint.class); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ContractAgreementFromIdsContractAgreementTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ContractAgreementFromIdsContractAgreementTransformerTest.java index 026fbb04394..cb7493d6a31 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ContractAgreementFromIdsContractAgreementTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ContractAgreementFromIdsContractAgreementTransformerTest.java @@ -62,13 +62,6 @@ void setUp() { idsDuty = new de.fraunhofer.iais.eis.DutyBuilder().build(); } - @Test - void shouldReturnNull_ifInputIsNull() { - var result = transformer.transform(null, context); - - assertThat(result).isNull(); - } - @Test void shouldTransform() { var edcPermission = Permission.Builder.newInstance().target("assetId").build(); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ContractOfferFromIdsContractRequestTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ContractOfferFromIdsContractRequestTransformerTest.java index c2df0207d97..d316f1eb660 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ContractOfferFromIdsContractRequestTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ContractOfferFromIdsContractRequestTransformerTest.java @@ -87,36 +87,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext_contractRequest() { - var input = ContractTransformerInput.Builder.newInstance().contract(idsContractRequest).asset(asset).build(); - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(input, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext_contractOffer() { - var input = ContractTransformerInput.Builder.newInstance().contract(idsContractOffer).asset(asset).build(); - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(input, null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulSimple_contractRequest() { var input = ContractTransformerInput.Builder.newInstance().contract(idsContractRequest).asset(asset).build(); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ContractOfferToIdsContractOfferTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ContractOfferToIdsContractOfferTransformerTest.java index 28be7cec320..8f988a9ce87 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ContractOfferToIdsContractOfferTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ContractOfferToIdsContractOfferTransformerTest.java @@ -24,7 +24,6 @@ import org.eclipse.edc.spi.types.domain.asset.Asset; import org.eclipse.edc.transform.spi.TransformerContext; import org.jetbrains.annotations.NotNull; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -54,27 +53,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(contractOffer(Policy.Builder.newInstance().build()), null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulSimple() { Permission edcPermission = mock(Permission.class); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/DutyToIdsDutyTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/DutyToIdsDutyTransformerTest.java index 2eb9071fe3a..04d86110b60 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/DutyToIdsDutyTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/DutyToIdsDutyTransformerTest.java @@ -52,27 +52,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(Duty.Builder.newInstance().build(), null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulMap() { Action edcAction = mock(Action.class); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionFromIdsLeftOperandTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionFromIdsLeftOperandTransformerTest.java index 24338cc5f73..a413b4e81f9 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionFromIdsLeftOperandTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionFromIdsLeftOperandTransformerTest.java @@ -19,9 +19,7 @@ import org.eclipse.edc.policy.model.LiteralExpression; import org.eclipse.edc.protocol.ids.transform.type.policy.ExpressionFromIdsLeftOperandTransformer; import org.eclipse.edc.transform.spi.TransformerContext; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -48,27 +46,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(leftOperand, null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @ParameterizedTest @ArgumentsSource(TransformParameterArgumentSource.class) void transform(String leftOperand, String expected) { diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionFromIdsRdfResourceTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionFromIdsRdfResourceTransformerTest.java index 54597ffffe0..019178e64a9 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionFromIdsRdfResourceTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionFromIdsRdfResourceTransformerTest.java @@ -39,27 +39,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(rdfResource, null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulMap() { var result = transformer.transform(rdfResource, context); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionToIdsLeftOperandTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionToIdsLeftOperandTransformerTest.java index 8c3016b5050..c91ab107a36 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionToIdsLeftOperandTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionToIdsLeftOperandTransformerTest.java @@ -35,27 +35,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(new LiteralExpression("any"), null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulMap() { var expression = new LiteralExpression("absoluteSpatialPosition"); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionToIdsRdfResourceTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionToIdsRdfResourceTransformerTest.java index 22a0c5297dc..1b69ab20b0e 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionToIdsRdfResourceTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ExpressionToIdsRdfResourceTransformerTest.java @@ -35,27 +35,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(new LiteralExpression("any"), null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulMap() { var expression = new LiteralExpression("COUNT"); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/IdsTransformServiceExtensionTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/IdsTransformServiceExtensionTest.java index 93167c3ed51..4e4f8cd9cb5 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/IdsTransformServiceExtensionTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/IdsTransformServiceExtensionTest.java @@ -34,13 +34,10 @@ import org.eclipse.edc.protocol.ids.spi.domain.connector.Connector; import org.eclipse.edc.protocol.ids.spi.domain.connector.SecurityProfile; import org.eclipse.edc.protocol.ids.spi.transform.IdsTransformerRegistry; -import org.eclipse.edc.protocol.ids.spi.transform.IdsTypeTransformer; import org.eclipse.edc.protocol.ids.spi.types.container.OfferedAsset; -import org.eclipse.edc.spi.result.Result; import org.eclipse.edc.spi.system.ServiceExtensionContext; import org.eclipse.edc.spi.system.injection.ObjectFactory; import org.eclipse.edc.spi.types.domain.asset.Asset; -import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtensionContext; @@ -49,29 +46,23 @@ import org.junit.jupiter.params.provider.ArgumentsProvider; import org.junit.jupiter.params.provider.ArgumentsSource; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Objects; import java.util.stream.Stream; -import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; @ExtendWith(DependencyInjectionExtension.class) class IdsTransformServiceExtensionTest { - private Map, List>> knownConvertibles; private IdsTransformServiceExtension idsTransformServiceExtension; private ServiceExtensionContext serviceExtensionContext; + private final IdsTransformerRegistry transformerRegistry = mock(IdsTransformerRegistry.class); @BeforeEach void setUp(ServiceExtensionContext context, ObjectFactory factory) { - knownConvertibles = new HashMap<>(); - - var transformerRegistry = new TestTransformerRegistry(knownConvertibles); context.registerService(IdsTransformerRegistry.class, transformerRegistry); idsTransformServiceExtension = factory.constructInstance(IdsTransformServiceExtension.class); serviceExtensionContext = context; @@ -82,12 +73,10 @@ void setUp(ServiceExtensionContext context, ObjectFactory factory) { void verifyRequiredTransformerRegistered(Class inputType, Class outputType) { idsTransformServiceExtension.initialize(serviceExtensionContext); - assertThat(knownConvertibles).containsKey(inputType); - assertThat(knownConvertibles).extracting((m) -> m.get(inputType)).isNotNull(); - assertThat(knownConvertibles.get(inputType)).contains(outputType); + verify(transformerRegistry).register(argThat(t -> t.getInputType().equals(inputType) && t.getOutputType().equals(outputType))); } - static class VerifyRequiredTransformerRegisteredArgumentsProvider implements ArgumentsProvider { + private static class VerifyRequiredTransformerRegisteredArgumentsProvider implements ArgumentsProvider { @Override public Stream provideArguments(ExtensionContext context) { return Stream.of( @@ -121,28 +110,4 @@ public Stream provideArguments(ExtensionContext context) { ); } } - - private static class TestTransformerRegistry implements IdsTransformerRegistry { - private final Map, List>> knownConvertibles; - - TestTransformerRegistry(Map, List>> knownConvertibles) { - this.knownConvertibles = knownConvertibles; - } - - @Override - public void register(IdsTypeTransformer transformer) { - Objects.requireNonNull(transformer.getInputType()); - Objects.requireNonNull(transformer.getOutputType()); - - knownConvertibles.computeIfAbsent( - transformer.getInputType(), - (k) -> new LinkedList<>()) - .add(transformer.getOutputType()); - } - - @Override - public Result transform(@NotNull INPUT object, @NotNull Class outputType) { - throw new RuntimeException("Not intended to be used within this Test"); - } - } } diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/OfferedAssetToIdsResourceTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/OfferedAssetToIdsResourceTransformerTest.java index d090c23a4bc..1e707a4a993 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/OfferedAssetToIdsResourceTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/OfferedAssetToIdsResourceTransformerTest.java @@ -67,13 +67,6 @@ void setUp() { transformer = new OfferedAssetToIdsResourceTransformer(); } - @Test - void transform_nullInputGivesNullResult() { - var result = transformer.transform(null, context); - - assertThat(result).isNull(); - } - @Test void transform() { var representation = new RepresentationBuilder().build(); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/PermissionFromIdsPermissionTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/PermissionFromIdsPermissionTransformerTest.java index 678ef665c55..32655e12fde 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/PermissionFromIdsPermissionTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/PermissionFromIdsPermissionTransformerTest.java @@ -67,27 +67,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(idsPermission, null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulSimple() { var edcConstraint = AtomicConstraint.Builder.newInstance().build(); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/PermissionToIdsPermissionTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/PermissionToIdsPermissionTransformerTest.java index ab468962bf7..90a26a37d6d 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/PermissionToIdsPermissionTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/PermissionToIdsPermissionTransformerTest.java @@ -52,27 +52,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(Permission.Builder.newInstance().build(), null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulSimple() { Action edcAction = mock(Action.class); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ProhibitionFromIdsProhibitionTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ProhibitionFromIdsProhibitionTransformerTest.java index b234cad0f3d..266b11eaa0b 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ProhibitionFromIdsProhibitionTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ProhibitionFromIdsProhibitionTransformerTest.java @@ -65,27 +65,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(idsPermission, null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulSimple() { Constraint edcConstraint = mock(Constraint.class); diff --git a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ProhibitionToIdsProhibitionTransformerTest.java b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ProhibitionToIdsProhibitionTransformerTest.java index 28568e9d9d5..d11831cc088 100644 --- a/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ProhibitionToIdsProhibitionTransformerTest.java +++ b/data-protocols/ids/ids-transform-v1/src/test/java/org/eclipse/edc/protocol/ids/transform/ProhibitionToIdsProhibitionTransformerTest.java @@ -45,27 +45,6 @@ void setUp() { context = mock(TransformerContext.class); } - @Test - void testThrowsNullPointerExceptionForAll() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(null, null); - }); - } - - @Test - void testThrowsNullPointerExceptionForContext() { - Assertions.assertThrows(NullPointerException.class, () -> { - transformer.transform(Prohibition.Builder.newInstance().build(), null); - }); - } - - @Test - void testReturnsNull() { - var result = transformer.transform(null, context); - - Assertions.assertNull(result); - } - @Test void testSuccessfulSimple() { Action edcAction = mock(Action.class); diff --git a/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/ApiCoreExtension.java b/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/ApiCoreExtension.java index 397ab60ed3d..c745709a026 100644 --- a/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/ApiCoreExtension.java +++ b/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/ApiCoreExtension.java @@ -20,11 +20,9 @@ import org.eclipse.edc.api.transformer.DtoTransformerRegistryImpl; import org.eclipse.edc.api.transformer.QuerySpecDtoToQuerySpecTransformer; import org.eclipse.edc.runtime.metamodel.annotation.Extension; -import org.eclipse.edc.runtime.metamodel.annotation.Provides; +import org.eclipse.edc.runtime.metamodel.annotation.Provider; import org.eclipse.edc.spi.system.ServiceExtension; -import org.eclipse.edc.spi.system.ServiceExtensionContext; -@Provides(DtoTransformerRegistry.class) @Extension(value = ApiCoreExtension.NAME) public class ApiCoreExtension implements ServiceExtension { @@ -35,12 +33,12 @@ public String name() { return NAME; } - @Override - public void initialize(ServiceExtensionContext context) { + @Provider + public DtoTransformerRegistry transformerRegistry() { var transformerRegistry = new DtoTransformerRegistryImpl(); transformerRegistry.register(new QuerySpecDtoToQuerySpecTransformer()); transformerRegistry.register(new CriterionToCriterionDtoTransformer()); transformerRegistry.register(new CriterionDtoToCriterionTransformer()); - context.registerService(DtoTransformerRegistry.class, transformerRegistry); + return transformerRegistry; } } diff --git a/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/CriterionDtoToCriterionTransformer.java b/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/CriterionDtoToCriterionTransformer.java index cb9d30edd45..e24000f4602 100644 --- a/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/CriterionDtoToCriterionTransformer.java +++ b/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/CriterionDtoToCriterionTransformer.java @@ -20,8 +20,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Optional; - public class CriterionDtoToCriterionTransformer implements DtoTransformer { @Override @@ -35,12 +33,7 @@ public Class getOutputType() { } @Override - public @Nullable Criterion transform(@Nullable CriterionDto object, @NotNull TransformerContext context) { - return Optional.ofNullable(object) - .map(criterion -> new Criterion(criterion.getOperandLeft(), criterion.getOperator(), criterion.getOperandRight())) - .orElseGet(() -> { - context.reportProblem("input criterion is null"); - return null; - }); + public @Nullable Criterion transform(@NotNull CriterionDto object, @NotNull TransformerContext context) { + return new Criterion(object.getOperandLeft(), object.getOperator(), object.getOperandRight()); } } diff --git a/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/CriterionToCriterionDtoTransformer.java b/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/CriterionToCriterionDtoTransformer.java index cbd360e391f..bbbe49a754d 100644 --- a/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/CriterionToCriterionDtoTransformer.java +++ b/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/CriterionToCriterionDtoTransformer.java @@ -20,8 +20,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Optional; - public class CriterionToCriterionDtoTransformer implements DtoTransformer { @Override @@ -35,16 +33,11 @@ public Class getOutputType() { } @Override - public @Nullable CriterionDto transform(@Nullable Criterion object, @NotNull TransformerContext context) { - return Optional.ofNullable(object) - .map(criterion -> CriterionDto.Builder.newInstance() - .operandLeft(criterion.getOperandLeft()) - .operator(criterion.getOperator()) - .operandRight(criterion.getOperandRight()) - .build()) - .orElseGet(() -> { - context.reportProblem("input criterion is null"); - return null; - }); + public @Nullable CriterionDto transform(@NotNull Criterion object, @NotNull TransformerContext context) { + return CriterionDto.Builder.newInstance() + .operandLeft(object.getOperandLeft()) + .operator(object.getOperator()) + .operandRight(object.getOperandRight()) + .build(); } } diff --git a/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/DtoTransformerRegistryImpl.java b/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/DtoTransformerRegistryImpl.java index b20d0dea5ff..7338a2088ac 100644 --- a/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/DtoTransformerRegistryImpl.java +++ b/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/DtoTransformerRegistryImpl.java @@ -15,76 +15,11 @@ package org.eclipse.edc.api.transformer; -import org.eclipse.edc.spi.EdcException; -import org.eclipse.edc.spi.result.Result; -import org.eclipse.edc.spi.system.ServiceExtensionContext; -import org.eclipse.edc.transform.spi.TransformerContext; -import org.eclipse.edc.transform.spi.TransformerContextImpl; -import org.jetbrains.annotations.NotNull; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Objects; -import java.util.Set; - -import static java.lang.String.format; +import org.eclipse.edc.transform.spi.TypeTransformerRegistryImpl; /** - * Default in-mem implementation of the {@link DtoTransformerRegistry} interface. - * Every REST API extension should check whether an {@link DtoTransformerRegistry} is already available in the {@link ServiceExtensionContext} - * and register one otherwise: - *
- * \@Inject(required=false)
- * private DtoTransformerRegistry registry;
- *
- * void initialize(ServiceExtensionContext context){
- *      if(registry == null){
- *          registry= new DtoTransformerRegistryImpl();
- *          context.registerService(DtoTransformerRegistry.class, registry);
- *      }
- *
- *      var yourController = new YourApiController(..., registry);
- * }
- * 
+ * Marker class to allow for a type-safe registry that only holds implementations of the {@link DtoTransformer} interface. */ -public class DtoTransformerRegistryImpl implements DtoTransformerRegistry { - - private final Set> transformers; - - public DtoTransformerRegistryImpl() { - transformers = new HashSet<>(); - } - - public Set> getTransformers() { - return Collections.unmodifiableSet(transformers); - } - - @Override - public void register(DtoTransformer transformer) { - transformers.add(transformer); - } - - @Override - public Result transform(@NotNull INPUT object, @NotNull Class outputType) { - - var ctx = new TransformerContextImpl(this); - - var output = transform(object, outputType, ctx); - - return ctx.hasProblems() ? Result.failure(ctx.getProblems()) : Result.success(output); - } - - private OUTPUT transform(INPUT object, Class outputType, TransformerContext context) { - Objects.requireNonNull(object); - - var t = transformers.stream() - .filter(tr -> tr.getInputType().isInstance(object) && tr.getOutputType().equals(outputType)) - .findFirst().orElse(null); - - if (t == null) { - throw new EdcException(format("No DtoTransformer registered that can handle %s -> %s", object, outputType)); - } - return outputType.cast(((DtoTransformer) t).transform(object, context)); - } +public class DtoTransformerRegistryImpl extends TypeTransformerRegistryImpl> implements DtoTransformerRegistry { } diff --git a/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/QuerySpecDtoToQuerySpecTransformer.java b/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/QuerySpecDtoToQuerySpecTransformer.java index 533f2aefe32..39e1212d6e5 100644 --- a/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/QuerySpecDtoToQuerySpecTransformer.java +++ b/extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/transformer/QuerySpecDtoToQuerySpecTransformer.java @@ -40,7 +40,7 @@ public Class getOutputType() { } @Override - public @Nullable QuerySpec transform(@Nullable QuerySpecDto query, @NotNull TransformerContext context) { + public @Nullable QuerySpec transform(@NotNull QuerySpecDto query, @NotNull TransformerContext context) { var builder = QuerySpec.Builder.newInstance() .limit(query.getLimit()) .offset(query.getOffset()) diff --git a/extensions/common/api/api-core/src/test/java/org/eclipse/edc/api/transformer/CriterionDtoToCriterionTransformerTest.java b/extensions/common/api/api-core/src/test/java/org/eclipse/edc/api/transformer/CriterionDtoToCriterionTransformerTest.java index 2929ab35ac3..a9f32d2e3b0 100644 --- a/extensions/common/api/api-core/src/test/java/org/eclipse/edc/api/transformer/CriterionDtoToCriterionTransformerTest.java +++ b/extensions/common/api/api-core/src/test/java/org/eclipse/edc/api/transformer/CriterionDtoToCriterionTransformerTest.java @@ -20,7 +20,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; class CriterionDtoToCriterionTransformerTest { @@ -46,13 +45,4 @@ void transform() { assertThat(criterion).usingRecursiveComparison().isEqualTo(dto); } - @Test - void transform_nullInput() { - var context = mock(TransformerContext.class); - - var definition = transformer.transform(null, context); - - assertThat(definition).isNull(); - verify(context).reportProblem("input criterion is null"); - } -} \ No newline at end of file +} diff --git a/extensions/common/api/api-core/src/test/java/org/eclipse/edc/api/transformer/CriterionToCriterionDtoTransformerTest.java b/extensions/common/api/api-core/src/test/java/org/eclipse/edc/api/transformer/CriterionToCriterionDtoTransformerTest.java index d7898c4b9aa..06dae032798 100644 --- a/extensions/common/api/api-core/src/test/java/org/eclipse/edc/api/transformer/CriterionToCriterionDtoTransformerTest.java +++ b/extensions/common/api/api-core/src/test/java/org/eclipse/edc/api/transformer/CriterionToCriterionDtoTransformerTest.java @@ -20,7 +20,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; class CriterionToCriterionDtoTransformerTest { @@ -42,13 +41,4 @@ void transform() { assertThat(dto).usingRecursiveComparison().isEqualTo(criterion); } - @Test - void transform_nullInput() { - var context = mock(TransformerContext.class); - - var definition = transformer.transform(null, context); - - assertThat(definition).isNull(); - verify(context).reportProblem("input criterion is null"); - } -} \ No newline at end of file +} diff --git a/extensions/common/api/api-core/src/test/java/org/eclipse/edc/api/transformer/DtoTransformerRegistryImplTest.java b/extensions/common/api/api-core/src/test/java/org/eclipse/edc/api/transformer/DtoTransformerRegistryImplTest.java deleted file mode 100644 index 4808da3fbee..00000000000 --- a/extensions/common/api/api-core/src/test/java/org/eclipse/edc/api/transformer/DtoTransformerRegistryImplTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (c) 2020 - 2022 Microsoft Corporation - * - * 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: - * Microsoft Corporation - initial API and implementation - * - */ - -package org.eclipse.edc.api.transformer; - -import org.eclipse.edc.spi.EdcException; -import org.eclipse.edc.transform.spi.TransformerContext; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - - -class DtoTransformerRegistryImplTest { - - private DtoTransformerRegistryImpl registry; - - @BeforeEach - void setUp() { - registry = new DtoTransformerRegistryImpl(); - } - - @Test - void register() { - var transformer = new TestTransformer(); - registry.register(transformer); - assertThat(registry.getTransformers()).containsOnly(transformer); - } - - @Test - void register_many() { - registry.register(new TestTransformer()); - registry.register(new TestTransformer()); - assertThat(registry.getTransformers()).hasSize(2); - } - - @Test - void register_same() { - var t = new TestTransformer(); - registry.register(t); - registry.register(t); - assertThat(registry.getTransformers()).hasSize(1).containsOnly(t); - } - - @Test - void transform_canHandle_success() { - registry.register(new TestTransformer()); - - var r = registry.transform("foo", String.class); - assertThat(r.succeeded()).isTrue(); - assertThat(r.getContent()).isEqualTo("foo"); - } - - @Test - void transform_cannotHandle() { - registry.register(new TestTransformer()); - - assertThatThrownBy(() -> registry.transform("foo", Integer.class)).isInstanceOf(EdcException.class) - .hasMessageStartingWith("No DtoTransformer registered that can handle foo -> " + Integer.class); - } - - @Test - void transform_canHandle_withErrors() { - - var tr = new FailingTransformer(); - registry.register(tr); - - var r = registry.transform("foo", String.class); - assertThat(r.succeeded()).isFalse(); - assertThat(r.getFailureMessages()).containsExactly("some problem"); - - } - - private static class TestTransformer implements DtoTransformer { - - @Override - public Class getInputType() { - return String.class; - } - - @Override - public Class getOutputType() { - return String.class; - } - - @Override - public @Nullable String transform(@Nullable String object, @NotNull TransformerContext context) { - return object; - } - } - - private static class FailingTransformer extends TestTransformer { - @Override - public @Nullable String transform(@Nullable String object, @NotNull TransformerContext context) { - context.reportProblem("some problem"); - return super.transform(object, context); - } - } -} \ No newline at end of file diff --git a/extensions/control-plane/api/management-api/asset-api/src/main/java/org/eclipse/edc/connector/api/management/asset/transform/AssetRequestDtoToAssetTransformer.java b/extensions/control-plane/api/management-api/asset-api/src/main/java/org/eclipse/edc/connector/api/management/asset/transform/AssetRequestDtoToAssetTransformer.java index 57217956b75..8ca88a2cce4 100644 --- a/extensions/control-plane/api/management-api/asset-api/src/main/java/org/eclipse/edc/connector/api/management/asset/transform/AssetRequestDtoToAssetTransformer.java +++ b/extensions/control-plane/api/management-api/asset-api/src/main/java/org/eclipse/edc/connector/api/management/asset/transform/AssetRequestDtoToAssetTransformer.java @@ -21,8 +21,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Optional; - public class AssetRequestDtoToAssetTransformer implements DtoTransformer { @Override @@ -36,17 +34,10 @@ public Class getOutputType() { } @Override - public @Nullable Asset transform(@Nullable AssetRequestDto object, @NotNull TransformerContext context) { - return Optional.ofNullable(object) - .map(input -> Asset.Builder.newInstance() - .id(input.getId()) - .properties(input.getProperties()) - .build() - ) - .orElseGet(() -> { - context.reportProblem("input asset is null"); - return null; - }); - + public @Nullable Asset transform(@NotNull AssetRequestDto object, @NotNull TransformerContext context) { + return Asset.Builder.newInstance() + .id(object.getId()) + .properties(object.getProperties()) + .build(); } } diff --git a/extensions/control-plane/api/management-api/asset-api/src/main/java/org/eclipse/edc/connector/api/management/asset/transform/AssetToAssetResponseDtoTransformer.java b/extensions/control-plane/api/management-api/asset-api/src/main/java/org/eclipse/edc/connector/api/management/asset/transform/AssetToAssetResponseDtoTransformer.java index 43af3324689..a482ff2eadd 100644 --- a/extensions/control-plane/api/management-api/asset-api/src/main/java/org/eclipse/edc/connector/api/management/asset/transform/AssetToAssetResponseDtoTransformer.java +++ b/extensions/control-plane/api/management-api/asset-api/src/main/java/org/eclipse/edc/connector/api/management/asset/transform/AssetToAssetResponseDtoTransformer.java @@ -21,8 +21,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Optional; - public class AssetToAssetResponseDtoTransformer implements DtoTransformer { @Override @@ -36,18 +34,11 @@ public Class getOutputType() { } @Override - public @Nullable AssetResponseDto transform(@Nullable Asset object, @NotNull TransformerContext context) { - return Optional.ofNullable(object) - .map(input -> AssetResponseDto.Builder.newInstance() - .id(input.getId()) - .properties(input.getProperties()) - .createdAt(input.getCreatedAt()) - .build() - ) - .orElseGet(() -> { - context.reportProblem("input asset is null"); - return null; - }); - + public @Nullable AssetResponseDto transform(@NotNull Asset object, @NotNull TransformerContext context) { + return AssetResponseDto.Builder.newInstance() + .id(object.getId()) + .properties(object.getProperties()) + .createdAt(object.getCreatedAt()) + .build(); } } diff --git a/extensions/control-plane/api/management-api/asset-api/src/main/java/org/eclipse/edc/connector/api/management/asset/transform/DataAddressDtoToDataAddressTransformer.java b/extensions/control-plane/api/management-api/asset-api/src/main/java/org/eclipse/edc/connector/api/management/asset/transform/DataAddressDtoToDataAddressTransformer.java index 3ed3fac2aa2..3652b287e1a 100644 --- a/extensions/control-plane/api/management-api/asset-api/src/main/java/org/eclipse/edc/connector/api/management/asset/transform/DataAddressDtoToDataAddressTransformer.java +++ b/extensions/control-plane/api/management-api/asset-api/src/main/java/org/eclipse/edc/connector/api/management/asset/transform/DataAddressDtoToDataAddressTransformer.java @@ -34,7 +34,7 @@ public Class getOutputType() { } @Override - public @Nullable DataAddress transform(@Nullable DataAddressDto object, @NotNull TransformerContext context) { + public @Nullable DataAddress transform(@NotNull DataAddressDto object, @NotNull TransformerContext context) { return DataAddress.Builder.newInstance().properties(object.getProperties()).build(); } diff --git a/extensions/control-plane/api/management-api/asset-api/src/test/java/org/eclipse/edc/connector/api/management/asset/transform/AssetRequestDtoToAssetTransformerTest.java b/extensions/control-plane/api/management-api/asset-api/src/test/java/org/eclipse/edc/connector/api/management/asset/transform/AssetRequestDtoToAssetTransformerTest.java index 146f2f63cd6..078ee5b92ea 100644 --- a/extensions/control-plane/api/management-api/asset-api/src/test/java/org/eclipse/edc/connector/api/management/asset/transform/AssetRequestDtoToAssetTransformerTest.java +++ b/extensions/control-plane/api/management-api/asset-api/src/test/java/org/eclipse/edc/connector/api/management/asset/transform/AssetRequestDtoToAssetTransformerTest.java @@ -23,7 +23,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; class AssetRequestDtoToAssetTransformerTest { @@ -71,13 +70,4 @@ void transform_idFromProperties() { assertThat(asset.getProperties()).containsExactlyInAnyOrderEntriesOf(assetDto.getProperties()); } - @Test - void transform_nullInput() { - var context = mock(TransformerContext.class); - - var asset = transformer.transform(null, context); - - assertThat(asset).isNull(); - verify(context).reportProblem("input asset is null"); - } } diff --git a/extensions/control-plane/api/management-api/asset-api/src/test/java/org/eclipse/edc/connector/api/management/asset/transform/AssetToAssetResponseDtoTransformerTest.java b/extensions/control-plane/api/management-api/asset-api/src/test/java/org/eclipse/edc/connector/api/management/asset/transform/AssetToAssetResponseDtoTransformerTest.java index 9ea5a6830c0..4c213d35f9f 100644 --- a/extensions/control-plane/api/management-api/asset-api/src/test/java/org/eclipse/edc/connector/api/management/asset/transform/AssetToAssetResponseDtoTransformerTest.java +++ b/extensions/control-plane/api/management-api/asset-api/src/test/java/org/eclipse/edc/connector/api/management/asset/transform/AssetToAssetResponseDtoTransformerTest.java @@ -22,7 +22,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; class AssetToAssetResponseDtoTransformerTest { @@ -47,13 +46,4 @@ void transform() { assertThat(assetDto.getId()).isEqualTo(asset.getId()); } - @Test - void transform_nullInput() { - var context = mock(TransformerContext.class); - - var asset = transformer.transform(null, context); - - assertThat(asset).isNull(); - verify(context).reportProblem("input asset is null"); - } } diff --git a/extensions/control-plane/api/management-api/contract-agreement-api/src/main/java/org/eclipse/edc/connector/api/management/contractagreement/transform/ContractAgreementToContractAgreementDtoTransformer.java b/extensions/control-plane/api/management-api/contract-agreement-api/src/main/java/org/eclipse/edc/connector/api/management/contractagreement/transform/ContractAgreementToContractAgreementDtoTransformer.java index f65d311de35..3e94ac5244c 100644 --- a/extensions/control-plane/api/management-api/contract-agreement-api/src/main/java/org/eclipse/edc/connector/api/management/contractagreement/transform/ContractAgreementToContractAgreementDtoTransformer.java +++ b/extensions/control-plane/api/management-api/contract-agreement-api/src/main/java/org/eclipse/edc/connector/api/management/contractagreement/transform/ContractAgreementToContractAgreementDtoTransformer.java @@ -34,7 +34,7 @@ public Class getOutputType() { } @Override - public @Nullable ContractAgreementDto transform(@Nullable ContractAgreement object, @NotNull TransformerContext context) { + public @Nullable ContractAgreementDto transform(@NotNull ContractAgreement object, @NotNull TransformerContext context) { return ContractAgreementDto.Builder.newInstance() .id(object.getId()) .assetId(object.getAssetId()) diff --git a/extensions/control-plane/api/management-api/contract-definition-api/src/main/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionRequestDtoToContractDefinitionTransformer.java b/extensions/control-plane/api/management-api/contract-definition-api/src/main/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionRequestDtoToContractDefinitionTransformer.java index 4470e6da9ac..a9f661dbada 100644 --- a/extensions/control-plane/api/management-api/contract-definition-api/src/main/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionRequestDtoToContractDefinitionTransformer.java +++ b/extensions/control-plane/api/management-api/contract-definition-api/src/main/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionRequestDtoToContractDefinitionTransformer.java @@ -23,7 +23,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Optional; import java.util.stream.Collectors; public class ContractDefinitionRequestDtoToContractDefinitionTransformer implements DtoTransformer { @@ -39,22 +38,15 @@ public Class getOutputType() { } @Override - public @Nullable ContractDefinition transform(@Nullable ContractDefinitionRequestDto object, @NotNull TransformerContext context) { - return Optional.ofNullable(object) - .map(input -> { - var criteria = input.getCriteria().stream().map(it -> context.transform(it, Criterion.class)).collect(Collectors.toList()); - var selectorExpression = AssetSelectorExpression.Builder.newInstance().criteria(criteria).build(); - return ContractDefinition.Builder.newInstance() - .id(input.getId()) - .accessPolicyId(input.getAccessPolicyId()) - .contractPolicyId(input.getContractPolicyId()) - .selectorExpression(selectorExpression) - .validity(input.getValidity()) - .build(); - }) - .orElseGet(() -> { - context.reportProblem("input contract definition is null"); - return null; - }); + public @Nullable ContractDefinition transform(@NotNull ContractDefinitionRequestDto object, @NotNull TransformerContext context) { + var criteria = object.getCriteria().stream().map(it -> context.transform(it, Criterion.class)).collect(Collectors.toList()); + var selectorExpression = AssetSelectorExpression.Builder.newInstance().criteria(criteria).build(); + return ContractDefinition.Builder.newInstance() + .id(object.getId()) + .accessPolicyId(object.getAccessPolicyId()) + .contractPolicyId(object.getContractPolicyId()) + .selectorExpression(selectorExpression) + .validity(object.getValidity()) + .build(); } } diff --git a/extensions/control-plane/api/management-api/contract-definition-api/src/main/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionToContractDefinitionResponseDtoTransformer.java b/extensions/control-plane/api/management-api/contract-definition-api/src/main/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionToContractDefinitionResponseDtoTransformer.java index 8c2d19b7821..44ca25da5cf 100644 --- a/extensions/control-plane/api/management-api/contract-definition-api/src/main/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionToContractDefinitionResponseDtoTransformer.java +++ b/extensions/control-plane/api/management-api/contract-definition-api/src/main/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionToContractDefinitionResponseDtoTransformer.java @@ -22,8 +22,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Optional; - import static java.util.stream.Collectors.toList; public class ContractDefinitionToContractDefinitionResponseDtoTransformer implements DtoTransformer { @@ -39,22 +37,15 @@ public Class getOutputType() { } @Override - public @Nullable ContractDefinitionResponseDto transform(@Nullable ContractDefinition object, @NotNull TransformerContext context) { - return Optional.ofNullable(object) - .map(input -> { - var criteria = object.getSelectorExpression().getCriteria().stream().map(it -> context.transform(it, CriterionDto.class)).collect(toList()); - return ContractDefinitionResponseDto.Builder.newInstance() - .id(object.getId()) - .accessPolicyId(object.getAccessPolicyId()) - .createdAt(object.getCreatedAt()) - .contractPolicyId(object.getContractPolicyId()) - .criteria(criteria) - .validity(input.getValidity()) - .build(); - }) - .orElseGet(() -> { - context.reportProblem("input contract definition is null"); - return null; - }); + public @Nullable ContractDefinitionResponseDto transform(@NotNull ContractDefinition object, @NotNull TransformerContext context) { + var criteria = object.getSelectorExpression().getCriteria().stream().map(it -> context.transform(it, CriterionDto.class)).collect(toList()); + return ContractDefinitionResponseDto.Builder.newInstance() + .id(object.getId()) + .accessPolicyId(object.getAccessPolicyId()) + .createdAt(object.getCreatedAt()) + .contractPolicyId(object.getContractPolicyId()) + .criteria(criteria) + .validity(object.getValidity()) + .build(); } } diff --git a/extensions/control-plane/api/management-api/contract-definition-api/src/test/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionRequestDtoToContractDefinitionTransformerTest.java b/extensions/control-plane/api/management-api/contract-definition-api/src/test/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionRequestDtoToContractDefinitionTransformerTest.java index 5db3c8f1ec1..320107a09a9 100644 --- a/extensions/control-plane/api/management-api/contract-definition-api/src/test/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionRequestDtoToContractDefinitionTransformerTest.java +++ b/extensions/control-plane/api/management-api/contract-definition-api/src/test/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionRequestDtoToContractDefinitionTransformerTest.java @@ -67,14 +67,4 @@ void transform() { verify(context, times(1)).transform(isA(CriterionDto.class), eq(Criterion.class)); } - @Test - void transform_nullInput() { - var context = mock(TransformerContext.class); - - var definition = transformer.transform(null, context); - - assertThat(definition).isNull(); - verify(context).reportProblem("input contract definition is null"); - } - } diff --git a/extensions/control-plane/api/management-api/contract-definition-api/src/test/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionToContractDefinitionResponseDtoTransformerTest.java b/extensions/control-plane/api/management-api/contract-definition-api/src/test/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionToContractDefinitionResponseDtoTransformerTest.java index 739add7478d..312e2f1b01c 100644 --- a/extensions/control-plane/api/management-api/contract-definition-api/src/test/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionToContractDefinitionResponseDtoTransformerTest.java +++ b/extensions/control-plane/api/management-api/contract-definition-api/src/test/java/org/eclipse/edc/connector/api/management/contractdefinition/transform/ContractDefinitionToContractDefinitionResponseDtoTransformerTest.java @@ -65,14 +65,4 @@ void transform() { verify(context).transform(isA(Criterion.class), eq(CriterionDto.class)); } - @Test - void transform_nullInput() { - var context = mock(TransformerContext.class); - - var definition = transformer.transform(null, context); - - assertThat(definition).isNull(); - verify(context).reportProblem("input contract definition is null"); - } - } diff --git a/extensions/control-plane/api/management-api/contract-negotiation-api/src/main/java/org/eclipse/edc/connector/api/management/contractnegotiation/transform/ContractAgreementToContractAgreementDtoTransformer.java b/extensions/control-plane/api/management-api/contract-negotiation-api/src/main/java/org/eclipse/edc/connector/api/management/contractnegotiation/transform/ContractAgreementToContractAgreementDtoTransformer.java index a154b6e9627..58a509ad5fa 100644 --- a/extensions/control-plane/api/management-api/contract-negotiation-api/src/main/java/org/eclipse/edc/connector/api/management/contractnegotiation/transform/ContractAgreementToContractAgreementDtoTransformer.java +++ b/extensions/control-plane/api/management-api/contract-negotiation-api/src/main/java/org/eclipse/edc/connector/api/management/contractnegotiation/transform/ContractAgreementToContractAgreementDtoTransformer.java @@ -34,7 +34,7 @@ public Class getOutputType() { } @Override - public @Nullable ContractAgreementDto transform(@Nullable ContractAgreement object, @NotNull TransformerContext context) { + public @Nullable ContractAgreementDto transform(@NotNull ContractAgreement object, @NotNull TransformerContext context) { return ContractAgreementDto.Builder.newInstance() .id(object.getId()) .assetId(object.getAssetId()) diff --git a/extensions/control-plane/api/management-api/contract-negotiation-api/src/main/java/org/eclipse/edc/connector/api/management/contractnegotiation/transform/ContractNegotiationToContractNegotiationDtoTransformer.java b/extensions/control-plane/api/management-api/contract-negotiation-api/src/main/java/org/eclipse/edc/connector/api/management/contractnegotiation/transform/ContractNegotiationToContractNegotiationDtoTransformer.java index fa1bb01fecd..c48a59c2514 100644 --- a/extensions/control-plane/api/management-api/contract-negotiation-api/src/main/java/org/eclipse/edc/connector/api/management/contractnegotiation/transform/ContractNegotiationToContractNegotiationDtoTransformer.java +++ b/extensions/control-plane/api/management-api/contract-negotiation-api/src/main/java/org/eclipse/edc/connector/api/management/contractnegotiation/transform/ContractNegotiationToContractNegotiationDtoTransformer.java @@ -37,7 +37,7 @@ public Class getOutputType() { } @Override - public @Nullable ContractNegotiationDto transform(@Nullable ContractNegotiation object, @NotNull TransformerContext context) { + public @Nullable ContractNegotiationDto transform(@NotNull ContractNegotiation object, @NotNull TransformerContext context) { return ContractNegotiationDto.Builder.newInstance() .id(object.getId()) .type(object.getType()) diff --git a/extensions/control-plane/api/management-api/contract-negotiation-api/src/main/java/org/eclipse/edc/connector/api/management/contractnegotiation/transform/NegotiationInitiateRequestDtoToDataRequestTransformer.java b/extensions/control-plane/api/management-api/contract-negotiation-api/src/main/java/org/eclipse/edc/connector/api/management/contractnegotiation/transform/NegotiationInitiateRequestDtoToDataRequestTransformer.java index 79ef06ff3eb..fab8baec539 100644 --- a/extensions/control-plane/api/management-api/contract-negotiation-api/src/main/java/org/eclipse/edc/connector/api/management/contractnegotiation/transform/NegotiationInitiateRequestDtoToDataRequestTransformer.java +++ b/extensions/control-plane/api/management-api/contract-negotiation-api/src/main/java/org/eclipse/edc/connector/api/management/contractnegotiation/transform/NegotiationInitiateRequestDtoToDataRequestTransformer.java @@ -26,7 +26,6 @@ import java.net.URI; import java.time.Clock; import java.time.ZonedDateTime; -import java.util.Optional; public class NegotiationInitiateRequestDtoToDataRequestTransformer implements DtoTransformer { @@ -47,32 +46,25 @@ public Class getOutputType() { } @Override - public @Nullable ContractOfferRequest transform(@Nullable NegotiationInitiateRequestDto object, @NotNull TransformerContext context) { - return Optional.ofNullable(object) - .map(input -> { - // TODO: ContractOfferRequest should contain only the contractOfferId and the contract offer should be retrieved from the catalog. Ref #985 - var now = ZonedDateTime.ofInstant(clock.instant(), clock.getZone()); - var contractOffer = ContractOffer.Builder.newInstance() - .id(input.getOffer().getOfferId()) - .asset(Asset.Builder.newInstance().id(input.getOffer().getAssetId()).build()) - // TODO: this is a workaround for the bug described in https://github.com/eclipse-edc/Connector/issues/753 - .consumer(URI.create("urn:connector:consumer")) - .provider(URI.create("urn:connector:provider")) - .policy(input.getOffer().getPolicy()) - .contractStart(now) - .contractEnd(now.plusSeconds(input.getOffer().getValidity())) - .build(); - return ContractOfferRequest.Builder.newInstance() - .connectorId(input.getConnectorId()) - .connectorAddress(input.getConnectorAddress()) - .protocol(input.getProtocol()) - .contractOffer(contractOffer) - .type(ContractOfferRequest.Type.INITIAL) - .build(); - }) - .orElseGet(() -> { - context.reportProblem("input negotiation initiate request is null"); - return null; - }); + public @Nullable ContractOfferRequest transform(@NotNull NegotiationInitiateRequestDto object, @NotNull TransformerContext context) { + // TODO: ContractOfferRequest should contain only the contractOfferId and the contract offer should be retrieved from the catalog. Ref #985 + var now = ZonedDateTime.ofInstant(clock.instant(), clock.getZone()); + var contractOffer = ContractOffer.Builder.newInstance() + .id(object.getOffer().getOfferId()) + .asset(Asset.Builder.newInstance().id(object.getOffer().getAssetId()).build()) + // TODO: this is a workaround for the bug described in https://github.com/eclipse-edc/Connector/issues/753 + .consumer(URI.create("urn:connector:consumer")) + .provider(URI.create("urn:connector:provider")) + .policy(object.getOffer().getPolicy()) + .contractStart(now) + .contractEnd(now.plusSeconds(object.getOffer().getValidity())) + .build(); + return ContractOfferRequest.Builder.newInstance() + .connectorId(object.getConnectorId()) + .connectorAddress(object.getConnectorAddress()) + .protocol(object.getProtocol()) + .contractOffer(contractOffer) + .type(ContractOfferRequest.Type.INITIAL) + .build(); } } diff --git a/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionRequestDtoToPolicyDefinitionTransformer.java b/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionRequestDtoToPolicyDefinitionTransformer.java index 3322327fd2a..20e4de5ec6b 100644 --- a/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionRequestDtoToPolicyDefinitionTransformer.java +++ b/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionRequestDtoToPolicyDefinitionTransformer.java @@ -21,8 +21,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Optional; - public class PolicyDefinitionRequestDtoToPolicyDefinitionTransformer implements DtoTransformer { @Override @@ -36,16 +34,10 @@ public Class getOutputType() { } @Override - public @Nullable PolicyDefinition transform(@Nullable PolicyDefinitionRequestDto object, @NotNull TransformerContext context) { - return Optional.ofNullable(object) - .map(input -> PolicyDefinition.Builder.newInstance() - .id(input.getId()) - .policy(input.getPolicy()) - .build() - ) - .orElseGet(() -> { - context.reportProblem("input policy definition is null"); - return null; - }); + public @Nullable PolicyDefinition transform(@NotNull PolicyDefinitionRequestDto object, @NotNull TransformerContext context) { + return PolicyDefinition.Builder.newInstance() + .id(object.getId()) + .policy(object.getPolicy()) + .build(); } } diff --git a/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionToPolicyDefinitionResponseDtoTransformer.java b/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionToPolicyDefinitionResponseDtoTransformer.java index 282d893217d..5487d6f2f5d 100644 --- a/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionToPolicyDefinitionResponseDtoTransformer.java +++ b/extensions/control-plane/api/management-api/policy-definition-api/src/main/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionToPolicyDefinitionResponseDtoTransformer.java @@ -21,8 +21,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Optional; - public class PolicyDefinitionToPolicyDefinitionResponseDtoTransformer implements DtoTransformer { @Override @@ -36,17 +34,11 @@ public Class getOutputType() { } @Override - public @Nullable PolicyDefinitionResponseDto transform(@Nullable PolicyDefinition object, @NotNull TransformerContext context) { - return Optional.ofNullable(object) - .map(input -> PolicyDefinitionResponseDto.Builder.newInstance() - .id(object.getId()) - .policy(object.getPolicy()) - .createdAt(object.getCreatedAt()) - .build() - ) - .orElseGet(() -> { - context.reportProblem("input contract definition is null"); - return null; - }); + public @Nullable PolicyDefinitionResponseDto transform(@NotNull PolicyDefinition object, @NotNull TransformerContext context) { + return PolicyDefinitionResponseDto.Builder.newInstance() + .id(object.getId()) + .policy(object.getPolicy()) + .createdAt(object.getCreatedAt()) + .build(); } } diff --git a/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionRequestDtoToPolicyDefinitionTransformerTest.java b/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionRequestDtoToPolicyDefinitionTransformerTest.java index 04358900803..117c43da457 100644 --- a/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionRequestDtoToPolicyDefinitionTransformerTest.java +++ b/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionRequestDtoToPolicyDefinitionTransformerTest.java @@ -23,7 +23,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; class PolicyDefinitionRequestDtoToPolicyDefinitionTransformerTest { @@ -51,14 +50,4 @@ void transform() { assertThat(policyDefinition.getCreatedAt()).isNotEqualTo(0L); //should be set automatically } - @Test - void transform_nullInput() { - var context = mock(TransformerContext.class); - - var definition = transformer.transform(null, context); - - assertThat(definition).isNull(); - verify(context).reportProblem("input policy definition is null"); - } - } diff --git a/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionToPolicyDefinitionResponseDtoTransformerTest.java b/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionToPolicyDefinitionResponseDtoTransformerTest.java index 50385406aaf..d400badc998 100644 --- a/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionToPolicyDefinitionResponseDtoTransformerTest.java +++ b/extensions/control-plane/api/management-api/policy-definition-api/src/test/java/org/eclipse/edc/connector/api/management/policy/transform/PolicyDefinitionToPolicyDefinitionResponseDtoTransformerTest.java @@ -23,7 +23,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; class PolicyDefinitionToPolicyDefinitionResponseDtoTransformerTest { @@ -52,14 +51,4 @@ void transform() { assertThat(dto.getCreatedAt()).isEqualTo(10L); } - @Test - void transform_nullInput() { - var context = mock(TransformerContext.class); - - var definition = transformer.transform(null, context); - - assertThat(definition).isNull(); - verify(context).reportProblem("input contract definition is null"); - } - } diff --git a/extensions/control-plane/api/management-api/transfer-process-api/src/main/java/org/eclipse/edc/connector/api/management/transferprocess/transform/DataRequestToDataRequestDtoTransformer.java b/extensions/control-plane/api/management-api/transfer-process-api/src/main/java/org/eclipse/edc/connector/api/management/transferprocess/transform/DataRequestToDataRequestDtoTransformer.java index 2b10d1919a2..90128dbb1c9 100644 --- a/extensions/control-plane/api/management-api/transfer-process-api/src/main/java/org/eclipse/edc/connector/api/management/transferprocess/transform/DataRequestToDataRequestDtoTransformer.java +++ b/extensions/control-plane/api/management-api/transfer-process-api/src/main/java/org/eclipse/edc/connector/api/management/transferprocess/transform/DataRequestToDataRequestDtoTransformer.java @@ -21,8 +21,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; - public class DataRequestToDataRequestDtoTransformer implements DtoTransformer { @Override @@ -36,11 +34,7 @@ public Class getOutputType() { } @Override - public @Nullable DataRequestDto transform(@Nullable DataRequest object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } + public @Nullable DataRequestDto transform(@NotNull DataRequest object, @NotNull TransformerContext context) { return DataRequestDto.Builder.newInstance() .id(object.getId()) .assetId(object.getAssetId()) diff --git a/extensions/control-plane/api/management-api/transfer-process-api/src/main/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferProcessToTransferProcessDtoTransformer.java b/extensions/control-plane/api/management-api/transfer-process-api/src/main/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferProcessToTransferProcessDtoTransformer.java index afd52265a66..facf9109fbd 100644 --- a/extensions/control-plane/api/management-api/transfer-process-api/src/main/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferProcessToTransferProcessDtoTransformer.java +++ b/extensions/control-plane/api/management-api/transfer-process-api/src/main/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferProcessToTransferProcessDtoTransformer.java @@ -37,10 +37,7 @@ public Class getOutputType() { } @Override - public @Nullable TransferProcessDto transform(@Nullable TransferProcess object, @NotNull TransformerContext context) { - if (object == null) { - return null; - } + public @Nullable TransferProcessDto transform(@NotNull TransferProcess object, @NotNull TransformerContext context) { return TransferProcessDto.Builder.newInstance() .id(object.getId()) .type(object.getType().name()) diff --git a/extensions/control-plane/api/management-api/transfer-process-api/src/main/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferRequestDtoToDataRequestTransformer.java b/extensions/control-plane/api/management-api/transfer-process-api/src/main/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferRequestDtoToDataRequestTransformer.java index 8458e7dcbf5..713f0b01be1 100644 --- a/extensions/control-plane/api/management-api/transfer-process-api/src/main/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferRequestDtoToDataRequestTransformer.java +++ b/extensions/control-plane/api/management-api/transfer-process-api/src/main/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferRequestDtoToDataRequestTransformer.java @@ -37,12 +37,7 @@ public Class getOutputType() { } @Override - public @Nullable DataRequest transform(@Nullable TransferRequestDto object, @NotNull TransformerContext context) { - Objects.requireNonNull(context); - if (object == null) { - return null; - } - + public @Nullable DataRequest transform(@NotNull TransferRequestDto object, @NotNull TransformerContext context) { // Generate a DataRequest ID if none is provided (used for idempotency) String id = Objects.requireNonNullElseGet(object.getId(), () -> UUID.randomUUID().toString()); diff --git a/extensions/control-plane/api/management-api/transfer-process-api/src/test/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferProcessToTransferProcessDtoTransformerTest.java b/extensions/control-plane/api/management-api/transfer-process-api/src/test/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferProcessToTransferProcessDtoTransformerTest.java index 0e3df3161b2..84844006be3 100644 --- a/extensions/control-plane/api/management-api/transfer-process-api/src/test/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferProcessToTransferProcessDtoTransformerTest.java +++ b/extensions/control-plane/api/management-api/transfer-process-api/src/test/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferProcessToTransferProcessDtoTransformerTest.java @@ -20,23 +20,23 @@ import org.eclipse.edc.connector.transfer.spi.types.DataRequest; import org.eclipse.edc.connector.transfer.spi.types.TransferProcess; import org.eclipse.edc.connector.transfer.spi.types.TransferProcessStates; -import org.eclipse.edc.spi.result.Result; import org.eclipse.edc.spi.types.domain.DataAddress; import org.junit.jupiter.api.Test; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.edc.connector.transfer.spi.types.TransferProcessStates.UNSAVED; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; class TransferProcessToTransferProcessDtoTransformerTest { - TransferProcessTransformerTestData data = new TransferProcessTransformerTestData(); - TransferProcessToTransferProcessDtoTransformer transformer = new TransferProcessToTransferProcessDtoTransformer(); - List problems = new ArrayList<>(); + private final TransferProcessTransformerTestData data = new TransferProcessTransformerTestData(); + private final TransferProcessToTransferProcessDtoTransformer transformer = new TransferProcessToTransferProcessDtoTransformer(); @Test void getInputType() { @@ -50,20 +50,36 @@ void getOutputType() { @Test void transform() { - assertThatEntityTransformsToDto(); + when(data.context.transform(any(), eq(DataRequestDto.class))).thenReturn(data.dataRequestDto); + + var result = transformer.transform(data.entity.build(), data.context); + + assertThat(result) + .usingRecursiveComparison() + .isEqualTo(data.dto.build()); + verify(data.context, never()).reportProblem(any()); } @Test void transform_whenInvalidState() { + when(data.context.transform(any(), eq(DataRequestDto.class))).thenReturn(data.dataRequestDto); + data.entity.state(invalidStateCode()); data.dto.state(null); - problems.add("Invalid value for TransferProcess.state"); - assertThatEntityTransformsToDto(); + when(data.context.transform(any(), eq(DataRequestDto.class))).thenReturn(data.dataRequestDto); + + var result = transformer.transform(data.entity.build(), data.context); + + assertThat(result) + .usingRecursiveComparison() + .isEqualTo(data.dto.build()); + verify(data.context).reportProblem("Invalid value for TransferProcess.state"); } @Test void transform_whenMinimalData() { + when(data.context.transform(any(), eq(DataRequestDto.class))).thenReturn(data.dataRequestDto); data.dto.state(UNSAVED.name()); data.dataDestination = DataAddress.Builder.newInstance().type(data.dataDestinationType); @@ -84,19 +100,13 @@ void transform_whenMinimalData() { .stateTimestamp(data.stateTimestamp) .errorDetail(null); - assertThatEntityTransformsToDto(); - } - - void assertThatEntityTransformsToDto() { - when(data.registry.transform(data.dataRequest, DataRequestDto.class)).thenReturn(Result.success(data.dataRequestDto)); + when(data.context.transform(any(), eq(DataRequestDto.class))).thenReturn(data.dataRequestDto); var result = transformer.transform(data.entity.build(), data.context); assertThat(result) .usingRecursiveComparison() .isEqualTo(data.dto.build()); - - assertThat(data.context.getProblems()).containsExactlyElementsOf(problems); } private int invalidStateCode() { diff --git a/extensions/control-plane/api/management-api/transfer-process-api/src/test/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferProcessTransformerTestData.java b/extensions/control-plane/api/management-api/transfer-process-api/src/test/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferProcessTransformerTestData.java index 2519bcd5793..be20484c9ed 100644 --- a/extensions/control-plane/api/management-api/transfer-process-api/src/test/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferProcessTransformerTestData.java +++ b/extensions/control-plane/api/management-api/transfer-process-api/src/test/java/org/eclipse/edc/connector/api/management/transferprocess/transform/TransferProcessTransformerTestData.java @@ -14,7 +14,6 @@ package org.eclipse.edc.connector.api.management.transferprocess.transform; -import org.eclipse.edc.api.transformer.DtoTransformerRegistry; import org.eclipse.edc.connector.api.management.transferprocess.model.DataAddressInformationDto; import org.eclipse.edc.connector.api.management.transferprocess.model.DataRequestDto; import org.eclipse.edc.connector.api.management.transferprocess.model.TransferProcessDto; @@ -23,7 +22,6 @@ import org.eclipse.edc.connector.transfer.spi.types.TransferProcessStates; import org.eclipse.edc.spi.types.domain.DataAddress; import org.eclipse.edc.transform.spi.TransformerContext; -import org.eclipse.edc.transform.spi.TransformerContextImpl; import java.util.HashMap; import java.util.Map; @@ -34,8 +32,7 @@ public class TransferProcessTransformerTestData { - DtoTransformerRegistry registry = mock(DtoTransformerRegistry.class); - TransformerContext context = new TransformerContextImpl(registry); + TransformerContext context = mock(TransformerContext.class); String id = UUID.randomUUID().toString(); TransferProcess.Type type = TransferProcess.Type.CONSUMER; TransferProcessStates state = TransferProcessStates.values()[ThreadLocalRandom.current().nextInt(TransferProcessStates.values().length)]; diff --git a/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TransformerContext.java b/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TransformerContext.java index 291afda150e..5ff78525146 100644 --- a/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TransformerContext.java +++ b/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TransformerContext.java @@ -19,7 +19,8 @@ import java.util.List; /** - * A context used to report problems and perform recursive transformation of a type tree. If a problem is reported, the transformation will fail. + * A context used to report problems and perform recursive transformation of a type tree. + * If a problem is reported, the transformation will fail. */ public interface TransformerContext { @@ -39,12 +40,13 @@ public interface TransformerContext { void reportProblem(String problem); /** - * Transforms the object and any contained types, returning its transformed representation or null if the operation cannot be completed. + * Transforms the input object and any contained types, returning its transformed representation or null if the + * operation cannot be completed or input is null. * - * @param object the instance to transform + * @param input the instance to transform * @param the instance type * @param the transformed object type * @return the transformed representation or null */ - @Nullable OUTPUT transform(INPUT object, Class outputType); + @Nullable OUTPUT transform(INPUT input, Class outputType); } diff --git a/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TransformerContextImpl.java b/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TransformerContextImpl.java index 566ab700eef..c143cdc85ce 100644 --- a/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TransformerContextImpl.java +++ b/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TransformerContextImpl.java @@ -44,6 +44,11 @@ public void reportProblem(String problem) { @Override public @Nullable OUTPUT transform(INPUT object, Class outputType) { - return registry.transform(object, outputType).getContent(); + if (object == null) { + return null; + } + + return registry.transformerFor(object, outputType) + .transform(object, this); } } diff --git a/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TypeTransformer.java b/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TypeTransformer.java index 2cfee9df811..925c9d8d559 100644 --- a/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TypeTransformer.java +++ b/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TypeTransformer.java @@ -35,8 +35,9 @@ public interface TypeTransformer { Class getOutputType(); /** - * Transforms the object. + * Transforms the object, the input can never be null. + * Returns null if the transformation failed. */ @Nullable - OUTPUT transform(@Nullable INPUT object, @NotNull TransformerContext context); + OUTPUT transform(@NotNull INPUT input, @NotNull TransformerContext context); } diff --git a/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TypeTransformerRegistry.java b/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TypeTransformerRegistry.java index 07d444ec6d4..37234552117 100644 --- a/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TypeTransformerRegistry.java +++ b/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TypeTransformerRegistry.java @@ -21,19 +21,32 @@ * Generic registry to hold {@link TypeTransformer} objects */ public interface TypeTransformerRegistry> { + /** * Registers a transformer. */ void register(T transformer); /** - * Transforms the object and any contained types, returning its transformed representation or null if the operation cannot be completed. + * Returns a transformer that can transfor input to outputType + * Throws an exception if the transformer cannot be found. + * + * @param input type + * @param output type + * @param input the input object + * @param outputType the output type + * @return a transformer able to transform the input to the output + */ + @NotNull TypeTransformer transformerFor(@NotNull INPUT input, @NotNull Class outputType); + + /** + * Transforms the input and any contained types, returning its transformed representation or null if the operation cannot be completed. * * @param the instance type - * @param the transformed object type - * @param object the instance to transform + * @param the transformed input type + * @param input the instance to transform * @param outputType the transformed output type * @return the transform result */ - Result transform(@NotNull INPUT object, @NotNull Class outputType); + Result transform(@NotNull INPUT input, @NotNull Class outputType); } diff --git a/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TypeTransformerRegistryImpl.java b/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TypeTransformerRegistryImpl.java new file mode 100644 index 00000000000..4b988392eb9 --- /dev/null +++ b/spi/common/transform-spi/src/main/java/org/eclipse/edc/transform/spi/TypeTransformerRegistryImpl.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022 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.transform.spi; + +import org.eclipse.edc.spi.EdcException; +import org.eclipse.edc.spi.result.Result; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import static java.lang.String.format; + +public class TypeTransformerRegistryImpl> implements TypeTransformerRegistry { + + private final List transformers = new ArrayList<>(); + + @Override + public void register(T transformer) { + this.transformers.add(transformer); + } + + @Override + public @NotNull TypeTransformer transformerFor(@NotNull INPUT input, @NotNull Class outputType) { + return transformers.stream() + .filter(t -> t.getInputType().isInstance(input) && t.getOutputType().equals(outputType)) + .findAny() + .map(it -> (TypeTransformer) it) + .orElseThrow(() -> new EdcException(format("No Transformer registered that can handle %s -> %s", input.getClass(), outputType))); + } + + @Override + public Result transform(@NotNull INPUT input, @NotNull Class outputType) { + Objects.requireNonNull(input); + + var context = new TransformerContextImpl(this); + + var result = context.transform(input, outputType); + if (context.hasProblems()) { + return Result.failure(context.getProblems()); + } else { + return Result.success(result); + } + } +} diff --git a/spi/common/transform-spi/src/test/java/org/eclipse/edc/transform/spi/StringIntegerTypeTransformer.java b/spi/common/transform-spi/src/test/java/org/eclipse/edc/transform/spi/StringIntegerTypeTransformer.java new file mode 100644 index 00000000000..cf996331093 --- /dev/null +++ b/spi/common/transform-spi/src/test/java/org/eclipse/edc/transform/spi/StringIntegerTypeTransformer.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022 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.transform.spi; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import static java.lang.String.format; + +class StringIntegerTypeTransformer implements TypeTransformer { + + @Override + public Class getInputType() { + return String.class; + } + + @Override + public Class getOutputType() { + return Integer.class; + } + + @Override + public @Nullable Integer transform(@NotNull String object, @NotNull TransformerContext context) { + try { + return Integer.valueOf(object); + } catch (Exception e) { + context.reportProblem(format("String %s cannot be transformed to integer: %s", object, e.getMessage())); + return null; + } + + } +} diff --git a/spi/common/transform-spi/src/test/java/org/eclipse/edc/transform/spi/TransformerContextImplTest.java b/spi/common/transform-spi/src/test/java/org/eclipse/edc/transform/spi/TransformerContextImplTest.java new file mode 100644 index 00000000000..584a677caf5 --- /dev/null +++ b/spi/common/transform-spi/src/test/java/org/eclipse/edc/transform/spi/TransformerContextImplTest.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2022 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.transform.spi; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +class TransformerContextImplTest { + + private final TypeTransformerRegistry registry = mock(TypeTransformerRegistry.class); + private final TransformerContextImpl context = new TransformerContextImpl(registry); + + @Test + void shouldReturnTransformedInput() { + when(registry.transformerFor(anyString(), eq(Integer.class))).thenReturn(new StringIntegerTypeTransformer()); + + var result = context.transform("5", Integer.class); + + assertThat(result).isEqualTo(5); + } + + @Test + void shouldCollectProblems_whenTransformFails() { + when(registry.transformerFor(anyString(), eq(Integer.class))).thenReturn(new StringIntegerTypeTransformer()); + + var result = context.transform("not an integer", Integer.class); + + assertThat(result).isEqualTo(null); + assertThat(context.getProblems()).hasSize(1); + } + + @Test + void shouldNotTransform_whenInputIsNull() { + var result = context.transform(null, Integer.class); + + assertThat(result).isNull(); + verifyNoInteractions(registry); + } +} diff --git a/spi/common/transform-spi/src/test/java/org/eclipse/edc/transform/spi/TypeTransformerRegistryImplTest.java b/spi/common/transform-spi/src/test/java/org/eclipse/edc/transform/spi/TypeTransformerRegistryImplTest.java new file mode 100644 index 00000000000..b2f8654c5e7 --- /dev/null +++ b/spi/common/transform-spi/src/test/java/org/eclipse/edc/transform/spi/TypeTransformerRegistryImplTest.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2022 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.transform.spi; + +import org.eclipse.edc.spi.EdcException; +import org.eclipse.edc.spi.result.Result; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; + +public class TypeTransformerRegistryImplTest { + + private final TypeTransformerRegistryImpl> registry = new TypeTransformerRegistryImpl<>(); + + @BeforeEach + void setUp() { + registry.register(new StringIntegerTypeTransformer()); + } + + @Test + void transformerFor_shouldReturnTheCorrectTransformer() { + var transformer = registry.transformerFor("a string", Integer.class); + + assertThat(transformer).isInstanceOf(StringIntegerTypeTransformer.class); + } + + @Test + void transformerFor_shouldThrowExceptionWhenTransformerDoesNotExist() { + var notString = 4L; + assertThatThrownBy(() -> registry.transformerFor(notString, Integer.class)).isInstanceOf(EdcException.class); + assertThatThrownBy(() -> registry.transformerFor(String.class, Long.class)).isInstanceOf(EdcException.class); + assertThatThrownBy(() -> registry.transformerFor(notString, Integer.class)).isInstanceOf(EdcException.class); + assertThatThrownBy(() -> registry.transformerFor(notString, Float.class)).isInstanceOf(EdcException.class); + } + + @Test + void transform_shouldSucceed_whenInputAndOutputTypesAreHandledByRegisteredTransformer() { + var result = registry.transform("5", Integer.class); + + assertThat(result).matches(Result::succeeded).extracting(Result::getContent).isEqualTo(5); + } + + @Test + void transform_shouldFail_whenTransformerFails() { + var result = registry.transform("not an integer", Integer.class); + + assertThat(result).matches(Result::failed).extracting(Result::getFailureMessages).asList().hasSize(1); + } + + @Test + void transform_shouldThrowException_whenTransformerIsNotFound() { + assertThatThrownBy(() -> registry.transform(3, String.class)).isInstanceOf(EdcException.class); + } + + @Test + void transform_shouldThrowException_whenInputIsNull() { + assertThatThrownBy(() -> registry.transform(null, Integer.class)).isInstanceOf(NullPointerException.class); + } + +}