# HG changeset patch # User Raimund Renkert # Date 1424187874 -3600 # Node ID 716152f4197ad533b472a6039ad8fb45bd7b1ddc # Parent f033d00681d6d838f3c99d7ab9e0877f7371e330 Added tests for proben kommentar. diff -r f033d00681d6 -r 716152f4197a src/test/java/de/intevation/lada/LadaTest.java --- a/src/test/java/de/intevation/lada/LadaTest.java Tue Feb 17 16:44:08 2015 +0100 +++ b/src/test/java/de/intevation/lada/LadaTest.java Tue Feb 17 16:44:34 2015 +0100 @@ -45,7 +45,7 @@ private static Logger logger = Logger.getLogger(LadaTest.class); - private static boolean verboseLogging = true; + private static boolean verboseLogging = false; private Probe probeTest; private Query queryTest; @@ -67,6 +67,7 @@ */ @Deployment(testable=true) public static WebArchive createDeployment() throws Exception { + logger.info("\n\n---------- Test Protocol ----------"); logger.info("Create and deploy: " + ARCHIVE_NAME); WebArchive archive = ShrinkWrap.create(WebArchive.class, ARCHIVE_NAME) .addPackages(true, Package.getPackage("de.intevation.lada")) @@ -176,6 +177,16 @@ } /** + * Testing GET Services. + */ + @Test + @RunAsClient + public final void testA_KommentarPGetFilterServices(@ArquillianResource URL baseUrl) + throws Exception { + this.kommentarPTest.filterService(baseUrl, testProtocol); + } + + /** * Testing CREATE services. */ @Test @@ -188,6 +199,10 @@ baseUrl, testProtocol, this.probeTest.getCreatedProbeId()); + this.kommentarPTest.createService( + baseUrl, + testProtocol, + this.probeTest.getCreatedProbeId()); } /** @@ -213,12 +228,25 @@ } /** + * Testing UPDATE services. + */ + @Test + @RunAsClient + public final void testC_kommentarPUpdateService(@ArquillianResource URL baseUrl) + throws Exception { + Assert.assertNotNull(this.kommentarPTest.getCreatedKommentarId()); + this.kommentarPTest.updateService(baseUrl, testProtocol); + } + + /** * Testing DELETE services. */ @Test @RunAsClient public final void testD_DeleteServices(@ArquillianResource URL baseUrl) throws Exception { + Assert.assertNotNull(this.kommentarPTest.getCreatedKommentarId()); + this.kommentarPTest.deleteService(baseUrl, testProtocol); Assert.assertNotNull(this.messungTest.getCreatedMessungId()); this.messungTest.deleteService(baseUrl, testProtocol); Assert.assertNotNull(this.probeTest.getCreatedProbeId()); diff -r f033d00681d6 -r 716152f4197a src/test/java/de/intevation/lada/test/KommentarP.java --- a/src/test/java/de/intevation/lada/test/KommentarP.java Tue Feb 17 16:44:08 2015 +0100 +++ b/src/test/java/de/intevation/lada/test/KommentarP.java Tue Feb 17 16:44:34 2015 +0100 @@ -10,7 +10,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; @@ -19,13 +21,19 @@ public class KommentarP { - private List protocol; - private static final String COMPARE_KOMMENTARP = "{\"datum\":1321002077000,\"erzeuger\":\"06010\",\"id\":1,\"text\":" + "\"Die Probe wurde in Darmstadt gammaspektrometrisch gemessen und " + "für die Sr-Bestimmung verascht. \",\"probeId\":361}"; + private static final String CREATE_KOMMENTARP = + "{\"probeId\":\"PID\",\"erzeuger\":\"11010\",\"text\":" + + "\"test\",\"datum\":\"2015-02-09T10:58:36\"}"; + + private List protocol; + + private static Integer createdKommentarId; + /** * @return the protocol */ @@ -34,6 +42,13 @@ } /** + * @return the createdKommentarId + */ + public Integer getCreatedKommentarId() { + return createdKommentarId; + } + + /** * Test the GET Service by requesting all KommentarP objects. * * @param baseUrl The url pointing to the test deployment. @@ -115,4 +130,180 @@ } prot.setPassed(true); } + + /** + * Test the GET service using filters. + * + * @param baseUrl The url poining to the test deployment. + */ + public final void filterService(URL baseUrl, List protocol) { + System.out.print("."); + Protocol prot = new Protocol(); + prot.setName("ProbeKommentarService"); + prot.setType("get by filter"); + prot.setPassed(false); + protocol.add(prot); + try { + /* Create a client*/ + Client client = ClientBuilder.newClient(); + WebTarget target = + client.target(baseUrl + "pkommentar?probeId=400"); + prot.addInfo("filter", "probeId=400"); + /* Request the objects using the filter*/ + Response response = target.request().get(); + 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")); + prot.addInfo("success", respObj.getBoolean("success")); + Assert.assertEquals("200", respObj.getString("message")); + prot.addInfo("message", respObj.getString("message")); + Assert.assertNotNull(respObj.getJsonArray("data")); + prot.addInfo("objects", respObj.getJsonArray("data").size()); + } + catch(JsonException je) { + prot.addInfo("exception", je.getMessage()); + Assert.fail(je.getMessage()); + } + prot.setPassed(true); + } + + /** + * Test the CREATE Service. + * + * @param baseUrl The url pointing to the test deployment. + */ + public final void createService( + URL baseUrl, + List protocol, + Integer probeId) + throws Exception { + System.out.print("."); + Protocol prot = new Protocol(); + prot.setName("ProbeKommentarService"); + prot.setType("create"); + prot.setPassed(false); + protocol.add(prot); + try { + /* Create a client*/ + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(baseUrl + "pkommentar"); + /* Send a post request containing a new probe*/ + String mess = CREATE_KOMMENTARP.replace("PID", probeId.toString()); + Response response = target.request().post( + Entity.entity(mess, 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 id*/ + createdKommentarId = + content.getJsonObject("data").getJsonNumber("id").intValue(); + prot.addInfo("kommentarId", createdKommentarId); + /* 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")); + } + catch(JsonException je) { + prot.addInfo("exception", je.getMessage()); + Assert.fail(je.getMessage()); + } + prot.setPassed(true); + } + + /** + * Test the UPDATE Service. + * + * @param baseUrl The url pointing to the test deployment. + */ + public final void updateService(URL baseUrl, List protocol) + throws Exception { + System.out.print("."); + Protocol prot = new Protocol(); + prot.setName("ProbeKommentarService"); + prot.setType("update"); + prot.setPassed(false); + protocol.add(prot); + try { + /* Create a client*/ + Client client = ClientBuilder.newClient(); + WebTarget target = + client.target(baseUrl + "pkommentar/" + createdKommentarId); + prot.addInfo("kommentarId", createdKommentarId); + /* Request a kommentar with the id saved when created a kommentar*/ + Response response = target.request().get(); + String entity = response.readEntity(String.class); + /* Try to parse the response*/ + JsonReader reader = Json.createReader(new StringReader(entity)); + JsonObject oldKommentar = reader.readObject().getJsonObject("data"); + /* Change the text*/ + String updatedEntity = + oldKommentar.toString().replace("test", "neu"); + prot.addInfo("updated field", "text"); + prot.addInfo("updated value", "test"); + prot.addInfo("updated to", "neu"); + /* Send the updated probe via put reauest*/ + WebTarget putTarget = client.target(baseUrl + "pkommentar"); + 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 updatedMessung = updatedReader.readObject(); + /* Verify the response*/ + Assert.assertTrue(updatedMessung.getBoolean("success")); + prot.addInfo("success", updatedMessung.getBoolean("success")); + Assert.assertEquals("200", updatedMessung.getString("message")); + prot.addInfo("message", updatedMessung.getString("message")); + Assert.assertEquals("neu", + updatedMessung.getJsonObject("data").getString("text")); + } + catch(JsonException je) { + prot.addInfo("exception", je.getMessage()); + Assert.fail(je.getMessage()); + } + prot.setPassed(true); + } + + /** + * Test the DELETE Service. + * + * @param baseUrl The url pointing to the test deployment. + */ + public final void deleteService(URL baseUrl, List protocol) { + System.out.print("."); + Protocol prot = new Protocol(); + prot.setName("ProbeKommentarService"); + prot.setType("delete"); + prot.setPassed(false); + protocol.add(prot); + try { + /* Create a client*/ + Client client = ClientBuilder.newClient(); + WebTarget target = + client.target(baseUrl + "pkommentar/" + createdKommentarId); + prot.addInfo("kommentarId", createdKommentarId); + /* Delete a probe with the 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")); + prot.addInfo("success", respObj.getBoolean("success")); + Assert.assertEquals("200", respObj.getString("message")); + prot.addInfo("message", respObj.getString("message")); + } + catch(JsonException je) { + prot.addInfo("exception", je.getMessage()); + Assert.fail(je.getMessage()); + } + prot.setPassed(true); + } }