use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonMemoryHttpServiceTest method testDoGetList.
public void testDoGetList() {
try {
ObjectMapper mapper = new ObjectMapper();
JsonMemoryHttpService service = new JsonMemoryHttpService(mapper);
MemoryManager manager = InstanceManager.getDefault(MemoryManager.class);
JsonNode result;
result = service.doGetList(JsonMemory.MEMORY, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(0, result.size());
manager.provideMemory("IM1");
manager.provideMemory("IM2");
result = service.doGetList(JsonMemory.MEMORY, 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 JsonMemoryHttpServiceTest method testDoPost.
public void testDoPost() throws JmriException {
ObjectMapper mapper = new ObjectMapper();
JsonMemoryHttpService service = new JsonMemoryHttpService(mapper);
MemoryManager manager = InstanceManager.getDefault(MemoryManager.class);
Memory memory1 = manager.provideMemory("IM1");
JsonNode result;
JsonNode message;
try {
// set off
message = mapper.createObjectNode().put(JSON.NAME, "IM1").put(JSON.VALUE, "close");
result = service.doPost(JsonMemory.MEMORY, "IM1", message, Locale.ENGLISH);
Assert.assertEquals("close", memory1.getValue());
Assert.assertNotNull(result);
Assert.assertEquals("close", result.path(JSON.DATA).path(JSON.VALUE).asText());
// set on
message = mapper.createObjectNode().put(JSON.NAME, "IM1").put(JSON.VALUE, "throw");
result = service.doPost(JsonMemory.MEMORY, "IM1", message, Locale.ENGLISH);
Assert.assertEquals("throw", memory1.getValue());
Assert.assertNotNull(result);
Assert.assertEquals("throw", result.path(JSON.DATA).path(JSON.VALUE).asText());
// set null
message = mapper.createObjectNode().put(JSON.NAME, "IM1").putNull(JSON.VALUE);
result = service.doPost(JsonMemory.MEMORY, "IM1", message, Locale.ENGLISH);
Assert.assertNull(memory1.getValue());
Assert.assertEquals("null", result.path(JSON.DATA).path(JSON.VALUE).asText());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException 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.JsonException 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");
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonRosterSocketServiceTest method testOnMessageDeleteRoster.
/**
* Test of onMessage method DELETE on a ROSTER
*
* @throws java.io.IOException this is an error, not a failure, in the test
* @throws jmri.JmriException this is an error, not a failure, in the test
*/
@Test
public void testOnMessageDeleteRoster() throws IOException, JmriException {
JsonNode data = this.connection.getObjectMapper().createObjectNode().put(JSON.METHOD, JSON.DELETE);
Locale locale = Locale.ENGLISH;
JsonException exception = null;
JsonRosterSocketService instance = new JsonRosterSocketService(this.connection);
try {
instance.onMessage(JsonRoster.ROSTER, data, locale);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
Assert.assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, exception.getCode());
}
Aggregations