Search in sources :

Example 86 with JsonException

use of jmri.server.json.JsonException 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 87 with JsonException

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

Example 88 with JsonException

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

the class JsonRosterHttpServiceTest method testDoGet.

/**
     * Tests only that this does not throw an error with a valid call, and
     * throws an error with an invalid call, but does not test the full range of
     * possible valid calls, since this method is merely a switch on it's first
     * argument.
     *
     * @throws java.lang.Exception
     */
@Test
public void testDoGet() throws Exception {
    JsonRosterHttpService instance = new JsonRosterHttpService(this.objectMapper);
    // call with valid first argument
    Assert.assertEquals(Roster.getDefault().numEntries(), instance.doGet(JsonRoster.ROSTER, null, locale).size());
    Assert.assertEquals(2, instance.doGet(JsonRoster.ROSTER, null, locale).size());
    // call with invalid first argument
    JsonException exception = null;
    try {
        instance.doGet(TEST_GROUP1, TEST_GROUP1, locale);
    } catch (JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
    Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, exception.getCode());
}
Also used : JsonException(jmri.server.json.JsonException) Test(org.junit.Test)

Example 89 with JsonException

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

the class JsonRosterHttpServiceTest method testDoGetList.

/**
     * Tests only that this does not throw an error with a valid call, and
     * throws an error with an invalid call, but does not test the full range of
     * possible valid calls, since this method is merely a switch on it's first
     * argument.
     *
     * @throws java.lang.Exception
     */
@Test
public void testDoGetList() throws Exception {
    JsonRosterHttpService instance = new JsonRosterHttpService(this.objectMapper);
    // call with valid first argument
    Assert.assertEquals(Roster.getDefault().numEntries(), instance.doGet(JsonRoster.ROSTER, null, locale).size());
    // call with invalid first argument
    JsonException exception = null;
    try {
        instance.doGet(TEST_GROUP1, TEST_GROUP1, locale);
    } catch (JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
    Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, exception.getCode());
}
Also used : JsonException(jmri.server.json.JsonException) Test(org.junit.Test)

Example 90 with JsonException

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

the class JsonMemorySocketServiceTest method testMemoryChange.

public void testMemoryChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IM1");
        JsonMemorySocketService service = new JsonMemorySocketService(connection);
        MemoryManager manager = InstanceManager.getDefault(MemoryManager.class);
        Memory memory1 = manager.provideMemory("IM1");
        service.onMessage(JsonMemory.MEMORY, message, Locale.ENGLISH);
        // TODO: test that service is listener in MemoryManager
        // default null value of memory1 has text representation "null" in JSON
        Assert.assertEquals("null", connection.getMessage().path(JSON.DATA).path(JSON.VALUE).asText());
        memory1.setValue("throw");
        JUnitUtil.waitFor(() -> {
            return memory1.getValue().equals("throw");
        }, "Memory to throw");
        Assert.assertEquals("throw", connection.getMessage().path(JSON.DATA).path(JSON.VALUE).asText());
        memory1.setValue("close");
        JUnitUtil.waitFor(() -> {
            return memory1.getValue().equals("close");
        }, "Memory to close");
        Assert.assertEquals("close", memory1.getValue());
        Assert.assertEquals("close", connection.getMessage().path(JSON.DATA).path(JSON.VALUE).asText());
        service.onClose();
    // TODO: test that service is no longer a listener in MemoryManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) Memory(jmri.Memory) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) MemoryManager(jmri.MemoryManager)

Aggregations

JsonException (jmri.server.json.JsonException)112 JsonNode (com.fasterxml.jackson.databind.JsonNode)65 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)42 Test (org.junit.Test)31 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)25 JmriException (jmri.JmriException)22 IOException (java.io.IOException)21 JsonMockConnection (jmri.server.json.JsonMockConnection)18 Sensor (jmri.Sensor)13 SensorManager (jmri.SensorManager)13 Locale (java.util.Locale)10 Route (jmri.Route)9 RouteManager (jmri.RouteManager)9 Turnout (jmri.Turnout)9 SignalMast (jmri.SignalMast)8 TurnoutManager (jmri.TurnoutManager)8 Light (jmri.Light)7 ReporterManager (jmri.ReporterManager)7 SignalHead (jmri.SignalHead)7 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)6