Tests: handle empty/missing parameters

Change-Id: I7d86e7909bfc3c213faf50391dcb873401a5df65
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
index 7595e87..1cfac2a 100644
--- a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
@@ -6,6 +6,8 @@
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.LinkedList;
+
+import javax.ws.rs.BadRequestException;
 import javax.ws.rs.FormParam;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
@@ -16,6 +18,8 @@
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.ResponseBuilder;
+import javax.ws.rs.core.Response.Status;
+
 import com.fasterxml.jackson.core.JsonFactory;
 import com.fasterxml.jackson.core.JsonParser;
 import com.fasterxml.jackson.databind.JsonNode;
@@ -33,6 +37,16 @@
      * response as json(all of the response) and
      * as rtf(matches)
      * 
+     * @param fname
+     *            file name
+     * @param format
+     *            the file format value rtf or json.
+     * @param q
+     *            the query
+     * @param ql
+     *            the query language
+     * 
+     * 
      */
     @POST
     @Path("export")
@@ -41,7 +55,18 @@
             @FormParam("format") String format, @FormParam("q") String q,
             @FormParam("ql") String ql) throws IOException {
 
+        String[][] params = { { "fname", fname }, { "format", format },
+                { "q", q }, { "ql", ql } };
 
+        for (int i = 0; i < params.length; i++) {
+            if (params[i][1] == null || params[i][1].trim().isEmpty())
+                throw new BadRequestException(Response
+                        .status(Status.BAD_REQUEST).entity("Parameter " + "\""
+                                + params[i][0] + "\"" + " is missing or empty")
+                        .build());
+        }
+
+        ResponseBuilder builder;
         Client client = ClientBuilder.newClient();
 
         String url = "http://localhost:8089/api/v1.0/search?context=sentence"
@@ -49,7 +74,6 @@
         WebTarget resource = client.target(url);
         String resp = resource.request(MediaType.APPLICATION_JSON)
                 .get(String.class);
-        ResponseBuilder builder;
 
         //format == json
         if (format.equals("json")) {
diff --git a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/IdsExportServiceTest.java b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/IdsExportServiceTest.java
index 5b73cba..57c1d91 100644
--- a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/IdsExportServiceTest.java
+++ b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/IdsExportServiceTest.java
@@ -3,6 +3,10 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
 import javax.ws.rs.client.Entity;
 import javax.ws.rs.core.Application;
 import javax.ws.rs.core.HttpHeaders;
@@ -33,8 +37,7 @@
     // Client is pre-configured in JerseyTest
     /**
      * Tests if webservice returns a document with the right filename
-     * and file
-     * format.
+     * and file format and handles empty/missing parameters correctly.
      */
     @Test
     public void testExportWs () {
@@ -47,6 +50,8 @@
         frmap.add("q", "Wasser");
         frmap.add("ql", "poliqarp");
 
+        String message;
+
         Response responsejson = target("/export").request()
                 .post(Entity.form(frmap));
         assertEquals("Request JSON: Http Response should be 200: ",
@@ -84,6 +89,37 @@
         assertTrue("Request RTF: Filename should be set correctly: ",
                 responsertf.getHeaderString(HttpHeaders.CONTENT_DISPOSITION)
                         .contains("filename=" + filenamer));
+
+        Response resp;
+        String fvalue;
+        MultivaluedHashMap<String, String> map = new MultivaluedHashMap<String, String>();
+        map.putAll(frmap);
+
+        //Checks missing or empty parameters
+        for (String fkey : frmap.keySet()) {
+            //parameter is missing
+            fvalue = frmap.getFirst(fkey);
+            map.remove(fkey);
+            resp = target("/export").request().post(Entity.form(map));
+            assertEquals("Request RTF: Http Response should be 400: ",
+                    Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
+            message = resp.readEntity(String.class);
+            assertTrue(
+                    "Right Exception Message should be returned for missing format",
+                    message.contains(
+                            "Parameter \"" + fkey + "\" is missing or empty"));
+            //parameter is empty
+            map.putSingle(fkey, "");
+            resp = target("/export").request().post(Entity.form(map));
+            assertEquals("Request RTF: Http Response should be 400: ",
+                    Status.BAD_REQUEST.getStatusCode(), resp.getStatus());
+            message = resp.readEntity(String.class);
+            assertTrue(
+                    "Right Exception Message should be returned for missing format",
+                    message.contains(
+                            "Parameter \"" + fkey + "\" is missing or empty"));
+            map.putSingle(fkey, fvalue);
+        }
     }
 
 }