Search in sources :

Example 91 with JsonException

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

the class JsonMemoryHttpServiceTest method testDoGetList.

public void testDoGetList() {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonMemoryHttpService service = new JsonMemoryHttpService(mapper);
        MemoryManager manager = InstanceManager.getDefault(MemoryManager.class);
        JsonNode result;
        result = service.doGetList(JsonMemory.MEMORY, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(0, result.size());
        manager.provideMemory("IM1");
        manager.provideMemory("IM2");
        result = service.doGetList(JsonMemory.MEMORY, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(2, result.size());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) MemoryManager(jmri.MemoryManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 92 with JsonException

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

the class JsonMemoryHttpServiceTest method testDoPost.

public void testDoPost() throws JmriException {
    ObjectMapper mapper = new ObjectMapper();
    JsonMemoryHttpService service = new JsonMemoryHttpService(mapper);
    MemoryManager manager = InstanceManager.getDefault(MemoryManager.class);
    Memory memory1 = manager.provideMemory("IM1");
    JsonNode result;
    JsonNode message;
    try {
        // set off
        message = mapper.createObjectNode().put(JSON.NAME, "IM1").put(JSON.VALUE, "close");
        result = service.doPost(JsonMemory.MEMORY, "IM1", message, Locale.ENGLISH);
        Assert.assertEquals("close", memory1.getValue());
        Assert.assertNotNull(result);
        Assert.assertEquals("close", result.path(JSON.DATA).path(JSON.VALUE).asText());
        // set on
        message = mapper.createObjectNode().put(JSON.NAME, "IM1").put(JSON.VALUE, "throw");
        result = service.doPost(JsonMemory.MEMORY, "IM1", message, Locale.ENGLISH);
        Assert.assertEquals("throw", memory1.getValue());
        Assert.assertNotNull(result);
        Assert.assertEquals("throw", result.path(JSON.DATA).path(JSON.VALUE).asText());
        // set null
        message = mapper.createObjectNode().put(JSON.NAME, "IM1").putNull(JSON.VALUE);
        result = service.doPost(JsonMemory.MEMORY, "IM1", message, Locale.ENGLISH);
        Assert.assertNull(memory1.getValue());
        Assert.assertEquals("null", result.path(JSON.DATA).path(JSON.VALUE).asText());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) Memory(jmri.Memory) JsonNode(com.fasterxml.jackson.databind.JsonNode) MemoryManager(jmri.MemoryManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 93 with JsonException

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

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

the class JsonPowerHttpServiceTest method testDoGet.

@Test
public void testDoGet() throws JmriException {
    JsonPowerHttpService service = new JsonPowerHttpService(new ObjectMapper());
    PowerManager power = InstanceManager.getDefault(PowerManager.class);
    JsonNode result;
    try {
        power.setPower(PowerManager.UNKNOWN);
        result = service.doGet(JsonPowerServiceFactory.POWER, null, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JsonPowerServiceFactory.POWER, result.path(JSON.TYPE).asText());
        Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
        power.setPower(PowerManager.ON);
        result = service.doGet(JsonPowerServiceFactory.POWER, null, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.ON, result.path(JSON.DATA).path(JSON.STATE).asInt());
        power.setPower(PowerManager.OFF);
        result = service.doGet(JsonPowerServiceFactory.POWER, null, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
    } catch (JsonException ex) {
        log.error("Threw", ex);
        Assert.fail("Unexpected Exception");
    }
}
Also used : PowerManager(jmri.PowerManager) JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 95 with JsonException

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

the class JsonRosterSocketServiceTest method testOnMessageDeleteRoster.

/**
     * Test of onMessage method DELETE on a ROSTER
     *
     * @throws java.io.IOException this is an error, not a failure, in the test
     * @throws jmri.JmriException  this is an error, not a failure, in the test
     */
@Test
public void testOnMessageDeleteRoster() throws IOException, JmriException {
    JsonNode data = this.connection.getObjectMapper().createObjectNode().put(JSON.METHOD, JSON.DELETE);
    Locale locale = Locale.ENGLISH;
    JsonException exception = null;
    JsonRosterSocketService instance = new JsonRosterSocketService(this.connection);
    try {
        instance.onMessage(JsonRoster.ROSTER, data, locale);
    } catch (JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
    Assert.assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, exception.getCode());
}
Also used : Locale(java.util.Locale) JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

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