use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonRouteHttpServiceTest method testDoGetWithRouteSensor.
public void testDoGetWithRouteSensor() throws JmriException {
JsonRouteHttpService service = new JsonRouteHttpService(new ObjectMapper());
RouteManager manager = InstanceManager.getDefault(RouteManager.class);
Route route1 = manager.provideRoute("IR1", "Route1");
Sensor sensor1 = InstanceManager.getDefault(SensorManager.class).provideSensor("IS1");
route1.setTurnoutsAlignedSensor(sensor1.getSystemName());
JsonNode result;
try {
result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JsonRouteServiceFactory.ROUTE, result.path(JSON.TYPE).asText());
Assert.assertEquals("IR1", result.path(JSON.DATA).path(JSON.NAME).asText());
Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
sensor1.setKnownState(Sensor.ACTIVE);
result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JSON.ACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt());
sensor1.setKnownState(Sensor.INACTIVE);
result = service.doGet(JsonRouteServiceFactory.ROUTE, "IR1", Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JSON.INACTIVE, result.path(JSON.DATA).path(JSON.STATE).asInt());
} catch (JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonRouteHttpServiceTest method testDoGetList.
public void testDoGetList() {
try {
ObjectMapper mapper = new ObjectMapper();
JsonRouteHttpService service = new JsonRouteHttpService(mapper);
RouteManager manager = InstanceManager.getDefault(RouteManager.class);
JsonNode result;
result = service.doGetList(JsonRouteServiceFactory.ROUTE, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(0, result.size());
manager.provideRoute("IR1", "Route1");
manager.provideRoute("IR2", "Route2");
result = service.doGetList(JsonRouteServiceFactory.ROUTE, 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 JsonRouteHttpServiceTest method testDoPostWithoutRouteSensor.
public void testDoPostWithoutRouteSensor() throws JmriException {
ObjectMapper mapper = new ObjectMapper();
JsonRouteHttpService service = new JsonRouteHttpService(mapper);
RouteManager manager = InstanceManager.getDefault(RouteManager.class);
Route route1 = manager.provideRoute("IR1", "Route1");
Assert.assertNull(route1.getTurnoutsAlgdSensor());
JsonNode result;
JsonNode message;
try {
// set off
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.INACTIVE);
result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set on
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.ACTIVE);
result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set unknown
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.UNKNOWN);
result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set toggle
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, JSON.TOGGLE);
result = service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
Assert.assertEquals(JSON.UNKNOWN, result.path(JSON.DATA).path(JSON.STATE).asInt());
// set invalid state
// Invalid value
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, 42);
JsonException exception = null;
try {
service.doPost(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
} catch (JsonException ex) {
exception = ex;
}
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 JsonRouteHttpServiceTest method testDoPut.
public void testDoPut() {
ObjectMapper mapper = new ObjectMapper();
JsonRouteHttpService service = new JsonRouteHttpService(mapper);
RouteManager manager = InstanceManager.getDefault(RouteManager.class);
JsonNode message;
JsonException exception = null;
try {
// add a route
Assert.assertNull(manager.getRoute("IR1"));
message = mapper.createObjectNode().put(JSON.NAME, "IR1").put(JSON.STATE, Sensor.INACTIVE);
service.doPut(JsonRouteServiceFactory.ROUTE, "IR1", message, Locale.ENGLISH);
Assert.assertNull(manager.getRoute("IR1"));
} catch (JsonException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
Assert.assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, exception.getCode());
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonRosterHttpServiceTest method testDoPost.
/**
* Test of doPost method, of class JsonRosterHttpService.
*
* @throws java.lang.Exception
*/
@Test
public void testDoPost() throws Exception {
JsonRosterHttpService instance = new JsonRosterHttpService(this.objectMapper);
JsonException exception = null;
try {
instance.doPost(JsonRoster.ROSTER, null, null, locale);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
Assert.assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, exception.getCode());
try {
instance.doPost(TEST_GROUP1, null, null, locale);
} catch (JsonException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, exception.getCode());
}
Aggregations