private HttpResponse getReports(String startDate) throws IOException, InterruptedException { HttpPost post = new HttpPost( String.format( "http://localhost:%d/ebodac/generateReports", TestContext.getJettyPort())); StringEntity dateEntity = new StringEntity(startDate); post.setEntity(dateEntity); post.setHeader(HttpHeaders.CONTENT_TYPE, "text/plain; charset=ISO-8859-1"); HttpResponse response = getHttpClient().execute(post); assertNotNull(response); return response; }
private String getVisitsByLookup(String fields, String lookupType, int page, int rows) throws IOException, InterruptedException { HttpPost post; post = new HttpPost( String.format( "http://localhost:%d/ebodac/getReport/dailyClinicVisitScheduleReport", TestContext.getJettyPort())); ArrayList<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("fields", fields)); nvps.add(new BasicNameValuePair("filter", "")); nvps.add(new BasicNameValuePair("lookup", lookupType)); nvps.add(new BasicNameValuePair("page", Integer.toString(page))); nvps.add(new BasicNameValuePair("rows", Integer.toString(rows))); nvps.add(new BasicNameValuePair("sortColumn", "")); nvps.add(new BasicNameValuePair("sortDirection", "asc")); post.setEntity(new UrlEncodedFormEntity(nvps, "UTF8")); post.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); String response = getHttpClient().execute(post, new BasicResponseHandler()); assertNotNull(response); return response; }
public class BaseTomcatIT { private final Logger logger = LoggerFactory.getLogger(getClass()); protected static final Long ONE_MINUTE = 60 * 1000L; protected static final String HOST = "localhost"; protected static final String MOTECH = "motech"; protected static final int PORT = TestContext.getTomcatPort(); protected static final PollingHttpClient HTTP_CLIENT; static { HTTP_CLIENT = new PollingHttpClient(); HTTP_CLIENT.setCookieStore(new BasicCookieStore()); } public void prepareTomcat() throws IOException, InterruptedException { waitForTomcat(); createAdminUser(); login(); } public void waitForBundles(JSONArray bundles) throws IOException, InterruptedException { assertNotNull("The bundle list cannot be empty", bundles); int retryCount = 10; boolean starting; do { starting = areBundlesStillStarting(bundles); if (!starting) { logger.info("All bundles are started"); break; } logger.info("Wait {} milliseconds before next retry", ONE_MINUTE); Thread.sleep(ONE_MINUTE); } while (--retryCount > 0); assertFalse("Failed to start bundles (TIMEOUT)", starting); } protected void login() throws IOException, InterruptedException { String uri = String.format( "http://%s:%d/motech-platform-server/module/server/motech-platform-server/j_spring_security_check", HOST, PORT); final HttpPost loginPost = new HttpPost(uri); List<NameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("j_username", MOTECH)); nvps.add(new BasicNameValuePair("j_password", MOTECH)); loginPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF8")); logger.info("Trying to login into MOTECH as {}", MOTECH); HttpResponse response = HTTP_CLIENT.execute(loginPost); logger.info("Response status: {}", response.getStatusLine().getStatusCode()); EntityUtils.consume(response.getEntity()); logger.info("Logged into MOTECH as {}", MOTECH); } protected void logout() throws IOException, InterruptedException { String uri = String.format( "http://%s:%d/motech-platform-server/module/server/j_spring_security_logout", HOST, PORT); final HttpGet logoutGet = new HttpGet(uri); logger.info("Trying to logout from MOTECH"); HttpResponse response = HTTP_CLIENT.execute(logoutGet); logger.info("Response status: {}", response.getStatusLine().getStatusCode()); EntityUtils.consume(response.getEntity()); logger.info("Logged out from MOTECH"); } protected void createAdminUser() throws IOException, InterruptedException { String url = String.format("http://%s:%d/motech-platform-server/module/server/startup", HOST, PORT); String json = "{\"language\":\"en\", \"adminLogin\":\"motech\", \"adminPassword\":\"motech\", \"adminConfirmPassword\": \"motech\", \"adminEmail\":\"[email protected]\", \"loginMode\":\"repository\"}"; StringEntity entity = new StringEntity(json, HTTP.UTF_8); entity.setContentType("application/json"); HttpPost post = new HttpPost(url); post.setEntity(entity); logger.info("Trying to create admin user ({}) in MOTECH", MOTECH); HttpResponse response = HTTP_CLIENT.execute(post); logger.info("Response status: {}", response.getStatusLine().getStatusCode()); EntityUtils.consume(response.getEntity()); logger.info("Created admin user ({}) in MOTECH", MOTECH); } protected void waitForTomcat() throws IOException, InterruptedException { logger.info("Waiting for tomcat"); String uri = String.format("http://%s:%d/motech-platform-server/module/server", HOST, PORT); HttpGet waitGet = new HttpGet(uri); HttpResponse response = HTTP_CLIENT.execute(waitGet); logger.info("Proceeding after getting a response: {}", response); logger.info("Tomcat is running"); } protected void assertBundleStatus(JSONObject object) throws JSONException { String status = object.getString("state"); String symbolicName = object.getString("symbolicName"); logger.info("The bundle {} is in {} status", symbolicName, status); if (symbolicName.startsWith("org.motechproject.motech")) { assertEquals( symbolicName + " not active after server startup. [" + status + "]", "ACTIVE", status); } } protected boolean areBundlesStillStarting(JSONArray bundles) throws JSONException { logger.info("Check if bundles are still starting"); for (int i = 0; i < bundles.length(); ++i) { JSONObject object = bundles.getJSONObject(i); String status = object.getString("state"); String symbolicName = object.getString("symbolicName"); logger.info("The bundle {} is in {} status", symbolicName, status); if ("STARTING".equalsIgnoreCase(status)) { logger.info("There is at least one bundle that still starting"); return true; } } logger.info("There is no bundle that still starting"); return false; } protected JSONArray getBundleStatusFromServer(PollingHttpClient httpClient) throws IOException, JSONException, InterruptedException { logger.info("Trying to get a list of bundles installed in MOTECH"); String uri = String.format("http://%s:%d/motech-platform-server/module/admin/api/bundles", HOST, PORT); String response = httpClient.execute(new HttpGet(uri), new BasicResponseHandler()); logger.info("Collected the list of bundles installed in MOTECH"); assertNotNull(response, "Unable to retrieve bundle status from server"); logger.debug("Server response for bundle status request: \n" + response); return new JSONArray(response); } }