Search in sources :

Example 21 with IDataMapper

use of org.eclipse.vorto.mapping.engine.IDataMapper in project vorto by eclipse.

the class JsonMappingTest method testMappingWithMalicousScriptUsingJavaImports.

@Test(expected = MappingException.class)
public void testMappingWithMalicousScriptUsingJavaImports() throws Exception {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithMaliciousFunction() {

        @Override
        protected String getMaliciousFunctionBody() {
            return "load('https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.8.0/highlight.min.js')";
        }
    }).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
    String json = "{\"clickType\" : \"DOUBLE\", \"batteryVoltage\": \"2322mV\"}";
    mapper.mapSource(gson.fromJson(json, Object.class));
}
Also used : IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) JavascriptEvalProvider(org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider) Test(org.junit.Test)

Example 22 with IDataMapper

use of org.eclipse.vorto.mapping.engine.IDataMapper in project vorto by eclipse.

the class JsonMappingTest method testMappingWithMalicousScript.

@Test(expected = MappingException.class)
public void testMappingWithMalicousScript() throws Exception {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithMaliciousFunction() {

        @Override
        protected String getMaliciousFunctionBody() {
            return "return quit();";
        }
    }).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
    String json = "{\"clickType\" : \"DOUBLE\", \"batteryVoltage\": \"2322mV\"}";
    mapper.mapSource(gson.fromJson(json, Object.class));
}
Also used : IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) JavascriptEvalProvider(org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider) Test(org.junit.Test)

Example 23 with IDataMapper

use of org.eclipse.vorto.mapping.engine.IDataMapper in project vorto by eclipse.

the class JsonMappingTest method testInvokeJsFunctionWithSingleByteArrayParam.

@Test
public void testInvokeJsFunctionWithSingleByteArrayParam() {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithArrayType()).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
    BinaryData data = new BinaryData();
    data.setData("Hallo".getBytes());
    InfomodelValue mappedOutput = mapper.mapSource(data);
    FunctionblockValue buttonFunctionblockData = mappedOutput.get("button");
    assertEquals("H", buttonFunctionblockData.getStatusProperty("flag").get().getValue());
}
Also used : FunctionblockValue(org.eclipse.vorto.model.runtime.FunctionblockValue) IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) BinaryData(org.eclipse.vorto.mapping.engine.model.binary.BinaryData) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) JavascriptEvalProvider(org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider) Test(org.junit.Test)

Example 24 with IDataMapper

use of org.eclipse.vorto.mapping.engine.IDataMapper in project vorto by eclipse.

the class DataMapperTest method testMappingWithEntity.

@Test
public void testMappingWithEntity() throws Exception {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithNestedEntity()).build();
    final String sampleDeviceData = "{\"temperature\" : 20.3 }";
    IPayloadDeserializer deserializer = new JSONDeserializer();
    InfomodelValue mappedOutput = mapper.mapSource(deserializer.deserialize(sampleDeviceData));
    EntityPropertyValue temperatureValue = (EntityPropertyValue) mappedOutput.get("outdoorTemperature").getStatusProperty("value").get();
    assertEquals(20.3, temperatureValue.getValue().getPropertyValue("value").get().getValue());
}
Also used : EntityPropertyValue(org.eclipse.vorto.model.runtime.EntityPropertyValue) IPayloadDeserializer(org.eclipse.vorto.mapping.engine.decoder.IPayloadDeserializer) IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) SpecWithNestedEntity(org.eclipse.vorto.service.mapping.spec.SpecWithNestedEntity) JSONDeserializer(org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) Test(org.junit.Test)

Example 25 with IDataMapper

use of org.eclipse.vorto.mapping.engine.IDataMapper in project vorto by eclipse.

the class DataMapperTest method testMapWithSimpleCondition.

@Test
public void testMapWithSimpleCondition() throws Exception {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithConditionalProperties()).build();
    String json = "{\"count\" : 2 }";
    IPayloadDeserializer deserializer = new JSONDeserializer();
    InfomodelValue mappedOutput = mapper.mapSource(deserializer.deserialize(json));
    assertFalse(mappedOutput.get("button").getStatusProperty("sensor_value").isPresent());
    assertEquals(2.0, mappedOutput.get("button").getStatusProperty("sensor_value2").get().getValue());
    json = "{\"count\" : 0 }";
    mappedOutput = mapper.mapSource(deserializer.deserialize(json));
    assertEquals(0.0, mappedOutput.get("button").getStatusProperty("sensor_value").get().getValue());
    assertFalse(mappedOutput.get("button").getStatusProperty("sensor_value2").isPresent());
}
Also used : IPayloadDeserializer(org.eclipse.vorto.mapping.engine.decoder.IPayloadDeserializer) IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) JSONDeserializer(org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) SpecWithConditionalProperties(org.eclipse.vorto.service.mapping.spec.SpecWithConditionalProperties) Test(org.junit.Test)

Aggregations

IDataMapper (org.eclipse.vorto.mapping.engine.IDataMapper)30 Test (org.junit.Test)30 InfomodelValue (org.eclipse.vorto.model.runtime.InfomodelValue)25 JavascriptEvalProvider (org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider)13 FunctionblockValue (org.eclipse.vorto.model.runtime.FunctionblockValue)12 IPayloadDeserializer (org.eclipse.vorto.mapping.engine.decoder.IPayloadDeserializer)10 JSONDeserializer (org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer)9 IMappingSpecification (org.eclipse.vorto.mapping.engine.model.spec.IMappingSpecification)4 EntityPropertyValue (org.eclipse.vorto.model.runtime.EntityPropertyValue)4 ClassFunction (org.eclipse.vorto.mapping.engine.functions.ClassFunction)2 BinaryData (org.eclipse.vorto.mapping.engine.model.binary.BinaryData)2 PropertyValue (org.eclipse.vorto.model.runtime.PropertyValue)2 SpecWithConfiguration (org.eclipse.vorto.service.mapping.spec.SpecWithConfiguration)2 Gson (com.google.gson.Gson)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 CSVDeserializer (org.eclipse.vorto.mapping.engine.decoder.CSVDeserializer)1 GattCharacteristic (org.eclipse.vorto.mapping.engine.model.blegatt.GattCharacteristic)1 GattDevice (org.eclipse.vorto.mapping.engine.model.blegatt.GattDevice)1