Updated tests for FCSQL queries.

Change-Id: I38435b45a9eb6a250231286f906ed8078ae86a90
diff --git a/src/test/java/de/ids_mannheim/korap/test/BaseTest.java b/src/test/java/de/ids_mannheim/korap/test/BaseTest.java
index 4acd467..27c0a63 100644
--- a/src/test/java/de/ids_mannheim/korap/test/BaseTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/BaseTest.java
@@ -25,9 +25,10 @@
 import org.xml.sax.SAXException;
 
 public class BaseTest extends KorapJerseyTest {
-    
+
     protected DocumentBuilder docBuilder;
-    protected DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+    protected DocumentBuilderFactory factory =
+            DocumentBuilderFactory.newInstance();
 
     protected ClientAndServer mockServer;
     protected MockServerClient mockClient;
@@ -42,16 +43,17 @@
     public void stopMockServer () {
         mockServer.stop();
     }
-    
-    protected void createExpectationForSearchLemmaFein () throws IOException {
+
+    protected void createExpectationForSearch (String query,
+            String jsonFilename) throws IOException {
         String searchResult = IOUtils.toString(
                 ClassLoader.getSystemResourceAsStream(
-                        "korap-api-responses/search-lemma-fein.jsonld"),
+                        "korap-api-responses/"+jsonFilename),
                 StandardCharsets.UTF_8);
 
         mockClient.reset()
                 .when(request().withMethod("GET").withPath("/search")
-                        .withQueryStringParameter("q", "[tt:lemma=\"fein\"]")
+                        .withQueryStringParameter("q", query)
                         .withQueryStringParameter("ql", "fcsql")
                         .withQueryStringParameter("v", "2.0")
                         .withQueryStringParameter("context", "sentence")
@@ -62,8 +64,27 @@
                                 "application/json; charset=utf-8"))
                         .withBody(searchResult).withStatusCode(200));
     }
