Search in sources :

Example 36 with Turnout

use of jmri.Turnout in project JMRI by JMRI.

the class JsonTurnoutSocketServiceTest method testOnMessageChange.

public void testOnMessageChange() {
    try {
        JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
        JsonNode message;
        JsonTurnoutSocketService service = new JsonTurnoutSocketService(connection);
        TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
        Turnout turnout1 = manager.provideTurnout("IT1");
        turnout1.setCommandedState(Turnout.UNKNOWN);
        // Turnout CLOSED
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.CLOSED);
        service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
        Assert.assertEquals(Turnout.CLOSED, turnout1.getState());
        // Turnout THROWN
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.THROWN);
        service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
        Assert.assertEquals(Turnout.THROWN, turnout1.getState());
        // Turnout UNKNOWN - remains THROWN
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, JSON.UNKNOWN);
        service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
        Assert.assertEquals(Turnout.THROWN, turnout1.getState());
        // Turnout Invalid State
        // invalid state
        message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1").put(JSON.STATE, 42);
        JsonException exception = null;
        try {
            service.onMessage(JsonTurnoutServiceFactory.TURNOUT, 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 (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 37 with Turnout

use of jmri.Turnout 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 38 with Turnout

use of jmri.Turnout in project JMRI by JMRI.

the class OrderedHashtableTest method testClone.

public void testClone() {
    OrderedHashtable<String, Turnout> oht1 = new OrderedHashtable<String, Turnout>();
    Turnout t1 = new TestTurnout("t1");
    Turnout t2 = new TestTurnout("t2");
    Turnout t4 = new TestTurnout("t4");
    oht1.put("1", t1);
    oht1.put("2", t2);
    @SuppressWarnings("unchecked") OrderedHashtable<String, Turnout> oht2 = (OrderedHashtable<String, Turnout>) oht1.clone();
    Assert.assertTrue("content equals", oht1.equals(oht2));
    Assert.assertFalse("different object", oht1 == oht2);
    oht1.put("4", t4);
    Assert.assertFalse("content no longer equals", oht1.equals(oht2));
}
Also used : AbstractTurnout(jmri.implementation.AbstractTurnout) Turnout(jmri.Turnout)

Example 39 with Turnout

use of jmri.Turnout in project JMRI by JMRI.

the class NamedBeanHandleTest method testHoldsTurnout.

public void testHoldsTurnout() {
    Turnout t = new AbstractTurnout("name") {

        @Override
        protected void forwardCommandChangeToLayout(int s) {
        }

        @Override
        protected void turnoutPushbuttonLockout(boolean b) {
        }
    };
    NamedBeanHandle<Turnout> n = new NamedBeanHandle<Turnout>("name", t);
    Assert.assertEquals("same TO", t, n.getBean());
}
Also used : AbstractTurnout(jmri.implementation.AbstractTurnout) AbstractTurnout(jmri.implementation.AbstractTurnout) Turnout(jmri.Turnout)

Example 40 with Turnout

use of jmri.Turnout in project JMRI by JMRI.

the class JsonUtil method getTurnout.

@Deprecated
public static JsonNode getTurnout(Locale locale, String name) throws JsonException {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, TURNOUT);
    ObjectNode data = root.putObject(DATA);
    try {
        Turnout turnout = InstanceManager.turnoutManagerInstance().getTurnout(name);
        data.put(NAME, turnout.getSystemName());
        data.put(USERNAME, turnout.getUserName());
        data.put(COMMENT, turnout.getComment());
        data.put(INVERTED, turnout.getInverted());
        switch(turnout.getKnownState()) {
            case Turnout.THROWN:
                data.put(STATE, THROWN);
                break;
            case Turnout.CLOSED:
                data.put(STATE, CLOSED);
                break;
            case Turnout.INCONSISTENT:
                data.put(STATE, INCONSISTENT);
                break;
            case Turnout.UNKNOWN:
            default:
                data.put(STATE, UNKNOWN);
                break;
        }
    } catch (NullPointerException e) {
        log.error("Unable to get turnout [{}].", name);
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", TURNOUT, name));
    }
    return root;
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Turnout(jmri.Turnout)

Aggregations

Turnout (jmri.Turnout)221 Test (org.junit.Test)63 Sensor (jmri.Sensor)26 SignalHead (jmri.SignalHead)20 Element (org.jdom2.Element)20 TurnoutManager (jmri.TurnoutManager)17 JmriException (jmri.JmriException)11 Light (jmri.Light)10 SignalMast (jmri.SignalMast)10 ActionEvent (java.awt.event.ActionEvent)9 NamedBeanHandle (jmri.NamedBeanHandle)9 JsonException (jmri.server.json.JsonException)9 ActionListener (java.awt.event.ActionListener)8 Block (jmri.Block)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 NamedBean (jmri.NamedBean)6 Route (jmri.Route)6 AbstractTurnout (jmri.implementation.AbstractTurnout)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5