changeset 449:ee5c7309e4b9

Updated and added tests for probe service.
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 11 Feb 2015 12:56:59 +0100
parents 4986ef8bff19
children 808ea2091c1d
files src/test/java/de/intevation/lada/test/ProbeServiceTest.java
diffstat 1 files changed, 114 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/test/java/de/intevation/lada/test/ProbeServiceTest.java	Wed Feb 11 12:53:57 2015 +0100
+++ b/src/test/java/de/intevation/lada/test/ProbeServiceTest.java	Wed Feb 11 12:56:59 2015 +0100
@@ -9,7 +9,9 @@
 import javax.json.JsonReader;
 import javax.ws.rs.client.Client;
 import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
 import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
 import org.junit.Assert;
@@ -27,15 +29,35 @@
         "null,\"probenartId\":1,\"test\":false,\"umwId\":\"N72\"," +
         "\"erzeugerId\":null,\"mpKat\":\"1\",\"mplId\":null,\"mprId\":3749," +
         "\"probeNehmerId\":726,\"solldatumBeginn\":1336341600000," +
-        "\"solldatumEnde\":1336939199000}";
+        "\"solldatumEnde\":1336939199000,\"probeIdAlt\":\"000007581034X\"," +
+        "\"hauptprobenNr\":\"120510002\"}";
+
+    private static final String CREATE_PROBE =
+        "{\"baId\":\"1\",\"datenbasisId\":2,\"erzeugerId\":\"\"," +
+        "\"hauptprobenNr\":\"1234567890\",\"media\":\"\",\"mediaDesk\":" +
+        "\"\",\"mittelungsdauer\":\"\",\"mpKat\":\"\",\"mplId\":\"\"," +
+        "\"mprId\":\"\",\"mstId\":\"11010\",\"netzbetreiberId\":\"11\"," +
+        "\"probeNehmerId\":3,\"probenartId\":1,\"test\":true,\"umwId\":" +
+        "\"A1\",\"letzteAenderung\":\"2015-02-09T10:58:36\"" +
+        ",\"probeentnahmeBeginn\":\"2015-02-08T10:58:36\"," +
+        "\"probeentnahmeEnde\":\"2015-02-09T10:58:36\",\"solldatumBeginn\":" +
+        "\"2015-02-09T10:58:36\",\"solldatumEnde\":\"2015-02-09T10:58:36\"}";
+
+
+    private Integer createdProbeId;
 
     public final void test(URL baseUrl) throws Exception {
+        System.out.println("\nStarting test (2) on Probe-Service:");
         probeGetAllService(baseUrl);
         probeGetByIdService(baseUrl);
+        probeCreate(baseUrl);
+        probeUpdate(baseUrl);
+        probeDelete(baseUrl);
     }
 
     private final void probeGetAllService(URL baseUrl)
     throws Exception {
+        System.out.println("Testing get: ");
         Client client = ClientBuilder.newClient();
         WebTarget target = client.target(baseUrl + "probe");
         Response response = target.request().get();
@@ -53,10 +75,11 @@
 
     private final void probeGetByIdService(URL baseUrl)
     throws Exception {
+        System.out.println("Testing getById: ");
         try {
-            JsonReader fromStrinRreader =
+            JsonReader fromStringRreader =
                 Json.createReader(new StringReader(COMPARE_PROBE));
-            JsonObject staticProbe = fromStrinRreader.readObject();
+            JsonObject staticProbe = fromStringRreader.readObject();
             Client client = ClientBuilder.newClient();
             WebTarget target = client.target(baseUrl + "probe/1");
             Response response = target.request().get();
@@ -72,5 +95,93 @@
         catch(JsonException je) {
             Assert.fail(je.getMessage());
         }
+        System.out.println("passed");
+    }
+    private final void probeCreate(URL baseUrl)
+    throws Exception {
+        System.out.println("Testing create: ");
+        try {
+            /* Create a client*/
+            Client client = ClientBuilder.newClient();
+            WebTarget target = client.target(baseUrl + "probe");
+            /* Send a post request containing a new probe*/
+            Response response = target.request().post(
+                    Entity.entity(CREATE_PROBE, MediaType.APPLICATION_JSON));
+            String entity = response.readEntity(String.class);
+            /* Try to parse the response*/
+            JsonReader fromServiceReader =
+                Json.createReader(new StringReader(entity));
+            JsonObject content = fromServiceReader.readObject();
+            /* Save the probeid*/
+            this.createdProbeId =
+                content.getJsonObject("data").getJsonNumber("id").intValue();
+            /* Verify the response*/
+            Assert.assertTrue(content.getBoolean("success"));
+            Assert.assertEquals("200", content.getString("message"));
+        }
+        catch(JsonException je) {
+            Assert.fail(je.getMessage());
+        }
+        System.out.println("passed");
+    }
+
+    private final void probeUpdate(URL baseUrl)
+    throws Exception {
+        System.out.println("Testing update: ");
+        try {
+            /* Create a client*/
+            Client client = ClientBuilder.newClient();
+            WebTarget target =
+                client.target(baseUrl + "probe/" + this.createdProbeId);
+            /* Request a probe with the id saved when created a probe*/
+            Response response = target.request().get();
+            String entity = response.readEntity(String.class);
+            /* Try to parse the response*/
+            JsonReader reader = Json.createReader(new StringReader(entity));
+            JsonObject oldProbe = reader.readObject().getJsonObject("data");
+            /* Change the hauptprobenNr*/
+            String updatedEntity =
+                oldProbe.toString().replace("1234567890", "2345678901");
+            /* Send the updated probe via put reauest*/
+            WebTarget putTarget = client.target(baseUrl + "probe");
+            Response updated = putTarget.request().put(
+                Entity.entity(updatedEntity, MediaType.APPLICATION_JSON));
+            /* Try to parse the response*/
+            JsonReader updatedReader = Json.createReader(
+                new StringReader(updated.readEntity(String.class)));
+            JsonObject updatedProbe = updatedReader.readObject();
+            /* Verify the response*/
+            Assert.assertTrue(updatedProbe.getBoolean("success"));
+            Assert.assertEquals("200", updatedProbe.getString("message"));
+            Assert.assertEquals("2345678901",
+                updatedProbe.getJsonObject("data").getString("hauptprobenNr"));
+        }
+        catch(JsonException je) {
+            Assert.fail(je.getMessage());
+        }
+        System.out.println("passed");
+    }
+
+    private final void probeDelete(URL baseUrl) {
+        System.out.println("Testing delete: ");
+        try {
+            /* Create a client*/
+            Client client = ClientBuilder.newClient();
+            WebTarget target =
+                client.target(baseUrl + "probe/" + this.createdProbeId);
+            /* Delete a probe with th id saved when created a probe*/
+            Response response = target.request().delete();
+            String entity = response.readEntity(String.class);
+            /* Try to parse the response*/
+            JsonReader reader = Json.createReader(new StringReader(entity));
+            JsonObject respObj = reader.readObject();
+            /* Verify the response*/
+            Assert.assertTrue(respObj.getBoolean("success"));
+            Assert.assertEquals("200", respObj.getString("message"));
+        }
+        catch(JsonException je) {
+            Assert.fail(je.getMessage());
+        }
+        System.out.println("passed");
     }
 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)