[Lada-commits] [PATCH 7 of 9] Updated and added tests for probe service
Wald Commits
scm-commit at wald.intevation.org
Wed Feb 11 13:02:45 CET 2015
# HG changeset patch
# User Raimund Renkert <raimund.renkert at intevation.de>
# Date 1423655819 -3600
# Node ID ee5c7309e4b978a7ac717b5c6f555b55a97713f3
# Parent 4986ef8bff19ca0fbea3065a4942b5e74bdf13ab
Updated and added tests for probe service.
diff -r 4986ef8bff19 -r ee5c7309e4b9 src/test/java/de/intevation/lada/test/ProbeServiceTest.java
--- 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");
}
}
More information about the Lada-commits
mailing list