use of jmri.Memory 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());
}
}
Aggregations