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