Search in sources :

Example 56 with JsonException

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

the class JsonLightSocketServiceTest method testLightChange.

public void testLightChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1");
        JsonLightSocketService service = new JsonLightSocketService(connection);
        LightManager manager = InstanceManager.getDefault(LightManager.class);
        Light light1 = manager.provideLight("IL1");
        service.onMessage(JsonLight.LIGHT, message, Locale.ENGLISH);
        // TODO: test that service is listener in LightManager
        Assert.assertEquals(JSON.OFF, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        light1.setState(Light.ON);
        JUnitUtil.waitFor(() -> {
            return light1.getState() == Light.ON;
        }, "Light to throw");
        Assert.assertEquals(JSON.ON, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        light1.setState(Light.OFF);
        JUnitUtil.waitFor(() -> {
            return light1.getState() == Light.OFF;
        }, "Light to close");
        Assert.assertEquals(Light.OFF, light1.getState());
        Assert.assertEquals(JSON.OFF, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        service.onClose();
    // TODO: test that service is no longer a listener in LightManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) Light(jmri.Light) LightManager(jmri.LightManager) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 57 with JsonException

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

the class JsonLightSocketServiceTest method testOnMessageChange.

public void testOnMessageChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message;
        JsonLightSocketService service = new JsonLightSocketService(connection);
        LightManager manager = InstanceManager.getDefault(LightManager.class);
        Light light1 = manager.provideLight("IL1");
        // Light OFF
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.OFF);
        service.onMessage(JsonLight.LIGHT, message, Locale.ENGLISH);
        Assert.assertEquals(Light.OFF, light1.getState());
        // Light ON
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.ON);
        service.onMessage(JsonLight.LIGHT, message, Locale.ENGLISH);
        Assert.assertEquals(Light.ON, light1.getState());
        // Light UNKNOWN - remains ON
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.UNKNOWN);
        service.onMessage(JsonLight.LIGHT, message, Locale.ENGLISH);
        Assert.assertEquals(Light.ON, light1.getState());
        // Light Invalid State
        // invalid state
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, 42);
        JsonException exception = null;
        try {
            service.onMessage(JsonLight.LIGHT, message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals(Light.ON, light1.getState());
        Assert.assertNotNull(exception);
        Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) Light(jmri.Light) LightManager(jmri.LightManager) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 58 with JsonException

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

the class JsonReporterHttpServiceTest method testDoGetList.

@Test
public void testDoGetList() {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonReporterHttpService service = new JsonReporterHttpService(mapper);
        ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
        JsonNode result;
        result = service.doGetList(REPORTER, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(0, result.size());
        manager.provideReporter("IR1");
        manager.provideReporter("IR2");
        result = service.doGetList(REPORTER, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(2, result.size());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) ReporterManager(jmri.ReporterManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 59 with JsonException

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

the class JsonLightHttpServiceTest method testDoGet.

public void testDoGet() throws JmriException {
    JsonLightHttpService service = new JsonLightHttpService(new ObjectMapper());
    LightManager manager = InstanceManager.getDefault(LightManager.class);
    Light light1 = manager.provideLight("IL1");
    JsonNode result;
    try {
        result = service.doGet(JsonLight.LIGHT, "IL1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JsonLight.LIGHT, result.path(JSON.TYPE).asText());
        Assert.assertEquals("IL1", result.path(JSON.DATA).path(JSON.NAME).asText());
        Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
        light1.setState(Light.ON);
        result = service.doGet(JsonLight.LIGHT, "IL1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.ON, result.path(JSON.DATA).path(JSON.STATE).asInt());
        light1.setState(Light.OFF);
        result = service.doGet(JsonLight.LIGHT, "IL1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) Light(jmri.Light) LightManager(jmri.LightManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 60 with JsonException

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

the class JsonTurnoutSocketServiceTest method testOnMessageChange.

public void testOnMessageChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message;
        JsonTurnoutSocketService service = new JsonTurnoutSocketService(connection);
        TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
        Turnout turnout1 = manager.provideTurnout("IT1");
        turnout1.setCommandedState(Turnout.UNKNOWN);
        // Turnout CLOSED
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.CLOSED);
        service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
        Assert.assertEquals(Turnout.CLOSED, turnout1.getState());
        // Turnout THROWN
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.THROWN);
        service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
        Assert.assertEquals(Turnout.THROWN, turnout1.getState());
        // Turnout UNKNOWN - remains THROWN
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.UNKNOWN);
        service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
        Assert.assertEquals(Turnout.THROWN, turnout1.getState());
        // Turnout Invalid State
        // invalid state
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, 42);
        JsonException exception = null;
        try {
            service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals(Turnout.THROWN, turnout1.getState());
        Assert.assertNotNull(exception);
        Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) TurnoutManager(jmri.TurnoutManager) Turnout(jmri.Turnout)

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