Search in sources :

Example 71 with SignalHead

use of jmri.SignalHead 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 72 with SignalHead

use of jmri.SignalHead 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)

Example 73 with SignalHead

use of jmri.SignalHead in project JMRI by JMRI.

the class JsonSignalHeadSocketServiceTest method testOnMessageChange.

@Test
public void testOnMessageChange() {
    JsonNode message;
    JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
    JsonSignalHeadSocketService service = new JsonSignalHeadSocketService(connection);
    //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);
    try {
        // SignalHead Yellow
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.YELLOW);
        service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
        //state should be Yellow
        Assert.assertEquals(SignalHead.YELLOW, s.getState());
    } catch (IOException | JmriException | JsonException ex) {
        Assert.fail(ex.getMessage());
    }
    // Try to set SignalHead to FLASHLUNAR, should throw error, remain at Yellow
    Exception exception = null;
    try {
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, userName).put(JSON.STATE, SignalHead.FLASHLUNAR);
        service.onMessage(JsonSignalHead.SIGNAL_HEAD, message, Locale.ENGLISH);
    } catch (IOException | JmriException | JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
    //state should be Yellow
    Assert.assertEquals(SignalHead.YELLOW, s.getAppearance());
}
Also used : JsonException(jmri.server.json.JsonException) SignalHead(jmri.SignalHead) JmriException(jmri.JmriException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JmriException(jmri.JmriException) IOException(java.io.IOException) JsonException(jmri.server.json.JsonException) JsonMockConnection(jmri.server.json.JsonMockConnection) Test(org.junit.Test)

Aggregations

SignalHead (jmri.SignalHead)73 Turnout (jmri.Turnout)20 Element (org.jdom2.Element)13 SignalMast (jmri.SignalMast)12 Sensor (jmri.Sensor)11 JsonException (jmri.server.json.JsonException)8 Test (org.junit.Test)7 NamedBean (jmri.NamedBean)6 DoubleTurnoutSignalHead (jmri.implementation.DoubleTurnoutSignalHead)6 QuadOutputSignalHead (jmri.implementation.QuadOutputSignalHead)6 SingleTurnoutSignalHead (jmri.implementation.SingleTurnoutSignalHead)6 TripleOutputSignalHead (jmri.implementation.TripleOutputSignalHead)6 TripleTurnoutSignalHead (jmri.implementation.TripleTurnoutSignalHead)6 SignalHeadManager (jmri.SignalHeadManager)5 DccSignalHead (jmri.implementation.DccSignalHead)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 JmriException (jmri.JmriException)4 Light (jmri.Light)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 ActionEvent (java.awt.event.ActionEvent)3