Updated KorapClientTest.

Change-Id: I400ae1a5c48eaa700a7a76e68107d2d6fff5fd3e
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 27c0a63..560a11e 100644
--- a/src/test/java/de/ids_mannheim/korap/test/BaseTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/BaseTest.java
@@ -44,36 +44,51 @@
         mockServer.stop();
     }
 
+    protected void createRetrieveResource () throws IOException {
+        String korapResources = IOUtils.toString(
+                ClassLoader.getSystemResourceAsStream(
+                        "korap-api-responses/resources.json"),
+                StandardCharsets.UTF_8);
+
+        mockClient.reset()
+                .when(request().withMethod("GET").withPath("/resource"))
+                .respond(response()
+                        .withHeader(new Header("Content-Type",
+                                "application/json; charset=utf-8"))
+                        .withBody(korapResources).withStatusCode(200));
+    }
+
     protected void createExpectationForSearch (String query,
+            String queryLanguage, String version, String offset,
             String jsonFilename) throws IOException {
         String searchResult = IOUtils.toString(
                 ClassLoader.getSystemResourceAsStream(
-                        "korap-api-responses/"+jsonFilename),
+                        "korap-api-responses/" + jsonFilename),
                 StandardCharsets.UTF_8);
 
         mockClient.reset()
                 .when(request().withMethod("GET").withPath("/search")
                         .withQueryStringParameter("q", query)
-                        .withQueryStringParameter("ql", "fcsql")
-                        .withQueryStringParameter("v", "2.0")
+                        .withQueryStringParameter("ql", queryLanguage)
+                        .withQueryStringParameter("v", version)
                         .withQueryStringParameter("context", "sentence")
                         .withQueryStringParameter("count", "1")
-                        .withQueryStringParameter("offset", "0"))
+                        .withQueryStringParameter("offset", offset))
                 .respond(response()
                         .withHeader(new Header("Content-Type",
                                 "application/json; charset=utf-8"))
                         .withBody(searchResult).withStatusCode(200));
     }
 
