use of jmri.JmriException in project JMRI by JMRI.
the class JsonPowerSocketServiceTest method testPowerChange.
public void testPowerChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message = connection.getObjectMapper().readTree("{}");
JsonPowerSocketService service = new JsonPowerSocketService(connection);
PowerManager power = InstanceManager.getDefault(PowerManager.class);
power.setPower(PowerManager.UNKNOWN);
service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
// TODO: test that service is listener in PowerManager
Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
power.setPower(PowerManager.ON);
Assert.assertEquals(JSON.ON, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
power.setPower(PowerManager.OFF);
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 PowerManager
} catch (IOException | JmriException | JsonException ex) {
log.error("testPowerChange threw", ex);
Assert.fail("Unexpected Exception");
}
}
use of jmri.JmriException 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.JmriException 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.JmriException 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.JmriException 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());
}
}
Aggregations