Search in sources :

Example 1 with RouteManager

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());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SensorManager(jmri.SensorManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Route(jmri.Route) RouteManager(jmri.RouteManager) Sensor(jmri.Sensor)

Example 2 with RouteManager

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());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RouteManager(jmri.RouteManager)

Example 3 with RouteManager

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());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Route(jmri.Route) RouteManager(jmri.RouteManager)

Example 4 with RouteManager

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());
}
Also used : JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RouteManager(jmri.RouteManager)

Example 5 with RouteManager

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());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SensorManager(jmri.SensorManager) JsonMockConnection(jmri.server.json.JsonMockConnection) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Route(jmri.Route) RouteManager(jmri.RouteManager) Sensor(jmri.Sensor)

Aggregations

RouteManager (jmri.RouteManager)16 Route (jmri.Route)11 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 JsonException (jmri.server.json.JsonException)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 Sensor (jmri.Sensor)4 SensorManager (jmri.SensorManager)4 Conditional (jmri.Conditional)3 Logix (jmri.Logix)3 Turnout (jmri.Turnout)3 SensorGroupConditional (jmri.implementation.SensorGroupConditional)3 DefaultRouteManager (jmri.managers.DefaultRouteManager)3 IOException (java.io.IOException)2 DefaultListModel (javax.swing.DefaultListModel)2 JmriException (jmri.JmriException)2 TurnoutManager (jmri.TurnoutManager)2 DefaultRoute (jmri.implementation.DefaultRoute)2 JsonMockConnection (jmri.server.json.JsonMockConnection)2 Element (org.jdom2.Element)2 ActionEvent (java.awt.event.ActionEvent)1