use of jmri.RouteManager in project JMRI by JMRI.
the class JsonRouteHttpServiceTest method testDoGetWithRouteSensor.
public void testDoGetWithRouteSensor() throws JmriException {
JsonRouteHttpService service = new JsonRouteHttpService(new ObjectMapper());
RouteManager manager = InstanceManager.getDefault(RouteManager.class);
Route route1 = manager.provideRoute("IR1", "Route1");
Sensor sensor1 = InstanceManager.getDefault(SensorManager.class).provideSensor("IS1");
route1.setTurnoutsAlignedSensor(sensor1.getSystemName());
JsonNode result;
try {
result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JsonRouteServiceFactory.ROUTE, result.path(JSON.TYPE).asText());
Assert.assertEquals("IR1", result.path(JSON.DATA).path(JSON.NAME).asText());
Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
sensor1.setKnownState(Sensor.ACTIVE);
result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JSON.ACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt());
sensor1.setKnownState(Sensor.INACTIVE);
result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JSON.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.RouteManager in project JMRI by JMRI.
the class JsonRouteHttpServiceTest method testDoGetList.
public void testDoGetList() {
try {
ObjectMapper mapper = new ObjectMapper();
JsonRouteHttpService service = new JsonRouteHttpService(mapper);
RouteManager manager = InstanceManager.getDefault(RouteManager.class);
JsonNode result;
result = service.doGetList(JsonRouteServiceFactory.ROUTE, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(0, result.size());
manager.provideRoute("IR1", "Route1");
manager.provideRoute("IR2", "Route2");
result = service.doGetList(JsonRouteServiceFactory.ROUTE, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(2, result.size());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.RouteManager in project JMRI by JMRI.
the class JsonRouteHttpServiceTest method testDoPostWithoutRouteSensor.
public void testDoPostWithoutRouteSensor() throws JmriException {
ObjectMapper mapper = new ObjectMapper();
JsonRouteHttpService service = new JsonRouteHttpService(mapper);
RouteManager manager = InstanceManager.getDefault(RouteManager.class);
Route route1 = manager.provideRoute("IR1", "Route1");
Assert.assertNull(route1.getTurnoutsAlgdSensor());
JsonNode result;
JsonNode message;
try {
// set off
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.INACTIVE);
result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set on
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.ACTIVE);
result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set unknown
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.UNKNOWN);
result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set toggle
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.TOGGLE);
result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set invalid state
// Invalid value
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, 42);
JsonException exception = null;
try {
service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.RouteManager in project JMRI by JMRI.
the class JsonRouteHttpServiceTest method testDoPut.
public void testDoPut() {
ObjectMapper mapper = new ObjectMapper();
JsonRouteHttpService service = new JsonRouteHttpService(mapper);
RouteManager manager = InstanceManager.getDefault(RouteManager.class);
JsonNode message;
JsonException exception = null;
try {
// add a route
Assert.assertNull(manager.getRoute("IR1"));
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, Sensor.INACTIVE);
service.doPut(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
Assert.assertNull(manager.getRoute("IR1"));
} catch (JsonException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
Assert.assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, exception.getCode());
}
use of jmri.RouteManager 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