use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonSignalHeadSocketServiceTest method testOnMessageChange.
@Test
public void testOnMessageChange() {
JsonNode message;
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonSignalHeadSocketService service = new JsonSignalHeadSocketService(connection);
//create a signalhead for testing
String sysName = "IH1";
String userName = "SH1";
SignalHead s = new jmri.implementation.VirtualSignalHead(sysName, userName);
jmri.InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
Assert.assertNotNull(s);
try {
// SignalHead Yellow
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.YELLOW);
service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
//state should be Yellow
Assert.assertEquals(SignalHead.YELLOW, s.getState());
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
// Try to set SignalHead to FLASHLUNAR, should throw error, remain at Yellow
Exception exception = null;
try {
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.FLASHLUNAR);
service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
} catch (IOException | JmriException | JsonException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
//state should be Yellow
Assert.assertEquals(SignalHead.YELLOW, s.getAppearance());
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonSignalMastSocketServiceTest method testOnMessageChange.
@Test
@Ignore("Needs setup completed")
public void testOnMessageChange() {
JsonNode message;
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonSignalMastSocketService service = new JsonSignalMastSocketService(connection);
//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);
try {
// SignalMast Stop
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, "Stop");
service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
//aspect should be Stop
Assert.assertEquals("Stop", s.getAspect());
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
// Try to set SignalMast to Unknown, should throw error, remain at Stop
Exception exception = null;
try {
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, JSON.ASPECT_UNKNOWN);
service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
Assert.assertEquals("Stop", s.getAspect());
} catch (IOException | JmriException | JsonException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
// set SignalMast no value, should throw error, remain at Stop
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName);
exception = null;
try {
service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
} catch (IOException | JmriException ex) {
Assert.fail(ex.getMessage());
}
Assert.assertNull(exception);
Assert.assertEquals("Stop", s.getAspect());
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonThrottleServiceFactoryTest method testGetSocketService.
/**
* Test of getSocketService method, of class JsonThrottleServiceFactory.
*/
@Test
public void testGetSocketService() {
JsonConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonThrottleServiceFactory instance = new JsonThrottleServiceFactory();
JsonSocketService result = instance.getSocketService(connection);
Assert.assertNotNull(result);
Assert.assertTrue(JsonSocketService.class.isInstance(result));
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonRouteSocketServiceTest method testOnMessageChange.
public void testOnMessageChange() {
// the route state on a sensorless route
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message;
JsonRouteSocketService service = new JsonRouteSocketService(connection);
RouteManager manager = InstanceManager.getDefault(RouteManager.class);
Route route1 = manager.provideRoute("IR1", "Route1");
Sensor sensor1 = InstanceManager.getDefault(SensorManager.class).provideSensor("IS1");
Turnout turnout1 = InstanceManager.getDefault(TurnoutManager.class).provideTurnout("IT1");
turnout1.setCommandedState(Turnout.CLOSED);
route1.setTurnoutsAlignedSensor(sensor1.getSystemName());
Assert.assertTrue(route1.addOutputTurnout(turnout1.getSystemName(), Turnout.THROWN));
route1.activateRoute();
Assert.assertNotNull(route1.getTurnoutsAlgdSensor());
// Route ACTIVE - becomes ACTIVE
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.ACTIVE);
service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
JUnitUtil.waitFor(() -> {
return route1.getState() == Sensor.ACTIVE;
}, "Route to activate");
Assert.assertEquals(Sensor.ACTIVE, route1.getState());
// Route INACTIVE - remains ACTIVE
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.INACTIVE);
service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
Assert.assertEquals(Sensor.ACTIVE, route1.getState());
// Route UNKNOWN - remains ACTIVE
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.UNKNOWN);
service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
JUnitUtil.waitFor(() -> {
return route1.getState() == Sensor.ACTIVE;
}, "Route to activate");
Assert.assertEquals(Sensor.ACTIVE, route1.getState());
// Route TOGGLE - remains ACTIVE
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.TOGGLE);
service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
JUnitUtil.waitFor(() -> {
return route1.getState() == Sensor.ACTIVE;
}, "Route to activate");
Assert.assertEquals(Sensor.ACTIVE, route1.getState());
// Route TOGGLE - becomes ACTIVE
sensor1.setKnownState(Sensor.INACTIVE);
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.TOGGLE);
service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
JUnitUtil.waitFor(() -> {
return route1.getState() == Sensor.ACTIVE;
}, "Route to activate");
Assert.assertEquals(Sensor.ACTIVE, route1.getState());
// Route Invalid State
// invalid state
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, 42);
JsonException exception = null;
try {
service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertEquals(Sensor.ACTIVE, route1.getState());
Assert.assertNotNull(exception);
Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonTurnoutSocketServiceTest method testCtorSuccess.
public void testCtorSuccess() {
JsonTurnoutSocketService service = new JsonTurnoutSocketService(new JsonMockConnection((DataOutputStream) null));
Assert.assertNotNull(service);
}
Aggregations