Search in sources :

Example 36 with JsonException

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

the class JsonRosterHttpServiceTest method testGetRosterEntry_Locale_String.

/**
     * Test of getRosterEntry method, of class JsonRosterHttpService.
     *
     * @throws java.lang.Exception
     */
@Test
public void testGetRosterEntry_Locale_String() throws Exception {
    JsonRosterHttpService instance = new JsonRosterHttpService(this.objectMapper);
    // existant entry
    Assert.assertEquals(TEST_ENTRY1, instance.getRosterEntry(locale, TEST_ENTRY1).path(JSON.DATA).path(JSON.NAME).asText());
    // non-existant entry
    JsonException exception = null;
    try {
        instance.getRosterEntry(locale, TEST_GROUP1);
    } catch (JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
    Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, exception.getCode());
}
Also used : JsonException(jmri.server.json.JsonException) Test(org.junit.Test)

Example 37 with JsonException

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

the class JsonSignalHeadHttpServiceTest method testDoGetList.

@Test
public void testDoGetList() {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonSignalHeadHttpService service = new JsonSignalHeadHttpService(mapper);
        JsonNode result;
        result = service.doGetList(JsonSignalHead.SIGNAL_HEAD, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(0, result.size());
        jmri.InstanceManager.getDefault(jmri.SignalHeadManager.class).register(new jmri.implementation.VirtualSignalHead("IH1", "Head 1"));
        jmri.InstanceManager.getDefault(jmri.SignalHeadManager.class).register(new jmri.implementation.VirtualSignalHead("IH2", "Head 2"));
        result = service.doGetList(JsonSignalHead.SIGNAL_HEAD, 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) Test(org.junit.Test)

Example 38 with JsonException

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

the class JsonSignalHeadSocketServiceTest method testSignalHeadChange.

@Test
public void testSignalHeadChange() {
    try {
        //create a signalhead for testing
        String sysName = "IH1";
        String userName = "SH1";
        SignalHead s = new jmri.implementation.VirtualSignalHead(sysName, userName);
        jmri.InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
        Assert.assertNotNull(s);
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName);
        JsonSignalHeadSocketService service = new JsonSignalHeadSocketService(connection);
        service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
        //signalhead defaults to Dark
        Assert.assertEquals(SignalHead.DARK, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        //change to Green, and wait for change to show up, then verify
        s.setAppearance(SignalHead.GREEN);
        JUnitUtil.waitFor(() -> {
            return s.getState() == SignalHead.GREEN;
        }, "SignalHead is now GREEN");
        Assert.assertEquals(SignalHead.GREEN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        //change to Red, and wait for change to show up, then verify
        s.setAppearance(SignalHead.RED);
        JUnitUtil.waitFor(() -> {
            return s.getState() == SignalHead.RED;
        }, "SignalHead is now RED");
        Assert.assertEquals(SignalHead.RED, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
        service.onClose();
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) SignalHead(jmri.SignalHead) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Test(org.junit.Test)

Example 39 with JsonException

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

the class JsonConsistHttpService method getConsist.

/**
     * Get the JSON representation of a consist.
     *
     * The JSON representation is an object with the following data attributes:
     * <ul>
     * <li>address - integer address</li>
     * <li>isLongAddress - boolean true if address is long, false if short</li>
     * <li>type - integer, see {@link jmri.Consist#getConsistType() }</li>
     * <li>id - string with consist Id</li>
     * <li>sizeLimit - the maximum number of locomotives the consist can
     * contain</li>
     * <li>engines - array listing every locomotive in the consist. Each entry
     * in the array contains the following attributes:
     * <ul>
     * <li>address - integer address</li>
     * <li>isLongAddress - boolean true if address is long, false if short</li>
     * <li>forward - boolean true if the locomotive running is forward in the
     * consists</li>
     * <li>position - integer locomotive's position in the consist</li>
     * </ul>
     * </ul>
     *
     * @param locale  The locale to throw exceptions in.
     * @param address The address of the consist to get.
     * @return The JSON representation of the consist.
     * @throws JsonException This exception has code 404 if the consist does not
     *                       exist.
     */
public JsonNode getConsist(Locale locale, DccLocoAddress address) throws JsonException {
    if (this.manager.getConsistList().contains(address)) {
        ObjectNode root = mapper.createObjectNode();
        root.put(TYPE, CONSIST);
        ObjectNode data = root.putObject(DATA);
        Consist consist = this.manager.getConsist(address);
        data.put(ADDRESS, consist.getConsistAddress().getNumber());
        data.put(IS_LONG_ADDRESS, consist.getConsistAddress().isLongAddress());
        data.put(TYPE, consist.getConsistType());
        ArrayNode engines = data.putArray(ENGINES);
        consist.getConsistList().stream().forEach((locomotive) -> {
            ObjectNode engine = mapper.createObjectNode();
            engine.put(ADDRESS, locomotive.getNumber());
            engine.put(IS_LONG_ADDRESS, locomotive.isLongAddress());
            engine.put(FORWARD, consist.getLocoDirection(locomotive));
            engine.put(POSITION, consist.getPosition(locomotive));
            engines.add(engine);
        });
        data.put(ID, consist.getConsistID());
        data.put(SIZE_LIMIT, consist.sizeLimit());
        return root;
    } else {
        // NOI18N
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", CONSIST, address.toString()));
    }
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Consist(jmri.Consist) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 40 with JsonException

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

the class JsonConsistHttpService method doPut.

@Override
public JsonNode doPut(String type, String name, JsonNode data, Locale locale) throws JsonException {
    if (!this.manager.isConsistManager()) {
        // NOI18N
        throw new JsonException(503, Bundle.getMessage(locale, "ErrorNoConsistManager"));
    }
    DccLocoAddress address;
    if (data.path(ADDRESS).canConvertToInt()) {
        address = new DccLocoAddress(data.path(ADDRESS).asInt(), data.path(IS_LONG_ADDRESS).asBoolean(false));
    } else {
        address = JsonUtilHttpService.addressForString(data.path(ADDRESS).asText());
    }
    this.manager.getConsist(address);
    return this.doPost(type, name, data, locale);
}
Also used : JsonException(jmri.server.json.JsonException) DccLocoAddress(jmri.DccLocoAddress)

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