Search in sources :

Example 6 with JmriException

use of jmri.JmriException in project JMRI by JMRI.

the class JsonPowerSocketServiceTest method testPowerChange.

public void testPowerChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().readTree("{}");
        JsonPowerSocketService service = new JsonPowerSocketService(connection);
        PowerManager power = InstanceManager.getDefault(PowerManager.class);
        power.setPower(PowerManager.UNKNOWN);
        service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
        // TODO: test that service is listener in PowerManager
        Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        power.setPower(PowerManager.ON);
        Assert.assertEquals(JSON.ON, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        power.setPower(PowerManager.OFF);
        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 PowerManager
    } catch (IOException | JmriException | JsonException ex) {
        log.error("testPowerChange threw", ex);
        Assert.fail("Unexpected Exception");
    }
}
Also used : PowerManager(jmri.PowerManager) JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 7 with JmriException

use of jmri.JmriException 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 8 with JmriException

use of jmri.JmriException 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 9 with JmriException

use of jmri.JmriException 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)

Example 10 with JmriException

use of jmri.JmriException 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());
    }
}
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

JmriException (jmri.JmriException)80 Sensor (jmri.Sensor)22 JsonException (jmri.server.json.JsonException)22 IOException (java.io.IOException)20 JsonNode (com.fasterxml.jackson.databind.JsonNode)19 JsonMockConnection (jmri.server.json.JsonMockConnection)18 ArrayList (java.util.ArrayList)10 NamedBean (jmri.NamedBean)10 Turnout (jmri.Turnout)10 Test (org.junit.Test)7 SignalMast (jmri.SignalMast)6 ConfigureManager (jmri.ConfigureManager)5 SensorManager (jmri.SensorManager)5 File (java.io.File)4 PowerManager (jmri.PowerManager)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Light (jmri.Light)3 SignalHead (jmri.SignalHead)3 FileNotFoundException (java.io.FileNotFoundException)2 Hashtable (java.util.Hashtable)2