use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider in project vorto by eclipse.
the class JsonMappingLoadTest method init.
@BeforeClass
public static void init() {
// // Test Case 1: No converter functions
testCaseOneMapper = IDataMapper.newBuilder().withSpecification(new SpecWithNestedEntity()).build();
testCaseOneJsonInput = new String[] { "{\"temperature\" : 21.3 }", "{\"temperature\" : 0.1 }", "{\"temperature\" : 11 }" };
testCaseOneOutput = new Double[] { 21.3, 0.1, 11.0 };
deserializer = new JSONDeserializer();
// // Test Case 2: One Built in Converter
testCaseTwoMapper = IDataMapper.newBuilder().withSpecification(new SpecWithTimestamp()).registerConverterFunction(DateFunctionFactory.createFunctions()).build();
timestamp = new Date();
testCaseTwoJson = "{\"time\" : " + timestamp.getTime() + "}";
// // Test Case 3: One Built in Converter + 1 Javascript Function
testCaseThreeMapper = IDataMapper.newBuilder().withSpecification(new SpecWithCustomFunction()).registerConverterFunction(TypeFunctionFactory.createFunctions()).registerConverterFunction(StringFunctionFactory.createFunctions()).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
testCaseThreeJson = "{\"clickType\" : \"DOUBLE\"}";
testCaseThreeJsonInput = new String[] { "{\"clickType\" : \"SINGLE\"}", "{\"clickType\" : \"DOUBLE\"}", "{\"clickType\" : \"\"}" };
testCaseThreeOutput = new Integer[] { 1, 2, 99 };
}
use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider in project vorto by eclipse.
the class JsonMappingTest method testMapDevicePayloadWithInitialValue.
@Test
public void testMapDevicePayloadWithInitialValue() {
IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithCustomFunction()).registerConverterFunction(TypeFunctionFactory.createFunctions()).registerConverterFunction(StringFunctionFactory.createFunctions()).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
String json = "{\"clickType\" : \"DOUBLE\", \"batteryVoltage\": \"0mV\"}";
InfomodelValue mappedOutput = mapper.mapSource(gson.fromJson(json, Object.class));
FunctionblockValue buttonFunctionblockData = mappedOutput.get("button");
assertEquals(true, (Boolean) buttonFunctionblockData.getStatusProperty("digital_input_state").get().getValue());
assertEquals(2, buttonFunctionblockData.getStatusProperty("digital_input_count").get().getValue());
FunctionblockValue voltageFunctionblockData = mappedOutput.get("voltage");
assertEquals(0f, voltageFunctionblockData.getStatusProperty("sensor_value").get().getValue());
assertEquals("mV", voltageFunctionblockData.getStatusProperty("sensor_units").get().getValue());
System.out.println(mappedOutput);
}
use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider in project vorto by eclipse.
the class BinaryMappingTest method testMappingWithBinary.
@Test
public void testMappingWithBinary() throws Exception {
IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithByteArrayConverter()).registerConverterFunction(StringFunctionFactory.createFunctions()).registerConverterFunction(TypeFunctionFactory.createFunctions()).registerConverterFunction(BinaryFunctionFactory.createFunctions()).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
String x = "4f00630063007500700061006e0063007900200002";
String json = "{\"data\" : \"" + x + "\"}";
InfomodelValue mappedDittoOutput = mapper.mapSource(gson.fromJson(json, Object.class));
FunctionblockValue button = mappedDittoOutput.get("button");
assertEquals(2, button.getStatusProperty("sensor_value").get().getValue());
System.out.println(mappedDittoOutput);
}
use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider in project vorto by eclipse.
the class JsonMappingTest method testMapNestedEntityWithCustomFunction.
@Test
public void testMapNestedEntityWithCustomFunction() {
IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithNestedEntityAndCustomFunction()).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
String json = "{\"clickType\" : \"DOUBLE\"}";
InfomodelValue mappedOutput = mapper.mapSource(gson.fromJson(json, Object.class));
FunctionblockValue buttonFunctionblockData = mappedOutput.get("button");
assertEquals(2, ((EntityPropertyValue) buttonFunctionblockData.getStatusProperty("count").get()).getValue().getPropertyValue("value").get().getValue());
System.out.println(gson.toJson(mappedOutput.serialize()));
}
use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider in project vorto by eclipse.
the class JsonMappingTest method testConfigMapping.
@Test
public void testConfigMapping() throws Exception {
IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithConfigMapping()).registerConverterFunction(TypeFunctionFactory.createFunctions()).registerConverterFunction(StringFunctionFactory.createFunctions()).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
String json = "{\"clickType\" : \"DOUBLE\", \"batteryVoltage\": \"2322mV\"}";
InfomodelValue mappedOutput = mapper.mapSource(gson.fromJson(json, Object.class));
System.out.println(mappedOutput);
}
Aggregations