use of jmri.server.json.JsonException 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.server.json.JsonException in project JMRI by JMRI.
the class JsonReporterHttpServiceTest method testDoPut.
@Test
public void testDoPut() {
ObjectMapper mapper = new ObjectMapper();
JsonReporterHttpService service = new JsonReporterHttpService(mapper);
ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
JsonNode message;
try {
// add a reporter
Assert.assertNull(manager.getReporter("IR1"));
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "close");
service.doPut(REPORTER, "IR1", message, Locale.ENGLISH);
Assert.assertNotNull(manager.getReporter("IR1"));
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonReporterHttpServiceTest method testDoGet.
@Test
public void testDoGet() throws JmriException {
JsonReporterHttpService service = new JsonReporterHttpService(new ObjectMapper());
ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
// no value
Reporter reporter1 = manager.provideReporter("IR1");
JsonNode result;
try {
result = service.doGet(REPORTER, "IR1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(REPORTER, result.path(JSON.TYPE).asText());
Assert.assertEquals("IR1", result.path(JSON.DATA).path(JSON.NAME).asText());
// JSON node has the text "null" if reporter is null
Assert.assertEquals("null", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
reporter1.setReport("throw");
result = service.doGet(REPORTER, "IR1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals("throw", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
reporter1.setReport("close");
result = service.doGet(REPORTER, "IR1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals("close", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonPowerHttpServiceTest method testDoGetList.
@Test
public void testDoGetList() {
try {
JsonNode result = (new JsonPowerHttpService(new ObjectMapper())).doGetList(JsonPowerServiceFactory.POWER, Locale.ENGLISH);
Assert.assertTrue(result.isArray());
Assert.assertEquals(1, result.size());
} catch (JsonException ex) {
log.error("Threw", ex);
Assert.fail("Unexpected Exception");
}
}
use of jmri.server.json.JsonException 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");
}
}
Aggregations