use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonUtilSocketServiceTest method testOnMessage.
/**
* Test of onMessage method, of class JsonUtilSocketService.
*
* @throws java.lang.Exception if an exception unexpected in the context of
* these tests occurs
*/
@Test
public void testOnMessage() throws Exception {
Locale locale = Locale.ENGLISH;
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode empty = connection.getObjectMapper().createObjectNode();
JsonUtilSocketService instance = new JsonUtilSocketService(connection);
// JSON.LOCALE
instance.onMessage(JSON.LOCALE, empty, locale);
assertNull(connection.getMessage());
// JSON.PING
instance.onMessage(JSON.PING, empty, locale);
JsonNode result = connection.getMessage().path(JSON.TYPE);
assertNotNull(result);
assertTrue(JsonNode.class.isInstance(result));
assertEquals(JSON.PONG, result.asText());
// JSON.GOODBYE
instance.onMessage(JSON.GOODBYE, empty, locale);
result = connection.getMessage().path(JSON.TYPE);
assertNotNull(result);
assertTrue(JsonNode.class.isInstance(result));
assertEquals(JSON.GOODBYE, result.asText());
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonUtilSocketServiceTest method testOnList.
/**
* Test of onList method, of class JsonUtilSocketService.
*
* @throws java.lang.Exception if an exception unexpected in the context of
* these tests occurs
*/
@Test
public void testOnList() throws Exception {
Locale locale = Locale.ENGLISH;
ObjectMapper mapper = new ObjectMapper();
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode empty = connection.getObjectMapper().createObjectNode();
JsonUtilSocketService instance = new JsonUtilSocketService(connection);
JsonUtilHttpService helper = new JsonUtilHttpService(mapper);
JsonServerPreferences.getDefault().setHeartbeatInterval(10);
instance.onList(JSON.METADATA, empty, locale);
Assert.assertEquals(helper.getMetadata(locale), connection.getMessage());
instance.onList(JSON.NETWORK_SERVICES, empty, locale);
Assert.assertEquals(helper.getNetworkServices(locale), connection.getMessage());
instance.onList(JSON.SYSTEM_CONNECTIONS, empty, locale);
Assert.assertEquals(helper.getSystemConnections(locale), connection.getMessage());
instance.onList(JSON.CONFIG_PROFILES, empty, locale);
Assert.assertEquals(helper.getConfigProfiles(locale), connection.getMessage());
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonTurnoutSocketServiceTest method testTurnoutChange.
public void testTurnoutChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1");
JsonTurnoutSocketService service = new JsonTurnoutSocketService(connection);
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
Turnout turnout1 = manager.provideTurnout("IT1");
turnout1.setCommandedState(Turnout.UNKNOWN);
service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
// TODO: test that service is listener in TurnoutManager
Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
turnout1.setCommandedState(Turnout.CLOSED);
JUnitUtil.waitFor(() -> {
return turnout1.getKnownState() == Turnout.CLOSED;
}, "Turnout to close");
Assert.assertEquals(Turnout.CLOSED, turnout1.getKnownState());
Assert.assertEquals(JSON.CLOSED, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
turnout1.setCommandedState(Turnout.THROWN);
JUnitUtil.waitFor(() -> {
return turnout1.getKnownState() == Turnout.THROWN;
}, "Turnout to throw");
Assert.assertEquals(JSON.THROWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
service.onClose();
// TODO: test that service is no longer a listener in TurnoutManager
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonRouteSocketServiceTest method testCtorSuccess.
public void testCtorSuccess() {
JsonRouteSocketService service = new JsonRouteSocketService(new JsonMockConnection((DataOutputStream) null));
Assert.assertNotNull(service);
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonRouteSocketServiceTest method testRouteChange.
public void testRouteChange() {
// the route state on a sensorless route
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1");
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");
sensor1.setKnownState(Sensor.UNKNOWN);
route1.setTurnoutsAlignedSensor(sensor1.getSystemName());
service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
// TODO: test that service is listener in RouteManager
Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
sensor1.setKnownState(Sensor.ACTIVE);
JUnitUtil.waitFor(() -> {
return route1.getState() == Sensor.ACTIVE;
}, "Route to activate");
Assert.assertEquals(JSON.ACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
sensor1.setKnownState(Sensor.INACTIVE);
JUnitUtil.waitFor(() -> {
return route1.getState() == Sensor.INACTIVE;
}, "Route to deactivate");
Assert.assertEquals(Sensor.INACTIVE, route1.getState());
Assert.assertEquals(JSON.INACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
service.onClose();
// TODO: test that service is no longer a listener in RouteManager
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
Aggregations