changeset 495:705e7d7c5f2c

Added test class for stammdaten including first tests for datenbasis.
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 18 Feb 2015 16:12:18 +0100
parents d8ad9b8e014c
children f61377527561
files src/test/java/de/intevation/lada/LadaStammTest.java src/test/java/de/intevation/lada/test/stamm/Stammdaten.java
diffstat 2 files changed, 128 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/test/java/de/intevation/lada/LadaStammTest.java	Wed Feb 18 16:11:41 2015 +0100
+++ b/src/test/java/de/intevation/lada/LadaStammTest.java	Wed Feb 18 16:12:18 2015 +0100
@@ -34,7 +34,10 @@
 
     private static Logger logger = Logger.getLogger(LadaStammTest.class);
 
+    private Stammdaten stammdatenTest;
+
     public LadaStammTest () {
+        stammdatenTest = new Stammdaten();
         testProtocol = new ArrayList<Protocol>();
         verboseLogging = true;
     }
@@ -43,4 +46,16 @@
     public static void beforeTests() {
         logger.info("---------- Testing Lada Stamm Services ----------");
     }
+
+    @Test
+    @RunAsClient
+    public final void testDatenbasisAll(@ArquillianResource URL baseUrl) {
+        stammdatenTest.getAll(baseUrl, "datenbasis", testProtocol);
+    }
+
+    @Test
+    @RunAsClient
+    public final void testDatenbasisById(@ArquillianResource URL baseUrl) {
+        stammdatenTest.getById(baseUrl, "datenbasis", 9, testProtocol);
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/de/intevation/lada/test/stamm/Stammdaten.java	Wed Feb 18 16:12:18 2015 +0100
@@ -0,0 +1,113 @@
+package de.intevation.lada.test.stamm;
+
+import java.io.StringReader;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.json.Json;
+import javax.json.JsonException;
+import javax.json.JsonObject;
+import javax.json.JsonReader;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.Response;
+
+import org.junit.Assert;
+
+import de.intevation.lada.Protocol;
+
+public class Stammdaten {
+
+    private static Map<String, String> compare;
+
+    public Stammdaten() {
+        compare = new HashMap<String, String>();
+        compare.put("datenbasis",
+            "{\"id\":9,\"beschreibung\":\"Europa\",\"datenbasis\":\"Europa\"}");
+    }
+
+    /**
+     * Test the GET Service by requesting all objects.
+     *
+     * @param baseUrl The url pointing to the test deployment.
+     */
+    public final void getAll(URL baseUrl, String type, List<Protocol> protocol) {
+        System.out.print(".");
+        Protocol prot = new Protocol();
+        prot.setName(type + "Service");
+        prot.setType("get all");
+        prot.setPassed(false);
+        protocol.add(prot);
+        Assert.assertNotNull(type);
+        /* Create a client*/
+        Client client = ClientBuilder.newClient();
+        WebTarget target = client.target(baseUrl + type);
+        /* Request all objects*/
+        Response response = target.request().get();
+        String entity = response.readEntity(String.class);
+        try{
+            /* Try to parse the response*/
+            JsonReader reader = Json.createReader(new StringReader(entity));
+            JsonObject content = reader.readObject();
+            /* Verify the response*/
+            Assert.assertTrue(content.getBoolean("success"));
+            prot.addInfo("success", content.getBoolean("success"));
+            Assert.assertEquals("200", content.getString("message"));
+            prot.addInfo("message", content.getString("message"));
+            Assert.assertNotNull(content.getJsonArray("data"));
+            prot.addInfo("objects", content.getJsonArray("data").size());
+        }
+        catch(JsonException je) {
+            prot.addInfo("exception", je.getMessage());
+            Assert.fail(je.getMessage());
+        }
+        prot.setPassed(true);
+    }
+
+    public final void getById(
+        URL baseUrl,
+        String type,
+        Integer id,
+        List<Protocol> protocol
+    ) {
+        System.out.print(".");
+        Protocol prot = new Protocol();
+        prot.setName(type + "Service");
+        prot.setType("get by Id");
+        prot.setPassed(false);
+        protocol.add(prot);
+        try {
+            /* Create a json object from static string*/
+            JsonReader fromStringRreader =
+                Json.createReader(new StringReader(compare.get(type)));
+            JsonObject comp = fromStringRreader.readObject();
+            /* Create a client*/
+            Client client = ClientBuilder.newClient();
+            WebTarget target = client.target(baseUrl + type +"/" + id);
+            prot.addInfo(type + "Id", id);
+            /* Request an object by id*/
+            Response response = target.request().get();
+            String entity = response.readEntity(String.class);
+            /* Try to parse the response*/
+            JsonReader fromServiceReader =
+                Json.createReader(new StringReader(entity));
+            JsonObject content = fromServiceReader.readObject();
+            /* Verify the response*/
+            Assert.assertTrue(content.getBoolean("success"));
+            prot.addInfo("success", content.getBoolean("success"));
+            Assert.assertEquals("200", content.getString("message"));
+            prot.addInfo("message", content.getString("message"));
+            Assert.assertEquals(comp,
+                content.getJsonObject("data"));
+            prot.addInfo("object", "equals");
+        }
+        catch(JsonException je) {
+            prot.addInfo("exception", je.getMessage());
+            Assert.fail(je.getMessage());
+        }
+        prot.setPassed(true);
+    }
+}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)