Skip to content

Commit

Permalink
feat(impl): [eclipse-tractusx#542] add a mapFromBase64String method w…
Browse files Browse the repository at this point in the history
…ith TypeReference to StringMapper
  • Loading branch information
dsmf committed Jul 18, 2024
1 parent eedb766 commit 749a52a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.Base64;
import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;
Expand Down Expand Up @@ -72,13 +74,13 @@ void shouldThrowParseExceptionWhenMappingFromString() {
}

@Test
void shouldMapFromStringUsingTypeReference() {
void shouldMapFromBase64StringUsingTypeReference() {

// ARRANGE
final TypeReference<List<Policy>> listOfPoliciesType = new TypeReference<>() {
};

final String originalJson = """
final String originalJsonStr = """
[{
"policyId": "default-trace-policy",
"createdOn": "2024-07-17T16:15:14.12345678Z",
Expand Down Expand Up @@ -108,10 +110,12 @@ void shouldMapFromStringUsingTypeReference() {
]
}]
""";
final String originalJsonBase64 = new String(
Base64.getEncoder().encode(originalJsonStr.getBytes(StandardCharsets.UTF_8)));

// ACT
// convert back andConstraints forth to facilitate comparison
final List<Policy> listOfPolicies = StringMapper.mapFromString(originalJson, listOfPoliciesType);
final List<Policy> listOfPolicies = StringMapper.mapFromBase64String(originalJsonBase64, listOfPoliciesType);
final String backToString = StringMapper.mapToString(listOfPolicies);
final List<Policy> backToObj = StringMapper.mapFromString(backToString, listOfPoliciesType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
********************************************************************************/
package org.eclipse.tractusx.irs.data;

import java.util.Base64;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
Expand All @@ -33,6 +35,7 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
* Maps objects to strings and vice-versa.
Expand Down Expand Up @@ -65,6 +68,10 @@ public static <T> T mapFromString(final String value, final Class<T> clazz) {
}
}

public static <T> T mapFromBase64String(final String value, final TypeReference<T> typeReference) {
return mapFromString(new String(Base64.getDecoder().decode(StringUtils.trim(value))), typeReference);
}

public static <T> T mapFromString(final String value, final TypeReference<T> typeReference) {
try {
return MAPPER.readValue(value, typeReference);
Expand Down

0 comments on commit 749a52a

Please sign in to comment.