Commit f5ca1129 authored by Nanfeng Liu's avatar Nanfeng Liu
Browse files

Adding the test cases

parent 9bdde651
Pipeline #63719 canceled with stages
in 15 minutes and 58 seconds
package ic.doc;
import ic.doc.web.NoneIndexPage;
import ic.doc.web.Page;
import java.io.BufferedWriter;
import java.io.File;
......@@ -9,22 +10,18 @@ import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
public class MarkDownCreator implements Page {
private final String query;
private String answer;
public MarkDownCreator(String query, String answer) {
this.query = query;
public class MarkDownPage extends NoneIndexPage {
public MarkDownPage(String query, String answer) {
super(query,answer);
if (answer == null || answer.isEmpty()) {
answer = "# Sorry\nSorry, we didn't understand " + query;
}else{
answer ="# "+ query + "\n" + answer;
}
this.answer = answer;
}
public static File toMarkDown(String result, String query) throws IOException {
if (result == null || result.isEmpty()) {
result = "# Sorry\nSorry, we didn't understand " + query;
}else{
result ="# "+ query + "\n" + result;
}
File temp = File.createTempFile(query,".md",null);
System.out.println(temp.getAbsolutePath());
BufferedWriter writer = new BufferedWriter(new FileWriter(temp));
......
package ic.doc;
import static ic.doc.MarkDownCreator.toMarkDown;
import static ic.doc.MarkDownPage.toMarkDown;
import ic.doc.web.NoneIndexPage;
import ic.doc.web.Page;
import java.io.*;
import javax.servlet.http.HttpServletResponse;
public class PdfCreator implements Page {
private final String query;
private String answer;
public class PdfPage extends MarkDownPage {
public PdfPage(String query, String answer) {
super(query,answer);
public PdfCreator(String query, String answer) {
this.query = query;
this.answer = answer;
}
@Override
public void writeTo(HttpServletResponse resp) throws IOException, InterruptedException {
resp.setContentType("application/pdf");
OutputStream writer = resp.getOutputStream();
File markdown = toMarkDown(answer, query);
// turn markdown to a pdf using pandoc
public static File create_pdf(File markdown,String query) throws IOException{
ProcessBuilder pb = new ProcessBuilder("pandoc",markdown.getName(), "-o",query + ".pdf");
File dir = new File("/tmp");
pb.directory(dir);
System.out.println("The new pdf will have name of "+query + ".pdf");
Process process = pb.start();
System.out.println("The process returns the result :" + process.waitFor());
System.out.println("=================================================");
String[] strs = dir.list();
for(String str: strs){
System.out.println(str);
try{
System.out.println("The process returns the result :" + process.waitFor());
}catch (InterruptedException e){
}
System.out.println("=================================================");
File pdf = new File("/tmp/"+query+".pdf");
return new File("/tmp/"+query+".pdf");
}
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("application/pdf");
OutputStream writer = resp.getOutputStream();
File markdown = toMarkDown(answer, query);
File pdf = create_pdf(markdown, query);
writer.write(new FileInputStream(pdf).readAllBytes());
writer.close();
boolean md = markdown.delete();
boolean pd = pdf.delete();
if(md && pd){
System.out.println("Temporary files successfully deleted");
}
}
}
package ic.doc;
import ic.doc.web.HTMLResultPage;
import ic.doc.web.IndexPage;
import ic.doc.web.Page;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
......@@ -11,9 +13,8 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class WebServer {
public WebServer() throws Exception {
Server server = new Server(5000);//Integer.valueOf(System.getenv("PORT")));
Server server = new Server(Integer.valueOf(System.getenv("PORT")));
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(new ServletHolder(new Website()), "/*");
server.setHandler(handler);
......@@ -26,29 +27,33 @@ public class WebServer {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException{
String query = req.getParameter("q");
String type = req.getParameter("t");
execute(query, type, resp);
Page p = execute(query, type);
try{
p.writeTo(resp);
}catch (InterruptedException e){
}
}
}
public static void execute(String query, String type, HttpServletResponse resp)throws IOException{
if (query == null) {
new IndexPage().writeTo(resp);
} else {
String result = new QueryProcessor().process(query);
if(type == null){
new HTMLResultPage(query, result).writeTo(resp);
}else if (type.equals("markdown")){
new MarkDownCreator(query, result).writeTo(resp);
}else{
try{
new PdfCreator(query, result).writeTo(resp);
}catch (InterruptedException e){
public static Page execute(String query, String type) {
if (query == null) {
return new IndexPage();
} else {
String result = new QueryProcessor().process(query);
if(type == null){
return new HTMLResultPage(query, result);
}else if (type.equals("markdown")){
return new MarkDownPage(query, result);
}else{
return new PdfPage(query, result);
}
}
}
}
public static void main(String[] args) throws Exception {
new WebServer();
}
......
......@@ -4,14 +4,18 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HTMLResultPage implements Page {
private final String query;
private final String answer;
public class HTMLResultPage extends NoneIndexPage {
public HTMLResultPage(String query, String answer) {
this.query = query;
super(query,answer);
if (answer == null || answer.isEmpty()) {
answer = "<h1>Sorry</h1>\n<p>Sorry, we didn't understand <em>"+ query + "</em></p>";
} else {
answer = "<h1>" + query + "</h1>\n<p>"
+ answer.replace("\n", "<br>") + "</p>";
}
this.answer = answer;
}
public void writeTo(HttpServletResponse resp) throws IOException {
......@@ -24,13 +28,7 @@ public class HTMLResultPage implements Page {
writer.println("<body>");
// Content
if (answer == null || answer.isEmpty()) {
writer.println("<h1>Sorry</h1>");
writer.print("<p>Sorry, we didn't understand <em>" + query + "</em></p>");
} else {
writer.println("<h1>" + query + "</h1>");
writer.println("<p>" + answer.replace("\n", "<br>") + "</p>");
}
writer.println(answer);
writer.println("<p><a href=\"/\">Back to Search Page</a></p>");
......
......@@ -6,7 +6,6 @@ import java.io.PrintWriter;
public class IndexPage implements Page {
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
......@@ -32,5 +31,10 @@ public class IndexPage implements Page {
writer.println("</body>");
writer.println("</html>");
}
@Override
public String getAnswer(){
return null;
}
}
package ic.doc.web;
public abstract class NoneIndexPage implements Page{
protected final String query;
protected String answer;
public NoneIndexPage(String query, String answer) {
this.query = query;
this.answer = answer;
}
@Override
public String getAnswer(){
return answer;
}
}
......@@ -5,4 +5,5 @@ import java.io.IOException;
public interface Page {
void writeTo(HttpServletResponse resp) throws IOException, InterruptedException;
String getAnswer();
}
package ic.doc;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class MarkDownTest {
@Test
public void markdownRadioButtonWorking() throws Exception {
//TODO: I dont know how to test
}
}
package ic.doc;
import static ic.doc.WebServer.execute;
import static junit.framework.TestCase.assertTrue;
import ic.doc.web.HTMLResultPage;
import ic.doc.web.IndexPage;
import ic.doc.web.Page;
import java.io.File;
import org.junit.Test;
public class doGetTest {
@Test
public void noRadioButtonReturnsHTML(){
Page p = execute("shakespeare", null);
assertTrue(p instanceof HTMLResultPage);
}
@Test
public void incorrectPdfPage(){
Page p = execute("qwertyuiop", "pdf");
assertTrue(p.getAnswer().equals("# Sorry\nSorry, we didn't understand " +"qwertyuiop" ));
}
@Test
public void incorrectHTMLPage(){
Page p = execute("shakespe", null);
assertTrue(p.getAnswer().equals("<h1>Sorry</h1>\n<p>Sorry, we didn't understand "
+ "<em>shakespe</em></p>"));
}
@Test
public void incorrectMarkDownPage(){
Page p = execute("shakespear", "markdown");
assertTrue(p.getAnswer().equals("# Sorry\nSorry, we didn't understand shakespear"));
}
@Test
public void correctPdfPage() {
Page p = execute("shakespeare", "pdf");
assertTrue(p instanceof PdfPage);
}
@Test
public void correctMarkdownPage(){
Page p = execute("shakespeare", "markdown");
assertTrue(p instanceof MarkDownPage);
}
@Test
public void emptyQueryReturnsHTML(){
Page p = execute(null, null);
assertTrue(p instanceof IndexPage);
}
//test that there is a markdown file created
@Test
public void markdownFileCreated() throws Exception {
String query = "shakespeare";
String result = new QueryProcessor().process(query);
File markdown = MarkDownPage.toMarkDown(result, query);
assertTrue(markdown.isFile());
}
//test that there is a pdf file created
@Test
public void pdfFileCreated() throws Exception {
String query = "shakespeare";
String result = new QueryProcessor().process(query);
File markDown = MarkDownPage.toMarkDown(result, query);
File pdf = PdfPage.create_pdf(markDown, query);
assertTrue(pdf.isFile());
}
}
\ No newline at end of file
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