public static Map<String, String> makeTestConfig() { HashMap<String, String> config = new HashMap<>(play.test.Helpers.inMemoryDatabase()); config.put("ebean.default", "models.*"); config.put("application.secret", "foo"); config.put("application.context", "/"); return config; }
@Override protected void test() { Http.RequestBuilder postRequest = new Http.RequestBuilder().method("POST").bodyJson(getJsonRequest()).uri("/project"); Result postResult = route(postRequest); String projects = Helpers.contentAsString(postResult); System.out.println("Projects returned POST " + projects); }
/** * Checks if the API returns only new or changed customer since the specified unix timestamp * passed by the query string. * * @author dal */ @Test public void getAllCustomerXml_Since() { FakeApplication fakeApp = Helpers.fakeApplication(); running( testServer(3333, fakeApp), new Runnable() { public void run() { // Wait and create new product try { Thread.sleep(3000L); } catch (InterruptedException e) { } long since = (new Date()).getTime() / 1000L; try { Thread.sleep(3000L); } catch (InterruptedException e) { } // create a user User user = new User(); user.setEmail("*****@*****.**"); user.setIsActive(true); user.setPassword("ihatesolo"); user.setType(UserType.find.byId("admin")); user.setIsActive(true); user.save(); String[] params = new String[] { "id", "all", "token", getUserActiveAdminUser().getToken(), "since", String.valueOf(since) }; WS.Response response = callApi(CUSTOMERL_URL, params, null); // Check if status is okay and content is correct assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getHeader("Content-Type")).isEqualTo("text/xml"); assertThat(response.getHeader("Etag")).isNotEmpty(); // Get Xml dom and check for products Document dom = response.asXml(); NodeList nodes = dom.getElementsByTagName("customer"); assertThat(nodes.getLength()).isEqualTo(1); } }); }
/** * Calls the API with the passed parameters and verifies the http status and content. * * @author dal * @param url The API Url * @param queryParams An array of parameters (key,value,key,value,...) * @param expectedStatus The expected status code * @param expectedContent The expected content */ private void checkTokenLogin( final String url, final String[] queryParams, final int expectedStatus, final String expectedContent) { FakeApplication fakeApp = Helpers.fakeApplication(); running( testServer(3333, fakeApp), new Runnable() { public void run() { WS.Response response = callApi(url, queryParams, new String[] {}); // Check if status is okay and content is correct assertThat(response.getStatus()).isEqualTo(expectedStatus); assertThat(response.getBody()).contains(expectedContent); } }); }
/** * Verifies the json behavior of the API. Only one test per url because the xml and json service * call, shares the same code base. */ @Test public void getOneCustomerJson() { FakeApplication fakeApp = Helpers.fakeApplication(); running( testServer(3333, fakeApp), new Runnable() { public void run() { createCarts(); User user = User.find.byId(1); String[] params = new String[] { "id", "1", "type", "json", "token", getUserActiveAdminUser().getToken() }; WS.Response response = callApi(CUSTOMERL_URL, params, null); // Check if status is okay and content is correct assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getHeader("Content-Type")).isEqualTo("application/json"); assertThat(response.getHeader("Etag")).isNotEmpty(); Address addr = user.getAddresses().get(0); // Get json object and check for articles JsonNode node = response.asJson(); node = node.get("customers").get(0); assertThat(node.path("id").asInt()).isEqualTo(user.getId()); assertThat(node.path("name").asText()).isEqualTo(addr.getLastname()); assertThat(node.path("firstname").asText()).isEqualTo(addr.getFirstname()); assertThat(node.path("email").asText()).isEqualTo(addr.getEmail()); assertThat(node.path("birthday").asLong()) .isEqualTo(addr.getBirthday().getTime() / 1000L); // Check Addresses assertThat(node.path("addresses").size()).isEqualTo(user.getAddresses().size()); int cnt = 0; for (JsonNode n : node.path("addresses")) { Address a = user.getAddresses().get(cnt++); assertThat(n.path("id").asInt()).isEqualTo(a.getId()); assertThat(n.path("type").asInt()).isEqualTo(1); assertThat(n.path("city").asText()).isEqualTo(a.getPlace()); assertThat(n.path("street").asText()).isEqualTo(a.getStreet()); assertThat(n.path("postcode").asText()).isEqualTo(a.getZip()); assertThat(n.path("country").asText()).isEqualTo(a.getCountry().getName()); } } }); }
/** * Checks if the API returns the article specified by the query parameter 'id' and verifies the * values with the one from the database. * * @author dal */ @Test public void getOneArticleXml() { FakeApplication fakeApp = Helpers.fakeApplication(); running( testServer(3333, fakeApp), new Runnable() { public void run() { Product p = Product.find.byId(1); String[] params = new String[] {"id", "1", "token", getUserActiveAdminUser().getToken()}; WS.Response response = callApi(ARTICLES_URL, params, null); assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getHeader("Content-Type")).isEqualTo("text/xml"); assertThat(response.getHeader("Etag")).isNotEmpty(); // Get Xml dom and check for products Document dom = response.asXml(); Element node = (Element) dom.getElementsByTagName("article").item(0); DecimalFormat df = new DecimalFormat("0.00"); assertThat(node.getAttribute("id")).isEqualTo(p.getId().toString()); assertThat(node.getElementsByTagName("title").item(0).getTextContent()) .isEqualTo(p.getTitle()); assertThat(node.getElementsByTagName("description").item(0).getTextContent()) .isEqualTo(p.getDescription()); assertThat(node.getElementsByTagName("ean").item(0).getTextContent()) .isEqualTo(p.getEan().toString()); assertThat(node.getElementsByTagName("price").item(0).getTextContent()) .isEqualTo(df.format(p.getPrice()).toString()); assertThat(node.getElementsByTagName("currency").item(0).getTextContent()) .isEqualTo(CURRENCY); NodeList list = node.getElementsByTagName("attribute"); int attrMatches = 0; for (int i = 0; i < list.getLength(); i++) { for (Attribute att : p.getAttributes()) { if (list.item(i).getTextContent().equalsIgnoreCase(att.getValue())) { attrMatches++; break; } } } assertThat(attrMatches).isEqualTo(p.getAttributes().size()); } }); }
/** * Checks if the API returns the specified customer by the query parameter 'id' and has the same * values as the db entity. * * @author dal */ @Test public void getOneCustomerXml() { FakeApplication fakeApp = Helpers.fakeApplication(); running( testServer(3333, fakeApp), new Runnable() { public void run() { User user = User.find.byId(1); Address address = user.getAddresses().get(0); String[] params = new String[] {"id", "1", "token", getUserActiveAdminUser().getToken()}; WS.Response response = callApi(CUSTOMERL_URL, params, null); assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getHeader("Content-Type")).isEqualTo("text/xml"); assertThat(response.getHeader("Etag")).isNotEmpty(); // Get Xml dom and check for products Document dom = response.asXml(); Element node = (Element) dom.getElementsByTagName("customer").item(0); assertThat(node.getAttribute("id")).isEqualTo(user.getId().toString()); assertThat(node.getElementsByTagName("name").item(0).getTextContent()) .isEqualTo(address.getLastname()); assertThat(node.getElementsByTagName("firstname").item(0).getTextContent()) .isEqualTo(address.getFirstname()); assertThat(node.getElementsByTagName("email").item(0).getTextContent()) .isEqualTo(address.getEmail()); assertThat(node.getElementsByTagName("birthdate").item(0).getTextContent()) .isEqualTo(String.valueOf(address.getBirthday().getTime() / 1000L)); node = (Element) node.getElementsByTagName("address").item(0); assertThat(node.getAttribute("id")).isEqualTo(address.getId().toString()); assertThat(node.getAttribute("type")).isEqualTo("1"); assertThat(node.getElementsByTagName("city").item(0).getTextContent()) .isEqualTo(address.getPlace()); assertThat(node.getElementsByTagName("street").item(0).getTextContent()) .isEqualTo(address.getStreet()); assertThat(node.getElementsByTagName("postcode").item(0).getTextContent()) .isEqualTo(address.getZip().toString()); assertThat(node.getElementsByTagName("country").item(0).getTextContent()) .isEqualTo(address.getCountry().getName()); } }); }
/** * Checks if the API calls returns only changed or new orders as the specified unix timestamp in * the query parameter. * * @author dal */ @Test public void getAllOrdersXml_Since() { FakeApplication fakeApp = Helpers.fakeApplication(); running( testServer(3333, fakeApp), new Runnable() { public void run() { // Wait and create new order createCarts(); try { Thread.sleep(3000L); } catch (InterruptedException e) { } long since = (new Date()).getTime() / 1000L; try { Thread.sleep(3000L); } catch (InterruptedException e) { } // Create cart createCart(new int[][] {{1, 3, 1}}, User.find.byId(1), CartStatus.OPEN); // Get carts String[] params = new String[] { "id", "all", "token", getUserActiveAdminUser().getToken(), "since", String.valueOf(since) }; WS.Response response = callApi(ORDERS_URL, params, null); // Check if status is okay and content is correct assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getHeader("Content-Type")).isEqualTo("text/xml"); assertThat(response.getHeader("Etag")).isNotEmpty(); // Get Xml dom and check for products Document dom = response.asXml(); NodeList nodes = dom.getElementsByTagName("order"); assertThat(nodes.getLength()).isEqualTo(1); } }); }
/** * Checks if the return order has the correct xml format and the same values as the db entity. * * @author dal */ @Test public void getOneOrderXml() { FakeApplication fakeApp = Helpers.fakeApplication(); running( testServer(3333, fakeApp), new Runnable() { public void run() { createCarts(); Cart cart = Cart.find.byId(1); Address address = cart.getAddress(); List<CartHasProduct> chp = cart.getCartHasProduct(); User user = cart.getUser(); String[] params = new String[] {"id", "1", "token", getUserActiveAdminUser().getToken()}; WS.Response response = callApi(ORDERS_URL, params, null); assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getHeader("Content-Type")).isEqualTo("text/xml"); assertThat(response.getHeader("Etag")).isNotEmpty(); // Get Xml dom and check for products Document dom = response.asXml(); Element node = (Element) dom.getElementsByTagName("order").item(0); assertThat(node.getAttribute("id")).isEqualTo(cart.getId().toString()); assertThat(node.getAttribute("customer")).isEqualTo(user.getId().toString()); assertThat(node.getAttribute("billaddress")).isEqualTo(address.getId().toString()); assertThat(node.getAttribute("shippingaddress")).isEqualTo(address.getId().toString()); assertThat(node.getAttribute("status")).isEqualTo(cart.getStatus().getId().toString()); NodeList nodeList = node.getElementsByTagName("position"); assertThat(nodeList.getLength()).isEqualTo(chp.size()); for (int i = 0; i < nodeList.getLength(); i++) { Element el = (Element) nodeList.item(i); CartHasProduct c = chp.get(i); assertThat(el.getAttribute("article")).isEqualTo(c.getProduct().getId().toString()); assertThat(el.getAttribute("amount")).isEqualTo(c.getAmount().toString()); assertThat(el.getAttribute("discount")).isEqualTo(c.getDiscount().toString()); } } }); }
/** * Verifies the json behavior of the API. Only one test per url because the xml and json service * call, shares the same code base. */ @Test public void getOneOrderJson() { FakeApplication fakeApp = Helpers.fakeApplication(); running( testServer(3333, fakeApp), new Runnable() { public void run() { createCarts(); Cart order = Cart.find.byId(1); String[] params = new String[] { "id", "1", "type", "json", "token", getUserActiveAdminUser().getToken() }; WS.Response response = callApi(ORDERS_URL, params, null); // Check if status is okay and content is correct assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getHeader("Content-Type")).isEqualTo("application/json"); assertThat(response.getHeader("Etag")).isNotEmpty(); Address addr = order.getAddress(); User user = order.getUser(); // Get json object and check for articles JsonNode node = response.asJson(); node = node.get("orders").get(0); assertThat(node.path("id").asInt()).isEqualTo(order.getId()); assertThat(node.path("customer").asInt()).isEqualTo(user.getId()); assertThat(node.path("billaddress").asInt()).isEqualTo(addr.getId()); assertThat(node.path("shippingaddress").asInt()).isEqualTo(addr.getId()); assertThat(node.path("status").asText()).isEqualTo(order.getStatus().getId()); // Check positions assertThat(node.path("positions").size()).isEqualTo(order.getCartHasProduct().size()); int cnt = 0; for (JsonNode n : node.path("positions")) { CartHasProduct prod = order.getCartHasProduct().get(cnt++); assertThat(n.path("article").asInt()).isEqualTo(prod.getProduct().getId()); assertThat(n.path("amount").asInt()).isEqualTo(prod.getAmount()); assertThat(n.path("discount").asDouble()).isEqualTo(prod.getDiscount()); } } }); }
/** * Checks if the API returns a 304 if the customer hasn't changed. * * @author dal */ @Test public void getOneCustomerXml_NotModified() { FakeApplication fakeApp = Helpers.fakeApplication(); running( testServer(3333, fakeApp), new Runnable() { public void run() { String[] params = new String[] {"id", "1", "token", getUserActiveAdminUser().getToken()}; WS.Response response = callApi(CUSTOMERL_URL, params, null); // Call service and get the etag String etag = response.getHeader("Etag"); // Call service again with the etag String[] headers = new String[] {"If-None-Match", etag}; response = callApi(CUSTOMERL_URL, params, headers); // Check response status assertThat(response.getStatus()).isEqualTo(304); assertThat(response.getBody()).isEmpty(); } }); }
/** * Creates a new user in the DB. * * @param isAdmin User is admin * @param isActive User is activ * @return The created user */ private User getNewUser(final Boolean isAdmin, final Boolean isActive) { final User user = new User(); FakeApplication fakeApp = Helpers.fakeApplication(); running( testServer(3333, fakeApp), new Runnable() { @Override public void run() { UserType type; if (isAdmin) type = UserType.find.byId("admin"); else type = UserType.find.byId("customer"); user.setEmail("*****@*****.**"); user.setIsActive(true); user.setPassword("ihatesolo"); user.setType(type); user.setIsActive(isActive); user.save(); } }); return user; }
/** * Verifies the json behavior of the API. Only one test per url because the xml and json service * call, shares the same code base. */ @Test public void getOneArticlesJson() { FakeApplication fakeApp = Helpers.fakeApplication(); running( testServer(3333, fakeApp), new Runnable() { public void run() { createCarts(); Product product = Product.find.byId(1); String[] params = new String[] { "id", "1", "type", "json", "token", getUserActiveAdminUser().getToken() }; WS.Response response = callApi(ARTICLES_URL, params, null); // Check if status is okay and content is correct assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getHeader("Content-Type")).isEqualTo("application/json"); assertThat(response.getHeader("Etag")).isNotEmpty(); // Get json object and check for articles JsonNode node = response.asJson(); node = node.get("articles").get(0); assertThat(node.path("id").asInt()).isEqualTo(product.getId()); assertThat(node.path("title").asText()).isEqualTo(product.getTitle()); assertThat(node.path("description").asText()).isEqualTo(product.getDescription()); assertThat(node.path("price").asDouble()).isEqualTo(product.getPrice()); assertThat(node.path("currency").asText()).isEqualTo(CURRENCY); // Check Attributes assertThat(node.path("attributes").size()).isEqualTo(product.getAttributes().size()); int cnt = 0; for (JsonNode n : node.path("attributes")) { Attribute att = product.getAttributes().get(cnt++); assertThat(n.asText()).isEqualTo(att.getValue()); } } }); }
/** * Checks if API returns the version as xml document. * * @author dal */ @Test public void getVersionJson() { FakeApplication fakeApp = Helpers.fakeApplication(); running( testServer(3333, fakeApp), new Runnable() { public void run() { String[] params = new String[] { "id", "all", "token", getUserActiveAdminUser().getToken(), "type", "json" }; WS.Response response = callApi(VERSION_URL, params, null); // Check if status is okay and content is correct assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getHeader("Content-Type")).isEqualTo("application/json"); // Get json object and check for version JsonNode node = response.asJson(); assertThat(node.path("version").asText()).isEqualTo(VERSION); } }); }
/** * Checks if API returns the version as xml document. * * @author dal */ @Test public void getVersionXml() { FakeApplication fakeApp = Helpers.fakeApplication(); running( testServer(3333, fakeApp), new Runnable() { public void run() { String[] params = new String[] {"id", "all", "token", getUserActiveAdminUser().getToken()}; WS.Response response = callApi(VERSION_URL, params, null); // Check if status is okay and content is correct assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getHeader("Content-Type")).isEqualTo("text/xml"); // Get Xml dom and check for products Document dom = response.asXml(); NodeList nodes = dom.getElementsByTagName("version"); assertThat(nodes.getLength()).isEqualTo(1); assertThat(nodes.item(0).getTextContent()).isEqualTo(VERSION); } }); }
/** * Checks if the API returns all customers and if the xml document is well formed. * * @author dal */ @Test public void getAllCustomersXml() { FakeApplication fakeApp = Helpers.fakeApplication(); running( testServer(3333, fakeApp), new Runnable() { public void run() { String[] params = new String[] {"id", "all", "token", getUserActiveAdminUser().getToken()}; WS.Response response = callApi(CUSTOMERL_URL, params, null); // Check if status is okay and content is correct assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getHeader("Content-Type")).isEqualTo("text/xml"); assertThat(response.getHeader("Etag")).isNotEmpty(); // Get Xml dom and check for products Document dom = response.asXml(); NodeList nodes = dom.getElementsByTagName("customer"); int count = User.find.all().size(); assertThat(nodes.getLength()).isEqualTo(count); } }); }
public static FakeApplication makeTestApplication(Map<String, String> config) { return play.test.Helpers.fakeApplication(config, makeTestGlobal()); }
@AfterClass public static void stopApp() { Helpers.stop(app); }
@Before public void before() { app = support.Helpers.makeTestApplication(); Helpers.start(app); }
@After public void stopApp() { Helpers.stop(app); }
@Before public void startApp() { app = Helpers.fakeApplication(Helpers.inMemoryDatabase()); Helpers.start(app); }
@Before public void setUp() { start(fakeApplication(Helpers.inMemoryDatabase())); }
public static FakeApplication makeTestApplicationWithServiceGlobal(Map<String, String> config) { return play.test.Helpers.fakeApplication(config); }