Handle resource pid & resolve resources (virtual corpora) using map #62

Change-Id: I0bba01e4417876b625ae6da5d135d412d4aeba43
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 560a11e..3fc8726 100644
--- a/src/test/java/de/ids_mannheim/korap/test/BaseTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/BaseTest.java
@@ -44,13 +44,13 @@
         mockServer.stop();
     }
 
-    protected void createRetrieveResource () throws IOException {
+    protected void createExpectationForRetrieveResource () throws IOException {
         String korapResources = IOUtils.toString(
                 ClassLoader.getSystemResourceAsStream(
                         "korap-api-responses/resources.json"),
                 StandardCharsets.UTF_8);
 
-        mockClient.reset()
+        mockClient
                 .when(request().withMethod("GET").withPath("/resource"))
                 .respond(response()
                         .withHeader(new Header("Content-Type",
@@ -66,7 +66,7 @@
                         "korap-api-responses/" + jsonFilename),
                 StandardCharsets.UTF_8);
 
-        mockClient.reset()
+        mockClient
                 .when(request().withMethod("GET").withPath("/search")
                         .withQueryStringParameter("q", query)
                         .withQueryStringParameter("ql", queryLanguage)
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 30b5ec3..0206da7 100644
--- a/src/test/java/de/ids_mannheim/korap/test/KorapClientTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/KorapClientTest.java
@@ -13,6 +13,7 @@
 import de.ids_mannheim.korap.sru.KorapResource;
 import de.ids_mannheim.korap.sru.KorapResult;
 import de.ids_mannheim.korap.sru.QueryLanguage;
+import eu.clarin.sru.server.SRUException;
 
 /**
  * The tests are based on the sample corpus from the Goethe corpus.
@@ -29,10 +30,11 @@
         c = new KorapClient("http://localhost:1080", 25, 50);
     }
 
-    @Test
-    public void testCQLQuery () throws HttpResponseException, IOException {
+	@Test
+	public void testCQLQuery ()
+			throws HttpResponseException, IOException, SRUException {
 
-        createExpectationForSearch("der", "cql", "1.2", "50",
+		   createExpectationForSearch("der", "cql", "1.2", "50",
                 "search-der.jsonld");
 
         result = c.query("der", QueryLanguage.CQL, "1.2", 51, 1, null);
@@ -49,8 +51,9 @@
 
     }
 
-    @Test
-    public void testOrQuery () throws HttpResponseException, IOException {
+	@Test
+	public void testOrQuery ()
+			throws HttpResponseException, IOException, SRUException {
 
         createExpectationForSearch("(\"blaue\"|\"grüne\")", "fcsql", "2.0", "0",
                 "search-or.jsonld");
@@ -106,11 +109,14 @@
     @Test
     public void testRetrieveResource ()
             throws HttpResponseException, Exception {
-        createRetrieveResource();
+        createExpectationForRetrieveResource();
         KorapResource[] resources = c.retrieveResources();
         assertEquals(3, resources.length);
-        assertEquals("WPD17", resources[0].getResourceId());
-        assertEquals("WDD17", resources[1].getResourceId());
-        assertEquals("WUD17", resources[2].getResourceId());
+		assertEquals("http://hdl.handle.net/10932/00-03B6-558F-4E10-6201-1",
+				resources[0].getResourceId());
+        assertEquals("http://hdl.handle.net/10932/00-03B6-558F-5EA0-6301-B", 
+        		resources[1].getResourceId());
+        assertEquals("http://hdl.handle.net/10932/00-03B6-558F-6EF0-6401-F", 
+        		resources[2].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 ef6511a..d04b0f2 100644
--- a/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java
@@ -40,7 +40,7 @@
                         "korap-api-responses/search-fein.jsonld"),
                 StandardCharsets.UTF_8);
 
-        mockClient.reset()
+        mockClient
                 .when(request().withMethod("GET").withPath("/search")
                         .withQueryStringParameter("q", "fein")
                         .withQueryStringParameter("ql", "cql")
@@ -53,6 +53,27 @@
                                 "application/json; charset=utf-8"))
                         .withBody(searchResult).withStatusCode(200));
     }
+    
+    private void createExpectationForSearchFeinWithUnknownCq () throws IOException {
+        String searchResult = IOUtils.toString(
+                ClassLoader.getSystemResourceAsStream(
+                        "korap-api-responses/search-fein-unknown-cq.jsonld"),
+                StandardCharsets.UTF_8);
+
+        mockClient
+                .when(request().withMethod("GET").withPath("/search")
+                        .withQueryStringParameter("q", "fein")
+                        .withQueryStringParameter("ql", "cql")
+                        .withQueryStringParameter("v", "1.2")
+                        .withQueryStringParameter("context", "sentence")
+                        .withQueryStringParameter("count", "25")
+                        .withQueryStringParameter("offset", "0")
+                        .withQueryStringParameter("cq","corpusSigle=unknown"))
+                .respond(response()
+                        .withHeader(new Header("Content-Type",
+                                "application/json; charset=utf-8"))
+                        .withBody(searchResult).withStatusCode(200));
+    }
 
     private void createExpectationForMatchInfoFein () throws IOException {
         String matchInfoResult = IOUtils.toString(
@@ -124,17 +145,45 @@
     public void searchRetrieveWithResourceId ()
             throws IOException, URISyntaxException, IllegalStateException,
             SAXException, ParserConfigurationException {
-        createExpectationForSearchFein();
+    	createExpectationForRetrieveResource();
+    	createExpectationForSearchFein();
         createExpectationForMatchInfoFein();
 
         ClientResponse response = resource()
                 .queryParam("operation", "searchRetrieve")
                 .queryParam("query", "fein").queryParam("version", "1.2")
-                .queryParam("x-fcs-context", "GOE").get(ClientResponse.class);
+                .queryParam("x-fcs-context", "http://hdl.handle.net/10932/00-03B6-558F-4E10-6201-1").get(ClientResponse.class);
 
         InputStream entity = response.getEntity(InputStream.class);
         checkSearchRetrieveResponseSRUVersion1_2(entity);
     }
+    
+    @Test
+    public void searchRetrieveWithUnknownResourceId ()
+            throws IOException, URISyntaxException, IllegalStateException,
+            SAXException, ParserConfigurationException {
+    	
+    	createExpectationForRetrieveResource();
+    	createExpectationForSearchFeinWithUnknownCq();
+        createExpectationForMatchInfoFein();
+
+        ClientResponse response = resource()
+                .queryParam("operation", "searchRetrieve")
+                .queryParam("query", "fein").queryParam("version", "1.2")
+                .queryParam("x-fcs-context", "unknown").get(ClientResponse.class);
+
+		InputStream entity = response.getEntity(InputStream.class);
+		docBuilder = factory.newDocumentBuilder();
+		Document doc = docBuilder.parse(entity);
+
+		NodeList diagnosticUri = doc.getElementsByTagName("diag:uri");
+		assertEquals("info:srw/diagnostic/1/1",
+				diagnosticUri.item(0).getTextContent());
+
+		NodeList diagnosticMessage = doc.getElementsByTagName("diag:message");
+		assertEquals("Virtual corpus with pid: unknown is not found.",
+				diagnosticMessage.item(0).getTextContent());
+	}
 
     @Test
     public void searchRetrieveFCSQLTest ()
@@ -279,7 +328,7 @@
             throws URISyntaxException, ClientProtocolException, IOException,
             IllegalStateException, SAXException {
 
-        createRetrieveResource();
+        createExpectationForRetrieveResource();
 
         ClientResponse response = resource().queryParam("operation", "explain")
                 .queryParam("x-fcs-endpoint-description", "true")