Search in sources :

Example 81 with JsonException

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

the class JsonTurnoutHttpService method doPost.

@Override
public JsonNode doPost(String type, String name, JsonNode data, Locale locale) throws JsonException {
    Turnout turnout = InstanceManager.turnoutManagerInstance().getTurnout(name);
    if (turnout == null) {
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", TURNOUT, name));
    }
    if (data.path(USERNAME).isTextual()) {
        turnout.setUserName(data.path(USERNAME).asText());
    }
    if (data.path(INVERTED).isBoolean()) {
        turnout.setInverted(data.path(INVERTED).asBoolean());
    }
    if (data.path(COMMENT).isTextual()) {
        turnout.setComment(data.path(COMMENT).asText());
    }
    int state = data.path(STATE).asInt(UNKNOWN);
    switch(state) {
        case THROWN:
            turnout.setCommandedState(Turnout.THROWN);
            break;
        case CLOSED:
            turnout.setCommandedState(Turnout.CLOSED);
            break;
        case UNKNOWN:
            // leave state alone in this case
            break;
        default:
            throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", TURNOUT, state));
    }
    return this.doGet(type, name, locale);
}
Also used : JsonException(jmri.server.json.JsonException) Turnout(jmri.Turnout)

Example 82 with JsonException

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

the class JsonLightHttpServiceTest method testDoPut.

public void testDoPut() {
    ObjectMapper mapper = new ObjectMapper();
    JsonLightHttpService service = new JsonLightHttpService(mapper);
    LightManager manager = InstanceManager.getDefault(LightManager.class);
    JsonNode message;
    try {
        // add a light
        Assert.assertNull(manager.getLight("IL1"));
        message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, Light.OFF);
        service.doPut(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
        Assert.assertNotNull(manager.getLight("IL1"));
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) LightManager(jmri.LightManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 83 with JsonException

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

the class JsonLightHttpServiceTest method testDoPost.

public void testDoPost() throws JmriException {
    ObjectMapper mapper = new ObjectMapper();
    JsonLightHttpService service = new JsonLightHttpService(mapper);
    LightManager manager = InstanceManager.getDefault(LightManager.class);
    Light light1 = manager.provideLight("IL1");
    JsonNode result;
    JsonNode message;
    try {
        // set off
        message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.OFF);
        result = service.doPost(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
        Assert.assertEquals(Light.OFF, light1.getState());
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // set on
        message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.ON);
        result = service.doPost(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
        Assert.assertEquals(Light.ON, light1.getState());
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.ON, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // set unknown - remains on
        message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.UNKNOWN);
        result = service.doPost(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
        Assert.assertEquals(Light.ON, light1.getState());
        Assert.assertEquals(JSON.ON, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // set invalid state
        // Invalid value
        message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, 42);
        JsonException exception = null;
        try {
            service.doPost(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals(Light.ON, light1.getState());
        Assert.assertNotNull(exception);
        Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) Light(jmri.Light) LightManager(jmri.LightManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 84 with JsonException

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

the class JsonLightHttpServiceTest method testDoGetList.

public void testDoGetList() {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonLightHttpService service = new JsonLightHttpService(mapper);
        LightManager manager = InstanceManager.getDefault(LightManager.class);
        JsonNode result;
        result = service.doGetList(JsonLight.LIGHT, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(0, result.size());
        manager.provideLight("IL1");
        manager.provideLight("IL2");
        result = service.doGetList(JsonLight.LIGHT, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(2, result.size());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) LightManager(jmri.LightManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 85 with JsonException

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

the class JsonReporterHttpServiceTest method testDoPost.

@Test
public void testDoPost() throws JmriException {
    ObjectMapper mapper = new ObjectMapper();
    JsonReporterHttpService service = new JsonReporterHttpService(mapper);
    ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
    Reporter reporter1 = manager.provideReporter("IR1");
    JsonNode result;
    JsonNode message;
    try {
        // set off
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "close");
        result = service.doPost(REPORTER, "IR1", message, Locale.ENGLISH);
        Assert.assertEquals("close", reporter1.getCurrentReport());
        Assert.assertNotNull(result);
        Assert.assertEquals("close", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
        // set on
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "throw");
        result = service.doPost(REPORTER, "IR1", message, Locale.ENGLISH);
        Assert.assertEquals("throw", reporter1.getCurrentReport());
        Assert.assertNotNull(result);
        Assert.assertEquals("throw", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
        // set null
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").putNull(JsonReporter.REPORT);
        result = service.doPost(REPORTER, "IR1", message, Locale.ENGLISH);
        Assert.assertNull(reporter1.getCurrentReport());
        Assert.assertEquals("null", 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)

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