Search in sources :

Example 31 with JsonMockConnection

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

the class JsonPowerSocketServiceTest method testOnMessageChange.

public void testOnMessageChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        // Power ON
        JsonNode message = connection.getObjectMapper().readTree("{\"state\":2}");
        JsonPowerSocketService service = new JsonPowerSocketService(connection);
        PowerManager power = InstanceManager.getDefault(PowerManager.class);
        power.setPower(PowerManager.UNKNOWN);
        service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
        Assert.assertEquals(PowerManager.ON, power.getPower());
        // Power OFF
        message = connection.getObjectMapper().readTree("{\"state\":4}");
        service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
        Assert.assertEquals(PowerManager.OFF, power.getPower());
        // JSON Power UNKNOWN
        message = connection.getObjectMapper().readTree("{\"state\":0}");
        service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
        // did not change
        Assert.assertEquals(PowerManager.OFF, power.getPower());
        // JSON Invalid
        message = connection.getObjectMapper().readTree("{\"state\":1}");
        JsonException exception = null;
        try {
            service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        // did not change
        Assert.assertEquals(PowerManager.OFF, power.getPower());
        Assert.assertNotNull(exception);
        Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
    } catch (IOException | JmriException | JsonException ex) {
        log.error("testOnMessageChange 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 32 with JsonMockConnection

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

the class JsonPowerSocketServiceTest method testCtorSuccess.

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

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