Search in sources :

Example 31 with JsonException

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

the class JsonRouteHttpServiceTest method testDoGetWithRouteSensor.

public void testDoGetWithRouteSensor() throws JmriException {
    JsonRouteHttpService service = new JsonRouteHttpService(new ObjectMapper());
    RouteManager manager = InstanceManager.getDefault(RouteManager.class);
    Route route1 = manager.provideRoute("IR1", "Route1");
    Sensor sensor1 = InstanceManager.getDefault(SensorManager.class).provideSensor("IS1");
    route1.setTurnoutsAlignedSensor(sensor1.getSystemName());
    JsonNode result;
    try {
        result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JsonRouteServiceFactory.ROUTE, result.path(JSON.TYPE).asText());
        Assert.assertEquals("IR1", result.path(JSON.DATA).path(JSON.NAME).asText());
        Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
        sensor1.setKnownState(Sensor.ACTIVE);
        result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.ACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt());
        sensor1.setKnownState(Sensor.INACTIVE);
        result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SensorManager(jmri.SensorManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Route(jmri.Route) RouteManager(jmri.RouteManager) Sensor(jmri.Sensor)

Example 32 with JsonException

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

the class JsonRouteHttpServiceTest method testDoGetList.

public void testDoGetList() {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonRouteHttpService service = new JsonRouteHttpService(mapper);
        RouteManager manager = InstanceManager.getDefault(RouteManager.class);
        JsonNode result;
        result = service.doGetList(JsonRouteServiceFactory.ROUTE, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(0, result.size());
        manager.provideRoute("IR1", "Route1");
        manager.provideRoute("IR2", "Route2");
        result = service.doGetList(JsonRouteServiceFactory.ROUTE, 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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RouteManager(jmri.RouteManager)

Example 33 with JsonException

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

the class JsonRouteHttpServiceTest method testDoPostWithoutRouteSensor.

public void testDoPostWithoutRouteSensor() throws JmriException {
    ObjectMapper mapper = new ObjectMapper();
    JsonRouteHttpService service = new JsonRouteHttpService(mapper);
    RouteManager manager = InstanceManager.getDefault(RouteManager.class);
    Route route1 = manager.provideRoute("IR1", "Route1");
    Assert.assertNull(route1.getTurnoutsAlgdSensor());
    JsonNode result;
    JsonNode message;
    try {
        // set off
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.INACTIVE);
        result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // set on
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.ACTIVE);
        result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // set unknown
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.UNKNOWN);
        result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
        Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // set toggle
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.TOGGLE);
        result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
        Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // set invalid state
        // Invalid value
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, 42);
        JsonException exception = null;
        try {
            service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        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) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Route(jmri.Route) RouteManager(jmri.RouteManager)

Example 34 with JsonException

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

the class JsonRouteHttpServiceTest method testDoPut.

public void testDoPut() {
    ObjectMapper mapper = new ObjectMapper();
    JsonRouteHttpService service = new JsonRouteHttpService(mapper);
    RouteManager manager = InstanceManager.getDefault(RouteManager.class);
    JsonNode message;
    JsonException exception = null;
    try {
        // add a route
        Assert.assertNull(manager.getRoute("IR1"));
        message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, Sensor.INACTIVE);
        service.doPut(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
        Assert.assertNull(manager.getRoute("IR1"));
    } catch (JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
    Assert.assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, exception.getCode());
}
Also used : JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RouteManager(jmri.RouteManager)

Example 35 with JsonException

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

the class JsonRosterHttpServiceTest method testDoPost.

/**
     * Test of doPost method, of class JsonRosterHttpService.
     *
     * @throws java.lang.Exception
     */
@Test
public void testDoPost() throws Exception {
    JsonRosterHttpService instance = new JsonRosterHttpService(this.objectMapper);
    JsonException exception = null;
    try {
        instance.doPost(JsonRoster.ROSTER, null, null, locale);
    } catch (JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
    Assert.assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, exception.getCode());
    try {
        instance.doPost(TEST_GROUP1, null, null, 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)

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