Search in sources :

Example 61 with JsonException

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

the class JsonUtilHttpServiceTest method testGetMetadata_Locale_String.

/**
     * Test of getMetadata method, of class JsonUtilHttpService.
     */
@Test
public void testGetMetadata_Locale_String() {
    Locale locale = Locale.ENGLISH;
    ObjectMapper mapper = new ObjectMapper();
    JsonUtilHttpService instance = new JsonUtilHttpService(mapper);
    JsonNode result;
    try {
        for (String metadata : Metadata.getSystemNameList()) {
            result = instance.getMetadata(locale, metadata);
            Assert.assertEquals(JSON.METADATA, result.path(JSON.TYPE).asText());
            Assert.assertEquals(metadata, result.path(JSON.DATA).path(JSON.NAME).asText());
            Assert.assertEquals(Metadata.getBySystemName(metadata), result.path(JSON.DATA).path(JSON.VALUE).asText());
        }
    } catch (JsonException ex) {
        log.error("Unexpected exception.", ex);
        Assert.fail("Unexpected exception");
    }
    JsonException exception = null;
    try {
        instance.getMetadata(locale, "invalid_metadata_entry");
    } catch (JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
}
Also used : Locale(java.util.Locale) JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 62 with JsonException

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

the class JsonUtilHttpServiceTest method testGetNetworkService.

/**
     * Test of getNetworkService method, of class JsonUtilHttpService.
     *
     * @throws jmri.server.json.JsonException if test fails in an unexpected
     *                                        manner
     */
@Test
public void testGetNetworkService() throws JsonException {
    Locale locale = Locale.ENGLISH;
    ObjectMapper mapper = new ObjectMapper();
    JsonUtilHttpService instance = new JsonUtilHttpService(mapper);
    JsonNode result = null;
    // non-existant service
    JsonException exception = null;
    try {
        // NOI18N
        result = instance.getNetworkService(locale, "non-existant-service");
    } catch (JsonException ex) {
        exception = ex;
    }
    Assert.assertNull(result);
    Assert.assertNotNull(exception);
    Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, exception.getCode());
    // published service
    ZeroConfService service = ZeroConfService.create(JSON.ZEROCONF_SERVICE_TYPE, 9999);
    service.publish();
    Assume.assumeTrue("Published ZeroConf Service", JUnitUtil.waitFor(() -> {
        return service.isPublished() == true;
    }));
    result = instance.getNetworkService(locale, JSON.ZEROCONF_SERVICE_TYPE);
    Assert.assertEquals(JSON.NETWORK_SERVICE, result.path(JSON.TYPE).asText());
    JsonNode data = result.path(JSON.DATA);
    Assert.assertFalse(data.isMissingNode());
    Assert.assertEquals(WebServerPreferences.getDefault().getRailRoadName(), data.path(JSON.NAME).asText());
    Assert.assertEquals(9999, data.path(JSON.PORT).asInt());
    Assert.assertEquals(JSON.ZEROCONF_SERVICE_TYPE, data.path(JSON.TYPE).asText());
    Assert.assertEquals(NodeIdentity.identity(), data.path(JSON.NODE).asText());
    Assert.assertEquals(Metadata.getBySystemName(Metadata.JMRIVERCANON), data.path("jmri").asText());
    Assert.assertEquals(Metadata.getBySystemName(Metadata.JMRIVERSION), data.path("version").asText());
}
Also used : Locale(java.util.Locale) JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ZeroConfService(jmri.util.zeroconf.ZeroConfService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 63 with JsonException

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

the class JsonTurnoutHttpServiceTest method testDoGetList.

public void testDoGetList() {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonTurnoutHttpService service = new JsonTurnoutHttpService(mapper);
        TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
        JsonNode result;
        result = service.doGetList(JsonTurnoutServiceFactory.TURNOUT, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(0, result.size());
        manager.provideTurnout("IT1");
        manager.provideTurnout("IT2");
        result = service.doGetList(JsonTurnoutServiceFactory.TURNOUT, 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) TurnoutManager(jmri.TurnoutManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 64 with JsonException

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

the class JsonTurnoutSocketServiceTest method testTurnoutChange.

public void testTurnoutChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1");
        JsonTurnoutSocketService service = new JsonTurnoutSocketService(connection);
        TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
        Turnout turnout1 = manager.provideTurnout("IT1");
        turnout1.setCommandedState(Turnout.UNKNOWN);
        service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
        // TODO: test that service is listener in TurnoutManager
        Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        turnout1.setCommandedState(Turnout.CLOSED);
        JUnitUtil.waitFor(() -> {
            return turnout1.getKnownState() == Turnout.CLOSED;
        }, "Turnout to close");
        Assert.assertEquals(Turnout.CLOSED, turnout1.getKnownState());
        Assert.assertEquals(JSON.CLOSED, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        turnout1.setCommandedState(Turnout.THROWN);
        JUnitUtil.waitFor(() -> {
            return turnout1.getKnownState() == Turnout.THROWN;
        }, "Turnout to throw");
        Assert.assertEquals(JSON.THROWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        service.onClose();
    // TODO: test that service is no longer a listener in TurnoutManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) TurnoutManager(jmri.TurnoutManager) Turnout(jmri.Turnout)

Example 65 with JsonException

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

the class JsonUtil method getLocation.

/**
     *
     * @param locale the client's locale
     * @param id     the location ID
     * @return the JSON representation of a Location
     * @throws JsonException if the location cannot be located by ID
     * @deprecated since 4.5.6
     */
@Deprecated
public static JsonNode getLocation(Locale locale, String id) throws JsonException {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, LOCATION);
    ObjectNode data = root.putObject(DATA);
    try {
        Location location = LocationManager.instance().getLocationById(id);
        data.put(NAME, location.getName());
        data.put(ID, location.getId());
        data.put(LENGTH, location.getLength());
        data.put(COMMENT, location.getComment());
    } catch (NullPointerException e) {
        log.error("Unable to get location id [{}].", id);
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", LOCATION, id));
    }
    return root;
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Location(jmri.jmrit.operations.locations.Location) RouteLocation(jmri.jmrit.operations.routes.RouteLocation)

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