use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonSignalMastSocketServiceTest method testSignalMastChange.
@Test
@Ignore("Needs setup completed")
public void testSignalMastChange() {
try {
//create a signalmast for testing
String sysName = "IF$shsm:basic:one-searchlight:SM2";
String userName = "SM2";
SignalMastManager manager = InstanceManager.getDefault(SignalMastManager.class);
SignalMast s = manager.provideSignalMast(sysName);
s.setUserName(userName);
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName);
JsonSignalMastSocketService service = new JsonSignalMastSocketService(connection);
service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
// TODO: test that service is listener in SignalMastManager
Assert.assertEquals(JSON.ASPECT_UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
//change to Approach, and wait for change to show up
s.setAspect("Approach");
JUnitUtil.waitFor(() -> {
return s.getAspect().equals("Approach");
}, "SignalMast is now Approach");
Assert.assertEquals("Approach", connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
//change to Stop, and wait for change to show up
s.setAspect("Stop");
JUnitUtil.waitFor(() -> {
return s.getAspect().equals("Stop");
}, "SignalMast is now Stop");
Assert.assertEquals("Stop", connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
service.onClose();
// // TODO: test that service is no longer a listener in SignalMastManager
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonSensorSocketServiceTest method testOnMessageChange.
public void testOnMessageChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message;
JsonSensorSocketService service = new JsonSensorSocketService(connection);
SensorManager manager = InstanceManager.getDefault(SensorManager.class);
Sensor sensor1 = manager.provideSensor("IS1");
// Sensor INACTIVE
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INACTIVE);
service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
// Sensor ACTIVE
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.ACTIVE);
service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
// Sensor UNKNOWN - remains ACTIVE
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.UNKNOWN);
service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
sensor1.setKnownState(Sensor.ACTIVE);
// Sensor INCONSISTENT - remains ACTIVE
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INCONSISTENT);
service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
sensor1.setKnownState(Sensor.ACTIVE);
// Sensor no value
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1");
JsonException exception = null;
try {
service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
Assert.assertNull(exception);
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonMockConnection 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.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonSensorSocketServiceTest method testCtorSuccess.
public void testCtorSuccess() {
JsonSensorSocketService service = new JsonSensorSocketService(new JsonMockConnection((DataOutputStream) null));
Assert.assertNotNull(service);
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonSignalHeadSocketServiceTest method testCtorSuccess.
@Test
public void testCtorSuccess() {
JsonSignalHeadSocketService service = new JsonSignalHeadSocketService(new JsonMockConnection((DataOutputStream) null));
Assert.assertNotNull(service);
}
Aggregations