-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added MessageApplicationService which can apply a given model to a gi…
…ven JSON String and generate a target JSON String
- Loading branch information
1 parent
c9b623c
commit 7c9304a
Showing
4 changed files
with
112 additions
and
4 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/java/me/steffenjacobs/jsonmatcher/service/MessageApplicationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/test/java/me/steffenjacobs/jsonmatcher/TestMessageApplicationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |