From eedb7668d63dc01739949b071d97bc3e6bf43a47 Mon Sep 17 00:00:00 2001 From: Matthias Fischer Date: Wed, 17 Jul 2024 19:03:41 +0200 Subject: [PATCH] feat(impl): [#542] add a mapFromString method with TypeReference to StringMapper reason: this allows type-safe mapping to List --- .../tractusx/irs/util/StringMapperTest.java | 91 ++++++++++++++++++- .../tractusx/irs/data/StringMapper.java | 9 ++ 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/irs-api/src/test/java/org/eclipse/tractusx/irs/util/StringMapperTest.java b/irs-api/src/test/java/org/eclipse/tractusx/irs/util/StringMapperTest.java index 4120d778cb..62028a5d30 100644 --- a/irs-api/src/test/java/org/eclipse/tractusx/irs/util/StringMapperTest.java +++ b/irs-api/src/test/java/org/eclipse/tractusx/irs/util/StringMapperTest.java @@ -26,10 +26,20 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import java.time.OffsetDateTime; +import java.util.List; + +import com.fasterxml.jackson.core.type.TypeReference; import org.eclipse.tractusx.irs.component.Bpn; import org.eclipse.tractusx.irs.component.Description; import org.eclipse.tractusx.irs.data.JsonParseException; import org.eclipse.tractusx.irs.data.StringMapper; +import org.eclipse.tractusx.irs.edc.client.policy.Constraint; +import org.eclipse.tractusx.irs.edc.client.policy.Constraints; +import org.eclipse.tractusx.irs.edc.client.policy.OperatorType; +import org.eclipse.tractusx.irs.edc.client.policy.Permission; +import org.eclipse.tractusx.irs.edc.client.policy.Policy; +import org.eclipse.tractusx.irs.edc.client.policy.PolicyType; import org.junit.jupiter.api.Test; class StringMapperTest { @@ -44,8 +54,7 @@ void mapToString() { @Test void shouldThrowParseExceptionWhenMappingToString() { final var object = new Object(); - assertThatThrownBy(() -> StringMapper.mapToString(object)).isInstanceOf( - JsonParseException.class); + assertThatThrownBy(() -> StringMapper.mapToString(object)).isInstanceOf(JsonParseException.class); } @Test @@ -61,4 +70,82 @@ void shouldThrowParseExceptionWhenMappingFromString() { assertThatThrownBy(() -> StringMapper.mapFromString("test", Description.class)).isInstanceOf( JsonParseException.class); } + + @Test + void shouldMapFromStringUsingTypeReference() { + + // ARRANGE + final TypeReference> listOfPoliciesType = new TypeReference<>() { + }; + + final String originalJson = """ + [{ + "policyId": "default-trace-policy", + "createdOn": "2024-07-17T16:15:14.12345678Z", + "validUntil": "9999-01-01T00:00:00.00000000Z", + "permissions": [ + { + "action": "use", + "constraint": { + "and": [ + { + "leftOperand": "https://w3id.org/catenax/policy/FrameworkAgreement", + "operator": { + "@id": "eq" + }, + "rightOperand": "traceability:1.0" + }, + { + "leftOperand": "https://w3id.org/catenax/policy/UsagePurpose", + "operator": { + "@id": "eq" + }, + "rightOperand": "cx.core.industrycore:1" + } + ] + } + } + ] + }] + """; + + // ACT + // convert back andConstraints forth to facilitate comparison + final List listOfPolicies = StringMapper.mapFromString(originalJson, listOfPoliciesType); + final String backToString = StringMapper.mapToString(listOfPolicies); + final List backToObj = StringMapper.mapFromString(backToString, listOfPoliciesType); + + // ASSERT + { + assertThat(listOfPolicies).hasSize(1); + assertThat(backToObj).hasSize(1); + assertThat(backToObj).usingRecursiveComparison().isEqualTo(listOfPolicies); + + final Policy policy = listOfPolicies.get(0); + assertThat(policy.getPolicyId()).isEqualTo("default-trace-policy"); + assertThat(policy.getValidUntil()).isEqualTo(OffsetDateTime.parse("9999-01-01T00:00:00.00000000Z")); + assertThat(policy.getCreatedOn()).isEqualTo(OffsetDateTime.parse("2024-07-17T16:15:14.12345678Z")); + assertThat(policy.getPermissions()).hasSize(1); + + final Permission permission = policy.getPermissions().get(0); + assertThat(permission.getAction()).isEqualTo(PolicyType.USE); + + final Constraints constraints = permission.getConstraint(); + final List andConstraints = constraints.getAnd(); + assertThat(andConstraints).hasSize(2); + { + final Constraint constraint = andConstraints.get(0); + assertThat(constraint.getLeftOperand()).isEqualTo("https://w3id.org/catenax/policy/FrameworkAgreement"); + assertThat(constraint.getOperator().getOperatorType()).isEqualTo(OperatorType.EQ); + assertThat(constraint.getRightOperand()).isEqualTo("traceability:1.0"); + } + { + final Constraint constraint = andConstraints.get(1); + assertThat(constraint.getLeftOperand()).isEqualTo("https://w3id.org/catenax/policy/UsagePurpose"); + assertThat(constraint.getOperator().getOperatorType()).isEqualTo(OperatorType.EQ); + assertThat(constraint.getRightOperand()).isEqualTo("cx.core.industrycore:1"); + } + } + } + } \ No newline at end of file diff --git a/irs-models/src/main/java/org/eclipse/tractusx/irs/data/StringMapper.java b/irs-models/src/main/java/org/eclipse/tractusx/irs/data/StringMapper.java index 9f60be9b4c..65e2f2e19f 100644 --- a/irs-models/src/main/java/org/eclipse/tractusx/irs/data/StringMapper.java +++ b/irs-models/src/main/java/org/eclipse/tractusx/irs/data/StringMapper.java @@ -24,6 +24,7 @@ package org.eclipse.tractusx.irs.data; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @@ -64,4 +65,12 @@ public static T mapFromString(final String value, final Class clazz) { } } + public static T mapFromString(final String value, final TypeReference typeReference) { + try { + return MAPPER.readValue(value, typeReference); + } catch (final JsonProcessingException e) { + throw new JsonParseException(e); + } + } + }