Search in sources :

Example 1 with JavascriptEvalProvider

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 };
}
Also used : SpecWithTimestamp(org.eclipse.vorto.mapping.engine.converter.date.SpecWithTimestamp) SpecWithNestedEntity(org.eclipse.vorto.service.mapping.spec.SpecWithNestedEntity) JSONDeserializer(org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer) SpecWithCustomFunction(org.eclipse.vorto.mapping.engine.converter.javascript.SpecWithCustomFunction) Date(java.util.Date) JavascriptEvalProvider(org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider) BeforeClass(org.junit.BeforeClass)

Example 2 with JavascriptEvalProvider

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);
}
Also used : FunctionblockValue(org.eclipse.vorto.model.runtime.FunctionblockValue) IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) JavascriptEvalProvider(org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider) Test(org.junit.Test)

Example 3 with JavascriptEvalProvider

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);
}
Also used : FunctionblockValue(org.eclipse.vorto.model.runtime.FunctionblockValue) IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) JavascriptEvalProvider(org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider) Test(org.junit.Test)

Example 4 with JavascriptEvalProvider

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()));
}
Also used : FunctionblockValue(org.eclipse.vorto.model.runtime.FunctionblockValue) EntityPropertyValue(org.eclipse.vorto.model.runtime.EntityPropertyValue) IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) JavascriptEvalProvider(org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider) Test(org.junit.Test)

Example 5 with JavascriptEvalProvider

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);
}
Also used : IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) JavascriptEvalProvider(org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider) Test(org.junit.Test)

Aggregations

JavascriptEvalProvider (org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider)15 IDataMapper (org.eclipse.vorto.mapping.engine.IDataMapper)13 Test (org.junit.Test)13 InfomodelValue (org.eclipse.vorto.model.runtime.InfomodelValue)10 FunctionblockValue (org.eclipse.vorto.model.runtime.FunctionblockValue)7 BinaryData (org.eclipse.vorto.mapping.engine.model.binary.BinaryData)3 BeforeClass (org.junit.BeforeClass)2 Gson (com.google.gson.Gson)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 SpecBinaryConverter (org.eclipse.vorto.mapping.engine.converter.binary.SpecBinaryConverter)1 SpecWithBase64Converter (org.eclipse.vorto.mapping.engine.converter.binary.SpecWithBase64Converter)1 SpecWithByteArrayConverter (org.eclipse.vorto.mapping.engine.converter.binary.SpecWithByteArrayConverter)1 SpecWithTimestamp (org.eclipse.vorto.mapping.engine.converter.date.SpecWithTimestamp)1 SpecWithCustomFunction (org.eclipse.vorto.mapping.engine.converter.javascript.SpecWithCustomFunction)1 JSONDeserializer (org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer)1 GattCharacteristic (org.eclipse.vorto.mapping.engine.model.blegatt.GattCharacteristic)1 GattDevice (org.eclipse.vorto.mapping.engine.model.blegatt.GattDevice)1 GattService (org.eclipse.vorto.mapping.engine.model.blegatt.GattService)1 EntityPropertyValue (org.eclipse.vorto.model.runtime.EntityPropertyValue)1