use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonTurnoutHttpService method doPost.
@Override
public JsonNode doPost(String type, String name, JsonNode data, Locale locale) throws JsonException {
Turnout turnout = InstanceManager.turnoutManagerInstance().getTurnout(name);
if (turnout == null) {
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", TURNOUT, name));
}
if (data.path(USERNAME).isTextual()) {
turnout.setUserName(data.path(USERNAME).asText());
}
if (data.path(INVERTED).isBoolean()) {
turnout.setInverted(data.path(INVERTED).asBoolean());
}
if (data.path(COMMENT).isTextual()) {
turnout.setComment(data.path(COMMENT).asText());
}
int state = data.path(STATE).asInt(UNKNOWN);
switch(state) {
case THROWN:
turnout.setCommandedState(Turnout.THROWN);
break;
case CLOSED:
turnout.setCommandedState(Turnout.CLOSED);
break;
case UNKNOWN:
// leave state alone in this case
break;
default:
throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", TURNOUT, state));
}
return this.doGet(type, name, locale);
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonLightHttpServiceTest method testDoPut.
public void testDoPut() {
ObjectMapper mapper = new ObjectMapper();
JsonLightHttpService service = new JsonLightHttpService(mapper);
LightManager manager = InstanceManager.getDefault(LightManager.class);
JsonNode message;
try {
// add a light
Assert.assertNull(manager.getLight("IL1"));
message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, Light.OFF);
service.doPut(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
Assert.assertNotNull(manager.getLight("IL1"));
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonLightHttpServiceTest method testDoPost.
public void testDoPost() throws JmriException {
ObjectMapper mapper = new ObjectMapper();
JsonLightHttpService service = new JsonLightHttpService(mapper);
LightManager manager = InstanceManager.getDefault(LightManager.class);
Light light1 = manager.provideLight("IL1");
JsonNode result;
JsonNode message;
try {
// set off
message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.OFF);
result = service.doPost(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
Assert.assertEquals(Light.OFF, light1.getState());
Assert.assertNotNull(result);
Assert.assertEquals(JSON.OFF, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set on
message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.ON);
result = service.doPost(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
Assert.assertEquals(Light.ON, light1.getState());
Assert.assertNotNull(result);
Assert.assertEquals(JSON.ON, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set unknown - remains on
message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, JSON.UNKNOWN);
result = service.doPost(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
Assert.assertEquals(Light.ON, light1.getState());
Assert.assertEquals(JSON.ON, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set invalid state
// Invalid value
message = mapper.createObjectNode().put(JSON.NAME, "IL1").put(JSON.STATE, 42);
JsonException exception = null;
try {
service.doPost(JsonLight.LIGHT, "IL1", message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertEquals(Light.ON, light1.getState());
Assert.assertNotNull(exception);
Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, exception.getCode());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonLightHttpServiceTest method testDoGetList.
public void testDoGetList() {
try {
ObjectMapper mapper = new ObjectMapper();
JsonLightHttpService service = new JsonLightHttpService(mapper);
LightManager manager = InstanceManager.getDefault(LightManager.class);
JsonNode result;
result = service.doGetList(JsonLight.LIGHT, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(0, result.size());
manager.provideLight("IL1");
manager.provideLight("IL2");
result = service.doGetList(JsonLight.LIGHT, 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 JsonReporterHttpServiceTest method testDoPost.
@Test
public void testDoPost() throws JmriException {
ObjectMapper mapper = new ObjectMapper();
JsonReporterHttpService service = new JsonReporterHttpService(mapper);
ReporterManager manager = InstanceManager.getDefault(ReporterManager.class);
Reporter reporter1 = manager.provideReporter("IR1");
JsonNode result;
JsonNode message;
try {
// set off
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "close");
result = service.doPost(REPORTER, "IR1", message, Locale.ENGLISH);
Assert.assertEquals("close", reporter1.getCurrentReport());
Assert.assertNotNull(result);
Assert.assertEquals("close", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
// set on
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JsonReporter.REPORT, "throw");
result = service.doPost(REPORTER, "IR1", message, Locale.ENGLISH);
Assert.assertEquals("throw", reporter1.getCurrentReport());
Assert.assertNotNull(result);
Assert.assertEquals("throw", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
// set null
message = mapper.createObjectNode().put(JSON.NAME, "IR1").putNull(JsonReporter.REPORT);
result = service.doPost(REPORTER, "IR1", message, Locale.ENGLISH);
Assert.assertNull(reporter1.getCurrentReport());
Assert.assertEquals("null", result.path(JSON.DATA).path(JsonReporter.REPORT).asText());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
Aggregations