Commit 4a74d1de authored by Pablo Gamito's avatar Pablo Gamito
Browse files

Added tests for actual content of files

parent b40de9e6
Pipeline #65814 passed with stages
in 3 minutes
......@@ -10,6 +10,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
......@@ -19,6 +20,8 @@ public class WebServerTest {
final Mockery context = new Mockery();
final HttpServletRequest req = context.mock(HttpServletRequest.class);
final HttpServletResponse resp = context.mock(HttpServletResponse.class);
final NullServletOutputStream outputStream = new NullServletOutputStream();
final CustomPrintWriter writer = new CustomPrintWriter(outputStream);
@Before
public void setUp() throws IOException {
......@@ -27,9 +30,9 @@ public class WebServerTest {
oneOf(req).getParameter("q");
will(returnValue("shakespeare"));
allowing(resp).getWriter();
will(returnValue(new PrintWriter(new NullOutputStream())));
will(returnValue(writer));
allowing(resp).getOutputStream();
will(returnValue(new NullServletOutputStream()));
will(returnValue(outputStream));
}
});
}
......@@ -67,13 +70,84 @@ public class WebServerTest {
checkContentType("json", "application/json");
}
private static class NullOutputStream extends OutputStream {
@Test
public void correctContentForHtml() throws IOException {
context.checking(new Expectations() {
{
oneOf(req).getParameter("format");
will(returnValue("html"));
allowing(req).getParameter("background_colour");
allowing(resp);
}
});
website.doGet(req, resp);
Assert.assertTrue(writer.writtenData.contains("<html>"));
Assert.assertTrue(writer.writtenData.contains("shakespeare"));
Assert.assertTrue(writer.writtenData.contains("greatest writer"));
}
@Test
public void correctContentForMarkdown() throws IOException {
context.checking(new Expectations() {
{
oneOf(req).getParameter("format");
will(returnValue("md"));
allowing(req).getParameter("background_colour");
allowing(resp);
}
});
website.doGet(req, resp);
System.out.println(writer.writtenData);
Assert.assertTrue(writer.writtenData.charAt(0) == '#');
Assert.assertTrue(writer.writtenData.contains("shakespeare"));
Assert.assertTrue(writer.writtenData.contains("greatest writer"));
}
@Test
public void correctContentForJSON() throws IOException {
context.checking(new Expectations() {
{
oneOf(req).getParameter("format");
will(returnValue("json"));
allowing(req).getParameter("background_colour");
allowing(resp);
}
});
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);
}
private static class CustomPrintWriter extends PrintWriter {
String writtenData = "";
public CustomPrintWriter(OutputStream out) {
super(out);
}
@Override
public void write(int b) throws IOException {
public void println(String s) {
writtenData += s;
}
@Override
public void print(String s) {
writtenData += s;
}
}
private static class NullServletOutputStream extends ServletOutputStream {
@Override
public void write(int b) throws IOException {
}
......
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