use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonSensorHttpService method doGet.
@Override
public JsonNode doGet(String type, String name, Locale locale) throws JsonException {
ObjectNode root = mapper.createObjectNode();
root.put(JSON.TYPE, SENSOR);
Sensor sensor = InstanceManager.getDefault(SensorManager.class).getSensor(name);
// throws JsonException if sensor == null
ObjectNode data = this.getNamedBean(sensor, name, type, locale);
if (sensor != null) {
root.put(JSON.DATA, data);
switch(sensor.getKnownState()) {
case Sensor.ACTIVE:
data.put(JSON.STATE, JSON.ACTIVE);
break;
case Sensor.INACTIVE:
data.put(JSON.STATE, JSON.INACTIVE);
break;
case Sensor.INCONSISTENT:
data.put(JSON.STATE, JSON.INCONSISTENT);
break;
case Sensor.UNKNOWN:
data.put(JSON.STATE, JSON.UNKNOWN);
break;
default:
// NOI18N
throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, Bundle.getMessage(locale, "ErrorInternal", type));
}
}
return root;
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonRouteSocketServiceTest method testRouteChange.
public void testRouteChange() {
// the route state on a sensorless route
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IR1");
JsonRouteSocketService service = new JsonRouteSocketService(connection);
RouteManager manager = InstanceManager.getDefault(RouteManager.class);
Route route1 = manager.provideRoute("IR1", "Route1");
Sensor sensor1 = InstanceManager.getDefault(SensorManager.class).provideSensor("IS1");
sensor1.setKnownState(Sensor.UNKNOWN);
route1.setTurnoutsAlignedSensor(sensor1.getSystemName());
service.onMessage(JsonRouteServiceFactory.ROUTE, message, Locale.ENGLISH);
// TODO: test that service is listener in RouteManager
Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
sensor1.setKnownState(Sensor.ACTIVE);
JUnitUtil.waitFor(() -> {
return route1.getState() == Sensor.ACTIVE;
}, "Route to activate");
Assert.assertEquals(JSON.ACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
sensor1.setKnownState(Sensor.INACTIVE);
JUnitUtil.waitFor(() -> {
return route1.getState() == Sensor.INACTIVE;
}, "Route to deactivate");
Assert.assertEquals(Sensor.INACTIVE, route1.getState());
Assert.assertEquals(JSON.INACTIVE, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
service.onClose();
// TODO: test that service is no longer a listener in RouteManager
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonSensorHttpServiceTest method testDoPost.
public void testDoPost() throws JmriException {
ObjectMapper mapper = new ObjectMapper();
JsonSensorHttpService service = new JsonSensorHttpService(mapper);
SensorManager manager = InstanceManager.getDefault(SensorManager.class);
Sensor sensor1 = manager.provideSensor("IS1");
JsonNode result;
JsonNode message;
try {
// set ACTIVE
message = mapper.createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.ACTIVE);
result = service.doPost(JsonSensor.SENSOR, "IS1", message, Locale.ENGLISH);
Assert.assertEquals(Sensor.ACTIVE, sensor1.getKnownState());
Assert.assertNotNull(result);
// -1 is not a possible value
Assert.assertEquals(JSON.ACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
// set INACTIVE
message = mapper.createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INACTIVE);
result = service.doPost(JsonSensor.SENSOR, "IS1", message, Locale.ENGLISH);
Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
Assert.assertNotNull(result);
Assert.assertEquals(JSON.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
// set UNKNOWN
message = mapper.createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.UNKNOWN);
result = service.doPost(JsonSensor.SENSOR, "IS1", message, Locale.ENGLISH);
Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
Assert.assertEquals(JSON.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
// set INCONSISTENT
message = mapper.createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.INCONSISTENT);
result = service.doPost(JsonSensor.SENSOR, "IS1", message, Locale.ENGLISH);
Assert.assertEquals(Sensor.INACTIVE, sensor1.getKnownState());
Assert.assertEquals(JSON.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt(-1));
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonSensorHttpServiceTest method testDoPut.
public void testDoPut() {
ObjectMapper mapper = new ObjectMapper();
JsonSensorHttpService service = new JsonSensorHttpService(mapper);
SensorManager manager = InstanceManager.getDefault(SensorManager.class);
JsonNode message;
try {
// add a sensor
Assert.assertNull(manager.getSensor("IS1"));
message = mapper.createObjectNode().put(JSON.NAME, "IS1").put(JSON.STATE, JSON.UNKNOWN);
service.doPut(JsonSensor.SENSOR, "IS1", message, Locale.ENGLISH);
Assert.assertNotNull(manager.getSensor("IS1"));
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonSensorHttpServiceTest method testDoGetList.
public void testDoGetList() {
try {
ObjectMapper mapper = new ObjectMapper();
JsonSensorHttpService service = new JsonSensorHttpService(mapper);
SensorManager manager = InstanceManager.getDefault(SensorManager.class);
JsonNode result;
result = service.doGetList(JsonSensor.SENSOR, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(0, result.size());
manager.provideSensor("IS1");
manager.provideSensor("IS2");
result = service.doGetList(JsonSensor.SENSOR, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(2, result.size());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
Aggregations