-    protected void createExpectationForMatchInfoLemmaBar () throws IOException {
+    protected void createExpectationForMatchInfo (String jsonFilename,
+            String uriPath) throws IOException {
         String matchInfoResult = IOUtils.toString(
                 ClassLoader.getSystemResourceAsStream(
-                        "korap-api-responses/GOE-AGA-01784-p614-615.jsonld"),
+                        "korap-api-responses/" + jsonFilename),
                 StandardCharsets.UTF_8);
 
         mockClient
-                .when(request().withMethod("GET")
-                        .withPath("/corpus/GOE/AGA/01784/p614-615/matchInfo")
+                .when(request().withMethod("GET").withPath(uriPath)
                         .withQueryStringParameter("foundry", "*")
                         .withQueryStringParameter("spans", "false"))
                 .respond(response()
@@ -82,26 +97,51 @@
                         .withBody(matchInfoResult).withStatusCode(200));
     }
 
+    protected Document checkSearchRetrieveResponseSRUVersion1_2 (InputStream entity)
+            throws SAXException, IOException, ParserConfigurationException {
 
-    protected void createExpectationForMatchInfoLemmaFein ()
-            throws IOException {
-        String matchInfoResult = IOUtils.toString(
-                ClassLoader.getSystemResourceAsStream(
-                        "korap-api-responses/GOE-AGF-00000-p4276-4277.jsonld"),
-                StandardCharsets.UTF_8);
+        docBuilder = factory.newDocumentBuilder();
+        Document doc = docBuilder.parse(entity);
 
-        mockClient
-                .when(request().withMethod("GET")
-                        .withPath("/corpus/GOE/AGF/00000/p4276-4277/matchInfo")
-                        .withQueryStringParameter("foundry", "*")
-                        .withQueryStringParameter("spans", "false"))
-                .respond(response()
-                        .withHeader(new Header("Content-Type",
-                                "application/json; charset=utf-8"))
-                        .withBody(matchInfoResult).withStatusCode(200));
+        NodeList nodelist = doc.getElementsByTagName("sru:version");
+        assertEquals("1.2", nodelist.item(0).getTextContent());
+        nodelist = doc.getElementsByTagName("sru:recordSchema");
+        assertEquals("http://clarin.eu/fcs/resource",
+                nodelist.item(0).getTextContent());
+
+        NodeList resources = doc.getElementsByTagName("fcs:Resource");
+        String attr = resources.item(0).getAttributes().getNamedItem("pid")
+                .getNodeValue();
+
+        nodelist = doc.getElementsByTagName("fcs:DataView");
+        attr = nodelist.item(0).getAttributes().getNamedItem("type")
+                .getNodeValue();
+        assertEquals("application/x-clarin-fcs-hits+xml", attr);
+
+        Node node = nodelist.item(0).getFirstChild();
+        assertEquals("hits:Result", node.getNodeName());
+        NodeList children = node.getChildNodes();
+        if (children.getLength() > 1) {
+            assertEquals("hits:Hit", children.item(1).getNodeName());
+        }
+
+        attr = nodelist.item(1).getAttributes().getNamedItem("type")
+                .getNodeValue();
+        assertEquals("application/x-clarin-fcs-adv+xml", attr);
+        node = nodelist.item(1).getFirstChild();
+        assertEquals("adv:Advanced", node.getNodeName());
+
+        nodelist = node.getChildNodes();
+        node = nodelist.item(0);
+        assertEquals("adv:Segments", node.getNodeName());
+        node = nodelist.item(1);
+        assertEquals("adv:Layers", node.getNodeName());
+
+        checkSegmentPosition(resources);
+        return doc;
     }
-
-    protected Document checkSRUSearchRetrieveResponse (InputStream entity)
+    
+    protected Document checkSearchRetrieveResponseSRUVersion2 (InputStream entity)
             throws SAXException, IOException, ParserConfigurationException {
 
         docBuilder = factory.newDocumentBuilder();
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 e4d9349..f0b9d49 100644
--- a/src/test/java/de/ids_mannheim/korap/test/FCSQLRequestTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/FCSQLRequestTest.java
@@ -39,9 +39,10 @@
     public void testLemmaRegex () throws URISyntaxException, IOException,
             SAXException, ParserConfigurationException {
 
-        createExpectationForSearch("[tt:lemma=\".*bar\"]",
+        createExpectationForSearch("[tt:lemma=\".*bar\"]", "fcsql", "2.0", "0",
                 "search-lemma-bar.jsonld");
-        createExpectationForMatchInfoLemmaBar();
+        createExpectationForMatchInfo("GOE-AGA-01784-p614-615.jsonld",
+                "/corpus/GOE/AGA/01784/p614-615/matchInfo");
 
         ClientResponse response = resource()
                 .queryParam("operation", "searchRetrieve")
@@ -50,7 +51,7 @@
                 .queryParam("maximumRecords", "1").get(ClientResponse.class);
 
         InputStream entity = response.getEntity(InputStream.class);
-        Document doc = checkSRUSearchRetrieveResponse(entity);
+        Document doc = checkSearchRetrieveResponseSRUVersion2(entity);
 
         NodeList nodeList =
                 doc.getElementsByTagName("sruResponse:numberOfRecords");
@@ -63,7 +64,8 @@
     public void testUnsupportedLayer () throws URISyntaxException, IOException,
             SAXException, ParserConfigurationException {
 
-        createExpectationForSearch("[unknown=\"fein\"]", "unknownLayer.jsonld");
+        createExpectationForSearch("[unknown=\"fein\"]", "fcsql", "2.0", "0",
+                "unknownLayer.jsonld");
 
         ClientResponse response = resource()
                 .queryParam("operation", "searchRetrieve")
@@ -83,8 +85,8 @@
     @Test
     public void testUnsupportedQualifer () throws URISyntaxException,
             IOException, SAXException, ParserConfigurationException {
-        createExpectationForSearch("[unknown:lemma=\"fein\"]",
-                "unknownQualifier.jsonld");
+        createExpectationForSearch("[unknown:lemma=\"fein\"]", "fcsql", "2.0",
+                "0", "unknownQualifier.jsonld");
 
         ClientResponse response = resource()
                 .queryParam("operation", "searchRetrieve")
diff --git a/src/test/java/de/ids_mannheim/korap/test/KorapClientTest.java b/src/test/java/de/ids_mannheim/korap/test/KorapClientTest.java
index 341f91d..30b5ec3 100644
--- a/src/test/java/de/ids_mannheim/korap/test/KorapClientTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/KorapClientTest.java
@@ -14,79 +14,99 @@
 import de.ids_mannheim.korap.sru.KorapResult;
 import de.ids_mannheim.korap.sru.QueryLanguage;
 
-
 /**
  * The tests are based on the sample corpus from the Goethe corpus.
  * 
  * @author margaretha
  *
  */
-public class KorapClientTest {
+public class KorapClientTest extends BaseTest {
     private KorapClient c;
     private KorapResult result;
     private KorapMatch match;
-    
+
     public KorapClientTest () {
-        c = new KorapClient("http://localhost:1080/api/v1.0", 25, 50);
+        c = new KorapClient("http://localhost:1080", 25, 50);
     }
 
     @Test
     public void testCQLQuery () throws HttpResponseException, IOException {
-        
-        
-        result = c.query("der", QueryLanguage.CQL, "1.2", 1, 1,
-                null);
+
+        createExpectationForSearch("der", "cql", "1.2", "50",
+                "search-der.jsonld");
+
+        result = c.query("der", QueryLanguage.CQL, "1.2", 51, 1, null);
         assertEquals(1, result.getMatchSize());
         assertEquals(1858, result.getTotalResults());
-    
+
         match = result.getMatch(0);
-        assertEquals("match-GOE/AGA/01784-p18-19",match.getMatchId());
-        
+        assertEquals("match-GOE/AGA/01784-p1856-1857", match.getMatchId());
+
         match.parseMatchId();
         assertEquals("GOE", match.getCorpusId());
         assertEquals("AGA", match.getDocId());
-        assertEquals("p18-19", match.getPositionId());
-        
+        assertEquals("p1856-1857", match.getPositionId());
+
     }
 
     @Test
-    public void testFCS2Query() throws HttpResponseException, IOException {
-    	result = c.query("(\"blaue\"|\"grüne\")", QueryLanguage.FCSQL, "2.0", 1,
-                25, null);
-    	
-    	assertEquals(25, result.getMatchSize());
+    public void testOrQuery () throws HttpResponseException, IOException {
+
+        createExpectationForSearch("(\"blaue\"|\"grüne\")", "fcsql", "2.0", "0",
+                "search-or.jsonld");
+
+        createExpectationForMatchInfo("GOE-AGF-00000-p7744-7745.jsonld",
+                "/corpus/GOE/AGF/00000/p7744-7745/matchInfo");
+
+        result = c.query("(\"blaue\"|\"grüne\")", QueryLanguage.FCSQL, "2.0", 1,
+                1, null);
+
+        assertEquals(1, result.getMatchSize());
         assertEquals(55, result.getTotalResults());
 
         match = result.getMatch(0);
-        assertEquals("match-GOE/AGF/00000-p7744-7745",match.getMatchId());
-        
+        assertEquals("match-GOE/AGF/00000-p7744-7745", match.getMatchId());
+
         match.parseMatchId();
         assertEquals("GOE", match.getCorpusId());
         assertEquals("AGF", match.getDocId());
         assertEquals("p7744-7745", match.getPositionId());
-	}
-    
+    }
+
     @Test
-    public void testRetrieveAnnotations() throws IOException, URISyntaxException {
-    	String annotationSnippet = c.retrieveAnnotations(
-    			"GOE", "AGF", "00000",
-    			"p7667-7668", "*");
-    	
-    	assertEquals(true, annotationSnippet.startsWith("<snippet><span class="
-    			+ "\"context-left\"></span><span class=\"match\"><span title="));
-	}
-    
+    public void testRetrieveAnnotations ()
+            throws IOException, URISyntaxException {
+        createExpectationForMatchInfo("GOE-AGF-00000-p4276-4277.jsonld",
+                "/corpus/GOE/AGF/00000/p4276-4277/matchInfo");
+
+        String annotationSnippet =
+                c.retrieveAnnotations("GOE", "AGF", "00000", "p4276-4277", "*");
+
+        assertEquals(
+                "<snippet><span class=\"context-left\"><span class=\"more\">"
+                        + "</span></span><span class=\"match\"><mark>feineren</mark>"
+                        + "</span><span class=\"context-right\"><span class=\"more\">"
+                        + "</span></span></snippet>",
+                annotationSnippet);
+    }
+
     @Test
-    public void testRetrieveNonexistingAnnotation() throws IOException, URISyntaxException {
-    	String annotationSnippet = c.retrieveAnnotations(
-    			"WPD15", "D18", "06488",
-    			"p588-589", "*");
-    	
-    	assertEquals("<snippet></snippet>", annotationSnippet);
-	}
-    
+    public void testRetrieveNonexistingAnnotation ()
+            throws IOException, URISyntaxException {
+
+        createExpectationForMatchInfo("unknownMatchInfo.jsonld",
+                "/corpus/WPD15/D18/06488/p588-589/matchInfo");
+
+        String annotationSnippet =
+                c.retrieveAnnotations("WPD15", "D18", "06488", "p588-589", "*");
+
+        assertEquals("<snippet></snippet>", annotationSnippet);
+    }
+
     @Test
-    public void testRetrieveResource () throws HttpResponseException, Exception {
+    public void testRetrieveResource ()
+            throws HttpResponseException, Exception {
+        createRetrieveResource();
         KorapResource[] resources = c.retrieveResources();
         assertEquals(3, resources.length);
         assertEquals("WPD17", resources[0].getResourceId());
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 7928982..ef6511a 100644
--- a/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java
@@ -44,7 +44,7 @@
                 .when(request().withMethod("GET").withPath("/search")
                         .withQueryStringParameter("q", "fein")
                         .withQueryStringParameter("ql", "cql")
-                        .withQueryStringParameter("v", "2.0")
+                        .withQueryStringParameter("v", "1.2")
                         .withQueryStringParameter("context", "sentence")
                         .withQueryStringParameter("count", "25")
                         .withQueryStringParameter("offset", "0"))
@@ -113,10 +113,11 @@
 
         ClientResponse response =
                 resource().queryParam("operation", "searchRetrieve")
+                        .queryParam("version", "1.2")
                         .queryParam("query", "fein").get(ClientResponse.class);
 
         InputStream entity = response.getEntity(InputStream.class);
-        checkSRUSearchRetrieveResponse(entity);
+        checkSearchRetrieveResponseSRUVersion1_2(entity);
     }
 
     @Test
@@ -128,20 +129,21 @@
 
         ClientResponse response = resource()
                 .queryParam("operation", "searchRetrieve")
-                .queryParam("query", "fein").queryParam("x-fcs-context", "GOE")
-                .get(ClientResponse.class);
+                .queryParam("query", "fein").queryParam("version", "1.2")
+                .queryParam("x-fcs-context", "GOE").get(ClientResponse.class);
 
         InputStream entity = response.getEntity(InputStream.class);
-        checkSRUSearchRetrieveResponse(entity);
+        checkSearchRetrieveResponseSRUVersion1_2(entity);
     }
 
     @Test
     public void searchRetrieveFCSQLTest ()
             throws IOException, SAXException, ParserConfigurationException {
 
-        createExpectationForSearch("[tt:lemma=\"fein\"]",
+        createExpectationForSearch("[tt:lemma=\"fein\"]", "fcsql", "2.0", "0",
                 "search-lemma-fein.jsonld");
-        createExpectationForMatchInfoLemmaFein();
+        createExpectationForMatchInfo("GOE-AGF-00000-p4276-4277.jsonld",
+                "/corpus/GOE/AGF/00000/p4276-4277/matchInfo");
 
         ClientResponse response = resource()
                 .queryParam("operation", "searchRetrieve")
@@ -150,7 +152,7 @@
                 .queryParam("maximumRecords", "1").get(ClientResponse.class);
 
         InputStream entity = response.getEntity(InputStream.class);
-        checkSRUSearchRetrieveResponse(entity);
+        checkSearchRetrieveResponseSRUVersion2(entity);
     }
 
     @Test
@@ -166,7 +168,7 @@
                 .when(request().withMethod("GET").withPath("/search")
                         .withQueryStringParameter("q", "der")
                         .withQueryStringParameter("ql", "cql")
-                        .withQueryStringParameter("v", "2.0")
+                        .withQueryStringParameter("v", "1.2")
                         .withQueryStringParameter("context", "sentence")
                         .withQueryStringParameter("count", "1")
                         .withQueryStringParameter("offset", "50"))
@@ -193,12 +195,13 @@
         ClientResponse response = resource()
                 .queryParam("operation", "searchRetrieve")
                 .queryParam("query", "der").queryParam("startRecord", "51")
-                .queryParam("maximumRecords", "1").get(ClientResponse.class);
+                .queryParam("version", "1.2").queryParam("maximumRecords", "1")
+                .get(ClientResponse.class);
         InputStream entity = response.getEntity(InputStream.class);
         Document doc = docBuilder.parse(entity);
 
         NodeList nodelist =
-                doc.getElementsByTagName("sruResponse:recordPosition");
+                doc.getElementsByTagName("sru:recordPosition");
         assertEquals("51", nodelist.item(0).getTextContent());
     }
 
@@ -276,17 +279,7 @@
             throws URISyntaxException, ClientProtocolException, IOException,
             IllegalStateException, SAXException {
 
-        String korapResources = IOUtils.toString(
-                ClassLoader.getSystemResourceAsStream(
-                        "korap-api-responses/resources.json"),
-                StandardCharsets.UTF_8);
-
-        mockClient.reset()
-                .when(request().withMethod("GET").withPath("/resource"))
-                .respond(response()
-                        .withHeader(new Header("Content-Type",
-                                "application/json; charset=utf-8"))
-                        .withBody(korapResources).withStatusCode(200));
+        createRetrieveResource();
 
         ClientResponse response = resource().queryParam("operation", "explain")
                 .queryParam("x-fcs-endpoint-description", "true")
diff --git a/src/test/java/de/ids_mannheim/korap/test/RedirectTest.java b/src/test/java/de/ids_mannheim/korap/test/RedirectTest.java
index 35df809..cda0c2f 100644
--- a/src/test/java/de/ids_mannheim/korap/test/RedirectTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/RedirectTest.java
@@ -12,16 +12,12 @@
 
 import org.apache.commons.io.IOUtils;
 import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.HttpClient;
-import org.apache.http.impl.client.HttpClientBuilder;
 import org.junit.Test;
 import org.mockserver.model.Header;
 import org.xml.sax.SAXException;
 
 import com.sun.jersey.api.client.ClientResponse;
 
-import de.ids_mannheim.korap.util.RedirectStrategy;
-
 /**
  * 
  * @author margaretha
@@ -62,6 +58,9 @@
                                 "application/json; charset=utf-8"))
                         .withBody(searchResult).withStatusCode(200));
 
+        createExpectationForMatchInfo("GOE-AGF-00000-p4276-4277.jsonld",
+                "/corpus/GOE/AGF/00000/p4276-4277/matchInfo");
+
         ClientResponse response = resource()
                 .queryParam("operation", "searchRetrieve")
                 .queryParam("query", "[tt:lemma=\"fein\"]")
@@ -69,6 +68,6 @@
                 .queryParam("maximumRecords", "1").get(ClientResponse.class);
 
         InputStream entity = response.getEntity(InputStream.class);
-        checkSRUSearchRetrieveResponse(entity);
+        checkSearchRetrieveResponseSRUVersion2(entity);
     }
 }