use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonPowerSocketServiceTest method testOnMessageChange.
public void testOnMessageChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
// Power ON
JsonNode message = connection.getObjectMapper().readTree("{\"state\":2}");
JsonPowerSocketService service = new JsonPowerSocketService(connection);
PowerManager power = InstanceManager.getDefault(PowerManager.class);
power.setPower(PowerManager.UNKNOWN);
service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
Assert.assertEquals(PowerManager.ON, power.getPower());
// Power OFF
message = connection.getObjectMapper().readTree("{\"state\":4}");
service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
Assert.assertEquals(PowerManager.OFF, power.getPower());
// JSON Power UNKNOWN
message = connection.getObjectMapper().readTree("{\"state\":0}");
service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
// did not change
Assert.assertEquals(PowerManager.OFF, power.getPower());
// JSON Invalid
message = connection.getObjectMapper().readTree("{\"state\":1}");
JsonException exception = null;
try {
service.onMessage(JsonPowerServiceFactory.POWER, message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
// did not change
Assert.assertEquals(PowerManager.OFF, power.getPower());
Assert.assertNotNull(exception);
Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
} catch (IOException | JmriException | JsonException ex) {
log.error("testOnMessageChange threw", ex);
Assert.fail("Unexpected Exception");
}
}
use of jmri.server.json.JsonMockConnection in project JMRI by JMRI.
the class JsonPowerSocketServiceTest method testCtorSuccess.
public void testCtorSuccess() {
JsonPowerSocketService service = new JsonPowerSocketService(new JsonMockConnection((DataOutputStream) null));
Assert.assertNotNull(service);
}
Aggregations