Skip to content

Commit

Permalink
Add some test
Browse files Browse the repository at this point in the history
Signed-off-by: Guido Grune <g.grune@datainmotion.com>
  • Loading branch information
gg-dim committed Apr 30, 2024
1 parent 7116491 commit a4543ad
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@

import java.lang.reflect.InvocationTargetException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Optional;

import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.sensinact.core.annotation.dto.Data;
import org.eclipse.sensinact.core.annotation.dto.Metadata;
import org.eclipse.sensinact.core.annotation.dto.Model;
import org.eclipse.sensinact.core.annotation.dto.ModelPackageUri;
import org.eclipse.sensinact.core.annotation.dto.Provider;
Expand Down Expand Up @@ -75,8 +77,7 @@ void stop(@InjectService GatewayThread gt) throws InvocationTargetException, Int
gt.execute(new AbstractTwinCommand<Void>() {
@Override
protected Promise<Void> call(SensinactDigitalTwin twin, PromiseFactory pf) {
Optional.ofNullable(twin.getProvider(PROVIDER))
.ifPresent(SensinactProvider::delete);
Optional.ofNullable(twin.getProvider(PROVIDER)).ifPresent(SensinactProvider::delete);
return pf.resolved(null);
}
}).getValue();
Expand Down Expand Up @@ -459,28 +460,50 @@ void testMiddleFails() throws Exception {
assertEquals(RESOURCE, due.getResource());
}
}



