use of jmri.PowerManager in project JMRI by JMRI.
the class PowerManagerMenu method getManager.
public PowerManager getManager() {
// start with default
PowerManager manager = InstanceManager.getNullableDefault(jmri.PowerManager.class);
if (manager == null) {
return null;
}
String name = manager.getUserName();
// find active name
for (JMenuItem item : items) {
if (item.isSelected()) {
name = item.getActionCommand();
break;
}
}
// find PowerManager and return
List<PowerManager> managers = InstanceManager.getList(PowerManager.class);
for (PowerManager mgr : managers) {
if (name.equals(mgr.getUserName())) {
return mgr;
}
}
// should not happen
return null;
}
use of jmri.PowerManager 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.PowerManager in project JMRI by JMRI.
the class JsonPowerHttpServiceTest method testDoPost.
@Test
public void testDoPost() throws JmriException {
ObjectMapper mapper = new ObjectMapper();
JsonPowerHttpService service = new JsonPowerHttpService(mapper);
PowerManager power = InstanceManager.getDefault(PowerManager.class);
JsonNode result;
JsonNode message;
try {
power.setPower(PowerManager.UNKNOWN);
message = mapper.createObjectNode().put(JSON.STATE, JSON.ON);
result = service.doPost(JsonPowerServiceFactory.POWER, null, message, Locale.ENGLISH);
Assert.assertEquals(PowerManager.ON, power.getPower());
Assert.assertNotNull(result);
Assert.assertEquals(JSON.ON, result.path(JSON.DATA).path(JSON.STATE).asInt());
message = mapper.createObjectNode().put(JSON.STATE, JSON.OFF);
result = service.doPost(JsonPowerServiceFactory.POWER, null, message, Locale.ENGLISH);
Assert.assertEquals(PowerManager.OFF, power.getPower());
Assert.assertNotNull(result);
Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
message = mapper.createObjectNode().put(JSON.STATE, JSON.UNKNOWN);
result = service.doPost(JsonPowerServiceFactory.POWER, null, message, Locale.ENGLISH);
Assert.assertEquals(PowerManager.OFF, power.getPower());
Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
// Invalid value
message = mapper.createObjectNode().put(JSON.STATE, 42);
JsonException exception = null;
try {
service.doPost(JsonPowerServiceFactory.POWER, null, message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertEquals(PowerManager.OFF, power.getPower());
Assert.assertNotNull(exception);
Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
} catch (JsonException ex) {
log.error("Threw", ex);
Assert.fail("Unexpected Exception");
}
}
use of jmri.PowerManager 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.PowerManager in project JMRI by JMRI.
the class JsonPowerHttpServiceTest method testDoGet.
@Test
public void testDoGet() throws JmriException {
JsonPowerHttpService service = new JsonPowerHttpService(new ObjectMapper());
PowerManager power = InstanceManager.getDefault(PowerManager.class);
JsonNode result;
try {
power.setPower(PowerManager.UNKNOWN);
result = service.doGet(JsonPowerServiceFactory.POWER, null, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JsonPowerServiceFactory.POWER, result.path(JSON.TYPE).asText());
Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
power.setPower(PowerManager.ON);
result = service.doGet(JsonPowerServiceFactory.POWER, null, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JSON.ON, result.path(JSON.DATA).path(JSON.STATE).asInt());
power.setPower(PowerManager.OFF);
result = service.doGet(JsonPowerServiceFactory.POWER, null, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
} catch (JsonException ex) {
log.error("Threw", ex);
Assert.fail("Unexpected Exception");
}
}
Aggregations