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());
}
}
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());
}
}
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));
}
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());
}
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;
}
Aggregations