@Nested
public class EMFBasedDTO {


private static final String RESOURCE = "v1";
private static final String SERVICE = "temp";
private static final String DYNAMIC_PROVIDER = "DynamicProvider";

@ModelPackageUri(TestdataPackage.eNS_URI)
@Model("DynamicTestSensor")
@Provider(PROVIDER)
@Service("temp")
@Provider(DYNAMIC_PROVIDER)
@Service(SERVICE)
public class TestModelDTO {
@Resource("v1")
@Resource(RESOURCE)
@Data
public String data;

@Metadata
public String units;
@Timestamp(ChronoUnit.MILLIS)
public long timestamp;
}

@Provider(DYNAMIC_PROVIDER)
@Service("blub")
public class TestModelDTO2 {

@Model
EClass providerEClass = TestdataPackage.Literals.TEST_SENSOR;

// @Service
// EClass service = TestdataPackage.Literals.TEST_TEMPERATUR;

@Service
EReference serviceRef = TestdataPackage.Literals.TEST_SENSOR__TEMP;

@Resource(RESOURCE)
@Data
public String data;

@Timestamp(ChronoUnit.MILLIS)
public long timestamp;
}

@Test
void basic() throws Exception {

TestSensor sensor = TestdataFactory.eINSTANCE.createTestSensor();
TestAdmin admin = TestdataFactory.eINSTANCE.createTestAdmin();
admin.setTestAdmin("meta1");
Expand All @@ -489,71 +512,73 @@ void basic() throws Exception {
sensor.setTemp(temp);
sensor.setId(PROVIDER);
sensor.setAdmin(admin);

Promise<?> update = push.pushUpdate(sensor);
assertNull(update.getFailure());
Object value = getResourceValue("TestSensor", "temp", "v1");

assertEquals("12 °C", value);

// Throwable failure = update.getFailure();
// assertNotNull(failure);
// failure.printStackTrace();
// AbstractUpdateDto abstractUpdateDto = updates.get(0 );
// System.out.println(abstractUpdateDto);

assertEquals("12 °C", getResourceValue("TestSensor", PROVIDER, SERVICE, RESOURCE));

temp.setV1("13 °C");
update = push.pushUpdate(sensor);
assertNull(update.getFailure());
assertEquals("13 °C", getResourceValue("TestSensor", PROVIDER, SERVICE, RESOURCE));
}

@Test
void serviceOnly() throws Exception {
// TestSensor sensor = TestdataFactory.eINSTANCE.createTestSensor();
// TestAdmin admin = TestdataFactory.eINSTANCE.createTestAdmin();
// admin.setTestAdmin("meta1");
TestSensor sensor = TestdataFactory.eINSTANCE.createTestSensor();
TestAdmin admin = TestdataFactory.eINSTANCE.createTestAdmin();
admin.setTestAdmin("meta1");
TestTemperatur temp = TestdataFactory.eINSTANCE.createTestTemperatur();
temp.setV1("12 °C");
// sensor.setTemp(temp);
// sensor.setId(PROVIDER);
// sensor.setAdmin(admin);
Promise<?> update = push.pushUpdate(temp);
sensor.setTemp(temp);
sensor.setId(PROVIDER);
sensor.setAdmin(admin);

Promise<?> update = push.pushUpdate(sensor);
assertNull(update.getFailure());
Object value = getResourceValue(PROVIDER, "temp", "v1");

assertEquals("12 °C", value);
assertEquals("12 °C", getResourceValue("TestSensor", PROVIDER, SERVICE, RESOURCE));

TestTemperatur temp2 = TestdataFactory.eINSTANCE.createTestTemperatur();
temp2.setV1("13 °C");

update = push.pushUpdate(temp2);
assertNull(update.getFailure());
assertEquals("13 °C", getResourceValue("TestSensor", PROVIDER, SERVICE, RESOURCE));
}

@Test
void dynamic() throws Exception {
void dynamicUpdateDTO() throws Exception {
DynamicTestSensor sensor = TestdataFactory.eINSTANCE.createDynamicTestSensor();
sensor.setId(PROVIDER);
sensor.setId(DYNAMIC_PROVIDER);
TestTemperatur temp = TestdataFactory.eINSTANCE.createTestTemperatur();
temp.setV1("12 °C");

EMap<String, org.eclipse.sensinact.model.core.provider.Service> services = sensor.getServices();
services.put("temp", temp);
services.put(SERVICE, temp);
Promise<?> update = push.pushUpdate(sensor);
assertNull(update.getFailure());
assertEquals("12 °C", getResourceValue("DynamicTestSensor", "temp", "v1"));
assertEquals("12 °C", getResourceValue("DynamicTestSensor", DYNAMIC_PROVIDER, SERVICE, RESOURCE));

TestModelDTO dto = new TestModelDTO();
dto.data = "13 °C";

dto.timestamp = Instant.now().toEpochMilli();

update = push.pushUpdate(dto);
Throwable t = update.getFailure();
t.printStackTrace();
assertNull(t);
assertEquals("13 °C", getResourceValue("TestSensor", "temp", "v1"));
assertEquals("13 °C", getResourceValue("DynamicTestSensor", DYNAMIC_PROVIDER, SERVICE, RESOURCE));
}



private Object getResourceValue(String model,String service, String resource) throws InvocationTargetException, InterruptedException {
Object value = gt.execute(new ResourceCommand<Object>(TestdataPackage.eNS_URI, model, PROVIDER, service, resource) {

@Override
protected Promise<Object> call(SensinactResource resource, PromiseFactory pf) {
return resource.getValue().map(t -> t.getValue());
}
}).getValue();
private Object getResourceValue(String model, String provider, String service, String resource)
throws InvocationTargetException, InterruptedException {
Object value = gt
.execute(new ResourceCommand<Object>(TestdataPackage.eNS_URI, model, provider, service, resource) {

@Override
protected Promise<Object> call(SensinactResource resource, PromiseFactory pf) {
return resource.getValue().map(t -> t.getValue());
}
}).getValue();
return value;
}
}
Expand Down
4 changes: 3 additions & 1 deletion core/models/testdata/src/main/resources/model/testdata.ecore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<details key="ecore" value="http://www.eclipse.org/emf/2002/Ecore"/>
</eAnnotations>
<eClassifiers xsi:type="ecore:EClass" name="TestSensor" eSuperTypes="../../../../../org.eclipse.sensinact.gateway.core.models.provider/src/main/resources/model/sensinact.ecore#//Provider">
<eStructuralFeatures xsi:type="ecore:EReference" name="temp" eType="#//TestTemperatur"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="temp" eType="#//TestTemperatur"
containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="tempNonContaint" eType="#//TestTemperatur"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="TestTemperatur" eSuperTypes="../../../../../org.eclipse.sensinact.gateway.core.models.provider/src/main/resources/model/sensinact.ecore#//Service">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="v1" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ecorePackage="testdata.ecore#/">
<genClasses ecoreClass="testdata.ecore#//TestSensor">
<genFeatures notify="false" createChild="false" propertySortChoices="true" ecoreFeature="ecore:EReference testdata.ecore#//TestSensor/temp"/>
<genFeatures notify="false" createChild="false" propertySortChoices="true" ecoreFeature="ecore:EReference testdata.ecore#//TestSensor/tempNonContaint"/>
</genClasses>
<genClasses ecoreClass="testdata.ecore#//TestTemperatur">
<genFeatures createChild="false" ecoreFeature="ecore:EAttribute testdata.ecore#//TestTemperatur/v1"/>
Expand Down

0 comments on commit a4543ad

Please sign in to comment.