Commit b30235e4 authored by Alexander Nielsen's avatar Alexander Nielsen
Browse files

Cleaned up code and used org.json library for JSON API

parent 4a74d1de
Pipeline #65860 passed with stages
in 3 minutes and 3 seconds
......@@ -52,10 +52,10 @@ public class QueryProcessor {
for (Entry<String, String> entry : data.entrySet()) {
if (query.contains(entry.getKey())) {
return entry.getValue() + System.lineSeparator();
return entry.getValue();
}
}
return dynamicQuery.query(query).map(s -> s + System.lineSeparator()).orElse("");
return dynamicQuery.query(query).orElse("");
}
}
......@@ -48,7 +48,7 @@ public class WebServer {
break;
case "json":
page = new APIResultPage(query, answer);
page = new JSONResultPage(query, answer);
break;
default:
......
package ic.doc.web;
import org.json.JSONObject;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class APIResultPage implements Page {
public class JSONResultPage implements Page {
private final String query;
private final String answer;
public APIResultPage(String query, String answer) {
public JSONResultPage(String query, String answer) {
this.query = query;
this.answer = answer;
}
......@@ -19,24 +21,20 @@ public class APIResultPage implements Page {
resp.setContentType("application/json");
PrintWriter writer = resp.getWriter();
StringBuilder sb = new StringBuilder();
boolean success = !(answer == null || answer.isEmpty());
sb.append("{");
sb.append("\"success\": \"").append(success).append("\"");
JSONObject result = new JSONObject();
result.put("success", success);
if (success) {
sb.append(", ");
sb.append("\"result\":");
sb.append("{").append("\"query\": \"").append(query).append("\", ").append("\"answer\": \"")
.append(answer.replace("\n", "").replace("\r", "")).append("\"}");
}
JSONObject data = new JSONObject();
data.put("query", query);
data.put("answer", answer);
sb.append("}");
result.put("result", data);
}
writer.println(sb.toString());
writer.print(result.toString());
}
}
package ic.doc;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
public class WebServerTest {
final WebServer.Website website = new WebServer.Website();
......@@ -50,6 +51,21 @@ public class WebServerTest {
website.doGet(req, resp);
}
private JSONObject getModelJSONObject() {
JSONObject modelResult = new JSONObject();
JSONObject modelData = new JSONObject();
modelData.put("query", "shakespeare");
modelData.put("answer", "William Shakespeare (26 April 1564 - 23 April 1616) was an English poet, playwright, " +
"and actor, widely regarded as the greatest writer in the English language and the world's pre-eminent " +
"dramatist.");
modelResult.put("success", true);
modelResult.put("result", modelData);
return modelResult;
}
@Test
public void correctContentTypeForHTML() throws IOException {
checkContentType("html", "text/html");
......@@ -121,10 +137,7 @@ public class WebServerTest {
website.doGet(req, resp);
Assert.assertEquals("{\"success\": \"true\", \"result\":{\"query\": \"shakespeare\", \"answer\": " +
"\"William Shakespeare (26 April 1564 - 23 April 1616) was an English poet, playwright, and actor, " +
"widely regarded as the greatest writer in the English language and the world's pre-eminent dramatist.\"}}",
writer.writtenData);
Assert.assertTrue(getModelJSONObject().similar(new JSONObject(writer.writtenData)));
}
private static class CustomPrintWriter extends PrintWriter {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment