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