From 7a990b25ca696bf0f76e5a8ee3f3308bb3c53d6d Mon Sep 17 00:00:00 2001 From: "Krzysztof Massalski (Extern)" Date: Wed, 27 Mar 2024 13:12:02 +0100 Subject: [PATCH] feat(impl):[#359] add new helper class --- .../eclipse/tractusx/irs/WiremockSupport.java | 21 +------- .../DecentralDigitalTwinRegistryClient.java | 33 +++--------- .../util/SerializationHelper.java | 52 +++++++++++++++++++ 3 files changed, 60 insertions(+), 46 deletions(-) create mode 100644 irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/util/SerializationHelper.java diff --git a/irs-api/src/test/java/org/eclipse/tractusx/irs/WiremockSupport.java b/irs-api/src/test/java/org/eclipse/tractusx/irs/WiremockSupport.java index cd65821189..33fe3a36c4 100644 --- a/irs-api/src/test/java/org/eclipse/tractusx/irs/WiremockSupport.java +++ b/irs-api/src/test/java/org/eclipse/tractusx/irs/WiremockSupport.java @@ -34,17 +34,12 @@ import static org.eclipse.tractusx.irs.util.TestMother.batchAspectName; import static org.eclipse.tractusx.irs.util.TestMother.singleLevelBomAsBuiltAspectName; -import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.List; import java.util.Map; import java.util.UUID; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; import org.eclipse.edc.spi.types.domain.edr.EndpointDataReference; import org.eclipse.tractusx.irs.component.PartChainIdentificationKey; import org.eclipse.tractusx.irs.component.RegisterJob; @@ -53,6 +48,7 @@ import org.eclipse.tractusx.irs.data.StringMapper; import org.eclipse.tractusx.irs.edc.client.configuration.JsonLdConfiguration; import org.eclipse.tractusx.irs.edc.client.model.EDRAuthCode; +import org.eclipse.tractusx.irs.registryclient.util.SerializationHelper; import org.eclipse.tractusx.irs.semanticshub.SemanticHubWireMockSupport; import org.eclipse.tractusx.irs.testing.wiremock.DiscoveryServiceWiremockSupport; import org.eclipse.tractusx.irs.testing.wiremock.DtrWiremockSupport; @@ -108,20 +104,7 @@ static String encodedAssetIds(final String assetIds) { .name("globalAssetId") .value(assetIds) .build(); - return Base64.getEncoder().encodeToString(serialize(globalAssetId)); - } - - private static byte[] serialize(final IdentifierKeyValuePair assetIds) { - final ByteArrayOutputStream stream = new ByteArrayOutputStream(); - try { - final ObjectMapper mapper = new ObjectMapper(); - mapper.enable(SerializationFeature.INDENT_OUTPUT); - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - mapper.writeValue(stream, assetIds); - return stream.toByteArray(); - } catch (final IOException e) { - return new byte[0]; - } + return Base64.getEncoder().encodeToString(new SerializationHelper().serialize(globalAssetId)); } static void verifyDiscoveryCalls(final int times) { diff --git a/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryClient.java b/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryClient.java index 6247c5e2be..2ee2e339f9 100644 --- a/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryClient.java +++ b/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/decentral/DecentralDigitalTwinRegistryClient.java @@ -23,21 +23,16 @@ ********************************************************************************/ package org.eclipse.tractusx.irs.registryclient.decentral; -import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.List; import java.util.Map; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; -import com.fasterxml.jackson.databind.SerializationFeature; import io.github.resilience4j.retry.annotation.Retry; import org.eclipse.edc.spi.types.domain.edr.EndpointDataReference; import org.eclipse.tractusx.irs.component.assetadministrationshell.AssetAdministrationShellDescriptor; import org.eclipse.tractusx.irs.component.assetadministrationshell.IdentifierKeyValuePair; +import org.eclipse.tractusx.irs.registryclient.util.SerializationHelper; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -54,18 +49,12 @@ public class DecentralDigitalTwinRegistryClient { private static final String PLACEHOLDER_AAS_IDENTIFIER = "aasIdentifier"; private static final String PLACEHOLDER_ASSET_IDS = "assetIds"; - private static final ObjectWriter WRITER; - static { - final ObjectMapper mapper = new ObjectMapper(); - mapper.enable(SerializationFeature.INDENT_OUTPUT); - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - WRITER = mapper.writer(); - } - private final RestTemplate edcRestTemplate; private final String shellDescriptorTemplate; private final String lookupShellsTemplate; + private final SerializationHelper serializationHelper = new SerializationHelper(); + public DecentralDigitalTwinRegistryClient(final RestTemplate edcRestTemplate, @Value("${digitalTwinRegistry.shellDescriptorTemplate:}") final String shellDescriptorTemplate, @Value("${digitalTwinRegistry.lookupShellsTemplate:}") final String lookupShellsTemplate) { @@ -95,22 +84,12 @@ public LookupShellsResponse getAllAssetAdministrationShellIdsByAssetLink( new HttpEntity<>(null, headers(endpointDataReference)), LookupShellsResponse.class).getBody(); } - private static String encodeWithBase64(final String aasIdentifier) { + private String encodeWithBase64(final String aasIdentifier) { return Base64.getEncoder().encodeToString(aasIdentifier.getBytes(StandardCharsets.UTF_8)); } - private static String encodeWithBase64(final IdentifierKeyValuePair assetIds) { - return Base64.getEncoder().encodeToString(serialize(assetIds)); - } - - private static byte[] serialize(final IdentifierKeyValuePair assetIds) { - final ByteArrayOutputStream stream = new ByteArrayOutputStream(); - try { - WRITER.writeValue(stream, assetIds); - return stream.toByteArray(); - } catch (final IOException e) { - return new byte[0]; - } + private String encodeWithBase64(final IdentifierKeyValuePair assetIds) { + return Base64.getEncoder().encodeToString(serializationHelper.serialize(assetIds)); } private HttpHeaders headers(final EndpointDataReference dataReference) { diff --git a/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/util/SerializationHelper.java b/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/util/SerializationHelper.java new file mode 100644 index 0000000000..ee05e8593b --- /dev/null +++ b/irs-registry-client/src/main/java/org/eclipse/tractusx/irs/registryclient/util/SerializationHelper.java @@ -0,0 +1,52 @@ +/******************************************************************************** + * Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * 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. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.irs.registryclient.util; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.fasterxml.jackson.databind.SerializationFeature; + +/** + * Serializer helper + */ +public class SerializationHelper { + + private static final ObjectWriter WRITER; + static { + final ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + WRITER = mapper.writer(); + } + + public byte[] serialize(final Object object) { + final ByteArrayOutputStream stream = new ByteArrayOutputStream(); + try { + WRITER.writeValue(stream, object); + return stream.toByteArray(); + } catch (final IOException e) { + return new byte[0]; + } + } +}