Search in sources :

Example 1 with JSONDeserializer

use of org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer 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 JSONDeserializer

use of org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer in project vorto by eclipse.

the class MappingSpecJsonReaderTest method testMappingFromJson.

@Test
public void testMappingFromJson() {
    IMappingSpecification spec = IMappingSpecification.newBuilder().fromInputStream(MappingSpecJsonReaderTest.class.getClassLoader().getResourceAsStream("mappingspec.json")).build();
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(spec).build();
    String json = "{\"t\" : 50}";
    IPayloadDeserializer deserializer = new JSONDeserializer();
    InfomodelValue mappedOutput = mapper.mapSource(deserializer.deserialize(json));
    assertEquals(50.0, ((EntityPropertyValue) mappedOutput.get("outdoorTemperature").getStatusProperty("value").get()).getValue().getPropertyValue("value").get().getValue());
}
Also used : IMappingSpecification(org.eclipse.vorto.mapping.engine.model.spec.IMappingSpecification) EntityPropertyValue(org.eclipse.vorto.model.runtime.EntityPropertyValue) 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) Test(org.junit.Test)

Example 3 with JSONDeserializer

use of org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer in project vorto by eclipse.

the class DataMapperTest method testMappingWithInfoModelUsingSameFunctionblock.

@Test
public void testMappingWithInfoModelUsingSameFunctionblock() throws Exception {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithSameFunctionblock()).build();
    String json = "{\"btnvalue1\" : 2, \"btnvalue2\": 10}";
    IPayloadDeserializer deserializer = new JSONDeserializer();
    InfomodelValue mappedOutput = mapper.mapSource(deserializer.deserialize(json));
    FunctionblockValue buttonFunctionblockData = mappedOutput.get("btn1");
    assertEquals(2.0, buttonFunctionblockData.getStatusProperty("sensor_value").get().getValue());
    FunctionblockValue button2FunctionblockData = mappedOutput.get("btn2");
    assertEquals(10.0, button2FunctionblockData.getStatusProperty("sensor_value").get().getValue());
    System.out.println(mappedOutput);
}
Also used : FunctionblockValue(org.eclipse.vorto.model.runtime.FunctionblockValue) IPayloadDeserializer(org.eclipse.vorto.mapping.engine.decoder.IPayloadDeserializer) IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) SpecWithSameFunctionblock(org.eclipse.vorto.service.mapping.spec.SpecWithSameFunctionblock) JSONDeserializer(org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) Test(org.junit.Test)

Example 4 with JSONDeserializer

use of org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer in project vorto by eclipse.

the class DataMapperTest method testMappingUsingListInput.

@Test
public void testMappingUsingListInput() throws Exception {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithArrayPayload()).build();
    String json = "[{\"clickType\" : \"DOUBLE\" }, {\"clickType\" : \"SINGLE\" }]";
    IPayloadDeserializer deserializer = new JSONDeserializer();
    InfomodelValue mappedOutput = mapper.mapSource(deserializer.deserialize(json));
    FunctionblockValue buttonFunctionblockData = mappedOutput.get("button");
    assertEquals("DOUBLE", buttonFunctionblockData.getStatusProperty("sensor_value").get().getValue());
    System.out.println(mappedOutput);
}
Also used : FunctionblockValue(org.eclipse.vorto.model.runtime.FunctionblockValue) SpecWithArrayPayload(org.eclipse.vorto.service.mapping.spec.SpecWithArrayPayload) 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) Test(org.junit.Test)

Example 5 with JSONDeserializer

use of org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer in project vorto by eclipse.

the class DataMapperTest method testMappingWithEntityUnmapped.

@Test
public void testMappingWithEntityUnmapped() throws Exception {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithTwoFunctionblocksWithNestedEntity()).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());
    assertNull(mappedOutput.get("humidity"));
}
Also used : EntityPropertyValue(org.eclipse.vorto.model.runtime.EntityPropertyValue) IPayloadDeserializer(org.eclipse.vorto.mapping.engine.decoder.IPayloadDeserializer) IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) SpecWithTwoFunctionblocksWithNestedEntity(org.eclipse.vorto.service.mapping.spec.SpecWithTwoFunctionblocksWithNestedEntity) JSONDeserializer(org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) Test(org.junit.Test)

Aggregations

JSONDeserializer (org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer)11 InfomodelValue (org.eclipse.vorto.model.runtime.InfomodelValue)10 Test (org.junit.Test)10 IDataMapper (org.eclipse.vorto.mapping.engine.IDataMapper)9 IPayloadDeserializer (org.eclipse.vorto.mapping.engine.decoder.IPayloadDeserializer)9 EntityPropertyValue (org.eclipse.vorto.model.runtime.EntityPropertyValue)3 FunctionblockValue (org.eclipse.vorto.model.runtime.FunctionblockValue)3 IMappingSpecification (org.eclipse.vorto.mapping.engine.model.spec.IMappingSpecification)2 SpecWithNestedEntity (org.eclipse.vorto.service.mapping.spec.SpecWithNestedEntity)2 Date (java.util.Date)1 JavascriptEvalProvider (org.eclipse.vorto.mapping.engine.converter.JavascriptEvalProvider)1 SpecWithTimestamp (org.eclipse.vorto.mapping.engine.converter.date.SpecWithTimestamp)1 SpecWithCustomFunction (org.eclipse.vorto.mapping.engine.converter.javascript.SpecWithCustomFunction)1 SpecWithArrayPayload (org.eclipse.vorto.service.mapping.spec.SpecWithArrayPayload)1 SpecWithConditionalProperties (org.eclipse.vorto.service.mapping.spec.SpecWithConditionalProperties)1 SpecWithConditionedRules (org.eclipse.vorto.service.mapping.spec.SpecWithConditionedRules)1 SpecWithNestedEnum (org.eclipse.vorto.service.mapping.spec.SpecWithNestedEnum)1 SpecWithPropertyConditionXpath (org.eclipse.vorto.service.mapping.spec.SpecWithPropertyConditionXpath)1 SpecWithSameFunctionblock (org.eclipse.vorto.service.mapping.spec.SpecWithSameFunctionblock)1 SpecWithTwoFunctionblocksWithNestedEntity (org.eclipse.vorto.service.mapping.spec.SpecWithTwoFunctionblocksWithNestedEntity)1