Search in sources :

Example 96 with JsonException

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

the class JsonRosterSocketServiceTest method testOnMessagePostRoster.

/**
     * Test of onMessage method POST on a ROSTER
     *
     * @throws java.io.IOException this is an error, not a failure, in the test
     * @throws jmri.JmriException  this is an error, not a failure, in the test
     */
@Test
public void testOnMessagePostRoster() throws IOException, JmriException {
    JsonNode data = this.connection.getObjectMapper().createObjectNode().put(JSON.METHOD, JSON.POST);
    Locale locale = Locale.ENGLISH;
    JsonException exception = null;
    JsonRosterSocketService instance = new JsonRosterSocketService(this.connection);
    try {
        instance.onMessage(JsonRoster.ROSTER, data, locale);
    } catch (JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
    Assert.assertEquals(HttpServletResponse.SC_NOT_IMPLEMENTED, exception.getCode());
}
Also used : Locale(java.util.Locale) JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 97 with JsonException

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

the class JsonSensorSocketServiceTest method testOnMessageChange.

public void testOnMessageChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message;
        JsonSensorSocketService service = new JsonSensorSocketService(connection);
        SensorManager manager = InstanceManager.getDefault(SensorManager.class);
        Sensor sensor1 = manager.provideSensor("IS1");
        // Sensor INACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INACTIVE);
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
        // Sensor ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.ACTIVE);
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
        // Sensor UNKNOWN - remains ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.UNKNOWN);
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
        sensor1.setKnownState(Sensor.ACTIVE);
        // Sensor INCONSISTENT - remains ACTIVE
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INCONSISTENT);
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
        sensor1.setKnownState(Sensor.ACTIVE);
        // Sensor no value
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1");
        JsonException exception = null;
        try {
            service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
        Assert.assertNull(exception);
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SensorManager(jmri.SensorManager) JsonMockConnection(jmri.server.json.JsonMockConnection) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Sensor(jmri.Sensor)

Example 98 with JsonException

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

the class JsonSensorSocketServiceTest method testSensorChange.

public void testSensorChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IS1");
        JsonSensorSocketService service = new JsonSensorSocketService(connection);
        SensorManager manager = InstanceManager.getDefault(SensorManager.class);
        Sensor sensor1 = manager.provideSensor("IS1");
        service.onMessage(JsonSensor.SENSOR, message, Locale.ENGLISH);
        // TODO: test that service is listener in SensorManager
        // -1 not possible value
        Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
        sensor1.setKnownState(Sensor.ACTIVE);
        JUnitUtil.waitFor(() -> {
            return sensor1.getKnownState() == Sensor.ACTIVE;
        }, "Sensor ACTIVE");
        Assert.assertEquals(JSON.ACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
        sensor1.setKnownState(Sensor.INACTIVE);
        JUnitUtil.waitFor(() -> {
            return sensor1.getKnownState() == Sensor.INACTIVE;
        }, "Sensor INACTIVE");
        Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
        Assert.assertEquals(JSON.INACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt(-1));
        service.onClose();
    // TODO: test that service is no longer a listener in SensorManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SensorManager(jmri.SensorManager) JsonMockConnection(jmri.server.json.JsonMockConnection) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Sensor(jmri.Sensor)

Example 99 with JsonException

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

the class JsonSignalHeadHttpServiceTest method testDoGet.

@Test
public void testDoGet() throws JmriException {
    //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);
    JsonNode result;
    JsonSignalHeadHttpService service = new JsonSignalHeadHttpService(new ObjectMapper());
    try {
        //retrieve by systemname
        result = service.doGet(JsonSignalHead.SIGNAL_HEAD, sysName, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JsonSignalHead.SIGNAL_HEAD, result.path(JSON.TYPE).asText());
        Assert.assertEquals(sysName, result.path(JSON.DATA).path(JSON.NAME).asText());
        //retrieve by username, should get systemname back
        result = service.doGet(JsonSignalHead.SIGNAL_HEAD, userName, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(sysName, result.path(JSON.DATA).path(JSON.NAME).asText());
        //verify initial aspect/state is Dark
        Assert.assertEquals(SignalHead.DARK, result.path(JSON.DATA).path(JSON.STATE).asInt());
        //change to Green, then verify change
        s.setAppearance(SignalHead.GREEN);
        result = service.doGet(JsonSignalHead.SIGNAL_HEAD, userName, Locale.ENGLISH);
        Assert.assertEquals(SignalHead.GREEN, result.path(JSON.DATA).path(JSON.STATE).asInt());
        //set Held, then verify change
        s.setHeld(true);
        result = service.doGet(JsonSignalHead.SIGNAL_HEAD, userName, Locale.ENGLISH);
        Assert.assertEquals(true, result.path(JSON.DATA).path(JSON.TOKEN_HELD).asBoolean());
        //set to Not Held, then verify change
        s.setHeld(false);
        result = service.doGet(JsonSignalHead.SIGNAL_HEAD, userName, Locale.ENGLISH);
        Assert.assertEquals(false, result.path(JSON.DATA).path(JSON.TOKEN_HELD).asBoolean());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SignalHead(jmri.SignalHead) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 100 with JsonException

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

the class JsonSignalHeadHttpServiceTest method testDoPost.

@Test
public void testDoPost() throws JmriException {
    //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);
    JsonNode result = null;
    JsonNode message = null;
    ObjectMapper mapper = new ObjectMapper();
    JsonSignalHeadHttpService service = new JsonSignalHeadHttpService(new ObjectMapper());
    Assert.assertNotNull(service);
    try {
        //set signalhead to Green and verify change
        message = mapper.createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.GREEN);
        result = service.doPost(JsonSignalHead.SIGNAL_HEAD, userName, message, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(SignalHead.GREEN, s.getState());
        Assert.assertEquals(SignalHead.GREEN, result.path(JSON.DATA).path(JSON.STATE).asInt());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
    // try to set to FLASHLUNAR, which should not be allowed for this signalHead, 
    //  so check for error, and verify state does not change
    JsonException exception = null;
    result = null;
    message = null;
    try {
        message = mapper.createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.FLASHLUNAR);
        result = service.doPost(JsonSignalHead.SIGNAL_HEAD, userName, message, Locale.ENGLISH);
    } catch (JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
    Assert.assertEquals(SignalHead.GREEN, s.getState());
    Assert.assertEquals(false, s.getHeld());
    // set signalmast to Held, then verify 
    try {
        message = mapper.createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.HELD);
        result = service.doPost(JsonSignalHead.SIGNAL_HEAD, userName, message, Locale.ENGLISH);
        Assert.assertEquals(true, s.getHeld());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
    Assert.assertEquals(true, s.getHeld());
    // set signalmast to something other than Held, then verify Held is released 
    try {
        message = mapper.createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.RED);
        result = service.doPost(JsonSignalHead.SIGNAL_HEAD, userName, message, Locale.ENGLISH);
        Assert.assertEquals(false, s.getHeld());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SignalHead(jmri.SignalHead) 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