[VIVO-1320] Convert some org.json usages to Jackson

This commit is contained in:
Graham Triggs 2017-09-15 16:07:26 +01:00
parent 58cba471a8
commit 826fb9c570
3 changed files with 29 additions and 38 deletions

View file

@ -20,7 +20,6 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.apache.jena.ontology.OntModel; import org.apache.jena.ontology.OntModel;
import org.apache.jena.query.Query; import org.apache.jena.query.Query;
@ -107,7 +106,7 @@ class ProfileAutoCompleter extends AbstractAjaxResponder implements
} }
@Override @Override
public String prepareResponse() throws IOException, JSONException { public String prepareResponse() throws IOException {
if (term.isEmpty()) { if (term.isEmpty()) {
return EMPTY_RESPONSE; return EMPTY_RESPONSE;
} }

View file

@ -19,7 +19,6 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import edu.cornell.mannlib.vitro.webapp.application.ApplicationUtils; import edu.cornell.mannlib.vitro.webapp.application.ApplicationUtils;
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties; import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
@ -80,7 +79,7 @@ public class BasicProfilesGetter extends AbstractAjaxResponder {
} }
@Override @Override
public String prepareResponse() throws IOException, JSONException { public String prepareResponse() throws IOException {
log.debug("search term is '" + term + "'"); log.debug("search term is '" + term + "'");
if (this.term.isEmpty() || this.profileTypes.isEmpty()) { if (this.term.isEmpty() || this.profileTypes.isEmpty()) {
return EMPTY_RESPONSE; return EMPTY_RESPONSE;

View file

@ -11,9 +11,10 @@ import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Level; import org.apache.log4j.Level;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -149,19 +150,16 @@ public class JsonServletTest extends AbstractTestClass {
*/ */
@Ignore @Ignore
@Test @Test
public void vclassesClassgroupNotRecognized() throws ServletException, public void vclassesClassgroupNotRecognized() throws ServletException, IOException {
IOException {
req.addParameter(GET_VCLASSES_FOR_VCLASS_GROUP, "true"); req.addParameter(GET_VCLASSES_FOR_VCLASS_GROUP, "true");
req.addParameter("classgroupUri", "http://bogusUri"); req.addParameter("classgroupUri", "http://bogusUri");
servlet.service(req, resp); servlet.service(req, resp);
assertEquals("empty response", "", resp.getOutput()); assertEquals("empty response", "", resp.getOutput());
assertEquals("status=failure", SC_INTERNAL_SERVER_ERROR, assertEquals("status=failure", SC_INTERNAL_SERVER_ERROR, resp.getStatus());
resp.getStatus());
} }
@Test @Test
public void individualsByClassNoVClass() throws ServletException, public void individualsByClassNoVClass() throws ServletException, IOException {
IOException {
setLoggerLevel(JsonServlet.class, Level.FATAL); setLoggerLevel(JsonServlet.class, Level.FATAL);
setLoggerLevel(JsonObjectProducer.class, Level.FATAL); setLoggerLevel(JsonObjectProducer.class, Level.FATAL);
req.addParameter(GET_SEARCH_INDIVIDUALS_BY_VCLASS, "true"); req.addParameter(GET_SEARCH_INDIVIDUALS_BY_VCLASS, "true");
@ -171,8 +169,7 @@ public class JsonServletTest extends AbstractTestClass {
} }
@Test @Test
public void individualsByClassUnrecognizedVClass() throws ServletException, public void individualsByClassUnrecognizedVClass() throws ServletException, IOException {
IOException {
setLoggerLevel(JsonServlet.class, Level.FATAL); setLoggerLevel(JsonServlet.class, Level.FATAL);
setLoggerLevel(JsonObjectProducer.class, Level.FATAL); setLoggerLevel(JsonObjectProducer.class, Level.FATAL);
String vclassId = "http://bogusVclass"; String vclassId = "http://bogusVclass";
@ -191,8 +188,7 @@ public class JsonServletTest extends AbstractTestClass {
* is required as a response to a request. * is required as a response to a request.
*/ */
@Test @Test
public void individualsByClassNoIndividuals() throws ServletException, public void individualsByClassNoIndividuals() throws ServletException, IOException {
IOException {
setLoggerLevel(JsonServlet.class, Level.FATAL); setLoggerLevel(JsonServlet.class, Level.FATAL);
setLoggerLevel(ModelAccess.class, Level.ERROR); setLoggerLevel(ModelAccess.class, Level.ERROR);
String vclassId = "http://myVclass"; String vclassId = "http://myVclass";
@ -214,42 +210,39 @@ public class JsonServletTest extends AbstractTestClass {
*/ */
private void assertFailureWithErrorMessage(String expected) { private void assertFailureWithErrorMessage(String expected) {
try { try {
JSONObject result = new JSONObject(resp.getOutput()); ObjectMapper mapper = new ObjectMapper();
assertEquals("errorMessage", expected, JsonNode result = mapper.readTree(resp.getOutput());
getFieldValue(result, "errorMessage")); assertEquals("errorMessage", expected, getFieldValue(result, "errorMessage").asText());
assertEquals("status", SC_INTERNAL_SERVER_ERROR, resp.getStatus()); assertEquals("status", SC_INTERNAL_SERVER_ERROR, resp.getStatus());
} catch (JSONException e) { } catch (JsonProcessingException e) {
fail(e.toString());
} catch (IOException e) {
fail(e.toString()); fail(e.toString());
} }
} }
private void assertSuccessWithIndividuals(String vclassId, int count) { private void assertSuccessWithIndividuals(String vclassId, int count) {
try { try {
JSONObject actual = new JSONObject(resp.getOutput()); ObjectMapper mapper = new ObjectMapper();
assertEquals("errorMessage", "", JsonNode actual = mapper.readTree(resp.getOutput());
getFieldValue(actual, "errorMessage")); assertEquals("errorMessage", "", getFieldValue(actual, "errorMessage").asText());
assertEquals("count", count, getFieldValue(actual, "totalCount")); assertEquals("count", count, getFieldValue(actual, "totalCount").asInt());
JSONObject vclassObj = (JSONObject) getFieldValue(actual, "vclass"); JsonNode vclassObj = getFieldValue(actual, "vclass");
assertEquals("vclass name", vclassId.split("://")[1], assertEquals("vclass name", vclassId.split("://")[1], getFieldValue(vclassObj, "name").asText());
getFieldValue(vclassObj, "name")); assertEquals("vclass uri", vclassId, getFieldValue(vclassObj, "URI").asText());
assertEquals("vclass uri", vclassId,
getFieldValue(vclassObj, "URI"));
assertEquals("status", SC_OK, resp.getStatus()); assertEquals("status", SC_OK, resp.getStatus());
} catch (JSONException e) { } catch (JsonProcessingException e) {
fail(e.toString());
} catch (IOException e) {
fail(e.toString()); fail(e.toString());
} }
} }
private Object getFieldValue(JSONObject json, String fieldName) { private JsonNode getFieldValue(JsonNode json, String fieldName) {
try { assertEquals("find " + fieldName, true, json.has(fieldName));
assertEquals("find " + fieldName, true, json.has(fieldName)); return json.get(fieldName);
return json.get(fieldName);
} catch (JSONException e) {
fail(e.toString());
return -1;
}
} }
} }