Search in sources :

Example 6 with JsonMockConnection

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

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

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

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

the class JsonReporterSocketServiceTest method testCtorSuccess.

@Test
public void testCtorSuccess() {
    JsonReporterSocketService service = new JsonReporterSocketService(new JsonMockConnection((DataOutputStream) null));
    Assert.assertNotNull(service);
}
Also used : JsonMockConnection(jmri.server.json.JsonMockConnection) DataOutputStream(java.io.DataOutputStream) Test(org.junit.Test)

Example 10 with JsonMockConnection

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

JsonMockConnection (jmri.server.json.JsonMockConnection)32 JsonNode (com.fasterxml.jackson.databind.JsonNode)20 IOException (java.io.IOException)18 JmriException (jmri.JmriException)18 JsonException (jmri.server.json.JsonException)18 Test (org.junit.Test)14 DataOutputStream (java.io.DataOutputStream)10 Sensor (jmri.Sensor)4 SensorManager (jmri.SensorManager)4 Turnout (jmri.Turnout)3 TurnoutManager (jmri.TurnoutManager)3 Locale (java.util.Locale)2 Light (jmri.Light)2 LightManager (jmri.LightManager)2 Memory (jmri.Memory)2 MemoryManager (jmri.MemoryManager)2 PowerManager (jmri.PowerManager)2 Reporter (jmri.Reporter)2 ReporterManager (jmri.ReporterManager)2 Route (jmri.Route)2