Skip to content

Commit

Permalink
feat(impl): [eclipse-tractusx#542] add a mapFromString method with Ty…
Browse files Browse the repository at this point in the history
…peReference to StringMapper

reason: this allows type-safe mapping to List
  • Loading branch information
dsmf committed Jul 17, 2024
1 parent 5ca63e5 commit eedb766
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -61,4 +70,82 @@ void shouldThrowParseExceptionWhenMappingFromString() {
assertThatThrownBy(() -> StringMapper.mapFromString("test", Description.class)).isInstanceOf(
JsonParseException.class);
}

@Test
void shouldMapFromStringUsingTypeReference() {

// ARRANGE
final TypeReference<List<Policy>> 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<Policy> listOfPolicies = StringMapper.mapFromString(originalJson, listOfPoliciesType);
final String backToString = StringMapper.mapToString(listOfPolicies);
final List<Policy> 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<Constraint> 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");
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,4 +65,12 @@ public static <T> T mapFromString(final String value, final Class<T> clazz) {
}
}

public static <T> T mapFromString(final String value, final TypeReference<T> typeReference) {
try {
return MAPPER.readValue(value, typeReference);
} catch (final JsonProcessingException e) {
throw new JsonParseException(e);
}
}

}

0 comments on commit eedb766

Please sign in to comment.