Mercurial > lada > lada-server
diff src/test/java/de/intevation/lada/test/ServiceTest.java @ 834:cb1cfc8c81ed
Moved base class for tests to package 'test'.
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Fri, 08 Jan 2016 12:07:26 +0100 |
parents | |
children | 2fe625c91ab3 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/de/intevation/lada/test/ServiceTest.java Fri Jan 08 12:07:26 2016 +0100 @@ -0,0 +1,363 @@ +package de.intevation.lada.test; + +import java.io.InputStream; +import java.io.StringReader; +import java.net.URL; +import java.sql.Timestamp; +import java.util.List; +import java.util.Map.Entry; +import java.util.Scanner; + +import javax.json.Json; +import javax.json.JsonException; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; +import javax.json.JsonReader; +import javax.json.JsonValue; +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.apache.commons.lang.WordUtils; +import org.junit.Assert; + +import de.intevation.lada.BaseTest; +import de.intevation.lada.Protocol; +import de.intevation.lada.test.land.ProbeTest; + +public class ServiceTest { + + protected List<Protocol> protocol; + + protected List<String> timestampAttributes; + + protected URL baseUrl; + + public void init(URL baseUrl, List<Protocol> protocol) { + this.baseUrl = baseUrl; + this.protocol = protocol; + } + + /** + * @return The test protocol + */ + public List<Protocol> getProtocol() { + return protocol; + } + + protected JsonObject readJsonResource(String resource) { + InputStream stream = + ProbeTest.class.getResourceAsStream(resource); + Scanner scanner = new Scanner(stream, "UTF-8"); + scanner.useDelimiter("\\A"); + String raw = scanner.next(); + scanner.close(); + JsonReader reader = Json.createReader(new StringReader(raw)); + JsonObject content = reader.readObject(); + reader.close(); + return content; + } + + protected JsonObjectBuilder convertObject(JsonObject object) { + JsonObjectBuilder builder = Json.createObjectBuilder(); + for (Entry<String, JsonValue> entry : object.entrySet()) { + String key = WordUtils.capitalize( + entry.getKey(), new char[]{'_'}).replaceAll("_",""); + key = key.replaceFirst(key.substring(0, 1), key.substring(0, 1).toLowerCase()); + if (timestampAttributes.contains(key)) { + Timestamp timestamp = Timestamp.valueOf(entry.getValue().toString().replaceAll("\"", "")); + builder.add(key, timestamp.getTime()); + } + else { + builder.add(key, entry.getValue()); + } + } + return builder; + } + + public JsonObject getAll(String name, String parameter) { + System.out.print("."); + Protocol prot = new Protocol(); + prot.setName(name + " service"); + prot.setType("get all"); + prot.setPassed(false); + protocol.add(prot); + /* Create a client*/ + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(baseUrl + parameter); + /* Request all objects*/ + Response response = target.request() + .header("X-SHIB-user", BaseTest.TEST_USER) + .header("X-SHIB-roles", BaseTest.TEST_ROLES) + .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()); + prot.setPassed(true); + return content; + } + catch(JsonException je) { + prot.addInfo("exception", je.getMessage()); + Assert.fail(je.getMessage()); + } + return null; + } + /** + * Test the GET Service by requesting a single object by id. + * + * @param baseUrl The url pointing to the test deployment. + */ + public JsonObject getById( + String name, + String parameter, + JsonObject expected + ) { + System.out.print("."); + Protocol prot = new Protocol(); + prot.setName(name + " service"); + prot.setType("get by Id"); + prot.setPassed(false); + protocol.add(prot); + try { + /* Create a client*/ + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(baseUrl + parameter); + prot.addInfo("parameter", parameter); + /* Request a object by id*/ + Response response = target.request() + .header("X-SHIB-user", BaseTest.TEST_USER) + .header("X-SHIB-roles", BaseTest.TEST_ROLES) + .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.assertFalse(content.getJsonObject("data").isEmpty()); + JsonObject object = content.getJsonObject("data"); + for (Entry<String, JsonValue> entry : expected.entrySet()) { + if (entry.getKey().equals("parentModified") || + entry.getKey().equals("treeModified")) { + continue; + } + Assert.assertEquals(entry.getValue(), object.get(entry.getKey())); + } + prot.addInfo("object", "equals"); + prot.setPassed(true); + return content; + } + catch(JsonException je) { + prot.addInfo("exception",je.getMessage()); + Assert.fail(je.getMessage()); + } + return null; + } + + /** + * Test the GET service using filters. + * + * @param baseUrl The url poining to the test deployment. + */ + public JsonObject filter(String name, String parameter) { + System.out.print("."); + Protocol prot = new Protocol(); + prot.setName(name + " service"); + prot.setType("filter"); + prot.setPassed(false); + protocol.add(prot); + try { + /* Create a client*/ + Client client = ClientBuilder.newClient(); + WebTarget target = + client.target(baseUrl + parameter);//"probe?qid=2&mst_id=11010&umw_id=N24"); + prot.addInfo("filter", parameter);//"qid=2&mst_id=11010&umw_id=N24"); + /* Request the objects using the filter*/ + Response response = target.request() + .header("X-SHIB-user", BaseTest.TEST_USER) + .header("X-SHIB-roles", BaseTest.TEST_ROLES) + .get(); + String entity = response.readEntity(String.class); + /* 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()); + prot.setPassed(true); + return content; + } + catch(JsonException je) { + prot.addInfo("exception", je.getMessage()); + Assert.fail(je.getMessage()); + } + return null; + } + + /** + * Test the CREATE Service. + * + * @param baseUrl The url pointing to the test deployment. + */ + public JsonObject create(String name, String parameter, JsonObject create) { + System.out.print("."); + Protocol prot = new Protocol(); + prot.setName(name + " service"); + prot.setType("create"); + prot.setPassed(false); + protocol.add(prot); + try { + /* Create a client*/ + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(baseUrl + parameter); + /* Send a post request containing a new probe*/ + Response response = target.request() + .header("X-SHIB-user", BaseTest.TEST_USER) + .header("X-SHIB-roles", BaseTest.TEST_ROLES) + .post(Entity.entity(create.toString(), 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(); + /* 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")); + prot.setPassed(true); + return content; + } + catch(JsonException je) { + prot.addInfo("exception", je.getMessage()); + Assert.fail(je.getMessage()); + } + return null; + } + + /** + * Test the probe update service. + * + * @param baseUrl The url pointing to the test deployment. + */ + public JsonObject update( + String name, + String parameter, + String updateAttribute, + String oldValue, + String newValue + ) { + System.out.print("."); + Protocol prot = new Protocol(); + prot.setName(name + " service"); + prot.setType("update"); + prot.setPassed(false); + protocol.add(prot); + try { + /* Create a client*/ + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(baseUrl + parameter); + /* Request a with the saved id*/ + Response response = target.request() + .header("X-SHIB-user", BaseTest.TEST_USER) + .header("X-SHIB-roles", BaseTest.TEST_ROLES) + .get(); + String entity = response.readEntity(String.class); + /* Try to parse the response*/ + JsonReader reader = Json.createReader(new StringReader(entity)); + JsonObject oldObject = reader.readObject().getJsonObject("data"); + /* Change the hauptprobenNr*/ + String updatedEntity = + oldObject.toString().replace(oldValue, newValue); + prot.addInfo("updated datafield", updateAttribute); + prot.addInfo("updated value", oldValue); + prot.addInfo("updated to", newValue); + /* Send the updated probe via put request*/ + WebTarget putTarget = client.target(baseUrl + parameter); + Response updated = putTarget.request() + .header("X-SHIB-user", BaseTest.TEST_USER) + .header("X-SHIB-roles", BaseTest.TEST_ROLES) + .put(Entity.entity(updatedEntity, MediaType.APPLICATION_JSON)); + /* Try to parse the response*/ + JsonReader updatedReader = Json.createReader( + new StringReader(updated.readEntity(String.class))); + JsonObject updatedObject = updatedReader.readObject(); + /* Verify the response*/ + Assert.assertTrue(updatedObject.getBoolean("success")); + prot.addInfo("success", updatedObject.getBoolean("success")); + Assert.assertEquals("200", updatedObject.getString("message")); + prot.addInfo("message", updatedObject.getString("message")); + Assert.assertEquals(newValue, + updatedObject.getJsonObject("data").getString(updateAttribute)); + prot.setPassed(true); + return updatedObject; + } + catch(JsonException je) { + prot.addInfo("exception", je.getMessage()); + Assert.fail(je.getMessage()); + } + return null; + } + + /** + * Test the DELETE Service. + * + * @param baseUrl The url pointing to the test deployment. + */ + public JsonObject delete(String name, String parameter) { + System.out.print("."); + Protocol prot = new Protocol(); + prot.setName(name + " service"); + prot.setType("delete"); + prot.setPassed(false); + protocol.add(prot); + try { + /* Create a client*/ + Client client = ClientBuilder.newClient(); + WebTarget target = + client.target(baseUrl + parameter); + prot.addInfo("parameter", parameter); + /* Delete a probe with the id saved when created a probe*/ + Response response = target.request() + .header("X-SHIB-user", BaseTest.TEST_USER) + .header("X-SHIB-roles", BaseTest.TEST_ROLES) + .delete(); + String entity = response.readEntity(String.class); + /* 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")); + prot.setPassed(true); + return content; + } + catch(JsonException je) { + prot.addInfo("exception", je.getMessage()); + Assert.fail(je.getMessage()); + } + return null; + } +}