Search in sources :

Example 11 with JavascriptEvalProvider

use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider in project vorto by eclipse.

the class JsonMappingTest method testInvokeJsFunctionWithMultipleParams.

@Test
public void testInvokeJsFunctionWithMultipleParams() {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithMultipleParams()).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
    MyData data = new MyData("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) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) JavascriptEvalProvider(org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider) Test(org.junit.Test)

Example 12 with JavascriptEvalProvider

use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider 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 13 with JavascriptEvalProvider

use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider 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 14 with JavascriptEvalProvider

use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider 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 15 with JavascriptEvalProvider

use of org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider in project vorto by eclipse.

the class BinaryMappingLoadTest method init.

@BeforeClass
public static void init() {
    // Test Case 1
    testCaseOneMapper = IDataMapper.newBuilder().withSpecification(new SpecWithBase64Converter()).registerConverterFunction(BinaryFunctionFactory.createFunctions()).build();
    testCaseOneJson = "{\"data\" : \"" + Base64.encodeBase64String("20".getBytes()) + "\"}";
    // Test Case 2
    testCaseTwoMapper = IDataMapper.newBuilder().withSpecification(new SpecBinaryConverter()).registerConverterFunction(BinaryFunctionFactory.createFunctions()).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
    // 2 byte temperature (Byte 1-2), 2 byte humidity (Byte 3-4)
    byte[] dest = new byte[4];
    byte[] value = Conversion.intToByteArray(2000, 0, dest, 0, 2);
    value = Conversion.intToByteArray(8819, 0, dest, 2, 2);
    testCaseTwoInput = new BinaryData(value);
    // Test Case 3
    testCaseThreeMapper = IDataMapper.newBuilder().withSpecification(new SpecWithByteArrayConverter()).registerConverterFunction(StringFunctionFactory.createFunctions()).registerConverterFunction(TypeFunctionFactory.createFunctions()).registerConverterFunction(BinaryFunctionFactory.createFunctions()).registerScriptEvalProvider(new JavascriptEvalProvider()).build();
    String x = "4f00630063007500700061006e0063007900200002";
    testCaseThreeJson = "{\"data\" : \"" + x + "\"}";
}
Also used : BinaryData(org.eclipse.vorto.mapping.engine.model.binary.BinaryData) SpecWithByteArrayConverter(org.eclipse.vorto.mapping.engine.converter.binary.SpecWithByteArrayConverter) SpecWithBase64Converter(org.eclipse.vorto.mapping.engine.converter.binary.SpecWithBase64Converter) SpecBinaryConverter(org.eclipse.vorto.mapping.engine.converter.binary.SpecBinaryConverter) JavascriptEvalProvider(org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider) BeforeClass(org.junit.BeforeClass)

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