use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtilHttpServiceTest method testGetMetadata_Locale_String.
/**
* Test of getMetadata method, of class JsonUtilHttpService.
*/
@Test
public void testGetMetadata_Locale_String() {
Locale locale = Locale.ENGLISH;
ObjectMapper mapper = new ObjectMapper();
JsonUtilHttpService instance = new JsonUtilHttpService(mapper);
JsonNode result;
try {
for (String metadata : Metadata.getSystemNameList()) {
result = instance.getMetadata(locale, metadata);
Assert.assertEquals(JSON.METADATA, result.path(JSON.TYPE).asText());
Assert.assertEquals(metadata, result.path(JSON.DATA).path(JSON.NAME).asText());
Assert.assertEquals(Metadata.getBySystemName(metadata), result.path(JSON.DATA).path(JSON.VALUE).asText());
}
} catch (JsonException ex) {
log.error("Unexpected exception.", ex);
Assert.fail("Unexpected exception");
}
JsonException exception = null;
try {
instance.getMetadata(locale, "invalid_metadata_entry");
} catch (JsonException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtilHttpServiceTest method testGetNetworkService.
/**
* Test of getNetworkService method, of class JsonUtilHttpService.
*
* @throws jmri.server.json.JsonException if test fails in an unexpected
* manner
*/
@Test
public void testGetNetworkService() throws JsonException {
Locale locale = Locale.ENGLISH;
ObjectMapper mapper = new ObjectMapper();
JsonUtilHttpService instance = new JsonUtilHttpService(mapper);
JsonNode result = null;
// non-existant service
JsonException exception = null;
try {
// NOI18N
result = instance.getNetworkService(locale, "non-existant-service");
} catch (JsonException ex) {
exception = ex;
}
Assert.assertNull(result);
Assert.assertNotNull(exception);
Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, exception.getCode());
// published service
ZeroConfService service = ZeroConfService.create(JSON.ZEROCONF_SERVICE_TYPE, 9999);
service.publish();
Assume.assumeTrue("Published ZeroConf Service", JUnitUtil.waitFor(() -> {
return service.isPublished() == true;
}));
result = instance.getNetworkService(locale, JSON.ZEROCONF_SERVICE_TYPE);
Assert.assertEquals(JSON.NETWORK_SERVICE, result.path(JSON.TYPE).asText());
JsonNode data = result.path(JSON.DATA);
Assert.assertFalse(data.isMissingNode());
Assert.assertEquals(WebServerPreferences.getDefault().getRailRoadName(), data.path(JSON.NAME).asText());
Assert.assertEquals(9999, data.path(JSON.PORT).asInt());
Assert.assertEquals(JSON.ZEROCONF_SERVICE_TYPE, data.path(JSON.TYPE).asText());
Assert.assertEquals(NodeIdentity.identity(), data.path(JSON.NODE).asText());
Assert.assertEquals(Metadata.getBySystemName(Metadata.JMRIVERCANON), data.path("jmri").asText());
Assert.assertEquals(Metadata.getBySystemName(Metadata.JMRIVERSION), data.path("version").asText());
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonTurnoutHttpServiceTest method testDoGetList.
public void testDoGetList() {
try {
ObjectMapper mapper = new ObjectMapper();
JsonTurnoutHttpService service = new JsonTurnoutHttpService(mapper);
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
JsonNode result;
result = service.doGetList(JsonTurnoutServiceFactory.TURNOUT, Locale.ENGLISH);
Assert.assertNotNull(result);
Assert.assertEquals(0, result.size());
manager.provideTurnout("IT1");
manager.provideTurnout("IT2");
result = service.doGetList(JsonTurnoutServiceFactory.TURNOUT, 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 JsonTurnoutSocketServiceTest method testTurnoutChange.
public void testTurnoutChange() {
try {
JsonMockConnection connection = new JsonMockConnection((DataOutputStream) null);
JsonNode message = connection.getObjectMapper().createObjectNode().put(JSON.NAME, "IT1");
JsonTurnoutSocketService service = new JsonTurnoutSocketService(connection);
TurnoutManager manager = InstanceManager.getDefault(TurnoutManager.class);
Turnout turnout1 = manager.provideTurnout("IT1");
turnout1.setCommandedState(Turnout.UNKNOWN);
service.onMessage(JsonTurnoutServiceFactory.TURNOUT, message, Locale.ENGLISH);
// TODO: test that service is listener in TurnoutManager
Assert.assertEquals(JSON.UNKNOWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
turnout1.setCommandedState(Turnout.CLOSED);
JUnitUtil.waitFor(() -> {
return turnout1.getKnownState() == Turnout.CLOSED;
}, "Turnout to close");
Assert.assertEquals(Turnout.CLOSED, turnout1.getKnownState());
Assert.assertEquals(JSON.CLOSED, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
turnout1.setCommandedState(Turnout.THROWN);
JUnitUtil.waitFor(() -> {
return turnout1.getKnownState() == Turnout.THROWN;
}, "Turnout to throw");
Assert.assertEquals(JSON.THROWN, connection.getMessage().path(JSON.DATA).path(JSON.STATE).asInt());
service.onClose();
// TODO: test that service is no longer a listener in TurnoutManager
} catch (IOException | JmriException | JsonException ex) {
Assert.fail(ex.getMessage());
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method getLocation.
/**
*
* @param locale the client's locale
* @param id the location ID
* @return the JSON representation of a Location
* @throws JsonException if the location cannot be located by ID
* @deprecated since 4.5.6
*/
@Deprecated
public static JsonNode getLocation(Locale locale, String id) throws JsonException {
ObjectNode root = mapper.createObjectNode();
root.put(TYPE, LOCATION);
ObjectNode data = root.putObject(DATA);
try {
Location location = LocationManager.instance().getLocationById(id);
data.put(NAME, location.getName());
data.put(ID, location.getId());
data.put(LENGTH, location.getLength());
data.put(COMMENT, location.getComment());
} catch (NullPointerException e) {
log.error("Unable to get location id [{}].", id);
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", LOCATION, id));
}
return root;
}
Aggregations