Skip to content

Commit 7c9304a

Browse files
committed
added MessageApplicationService which can apply a given model to a given JSON String and generate a target JSON String
1 parent c9b623c commit 7c9304a

File tree

4 files changed

+112
-4
lines changed

4 files changed

+112
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package me.steffenjacobs.jsonmatcher.service;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Set;
6+
7+
import org.json.JSONObject;
8+
9+
import me.steffenjacobs.jsonmatcher.domain.MappingDTO;
10+
11+
/** @author Steffen Jacobs */
12+
public class MessageApplicationService {
13+
14+
/***
15+
* applies the model to a given source object and performs e.g. unit
16+
* transformation
17+
*/
18+
public String applyModelToJson(String source, Set<MappingDTO<?, ?>> model) {
19+
20+
final Map<String, MappingDTO<?, ?>> modelBySourceKey = new HashMap<>();
21+
for (MappingDTO<?, ?> dto : model) {
22+
modelBySourceKey.put(dto.getKeySource(), dto);
23+
}
24+
25+
final JSONObject json = new JSONObject(source);
26+
27+
final StringBuilder sb = new StringBuilder();
28+
sb.append("{");
29+
int count = 0;
30+
for (String key : json.keySet()) {
31+
@SuppressWarnings("rawtypes")
32+
MappingDTO m = modelBySourceKey.get(key);
33+
Object value = json.get(key);
34+
@SuppressWarnings("unchecked")
35+
Object result = m.getValueTransformation().apply(value);
36+
sb.append("\"");
37+
sb.append(m.getKeyTarget());
38+
sb.append("\": ");
39+
if (result instanceof String) {
40+
sb.append("\"");
41+
sb.append(result);
42+
sb.append("\"");
43+
} else {
44+
sb.append(result);
45+
}
46+
count++;
47+
if (count < json.length()) {
48+
sb.append(", ");
49+
}
50+
51+
}
52+
sb.append("}");
53+
return sb.toString();
54+
}
55+
}

src/main/java/me/steffenjacobs/jsonmatcher/service/matcher/ValueMatcher.java

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ public class ValueMatcher {
1515

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

18-
// TODO handle unit in string and do conversion
19-
2018
// same types
2119
if (source.getClass() == target.getClass()) {
2220

src/test/java/me/steffenjacobs/jsonmatcher/TestMapperWithBigList.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void testMapperWithBigList() throws IOException, URISyntaxException {
7171
testForList(new TestDataGenerator().generateSampled());
7272
}
7373

74-
private boolean isMappingAllowed(MappingDTO<Object, Object> mapping) {
74+
private boolean isMappingAllowed(MappingDTO<Object, Object> mapping) throws NullPointerException{
7575
return allowedMappings.get(mapping.getKeySource()).contains(mapping.getKeyTarget());
7676
}
7777

@@ -100,7 +100,7 @@ public void testForList(List<String> lines) {
100100
println(printingService.mappingToString(line, line2, mapping, SHOW_JSON));
101101
try {
102102
assertTrue(isMappingAllowed(mapping));
103-
} catch (AssertionError error) {
103+
} catch (AssertionError | NullPointerException error) {
104104
countErrors++;
105105
if (PRINT_ERRORS) {
106106
System.out.println("Bad mapping: " + mapping.getKeySource() + " -> " + mapping.getKeyTarget());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package me.steffenjacobs.jsonmatcher;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
import java.util.function.Function;
6+
7+
import org.json.JSONObject;
8+
import org.junit.Assert;
9+
import org.junit.BeforeClass;
10+
import org.junit.Test;
11+
12+
import me.steffenjacobs.jsonmatcher.domain.MappingDTO;
13+
import me.steffenjacobs.jsonmatcher.service.MessageApplicationService;
14+
import me.steffenjacobs.jsonmatcher.service.matcher.ValueMatcher;
15+
16+
/** @author Steffen Jacobs */
17+
public class TestMessageApplicationService {
18+
19+
private static MessageApplicationService messageApplicationService;
20+
private static ValueMatcher valueMatcher;
21+
22+
@BeforeClass
23+
public static void setup() {
24+
messageApplicationService = new MessageApplicationService();
25+
valueMatcher = new ValueMatcher();
26+
}
27+
28+
@Test
29+
public void testApplyModelToJson() {
30+
31+
final String exemplaryJsonSource = "{\"t\": \"23.3°C\", \"hum\": 22}";
32+
// expected: "{\"temperature\": \"73.94°F\", \"humidity\": \"22%\"}";
33+
34+
// celsius to fahrenheit conversion formula
35+
final Function<Double, Double> convertCelsiusToFahrenheit = t -> t * 1.8 + 32;
36+
37+
// create mappings
38+
final MappingDTO<String, String> mapTemperature = new MappingDTO<>("t", "temperature",
39+
t -> convertCelsiusToFahrenheit.apply(valueMatcher.findNumberWithUnit(t).getA()) + "°F", 1);
40+
final MappingDTO<Integer, String> mapHumidity = new MappingDTO<>("hum", "humidity", h -> h + "%", 1);
41+
42+
final Set<MappingDTO<? extends Object, ? extends Object>> mappings = new HashSet<>();
43+
mappings.add(mapTemperature);
44+
mappings.add(mapHumidity);
45+
46+
// execute mapping
47+
String result = messageApplicationService.applyModelToJson(exemplaryJsonSource, mappings);
48+
49+
// check if mapping is correct
50+
JSONObject jsonResult = new JSONObject(result);
51+
Assert.assertEquals(2, jsonResult.length());
52+
Assert.assertEquals("73.94°F", jsonResult.get("temperature"));
53+
Assert.assertEquals("22%", jsonResult.get("humidity"));
54+
}
55+
}

0 commit comments

Comments
 (0)