Search in sources :

Example 6 with JSONDeserializer

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

the class PayloadMappingSpecificationTest method testRunMappingTest.

@Test
public void testRunMappingTest() {
    importModel("payloadmapping/org.eclipse.vorto_Voltage_1.0.0.fbmodel");
    importModel("payloadmapping/org.eclipse.vorto_Battery_1.0.0.fbmodel");
    importModel("payloadmapping/org.eclipse.vorto_MyDevice_1.0.0.infomodel");
    IMappingSpecification specification = mappingService.getOrCreateSpecification(ModelId.fromPrettyFormat("org.eclipse.vorto:MyDevice:1.0.0"));
    specification.getFunctionBlock("battery").getStatusProperty("remainingCapacity").get().getStereotype(Stereotype.SOURCE).get().setAttributes(createXpathRule("/cap"));
    specification.getFunctionBlock("battery").getStatusProperty("value").get().getStereotype(Stereotype.SOURCE).get().setAttributes(createXpathRule("/voltage"));
    JSONDeserializer deserializer = new JSONDeserializer();
    InfomodelValue testResult = mappingService.runTest(specification, deserializer.deserialize("{\"cap\": 23 ,\"voltage\": 2242.2 }"));
    assertEquals(23.0, testResult.get("battery").getStatusProperty("remainingCapacity").get().getValue());
    assertEquals(2242.2, testResult.get("battery").getStatusProperty("value").get().getValue());
}
Also used : IMappingSpecification(org.eclipse.vorto.mapping.engine.model.spec.IMappingSpecification) JSONDeserializer(org.eclipse.vorto.mapping.engine.decoder.JSONDeserializer) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) Test(org.junit.Test)

Example 7 with JSONDeserializer

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

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

Example 9 with JSONDeserializer

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

the class DataMapperTest method testMapWithJxpathCondition.

@Test
public void testMapWithJxpathCondition() throws Exception {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithPropertyConditionXpath()).build();
    String json = "{\"data\" : [{\"id\": 100,\"value\": \"x\"},{\"id\": 200,\"value\": \"y\"}]}";
    IPayloadDeserializer deserializer = new JSONDeserializer();
    InfomodelValue mappedOutput = mapper.mapSource(deserializer.deserialize(json));
    assertEquals(100.0, mappedOutput.get("button").getStatusProperty("sensor_value").get().getValue());
}
Also used : SpecWithPropertyConditionXpath(org.eclipse.vorto.service.mapping.spec.SpecWithPropertyConditionXpath) 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 10 with JSONDeserializer

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

the class DataMapperTest method testMapSingleFunctionblockOfInfomodel2.

@Test
public void testMapSingleFunctionblockOfInfomodel2() {
    IDataMapper mapper = IDataMapper.newBuilder().withSpecification(new SpecWithConditionedRules()).build();
    final String sampleHomeConnectRESTResponse = "{\"data\" : { \"key\" : \"DoorState\", \"value\" : \"Locked\"}}";
    IPayloadDeserializer deserializer = new JSONDeserializer();
    InfomodelValue mappedOutput = mapper.mapSource(deserializer.deserialize(sampleHomeConnectRESTResponse));
    System.out.println(mappedOutput);
    assertNull(mappedOutput.get("operationState"));
    FunctionblockValue doorStateFunctionblockData = mappedOutput.get("doorState");
    assertEquals("Locked", (String) doorStateFunctionblockData.getStatusProperty("sensor_value").get().getValue());
}
Also used : FunctionblockValue(org.eclipse.vorto.model.runtime.FunctionblockValue) IPayloadDeserializer(org.eclipse.vorto.mapping.engine.decoder.IPayloadDeserializer) IDataMapper(org.eclipse.vorto.mapping.engine.IDataMapper) SpecWithConditionedRules(org.eclipse.vorto.service.mapping.spec.SpecWithConditionedRules) 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