Search in sources :

Example 51 with JsonException

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

the class JsonPowerSocketServiceTest method testPowerChange.

public void testPowerChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().readTree("{}");
        JsonPowerSocketService service = new JsonPowerSocketService(connection);
        PowerManager power = InstanceManager.getDefault(PowerManager.class);
        power.setPower(PowerManager.UNKNOWN);
        service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
        // TODO: test that service is listener in PowerManager
        Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        power.setPower(PowerManager.ON);
        Assert.assertEquals(JSON.ON, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        power.setPower(PowerManager.OFF);
        Assert.assertEquals(JSON.OFF, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        service.onClose();
    // TODO: test that service is no longer a listener in PowerManager
    } catch (IOException | JmriException | JsonException ex) {
        log.error("testPowerChange 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 52 with JsonException

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

the class JsonReporterHttpServiceTest method testDoPut.

@Test
public void testDoPut() {
    ObjectMapper mapper = new ObjectMapper();
    JsonReporterHttpService service = new JsonReporterHttpService(mapper);
    ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
    JsonNode message;
    try {
        // add a reporter
        Assert.assertNull(manager.getReporter("IR1"));
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "close");
        service.doPut(REPORTER, "IR1", message, Locale.ENGLISH);
        Assert.assertNotNull(manager.getReporter("IR1"));
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) ReporterManager(jmri.ReporterManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 53 with JsonException

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

the class JsonReporterHttpServiceTest method testDoGet.

@Test
public void testDoGet() throws JmriException {
    JsonReporterHttpService service = new JsonReporterHttpService(new ObjectMapper());
    ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
    // no value
    Reporter reporter1 = manager.provideReporter("IR1");
    JsonNode result;
    try {
        result = service.doGet(REPORTER, "IR1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(REPORTER, result.path(JSON.TYPE).asText());
        Assert.assertEquals("IR1", result.path(JSON.DATA).path(JSON.NAME).asText());
        // JSON node has the text "null" if reporter is null
        Assert.assertEquals("null", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
        reporter1.setReport("throw");
        result = service.doGet(REPORTER, "IR1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals("throw", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
        reporter1.setReport("close");
        result = service.doGet(REPORTER, "IR1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals("close", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) ReporterManager(jmri.ReporterManager) Reporter(jmri.Reporter) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 54 with JsonException

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

the class JsonPowerHttpServiceTest method testDoGetList.

@Test
public void testDoGetList() {
    try {
        JsonNode result = (new JsonPowerHttpService(new ObjectMapper())).doGetList(JsonPowerServiceFactory.POWER, Locale.ENGLISH);
        Assert.assertTrue(result.isArray());
        Assert.assertEquals(1, result.size());
    } catch (JsonException ex) {
        log.error("Threw", ex);
        Assert.fail("Unexpected Exception");
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 55 with JsonException

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

the class JsonPowerHttpServiceTest method testDoPost.

@Test
public void testDoPost() throws JmriException {
    ObjectMapper mapper = new ObjectMapper();
    JsonPowerHttpService service = new JsonPowerHttpService(mapper);
    PowerManager power = InstanceManager.getDefault(PowerManager.class);
    JsonNode result;
    JsonNode message;
    try {
        power.setPower(PowerManager.UNKNOWN);
        message = mapper.createObjectNode().put(JSON.STATE, JSON.ON);
        result = service.doPost(JsonPowerServiceFactory.POWER, null, message, Locale.ENGLISH);
        Assert.assertEquals(PowerManager.ON, power.getPower());
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.ON, result.path(JSON.DATA).path(JSON.STATE).asInt());
        message = mapper.createObjectNode().put(JSON.STATE, JSON.OFF);
        result = service.doPost(JsonPowerServiceFactory.POWER, null, message, Locale.ENGLISH);
        Assert.assertEquals(PowerManager.OFF, power.getPower());
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
        message = mapper.createObjectNode().put(JSON.STATE, JSON.UNKNOWN);
        result = service.doPost(JsonPowerServiceFactory.POWER, null, message, Locale.ENGLISH);
        Assert.assertEquals(PowerManager.OFF, power.getPower());
        Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // Invalid value
        message = mapper.createObjectNode().put(JSON.STATE, 42);
        JsonException exception = null;
        try {
            service.doPost(JsonPowerServiceFactory.POWER, null, message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals(PowerManager.OFF, power.getPower());
        Assert.assertNotNull(exception);
        Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
    } 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)

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