Search in sources :

Example 16 with JsonMockConnection

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

the class JsonSignalMastSocketServiceTest method testSignalMastChange.

@Test
@Ignore("Needs setup completed")
public void testSignalMastChange() {
    try {
        //create a signalmast for testing
        String sysName = "IF$shsm:basic:one-searchlight:SM2";
        String userName = "SM2";
        SignalMastManager manager = InstanceManager.getDefault(SignalMastManager.class);
        SignalMast s = manager.provideSignalMast(sysName);
        s.setUserName(userName);
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName);
        JsonSignalMastSocketService service = new JsonSignalMastSocketService(connection);
        service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
        // TODO: test that service is listener in SignalMastManager
        Assert.assertEquals(JSON.ASPECT_UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
        //change to Approach, and wait for change to show up
        s.setAspect("Approach");
        JUnitUtil.waitFor(() -> {
            return s.getAspect().equals("Approach");
        }, "SignalMast is now Approach");
        Assert.assertEquals("Approach", connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
        //change to Stop, and wait for change to show up
        s.setAspect("Stop");
        JUnitUtil.waitFor(() -> {
            return s.getAspect().equals("Stop");
        }, "SignalMast is now Stop");
        Assert.assertEquals("Stop", connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
        service.onClose();
    //            // TODO: test that service is no longer a listener in SignalMastManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) SignalMastManager(jmri.SignalMastManager) SignalMast(jmri.SignalMast) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with JsonMockConnection

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

the class JsonSensorSocketServiceTest method testOnMessageChange.

public void testOnMessageChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message;
        JsonSensorSocketService service = new JsonSensorSocketService(connection);
        SensorManager manager = InstanceManager.getDefault(SensorManager.class);
        Sensor sensor1 = manager.provideSensor("IS1");
        // Sensor INACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INACTIVE);
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
        // Sensor ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.ACTIVE);
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
        // Sensor UNKNOWN - remains ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.UNKNOWN);
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
        sensor1.setKnownState(Sensor.ACTIVE);
        // Sensor INCONSISTENT - remains ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INCONSISTENT);
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
        sensor1.setKnownState(Sensor.ACTIVE);
        // Sensor no value
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1");
        JsonException exception = null;
        try {
            service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
        Assert.assertNull(exception);
    } 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) Sensor(jmri.Sensor)

Example 18 with JsonMockConnection

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

the class JsonSensorSocketServiceTest method testSensorChange.

public void testSensorChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1");
        JsonSensorSocketService service = new JsonSensorSocketService(connection);
        SensorManager manager = InstanceManager.getDefault(SensorManager.class);
        Sensor sensor1 = manager.provideSensor("IS1");
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        // TODO: test that service is listener in SensorManager
        // -1 not possible value
        Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
        sensor1.setKnownState(Sensor.ACTIVE);
        JUnitUtil.waitFor(() -> {
            return sensor1.getKnownState() == Sensor.ACTIVE;
        }, "Sensor ACTIVE");
        Assert.assertEquals(JSON.ACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
        sensor1.setKnownState(Sensor.INACTIVE);
        JUnitUtil.waitFor(() -> {
            return sensor1.getKnownState() == Sensor.INACTIVE;
        }, "Sensor INACTIVE");
        Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
        Assert.assertEquals(JSON.INACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
        service.onClose();
    // TODO: test that service is no longer a listener in SensorManager
    } 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) Sensor(jmri.Sensor)

Example 19 with JsonMockConnection

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

the class JsonSensorSocketServiceTest method testCtorSuccess.

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

Example 20 with JsonMockConnection

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

the class JsonSignalHeadSocketServiceTest method testCtorSuccess.

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

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