Search in sources :

Example 11 with ReporterManager

use of jmri.ReporterManager in project JMRI by JMRI.

the class JsonReporterSocketServiceTest method testReporterChange.

@Test
public void testReporterChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1");
        JsonReporterSocketService service = new JsonReporterSocketService(connection);
        ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
        Reporter memory1 = manager.provideReporter("IR1");
        service.onMessage(JsonReporter.REPORTER, message, Locale.ENGLISH);
        // TODO: test that service is listener in ReporterManager
        // default null value of memory1 has text representation "null" in JSON
        Assert.assertEquals("null", connection.getMessage().path(JSON.DATA).path(JsonReporter.REPORT).asText());
        memory1.setReport("throw");
        JUnitUtil.waitFor(() -> {
            return memory1.getCurrentReport().equals("throw");
        }, "Reporter to throw");
        Assert.assertEquals("throw", connection.getMessage().path(JSON.DATA).path(JsonReporter.REPORT).asText());
        memory1.setReport("close");
        JUnitUtil.waitFor(() -> {
            return memory1.getCurrentReport().equals("close");
        }, "Reporter to close");
        Assert.assertEquals("close", memory1.getCurrentReport());
        Assert.assertEquals("close", connection.getMessage().path(JSON.DATA).path(JsonReporter.REPORT).asText());
        service.onClose();
    // TODO: test that service is no longer a listener in ReporterManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) ReporterManager(jmri.ReporterManager) JsonMockConnection(jmri.server.json.JsonMockConnection) Reporter(jmri.Reporter) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with ReporterManager

use of jmri.ReporterManager in project JMRI by JMRI.

the class JsonReporterSocketServiceTest method testOnMessageChange.

@Test
public void testOnMessageChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message;
        JsonReporterSocketService service = new JsonReporterSocketService(connection);
        ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
        Reporter memory1 = manager.provideReporter("IR1");
        // Reporter "close"
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "close");
        service.onMessage(JsonReporter.REPORTER, message, Locale.ENGLISH);
        Assert.assertEquals("close", memory1.getCurrentReport());
        // Reporter "throw"
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "throw");
        service.onMessage(JsonReporter.REPORTER, message, Locale.ENGLISH);
        Assert.assertEquals("throw", memory1.getCurrentReport());
        // Reporter UNKNOWN - remains ON
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1").putNull(JsonReporter.REPORT);
        service.onMessage(JsonReporter.REPORTER, message, Locale.ENGLISH);
        Assert.assertEquals(null, memory1.getCurrentReport());
        memory1.setReport("throw");
        // Reporter no value
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1");
        JsonException exception = null;
        try {
            service.onMessage(JsonReporter.REPORTER, message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals("throw", memory1.getCurrentReport());
        Assert.assertNull(exception);
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) ReporterManager(jmri.ReporterManager) JsonMockConnection(jmri.server.json.JsonMockConnection) Reporter(jmri.Reporter) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

ReporterManager (jmri.ReporterManager)12 Reporter (jmri.Reporter)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 JsonException (jmri.server.json.JsonException)6 Test (org.junit.Test)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Block (jmri.Block)2 BlockManager (jmri.BlockManager)2 DccLocoAddress (jmri.DccLocoAddress)2 IdTag (jmri.IdTag)2 JmriException (jmri.JmriException)2 Location (jmri.jmrit.operations.locations.Location)2 LocationManager (jmri.jmrit.operations.locations.LocationManager)2 JsonMockConnection (jmri.server.json.JsonMockConnection)2 PhysicalLocation (jmri.util.PhysicalLocation)2 Element (org.jdom2.Element)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1