Search in sources :

Example 21 with JsonException

use of jmri.server.json.JsonException in project JMRI by JMRI.

the class JsonSensorHttpService method doGet.

@Override
public JsonNode doGet(String type, String name, Locale locale) throws JsonException {
    ObjectNode root = mapper.createObjectNode();
    root.put(JSON.TYPE, SENSOR);
    Sensor sensor = InstanceManager.getDefault(SensorManager.class).getSensor(name);
    // throws JsonException if sensor == null
    ObjectNode data = this.getNamedBean(sensor, name, type, locale);
    if (sensor != null) {
        root.put(JSON.DATA, data);
        switch(sensor.getKnownState()) {
            case Sensor.ACTIVE:
                data.put(JSON.STATE, JSON.ACTIVE);
                break;
            case Sensor.INACTIVE:
                data.put(JSON.STATE, JSON.INACTIVE);
                break;
            case Sensor.INCONSISTENT:
                data.put(JSON.STATE, JSON.INCONSISTENT);
                break;
            case Sensor.UNKNOWN:
                data.put(JSON.STATE, JSON.UNKNOWN);
                break;
            default:
                // NOI18N
                throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, Bundle.getMessage(locale, "ErrorInternal", type));
        }
    }
    return root;
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SensorManager(jmri.SensorManager) Sensor(jmri.Sensor)

Example 22 with JsonException

use of jmri.server.json.JsonException 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)

Example 23 with JsonException

use of jmri.server.json.JsonException in project JMRI by JMRI.

the class JsonSensorHttpServiceTest method testDoPost.

public void testDoPost() throws JmriException {
    ObjectMapper mapper = new ObjectMapper();
    JsonSensorHttpService service = new JsonSensorHttpService(mapper);
    SensorManager manager = InstanceManager.getDefault(SensorManager.class);
    Sensor sensor1 = manager.provideSensor("IS1");
    JsonNode result;
    JsonNode message;
    try {
        // set ACTIVE
        message = mapper.createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.ACTIVE);
        result = service.doPost(JsonSensor.SENSOR, "IS1", message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
        Assert.assertNotNull(result);
        // -1 is not a possible value
        Assert.assertEquals(JSON.ACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
        // set INACTIVE
        message = mapper.createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INACTIVE);
        result = service.doPost(JsonSensor.SENSOR, "IS1", message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
        // set UNKNOWN
        message = mapper.createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.UNKNOWN);
        result = service.doPost(JsonSensor.SENSOR, "IS1", message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
        Assert.assertEquals(JSON.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
        // set INCONSISTENT
        message = mapper.createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INCONSISTENT);
        result = service.doPost(JsonSensor.SENSOR, "IS1", message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
        Assert.assertEquals(JSON.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
    } 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) Sensor(jmri.Sensor)

Example 24 with JsonException

use of jmri.server.json.JsonException in project JMRI by JMRI.

the class JsonSensorHttpServiceTest method testDoPut.

public void testDoPut() {
    ObjectMapper mapper = new ObjectMapper();
    JsonSensorHttpService service = new JsonSensorHttpService(mapper);
    SensorManager manager = InstanceManager.getDefault(SensorManager.class);
    JsonNode message;
    try {
        // add a sensor
        Assert.assertNull(manager.getSensor("IS1"));
        message = mapper.createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.UNKNOWN);
        service.doPut(JsonSensor.SENSOR, "IS1", message, Locale.ENGLISH);
        Assert.assertNotNull(manager.getSensor("IS1"));
    } 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)

Example 25 with JsonException

use of jmri.server.json.JsonException in project JMRI by JMRI.

the class JsonSensorHttpServiceTest method testDoGetList.

public void testDoGetList() {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonSensorHttpService service = new JsonSensorHttpService(mapper);
        SensorManager manager = InstanceManager.getDefault(SensorManager.class);
        JsonNode result;
        result = service.doGetList(JsonSensor.SENSOR, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(0, result.size());
        manager.provideSensor("IS1");
        manager.provideSensor("IS2");
        result = service.doGetList(JsonSensor.SENSOR, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(2, result.size());
    } 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)

Aggregations

JsonException (jmri.server.json.JsonException)112 JsonNode (com.fasterxml.jackson.databind.JsonNode)65 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)42 Test (org.junit.Test)31 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)25 JmriException (jmri.JmriException)22 IOException (java.io.IOException)21 JsonMockConnection (jmri.server.json.JsonMockConnection)18 Sensor (jmri.Sensor)13 SensorManager (jmri.SensorManager)13 Locale (java.util.Locale)10 Route (jmri.Route)9 RouteManager (jmri.RouteManager)9 Turnout (jmri.Turnout)9 SignalMast (jmri.SignalMast)8 TurnoutManager (jmri.TurnoutManager)8 Light (jmri.Light)7 ReporterManager (jmri.ReporterManager)7 SignalHead (jmri.SignalHead)7 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)6