use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonLightSocketServiceTest method testLightChange.
public void testLightChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1");
JsonLightSocketService service = new JsonLightSocketService(connection);
LightManager manager = InstanceManager.getDefault(LightManager.class);
Light light1 = manager.provideLight("IL1");
service.onMessage(JsonLight.LIGHT, message, Locale.ENGLISH);
// TODO: test that service is listener in LightManager
Assert.assertEquals(JSON.OFF, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
light1.setState(Light.ON);
JUnitUtil.waitFor(() -> {
return light1.getState() == Light.ON;
}, "Light to throw");
Assert.assertEquals(JSON.ON, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
light1.setState(Light.OFF);
JUnitUtil.waitFor(() -> {
return light1.getState() == Light.OFF;
}, "Light to close");
Assert.assertEquals(Light.OFF, light1.getState());
Assert.assertEquals(JSON.OFF, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
service.onClose();
// TODO: test that service is no longer a listener in LightManager
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonLightSocketServiceTest method testOnMessageChange.
public void testOnMessageChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message;
JsonLightSocketService service = new JsonLightSocketService(connection);
LightManager manager = InstanceManager.getDefault(LightManager.class);
Light light1 = manager.provideLight("IL1");
// Light OFF
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.OFF);
service.onMessage(JsonLight.LIGHT, message, Locale.ENGLISH);
Assert.assertEquals(Light.OFF, light1.getState());
// Light ON
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.ON);
service.onMessage(JsonLight.LIGHT, message, Locale.ENGLISH);
Assert.assertEquals(Light.ON, light1.getState());
// Light UNKNOWN - remains ON
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.UNKNOWN);
service.onMessage(JsonLight.LIGHT, message, Locale.ENGLISH);
Assert.assertEquals(Light.ON, light1.getState());
// Light Invalid State
// invalid state
message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, 42);
JsonException exception = null;
try {
service.onMessage(JsonLight.LIGHT, message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertEquals(Light.ON, light1.getState());
Assert.assertNotNull(exception);
Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonReporterHttpServiceTest method testDoGetList.
@Test
public void testDoGetList() {
try {
ObjectMapper mapper = new ObjectMapper();
JsonReporterHttpService service = new JsonReporterHttpService(mapper);
ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
JsonNode result;
result = service.doGetList(REPORTER, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(0, result.size());
manager.provideReporter("IR1");
manager.provideReporter("IR2");
result = service.doGetList(REPORTER, 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 JsonLightHttpServiceTest method testDoGet.
public void testDoGet() throws JmriException {
JsonLightHttpService service = new JsonLightHttpService(new ObjectMapper());
LightManager manager = InstanceManager.getDefault(LightManager.class);
Light light1 = manager.provideLight("IL1");
JsonNode result;
try {
result = service.doGet(JsonLight.LIGHT, "IL1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JsonLight.LIGHT, result.path(JSON.TYPE).asText());
Assert.assertEquals("IL1", result.path(JSON.DATA).path(JSON.NAME).asText());
Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
light1.setState(Light.ON);
result = service.doGet(JsonLight.LIGHT, "IL1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JSON.ON, result.path(JSON.DATA).path(JSON.STATE).asInt());
light1.setState(Light.OFF);
result = service.doGet(JsonLight.LIGHT, "IL1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException 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());
}
}
Aggregations