Commit a693f8bc authored by Pamelpreet Jhinger's avatar Pamelpreet Jhinger
Browse files

refactored and fixed webpages test

parent 4772dee2
Pipeline #66005 passed with stages
in 3 minutes and 46 seconds
......@@ -6,7 +6,7 @@ public class Query {
private String imgURL;
private String wikiURL;
public Query(String name, String description, String imgURL, String wikiURL) {
Query(String name, String description, String imgURL, String wikiURL) {
this.name = name;
this.description = description;
if(name != null) {
......
......@@ -5,27 +5,21 @@ import ic.doc.web.DBReader;
import java.sql.ResultSet;
import java.util.*;
public class QueryProcessor {
class QueryProcessor {
final DBReader dbReader;
private final DBReader dbReader;
public QueryProcessor(DBReader dbReader) {
QueryProcessor(DBReader dbReader) {
this.dbReader = dbReader;
}
// this.queriesMap.add(new Query(Arrays.asList(""),
// ",
// "",
// ));
public ResultSet process(String query) {
ResultSet process(String query) {
ResultSet possibilities = null;
StringTokenizer queryTokens = new StringTokenizer(query);
StringBuilder sqlQueryBuilder = new StringBuilder();
if (queryTokens.hasMoreTokens()) {
String token = queryTokens.nextToken().toLowerCase();
sqlQueryBuilder.append("SELECT * FROM PEOPLE WHERE identifier LIKE '%" + token + "%'");
sqlQueryBuilder.append("SELECT * FROM PEOPLE WHERE identifier LIKE '%").append(token).append("%'");
while (queryTokens.hasMoreTokens()) {
token = queryTokens.nextToken().toLowerCase();
sqlQueryBuilder.append(" AND identifier LIKE '%" + token+"%'");
......
......@@ -15,7 +15,7 @@ import java.util.List;
public class WebServer {
public WebServer() throws Exception {
private WebServer() throws Exception {
Server server = new Server(Integer.valueOf(System.getenv("PORT")));
ServletHandler handler = new ServletHandler();
......@@ -30,7 +30,7 @@ public class WebServer {
final DBReader dbReader;
final QueryProcessor queryProcessor;
public Website(){
Website(){
dbReader = new DBReader();
queryProcessor = new QueryProcessor(dbReader);
}
......
......@@ -5,13 +5,12 @@ import java.sql.*;
public class DBReader {
Connection wiki;
Statement state;
private Statement state;
public DBReader(){
try {
Class.forName("org.sqlite.JDBC");
wiki = DriverManager.getConnection("jdbc:sqlite:wiki.db");
Connection wiki = DriverManager.getConnection("jdbc:sqlite:wiki.db");
state = wiki.createStatement();
} catch (Exception e) {
System.out.println("Failed to open database/statement");
......
......@@ -45,7 +45,7 @@ public class DownloadPage implements Page{
}
}
public File createFile() throws IOException{
private File createFile() throws IOException{
File tmp = File.createTempFile(query, ".md");
tmp.deleteOnExit();
FileWriter fw = new FileWriter(tmp);
......
......@@ -7,14 +7,14 @@ import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class QueryProcessorTest {
//
// QueryProcessor queryProcessor = new QueryProcessor();
//
// @Test
// public void returnsEmptyStringIfCannotProcessQuery() throws Exception {
// assertThat(queryProcessor.process("test").get(0).getAnswer(), is(""));
// assertThat(queryProcessor.process("test").next(), is((Class<Boolean>) null));
// }
//
// @Test
// public void knowsAboutShakespeare() throws Exception {
// assertThat(queryProcessor.process("Shakespeare").get(0).getAnswer(), containsString(
......
/*
package ic.doc;
import ic.doc.web.DownloadPage;
......@@ -15,6 +14,13 @@ import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
public class WebPagesTest {
......@@ -30,47 +36,33 @@ public class WebPagesTest {
WebServer.Website website = new WebServer.Website();
@Mock
HTMLResultPage htmlResultPage;
@Mock
DownloadPage pdfFilePage;
@Mock
DownloadPage mdFilePage;
@Test
public void htmlPageGenerated() throws IOException {
when(req.getParameter("q")).thenReturn("shakespeare");
when(req.getParameter("type")).thenReturn("html");
when(resp.getWriter()).thenReturn(new PrintWriter(File.createTempFile("tmp","html")));
when(resp.getWriter()).thenReturn(new PrintWriter(File.createTempFile("tmp","tmp")));
when(resp.getContentType()).thenReturn("text/html");
website.doGet(req, resp);
verify(HTMLResultPage, times(1)).writeTo(resp);
}
@Test
public void markdownPageGenerated() throws IOException {
when(req.getParameter("q")).thenReturn("shakespeare");
when(req.getParameter("type")).thenReturn("md");
when(resp.getWriter()).thenReturn(new PrintWriter(File.createTempFile("tmp","html")));
when(resp.getWriter()).thenReturn(new PrintWriter(File.createTempFile("tmp","tmp")));
when(resp.getContentType()).thenReturn("text/markdown");
website.doGet(req, resp);
verify(HTMLResultPage, times(1)).writeTo(resp);
}
@Test
public void pdfPageGenerated() throws IOException {
when(req.getParameter("q")).thenReturn("shakespeare");
when(req.getParameter("type")).thenReturn("pdf");
when(resp.getWriter()).thenReturn(new PrintWriter(File.createTempFile("tmp","html")));
when(resp.getWriter()).thenReturn(new PrintWriter(File.createTempFile("tmp","tmp")));
when(resp.getContentType()).thenReturn("application/pdf");
website.doGet(req, resp);
verify(HTMLResultPage, times(1)).writeTo(resp);
}
}
*/
\ No newline at end of file
}
\ 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