Skip to content

Commit

Permalink
added MessageApplicationService which can apply a given model to a gi…
Browse files Browse the repository at this point in the history
…ven JSON String and generate a target JSON String
  • Loading branch information
steffenjacobs committed Dec 2, 2018
1 parent c9b623c commit 7c9304a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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<MappingDTO<?, ?>> model) {

final Map<String, MappingDTO<?, ?>> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public class ValueMatcher {

public Function<Object, Object> matchValues(Object source, Object target) {

// TODO handle unit in string and do conversion

// same types
if (source.getClass() == target.getClass()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void testMapperWithBigList() throws IOException, URISyntaxException {
testForList(new TestDataGenerator().generateSampled());
}

private boolean isMappingAllowed(MappingDTO<Object, Object> mapping) {
private boolean isMappingAllowed(MappingDTO<Object, Object> mapping) throws NullPointerException{
return allowedMappings.get(mapping.getKeySource()).contains(mapping.getKeyTarget());
}

Expand Down Expand Up @@ -100,7 +100,7 @@ public void testForList(List<String> 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Double, Double> convertCelsiusToFahrenheit = t -> t * 1.8 + 32;

// create mappings
final MappingDTO<String, String> mapTemperature = new MappingDTO<>("t", "temperature",
t -> convertCelsiusToFahrenheit.apply(valueMatcher.findNumberWithUnit(t).getA()) + "°F", 1);
final MappingDTO<Integer, String> mapHumidity = new MappingDTO<>("hum", "humidity", h -> h + "%", 1);

final Set<MappingDTO<? extends Object, ? extends Object>> 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"));
}
}

0 comments on commit 7c9304a

Please sign in to comment.