use of jmri.SensorManager in project JMRI by JMRI.
the class JsonSensorSocketServiceTest method testSensorChange.
public void testSensorChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1");
JsonSensorSocketService service = new JsonSensorSocketService(connection);
SensorManager manager = InstanceManager.getDefault(SensorManager.class);
Sensor sensor1 = manager.provideSensor("IS1");
service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
// TODO: test that service is listener in SensorManager
// -1 not possible value
Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
sensor1.setKnownState(Sensor.ACTIVE);
JUnitUtil.waitFor(() -> {
return sensor1.getKnownState() == Sensor.ACTIVE;
}, "Sensor ACTIVE");
Assert.assertEquals(JSON.ACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
sensor1.setKnownState(Sensor.INACTIVE);
JUnitUtil.waitFor(() -> {
return sensor1.getKnownState() == Sensor.INACTIVE;
}, "Sensor INACTIVE");
Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
Assert.assertEquals(JSON.INACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
service.onClose();
// TODO: test that service is no longer a listener in SensorManager
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.SensorManager in project JMRI by JMRI.
the class JsonSensorHttpServiceTest method testDoGet.
public void testDoGet() throws JmriException {
JsonSensorHttpService service = new JsonSensorHttpService(new ObjectMapper());
SensorManager manager = InstanceManager.getDefault(SensorManager.class);
// no value
Sensor sensor1 = manager.provideSensor("IS1");
JsonNode result;
try {
result = service.doGet(JsonSensor.SENSOR, "IS1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JsonSensor.SENSOR, result.path(JSON.TYPE).asText());
Assert.assertEquals("IS1", result.path(JSON.DATA).path(JSON.NAME).asText());
// -1 is not a possible value
Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
sensor1.setKnownState(Sensor.ACTIVE);
result = service.doGet(JsonSensor.SENSOR, "IS1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JSON.ACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
sensor1.setKnownState(Sensor.INACTIVE);
result = service.doGet(JsonSensor.SENSOR, "IS1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(Sensor.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
Aggregations