Search in sources :

Example 26 with JsonException

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

the class JsonSignalMastSocketServiceTest method testSignalMastChange.

@Test
@Ignore("Needs setup completed")
public void testSignalMastChange() {
    try {
        //create a signalmast for testing
        String sysName = "IF$shsm:basic:one-searchlight:SM2";
        String userName = "SM2";
        SignalMastManager manager = InstanceManager.getDefault(SignalMastManager.class);
        SignalMast s = manager.provideSignalMast(sysName);
        s.setUserName(userName);
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, sysName);
        JsonSignalMastSocketService service = new JsonSignalMastSocketService(connection);
        service.onMessage(JsonSignalMast.SIGNAL_MAST, message, Locale.ENGLISH);
        // TODO: test that service is listener in SignalMastManager
        Assert.assertEquals(JSON.ASPECT_UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
        //change to Approach, and wait for change to show up
        s.setAspect("Approach");
        JUnitUtil.waitFor(() -> {
            return s.getAspect().equals("Approach");
        }, "SignalMast is now Approach");
        Assert.assertEquals("Approach", connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
        //change to Stop, and wait for change to show up
        s.setAspect("Stop");
        JUnitUtil.waitFor(() -> {
            return s.getAspect().equals("Stop");
        }, "SignalMast is now Stop");
        Assert.assertEquals("Stop", connection.getMessage().path(JSON.DATA).path(JSON.STATE).asText());
        service.onClose();
    //            // TODO: test that service is no longer a listener in SignalMastManager
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) SignalMastManager(jmri.SignalMastManager) SignalMast(jmri.SignalMast) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 27 with JsonException

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

the class JsonTurnoutHttpServiceTest method testDoPost.

public void testDoPost() throws JmriException {
    ObjectMapper mapper = new ObjectMapper();
    JsonTurnoutHttpService service = new JsonTurnoutHttpService(mapper);
    TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
    Turnout turnout1 = manager.provideTurnout("IT1");
    JsonNode result;
    JsonNode message;
    try {
        turnout1.setState(Turnout.UNKNOWN);
        // set closed
        message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.CLOSED);
        result = service.doPost(JsonTurnoutServiceFactory.TURNOUT, "IT1", message, Locale.ENGLISH);
        Assert.assertEquals(Turnout.CLOSED, turnout1.getState());
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.CLOSED, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // set thrown
        message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.THROWN);
        result = service.doPost(JsonTurnoutServiceFactory.TURNOUT, "IT1", message, Locale.ENGLISH);
        Assert.assertEquals(Turnout.THROWN, turnout1.getState());
        Assert.assertNotNull(result);
        Assert.assertEquals(JSON.THROWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // set unknown - remains thrown
        message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.UNKNOWN);
        result = service.doPost(JsonTurnoutServiceFactory.TURNOUT, "IT1", message, Locale.ENGLISH);
        Assert.assertEquals(Turnout.THROWN, turnout1.getState());
        Assert.assertEquals(JSON.THROWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
        // set invalid state
        // Invalid value
        message = mapper.createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, 42);
        JsonException exception = null;
        try {
            service.doPost(JsonTurnoutServiceFactory.TURNOUT, "IT1", message, Locale.ENGLISH);
        } catch (JsonException ex) {
            exception = ex;
        }
        Assert.assertEquals(Turnout.THROWN, turnout1.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) JsonNode(com.fasterxml.jackson.databind.JsonNode) TurnoutManager(jmri.TurnoutManager) Turnout(jmri.Turnout) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 28 with JsonException

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

the class JsonSignalMastHttpServiceTest method testDoGet.

@Test
@Ignore("Needs setup completed")
public void testDoGet() throws JmriException {
    //create a signalmast for testing
    String sysName = "IF$shsm:basic:one-searchlight:SM2";
    String userName = "SM2";
    SignalMast s = InstanceManager.getDefault(jmri.SignalMastManager.class).provideSignalMast(sysName);
    s.setUserName(userName);
    JsonNode result;
    JsonSignalMastHttpService service = new JsonSignalMastHttpService(new ObjectMapper());
    try {
        //retrieve by systemname
        result = service.doGet(JsonSignalMast.SIGNAL_MAST, sysName, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(JsonSignalMast.SIGNAL_MAST, 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(JsonSignalMast.SIGNAL_MAST, userName, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(sysName, result.path(JSON.DATA).path(JSON.NAME).asText());
        //verify initial aspect/state is "Unknown"
        Assert.assertEquals(JSON.ASPECT_UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asText());
        //change to Clear, then verify change
        s.setAspect("Clear");
        result = service.doGet(JsonSignalMast.SIGNAL_MAST, userName, Locale.ENGLISH);
        Assert.assertEquals("Clear", result.path(JSON.DATA).path(JSON.STATE).asText());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SignalMastManager(jmri.SignalMastManager) SignalMast(jmri.SignalMast) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 29 with JsonException

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

the class JsonSignalMastHttpServiceTest method testDoGetList.

@Test
@Ignore("Needs setup completed")
public void testDoGetList() {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonSignalMastHttpService service = new JsonSignalMastHttpService(mapper);
        SignalMastManager manager = InstanceManager.getDefault(SignalMastManager.class);
        JsonNode result;
        result = service.doGetList(JsonSignalMast.SIGNAL_MAST, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(0, result.size());
        manager.provideSignalMast("IF$shsm:basic:one-searchlight:SM1");
        manager.provideSignalMast("IF$shsm:basic:one-searchlight:SM2");
        result = service.doGetList(JsonSignalMast.SIGNAL_MAST, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals(2, result.size());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : JsonException(jmri.server.json.JsonException) SignalMastManager(jmri.SignalMastManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 30 with JsonException

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

the class JsonSignalMastHttpServiceTest method testDoPost.

@Test
@Ignore("Needs setup completed")
public void testDoPost() throws JmriException {
    //create a signalmast for testing
    String sysName = "IF$shsm:basic:one-searchlight:SM2";
    String userName = "SM2";
    SignalMast s = InstanceManager.getDefault(jmri.SignalMastManager.class).provideSignalMast(sysName);
    Assert.assertNotNull(s);
    s.setUserName(userName);
    JsonNode result;
    JsonNode message;
    ObjectMapper mapper = new ObjectMapper();
    JsonSignalMastHttpService service = new JsonSignalMastHttpService(new ObjectMapper());
    try {
        //set signalmast to Clear and verify change
        message = mapper.createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, "Clear");
        result = service.doPost(JsonSignalMast.SIGNAL_MAST, userName, message, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals("Clear", s.getAspect());
        Assert.assertEquals("Clear", result.path(JSON.DATA).path(JSON.STATE).asText());
    } catch (JsonException ex) {
        Assert.fail(ex.getMessage());
    }
    // try to set to UNKNOWN, which should not be allowed, so state should not change
    JsonException exception = null;
    try {
        message = mapper.createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, JSON.ASPECT_UNKNOWN);
        result = service.doPost(JsonSignalMast.SIGNAL_MAST, userName, message, Locale.ENGLISH);
        Assert.assertNotNull(result);
        Assert.assertEquals("Clear", s.getAspect());
        Assert.assertEquals("Clear", result.path(JSON.DATA).path(JSON.STATE).asText());
    } catch (JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
}
Also used : JsonException(jmri.server.json.JsonException) SignalMastManager(jmri.SignalMastManager) SignalMast(jmri.SignalMast) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Ignore(org.junit.Ignore) 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