# HG changeset patch # User Raimund Renkert # Date 1424272338 -3600 # Node ID 705e7d7c5f2c73bdc67f1d6e4e97b8dd3ba7c77b # Parent d8ad9b8e014cacf5043e3411a0269f58ff724e6a Added test class for stammdaten including first tests for datenbasis. diff -r d8ad9b8e014c -r 705e7d7c5f2c src/test/java/de/intevation/lada/LadaStammTest.java --- 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(); 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); + } } diff -r d8ad9b8e014c -r 705e7d7c5f2c src/test/java/de/intevation/lada/test/stamm/Stammdaten.java --- /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 compare; + + public Stammdaten() { + compare = new HashMap(); + 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) { + 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 + ) { + 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); + } +}