-    
-    protected void createExpectationForMatchInfoLemmaFein() throws IOException{
+
+    protected void createExpectationForMatchInfoLemmaBar () throws IOException {
+        String matchInfoResult = IOUtils.toString(
+                ClassLoader.getSystemResourceAsStream(
+                        "korap-api-responses/GOE-AGA-01784-p614-615.jsonld"),
+                StandardCharsets.UTF_8);
+
+        mockClient
+                .when(request().withMethod("GET")
+                        .withPath("/corpus/GOE/AGA/01784/p614-615/matchInfo")
+                        .withQueryStringParameter("foundry", "*")
+                        .withQueryStringParameter("spans", "false"))
+                .respond(response()
+                        .withHeader(new Header("Content-Type",
+                                "application/json; charset=utf-8"))
+                        .withBody(matchInfoResult).withStatusCode(200));
+    }
+
+
+    protected void createExpectationForMatchInfoLemmaFein ()
+            throws IOException {
         String matchInfoResult = IOUtils.toString(
                 ClassLoader.getSystemResourceAsStream(
                         "korap-api-responses/GOE-AGF-00000-p4276-4277.jsonld"),
@@ -80,7 +101,7 @@
                         .withBody(matchInfoResult).withStatusCode(200));
     }
 
-    protected void checkSRUSearchRetrieveResponse (InputStream entity)
+    protected Document checkSRUSearchRetrieveResponse (InputStream entity)
             throws SAXException, IOException, ParserConfigurationException {
 
         docBuilder = factory.newDocumentBuilder();
@@ -121,6 +142,7 @@
         assertEquals("adv:Layers", node.getNodeName());
 
         checkSegmentPosition(resources);
+        return doc;
     }
 
     protected void checkSegmentPosition (NodeList resources) {
diff --git a/src/test/java/de/ids_mannheim/korap/test/FCSQLRequestTest.java b/src/test/java/de/ids_mannheim/korap/test/FCSQLRequestTest.java
index 8e56007..e4d9349 100644
--- a/src/test/java/de/ids_mannheim/korap/test/FCSQLRequestTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/FCSQLRequestTest.java
@@ -2,45 +2,28 @@
 
 import static org.junit.Assert.assertEquals;
 
-import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.URI;
 import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.List;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
-import org.apache.http.NameValuePair;
-import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.utils.URIBuilder;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
-import org.apache.http.message.BasicNameValuePair;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.w3c.dom.Document;
 import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
 
+import com.sun.jersey.api.client.ClientResponse;
+
 /**
- * The tests in this class require a running KustvaktServer.
- * The results are based on the sample corpus from the Goethe corpus.
- * 
- * Specify the Kustvakt service URI at
- * /KorapSRU/src/main/webapp/WEB-INF/web.xml
- * 
  * @author margaretha
  *
  */
-public class FCSQLRequestTest extends KorapJerseyTest {
+public class FCSQLRequestTest extends BaseTest {
 
-    private String korapSruUri = "http://localhost:8080/KorapSRU";
     private static DocumentBuilder documentBuilder;
 
     @BeforeClass
@@ -53,106 +36,68 @@
     }
 
     @Test
-    public void testFCSQuery () throws URISyntaxException, IOException,
-            SAXException, ParserConfigurationException {
-        List<NameValuePair> params = new ArrayList<NameValuePair>();
-        params.add(new BasicNameValuePair("operation", "startRetrieve"));
-        params.add(new BasicNameValuePair("query", "[tt:lemma=\".*bar\"]"));
-        params.add(new BasicNameValuePair("queryType", "fcs"));
-
-        URIBuilder builder = new URIBuilder(korapSruUri);
-        builder.addParameters(params);
-
-        URI uri = builder.build();
-        assertEquals(
-                korapSruUri + "?operation=startRetrieve&query="
-                        + "%5Btt%3Alemma%3D%22.*bar%22%5D&queryType=fcs",
-                uri.toString());
-
-        HttpGet request = new HttpGet(uri);
-        CloseableHttpClient client = HttpClients.createDefault();
-        CloseableHttpResponse response = null;
-        response = client.execute(request);
-
-        assertEquals(200, response.getStatusLine().getStatusCode());
-
-        InputStream is = response.getEntity().getContent();
-        Document document = documentBuilder.parse(is);
-        NodeList nodeList =
-                document.getElementsByTagName("sruResponse:numberOfRecords");
-
-        assertEquals("134", nodeList.item(0).getTextContent());
-        response.close();
-    }
-
-    @Test
     public void testLemmaRegex () throws URISyntaxException, IOException,
             SAXException, ParserConfigurationException {
-        List<NameValuePair> params = new ArrayList<NameValuePair>();
-        params.add(new BasicNameValuePair("operation", "startRetrieve"));
-        params.add(new BasicNameValuePair("query", "[lemma=\".*bar\"]"));
-        params.add(new BasicNameValuePair("queryType", "fcs"));
 
-        URIBuilder builder = new URIBuilder(korapSruUri);
-        builder.addParameters(params);
+        createExpectationForSearch("[tt:lemma=\".*bar\"]",
+                "search-lemma-bar.jsonld");
+        createExpectationForMatchInfoLemmaBar();
 
-        URI uri = builder.build();
-        assertEquals(korapSruUri + "?operation=startRetrieve&query=%5Blemma"
-                + "%3D%22.*bar%22%5D&queryType=fcs", uri.toString());
+        ClientResponse response = resource()
+                .queryParam("operation", "searchRetrieve")
+                .queryParam("query", "[tt:lemma=\".*bar\"]")
+                .queryParam("queryType", "fcs")
+                .queryParam("maximumRecords", "1").get(ClientResponse.class);
 
-        HttpGet request = new HttpGet(uri);
-        CloseableHttpClient client = HttpClients.createDefault();
-        CloseableHttpResponse response = null;
-        response = client.execute(request);
+        InputStream entity = response.getEntity(InputStream.class);
+        Document doc = checkSRUSearchRetrieveResponse(entity);
 
-        assertEquals(200, response.getStatusLine().getStatusCode());
-
-        InputStream is = response.getEntity().getContent();
-        Document document = documentBuilder.parse(is);
         NodeList nodeList =
-                document.getElementsByTagName("sruResponse:numberOfRecords");
+                doc.getElementsByTagName("sruResponse:numberOfRecords");
 
-        assertEquals("134", nodeList.item(0).getTextContent());
-        response.close();
+        assertEquals("132", nodeList.item(0).getTextContent());
+
     }
 
     @Test
     public void testUnsupportedLayer () throws URISyntaxException, IOException,
             SAXException, ParserConfigurationException {
-        List<NameValuePair> params = new ArrayList<NameValuePair>();
-        params.add(new BasicNameValuePair("operation", "startRetrieve"));
-        params.add(new BasicNameValuePair("query", "[unknown=\"Feuer\"]"));
-        params.add(new BasicNameValuePair("queryType", "fcs"));
 
-        URIBuilder builder = new URIBuilder(korapSruUri);
-        builder.addParameters(params);
+        createExpectationForSearch("[unknown=\"fein\"]", "unknownLayer.jsonld");
 
-        URI uri = builder.build();
-        assertEquals(
-                korapSruUri + "?operation=startRetrieve&query="
-                        + "%5Bword%3D%22Feuer%22%5D&queryType=fcs",
-                uri.toString());
+        ClientResponse response = resource()
+                .queryParam("operation", "searchRetrieve")
+                .queryParam("query", "[unknown=\"fein\"]")
+                .queryParam("queryType", "fcs")
+                .queryParam("maximumRecords", "1").get(ClientResponse.class);
 
-        HttpGet request = new HttpGet(uri);
-        CloseableHttpClient client = HttpClients.createDefault();
-        CloseableHttpResponse response = null;
-        response = client.execute(request);
+        InputStream is = response.getEntity(InputStream.class);
 
-        assertEquals(200, response.getStatusLine().getStatusCode());
-
-        InputStream is = response.getEntity().getContent();
-//        BufferedReader br = new BufferedReader(new InputStreamReader(is));
-//        String line = null;
-//        while ((line=br.readLine())!=null){
-//            System.out.println(line); 
-//        }
-        
         Document document = documentBuilder.parse(is);
         NodeList nodeList = document.getElementsByTagName("diag:message");
-        assertEquals(
-                "status code: 6, reason phrase: Layer unknown is unsupported.",
+        assertEquals("Layer unknown is unsupported.",
                 nodeList.item(0).getTextContent());
         response.close();
     }
 
+    @Test
+    public void testUnsupportedQualifer () throws URISyntaxException,
+            IOException, SAXException, ParserConfigurationException {
+        createExpectationForSearch("[unknown:lemma=\"fein\"]",
+                "unknownQualifier.jsonld");
+
+        ClientResponse response = resource()
+                .queryParam("operation", "searchRetrieve")
+                .queryParam("query", "[unknown:lemma=\"fein\"]")
+                .queryParam("queryType", "fcs")
+                .queryParam("maximumRecords", "1").get(ClientResponse.class);
+
+        InputStream is = response.getEntity(InputStream.class);
+
+        Document document = documentBuilder.parse(is);
+        NodeList nodeList = document.getElementsByTagName("diag:message");
+        assertEquals("Qualifier unknown is unsupported.",
+                nodeList.item(0).getTextContent());
+        response.close();
+    }
 }
diff --git a/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java b/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java
index b620674..7928982 100644
--- a/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java
@@ -139,7 +139,8 @@
     public void searchRetrieveFCSQLTest ()
             throws IOException, SAXException, ParserConfigurationException {
 
-        createExpectationForSearchLemmaFein();
+        createExpectationForSearch("[tt:lemma=\"fein\"]",
+                "search-lemma-fein.jsonld");
         createExpectationForMatchInfoLemmaFein();
 
         ClientResponse response = resource()
diff --git a/src/test/resources/korap-api-responses/GOE-AGA-01784-p614-615.jsonld b/src/test/resources/korap-api-responses/GOE-AGA-01784-p614-615.jsonld
new file mode 100644
index 0000000..16e6b2a
--- /dev/null
+++ b/src/test/resources/korap-api-responses/GOE-AGA-01784-p614-615.jsonld
@@ -0,0 +1,25 @@
+{
+    "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
+    "meta": {"version": "Krill-0.59.3"},
+    "snippet": "<span class=\"context-left\"><span class=\"more\"><\/span><\/span><span class=\"match\"><mark>wunderbar<\/mark><\/span><span class=\"context-right\"><span class=\"more\"><\/span><\/span>",
+    "matchID": "match-GOE/AGA/01784-p614-615",
+    "textSigle": "GOE/AGA/01784",
+    "author": "Goethe, Johann Wolfgang von",
+    "docSigle": "GOE/AGA",
+    "layerInfos": "corenlp/c=spans corenlp/p=tokens corenlp/s=spans dereko/s=spans malt/d=rels marmot/m=tokens marmot/p=tokens opennlp/p=tokens opennlp/s=spans tt/l=tokens tt/p=tokens",
+    "pubPlace": "München",
+    "availability": "CC-BY-SA",
+    "title": "Belagerung von Mainz",
+    "pubDate": "1982",
+    "corpusSigle": "GOE",
+    "context": {
+        "left": [
+            "token",
+            0
+        ],
+        "right": [
+            "token",
+            0
+        ]
+    }
+}
diff --git a/src/test/resources/korap-api-responses/search-lemma-bar.jsonld b/src/test/resources/korap-api-responses/search-lemma-bar.jsonld
new file mode 100644
index 0000000..1744598
--- /dev/null
+++ b/src/test/resources/korap-api-responses/search-lemma-bar.jsonld
@@ -0,0 +1,72 @@
+{
+    "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
+    "meta": {
+        "count": 1,
+        "startIndex": 0,
+        "timeout": 10000,
+        "context": "base/s:s",
+        "fields": [
+            "ID",
+            "UID",
+            "textSigle",
+            "corpusID",
+            "author",
+            "title",
+            "subTitle",
+            "textClass",
+            "pubPlace",
+            "pubDate",
+            "availability",
+            "layerInfos",
+            "docSigle",
+            "corpusSigle"
+        ],
+        "version": "0.59.3",
+        "benchmark": "58.229914 ms",
+        "totalResults": 132,
+        "serialQuery": "SpanMultiTermQueryWrapper(tokens:/tt/l:.*bar/)",
+        "itemsPerPage": 1
+    },
+    "query": {
+        "@type": "koral:token",
+        "wrap": {
+            "@type": "koral:term",
+            "key": ".*bar",
+            "foundry": "tt",
+            "layer": "l",
+            "type": "type:regex",
+            "match": "match:eq"
+        }
+    },
+    "collection": {
+        "@type": "koral:doc",
+        "match": "match:eq",
+        "type": "type:regex",
+        "value": "CC-BY.*",
+        "key": "availability",
+        "rewrites": [
+            {
+                "@type": "koral:rewrite",
+                "src": "Kustvakt",
+                "operation": "operation:insertion",
+                "scope": "availability(FREE)"
+            }
+        ]
+    },
+    "matches": [
+        {
+            "snippet": "<span class=\"context-left\"><\/span><span class=\"match\"><mark>wunderbar<\/mark><\/span><span class=\"context-right\"> genug sah man diese Prophezeiung nicht etwa nur dem allgemeinen Sinn, sondern dem besondern Buchstaben nach genau erfüllt, indem die Franzosen ihren Kalender von diesen Tagen an datierten.<\/span>",
+            "matchID": "match-GOE/AGA/01784-p614-615",
+            "UID": 0,
+            "textSigle": "GOE/AGA/01784",
+            "author": "Goethe, Johann Wolfgang von",
+            "docSigle": "GOE/AGA",
+            "layerInfos": "corenlp/c=spans corenlp/p=tokens corenlp/s=spans dereko/s=spans malt/d=rels marmot/m=tokens marmot/p=tokens opennlp/p=tokens opennlp/s=spans tt/l=tokens tt/p=tokens",
+            "pubPlace": "München",
+            "availability": "CC-BY-SA",
+            "title": "Belagerung von Mainz",
+            "pubDate": "1982",
+            "corpusSigle": "GOE"
+        }
+    ]
+}
diff --git a/src/test/resources/korap-api-responses/unknownLayer.jsonld b/src/test/resources/korap-api-responses/unknownLayer.jsonld
new file mode 100644
index 0000000..b74ae30
--- /dev/null
+++ b/src/test/resources/korap-api-responses/unknownLayer.jsonld
@@ -0,0 +1,16 @@
+{
+    "meta": {
+        "count": 1,
+        "context": "sentence",
+        "startIndex": 0,
+        "snippets": true,
+        "timeout": 10000
+    },
+    "@context": "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
+    "errors": [
+        [
+            306,
+            "Layer unknown is unsupported."
+        ]
+    ]
+}
\ No newline at end of file
diff --git a/src/test/resources/korap-api-responses/unknownQualifier.jsonld b/src/test/resources/korap-api-responses/unknownQualifier.jsonld
new file mode 100644
index 0000000..21faf2b
--- /dev/null
+++ b/src/test/resources/korap-api-responses/unknownQualifier.jsonld
@@ -0,0 +1,16 @@
+{
+    "meta": {
+        "count": 1,
+        "context": "sentence",
+        "startIndex": 0,
+        "snippets": true,
+        "timeout": 10000
+    },
+    "@context": "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
+    "errors": [
+        [
+            306,
+            "Qualifier unknown is unsupported."
+        ]
+    ]
+}