diff --git a/src/main/java/me/steffenjacobs/jsonmatcher/service/MessageApplicationService.java b/src/main/java/me/steffenjacobs/jsonmatcher/service/MessageApplicationService.java new file mode 100644 index 0000000..b1610ad --- /dev/null +++ b/src/main/java/me/steffenjacobs/jsonmatcher/service/MessageApplicationService.java @@ -0,0 +1,55 @@ +package me.steffenjacobs.jsonmatcher.service; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.json.JSONObject; + +import me.steffenjacobs.jsonmatcher.domain.MappingDTO; + +/** @author Steffen Jacobs */ +public class MessageApplicationService { + + /*** + * applies the model to a given source object and performs e.g. unit + * transformation + */ + public String applyModelToJson(String source, Set> model) { + + final Map> modelBySourceKey = new HashMap<>(); + for (MappingDTO dto : model) { + modelBySourceKey.put(dto.getKeySource(), dto); + } + + final JSONObject json = new JSONObject(source); + + final StringBuilder sb = new StringBuilder(); + sb.append("{"); + int count = 0; + for (String key : json.keySet()) { + @SuppressWarnings("rawtypes") + MappingDTO m = modelBySourceKey.get(key); + Object value = json.get(key); + @SuppressWarnings("unchecked") + Object result = m.getValueTransformation().apply(value); + sb.append("\""); + sb.append(m.getKeyTarget()); + sb.append("\": "); + if (result instanceof String) { + sb.append("\""); + sb.append(result); + sb.append("\""); + } else { + sb.append(result); + } + count++; + if (count < json.length()) { + sb.append(", "); + } + + } + sb.append("}"); + return sb.toString(); + } +} diff --git a/src/main/java/me/steffenjacobs/jsonmatcher/service/matcher/ValueMatcher.java b/src/main/java/me/steffenjacobs/jsonmatcher/service/matcher/ValueMatcher.java index d4b54e5..9437cae 100644 --- a/src/main/java/me/steffenjacobs/jsonmatcher/service/matcher/ValueMatcher.java +++ b/src/main/java/me/steffenjacobs/jsonmatcher/service/matcher/ValueMatcher.java @@ -15,8 +15,6 @@ public class ValueMatcher { public Function matchValues(Object source, Object target) { - // TODO handle unit in string and do conversion - // same types if (source.getClass() == target.getClass()) { diff --git a/src/test/java/me/steffenjacobs/jsonmatcher/TestMapperWithBigList.java b/src/test/java/me/steffenjacobs/jsonmatcher/TestMapperWithBigList.java index dde3dc9..3ffd1ce 100644 --- a/src/test/java/me/steffenjacobs/jsonmatcher/TestMapperWithBigList.java +++ b/src/test/java/me/steffenjacobs/jsonmatcher/TestMapperWithBigList.java @@ -71,7 +71,7 @@ public void testMapperWithBigList() throws IOException, URISyntaxException { testForList(new TestDataGenerator().generateSampled()); } - private boolean isMappingAllowed(MappingDTO mapping) { + private boolean isMappingAllowed(MappingDTO mapping) throws NullPointerException{ return allowedMappings.get(mapping.getKeySource()).contains(mapping.getKeyTarget()); } @@ -100,7 +100,7 @@ public void testForList(List lines) { println(printingService.mappingToString(line, line2, mapping, SHOW_JSON)); try { assertTrue(isMappingAllowed(mapping)); - } catch (AssertionError error) { + } catch (AssertionError | NullPointerException error) { countErrors++; if (PRINT_ERRORS) { System.out.println("Bad mapping: " + mapping.getKeySource() + " -> " + mapping.getKeyTarget()); diff --git a/src/test/java/me/steffenjacobs/jsonmatcher/TestMessageApplicationService.java b/src/test/java/me/steffenjacobs/jsonmatcher/TestMessageApplicationService.java new file mode 100644 index 0000000..a7fecea --- /dev/null +++ b/src/test/java/me/steffenjacobs/jsonmatcher/TestMessageApplicationService.java @@ -0,0 +1,55 @@ +package me.steffenjacobs.jsonmatcher; + +import java.util.HashSet; +import java.util.Set; +import java.util.function.Function; + +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import me.steffenjacobs.jsonmatcher.domain.MappingDTO; +import me.steffenjacobs.jsonmatcher.service.MessageApplicationService; +import me.steffenjacobs.jsonmatcher.service.matcher.ValueMatcher; + +/** @author Steffen Jacobs */ +public class TestMessageApplicationService { + + private static MessageApplicationService messageApplicationService; + private static ValueMatcher valueMatcher; + + @BeforeClass + public static void setup() { + messageApplicationService = new MessageApplicationService(); + valueMatcher = new ValueMatcher(); + } + + @Test + public void testApplyModelToJson() { + + final String exemplaryJsonSource = "{\"t\": \"23.3°C\", \"hum\": 22}"; + // expected: "{\"temperature\": \"73.94°F\", \"humidity\": \"22%\"}"; + + // celsius to fahrenheit conversion formula + final Function convertCelsiusToFahrenheit = t -> t * 1.8 + 32; + + // create mappings + final MappingDTO mapTemperature = new MappingDTO<>("t", "temperature", + t -> convertCelsiusToFahrenheit.apply(valueMatcher.findNumberWithUnit(t).getA()) + "°F", 1); + final MappingDTO mapHumidity = new MappingDTO<>("hum", "humidity", h -> h + "%", 1); + + final Set> mappings = new HashSet<>(); + mappings.add(mapTemperature); + mappings.add(mapHumidity); + + // execute mapping + String result = messageApplicationService.applyModelToJson(exemplaryJsonSource, mappings); + + // check if mapping is correct + JSONObject jsonResult = new JSONObject(result); + Assert.assertEquals(2, jsonResult.length()); + Assert.assertEquals("73.94°F", jsonResult.get("temperature")); + Assert.assertEquals("22%", jsonResult.get("humidity")); + } +}