Search in sources :

Example 1 with RequestExecutor

use of org.apache.stanbol.commons.testing.http.RequestExecutor in project stanbol by apache.

the class StanbolTestBase method waitForServerReady.

@Before
public void waitForServerReady() throws Exception {
    log.debug("> before {}#waitForServerReady()", getClass().getSimpleName());
    // initialize instance request builder and HTTP client
    builder = new RequestBuilder(serverBaseUrl);
    //TODO:user name and pwd
    String credentials = getCredentials();
    if (credentials != null && !credentials.isEmpty()) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(HttpHost.create(serverBaseUrl)), new UsernamePasswordCredentials(credentials));
        httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    } else {
        httpClient = HttpClients.createDefault();
    }
    executor = new RequestExecutor(httpClient);
    if (serverReady) {
        log.debug(" ... server already marked as ready!");
        return;
    }
    // Timeout for readiness test
    final String sec = System.getProperty(SERVER_READY_TIMEOUT_PROP);
    final int timeoutSec = sec == null ? 60 : Integer.valueOf(sec);
    log.info("Will wait up to " + timeoutSec + " seconds for server to become ready");
    final long endTime = System.currentTimeMillis() + timeoutSec * 1000L;
    // Get the list of paths to test and expected content regexps
    final List<String> testPaths = new ArrayList<String>();
    final TreeSet<Object> propertyNames = new TreeSet<Object>();
    propertyNames.addAll(System.getProperties().keySet());
    for (Object o : propertyNames) {
        final String key = (String) o;
        if (key.startsWith(SERVER_READY_PROP_PREFIX)) {
            testPaths.add(System.getProperty(key));
        }
    }
    // Consider the server ready if it responds to a GET on each of 
    // our configured request paths with a 200 result and content
    // that matches the regexp supplied with the path
    long sleepTime = 100;
    readyLoop: while (!serverReady && System.currentTimeMillis() < endTime) {
        // Wait a bit between checks, to let the server come up
        Thread.sleep(sleepTime);
        sleepTime = Math.min(5000L, sleepTime * 2);
        // A test path is in the form path:substring or just path, in which case
        // we don't check that the content contains the substring 
        log.debug(" - check serverReady Paths");
        for (String p : testPaths) {
            final String[] s = p.split(":");
            final String path = s[0];
            final String substring = (s.length > 0 ? s[1] : null);
            final String url = serverBaseUrl + path;
            log.debug("    > url: {}", url);
            log.debug("    > content: {}", substring);
            final HttpGet get = new HttpGet(url);
            //authenticate as admin with password admin
            get.setHeader("Authorization", "Basic YWRtaW46YWRtaW4=");
            for (int i = 2; i + 1 < s.length; i = i + 2) {
                log.debug("    > header: {}:{}", s[i], s[i + 1]);
                if (s[i] != null && !s[i].isEmpty() && s[i + 1] != null && !s[i + 1].isEmpty()) {
                    get.setHeader(s[i], s[i + 1]);
                }
            }
            CloseableHttpResponse response = null;
            HttpEntity entity = null;
            try {
                log.debug("    > execute: {}", get);
                response = httpClient.execute(get);
                log.debug("    > response: {}", response);
                entity = response.getEntity();
                final int status = response.getStatusLine().getStatusCode();
                if (status != 200) {
                    log.info("Got {} at {} - will retry", status, url);
                    continue readyLoop;
                } else {
                    log.debug("Got {} at {} - will retry", status, url);
                }
                if (substring != null) {
                    if (entity == null) {
                        log.info("No entity returned for {} - will retry", url);
                        continue readyLoop;
                    }
                    final String content = EntityUtils.toString(entity);
                    if (!content.contains(substring)) {
                        log.info("Returned content for {}  does not contain " + "{} - will retry", url, substring);
                        continue readyLoop;
                    }
                }
            } catch (HttpHostConnectException e) {
                log.info("Got HttpHostConnectException at " + url + " - will retry");
                continue readyLoop;
            } finally {
                EntityUtils.consumeQuietly(entity);
                if (response != null) {
                    response.close();
                }
            }
        }
        serverReady = true;
        log.info("Got expected content for all configured requests, server is ready");
    }
    if (!serverReady) {
        throw new Exception("Server not ready after " + timeoutSec + " seconds");
    }
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) RequestBuilder(org.apache.stanbol.commons.testing.http.RequestBuilder) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) RequestExecutor(org.apache.stanbol.commons.testing.http.RequestExecutor) ArrayList(java.util.ArrayList) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) MalformedURLException(java.net.MalformedURLException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) TreeSet(java.util.TreeSet) AuthScope(org.apache.http.auth.AuthScope) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) Before(org.junit.Before)

Example 2 with RequestExecutor

use of org.apache.stanbol.commons.testing.http.RequestExecutor in project stanbol by apache.

the class EntityhubTestBase method checkEntityhubReady.

@Before
public void checkEntityhubReady() throws Exception {
    // Check only once per test run
    if (entityhubReady) {
        return;
    }
    // If we timed out previously, don't waste time checking again
    if (timedOut) {
        fail("Timeout in previous check of the entityhub, cannot run tests");
    }
    // We'll retry the check for all engines to be ready
    // for up to ENGINES_TIMEOUT_SECONDS 
    final RetryLoop.Condition c = new RetryLoop.Condition() {

        @Override
        public boolean isTrue() throws Exception {
            /*  Check the entityhub and the referenced site dbpedia
                 */
            executor.execute(builder.buildGetRequest("/entityhub").withHeader("Accept", "text/html")).assertStatus(200).assertContentType("text/html");
            /*  List of expected referencedSites could also be made 
                 *  configurable via system properties, but we don't expect it 
                 *  to change often. 
                 */
            RequestExecutor re = executor.execute(builder.buildGetRequest("/entityhub/sites/referenced").withHeader("Accept", "application/json"));
            re.assertStatus(200);
            re.assertContentType("application/json");
            //check if all the required referenced sites are available
            for (String referencedSite : referencedSites) {
                if (referencedSite != null && !referencedSite.isEmpty()) {
                    re.assertContentRegexp(String.format("http:\\\\/\\\\/.*\\\\/entityhub\\\\/site\\\\/%s\\\\/", referencedSite));
                }
            }
            //this ensures that JSON and RDF serializer services are up and running
            for (String referencedSite : referencedSites) {
                re = executor.execute(builder.buildGetRequest("/entityhub/site/" + referencedSite).withHeader("Accept", //check JSON serializer
                "application/json"));
                re.assertStatus(200);
                re.assertContentType("application/json");
                re = executor.execute(builder.buildGetRequest("/entityhub/site/" + referencedSite).withHeader("Accept", //check RDF serializer
                "application/rdf+xml"));
                re.assertStatus(200);
                re.assertContentType("application/rdf+xml");
            }
            log.info("Entityhub services checked, all present");
            return true;
        }

        @Override
        public String getDescription() {
            return "Checking that teh Entityhub and the dbpedia ReferencedSite are ready";
        }
    };
    new RetryLoop(c, ENTITYHUB_TIMEOUT_SECONDS, WAIT_BETWEEN_TRIES_MSEC) {

        @Override
        protected void reportException(Throwable t) {
            log.info("Exception in RetryLoop, will retry for up to " + getRemainingTimeSeconds() + " seconds: " + t);
        }

        protected void onTimeout() {
            timedOut = true;
        }
    };
    entityhubReady = true;
}
Also used : RetryLoop(org.apache.stanbol.commons.testing.http.RetryLoop) RequestExecutor(org.apache.stanbol.commons.testing.http.RequestExecutor) Before(org.junit.Before)

Example 3 with RequestExecutor

use of org.apache.stanbol.commons.testing.http.RequestExecutor in project stanbol by apache.

the class QueryTestBase method testLimitNegativeNumber.

@Test
public void testLimitNegativeNumber() throws IOException, JSONException {
    FieldQueryTestCase test = new FieldQueryTestCase("{" + "'selected': [ 'http:\\/\\/www.test.org\\/test#field'], " + "'offset': '0', " + "'limit': '-1', " + "'constraints': [{ " + "'type': 'reference', " + "'field': 'http:\\/\\/www.test.org\\/test#non-existing-field', " + "'value': 'http:\\/\\/www.test.org\\/test#NonExistingValue', " + "}]" + "}", //success but no result
    false);
    //now execute the test
    RequestExecutor re = executeQuery(test);
    //test the of the limit was set correctly set to the default (> 0)
    JSONObject jQuery = assertResponseQuery(re.getContent());
    assertTrue("Result Query does not contain Limit property", jQuery.has("limit"));
    assertTrue("Returned limit is <= 0", jQuery.getInt("limit") > 0);
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) RequestExecutor(org.apache.stanbol.commons.testing.http.RequestExecutor) Test(org.junit.Test)

Example 4 with RequestExecutor

use of org.apache.stanbol.commons.testing.http.RequestExecutor in project stanbol by apache.

the class QueryTestBase method testOffsetNegativeNumber.

@Test
public void testOffsetNegativeNumber() throws IOException, JSONException {
    FieldQueryTestCase test = new FieldQueryTestCase("{" + "'selected': [ 'http:\\/\\/www.test.org\\/test#field'], " + "'offset': '-3', " + "'limit': '3', " + "'constraints': [{ " + "'type': 'reference', " + "'field': 'http:\\/\\/www.test.org\\/test#non-existing-field', " + "'value': 'http:\\/\\/www.test.org\\/test#NonExistingValue', " + "}]" + "}", //success but no result
    false);
    //now execute the test
    RequestExecutor re = executeQuery(test);
    JSONObject jQuery = assertResponseQuery(re.getContent());
    assertTrue("Result Query does not contain offset property", jQuery.has("offset"));
    assertTrue("Returned offset is != 0", jQuery.getInt("offset") == 0);
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) RequestExecutor(org.apache.stanbol.commons.testing.http.RequestExecutor) Test(org.junit.Test)

Example 5 with RequestExecutor

use of org.apache.stanbol.commons.testing.http.RequestExecutor in project stanbol by apache.

the class SessionTest method testCRUD.

@Test
public void testCRUD() throws Exception {
    RequestExecutor request;
    // The needed Web resources to GET from.
    executor.execute(builder.buildGetRequest(SESSION_URI).withHeader("Accept", KRFormat.TURTLE)).assertStatus(200);
    log.info("Request: " + SESSION_URI + " ... DONE");
    String tempScopeUri = SESSION_URI + "/" + getClass().getCanonicalName() + "-" + System.currentTimeMillis();
    // Scope should not be there
    request = executor.execute(builder.buildGetRequest(tempScopeUri).withHeader("Accept", KRFormat.TURTLE));
    request.assertStatus(404);
    log.info("Request: " + tempScopeUri + " (should return 404) ... DONE");
    // Create scope
    executor.execute(builder.buildOtherRequest(new HttpPut(builder.buildUrl(tempScopeUri))));
    log.info("PUT Request: " + tempScopeUri + " ... DONE");
    // Scope should be there now
    request = executor.execute(builder.buildGetRequest(tempScopeUri).withHeader("Accept", KRFormat.TURTLE));
    request.assertStatus(200).assertContentContains(tempScopeUri);
    log.info("Request: " + tempScopeUri + " ... DONE");
    // TODO the U of CRUD
    // Delete scope
    executor.execute(builder.buildOtherRequest(new HttpDelete(builder.buildUrl(tempScopeUri))));
    log.info("DELETE Request: " + tempScopeUri + " ... DONE");
    // Scope should not be there
    request = executor.execute(builder.buildGetRequest(tempScopeUri).withHeader("Accept", KRFormat.TURTLE));
    request.assertStatus(404);
    log.info("Request: " + tempScopeUri + " (should return 404) ... DONE");
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) RequestExecutor(org.apache.stanbol.commons.testing.http.RequestExecutor) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Aggregations

RequestExecutor (org.apache.stanbol.commons.testing.http.RequestExecutor)31 Test (org.junit.Test)18 JSONObject (org.codehaus.jettison.json.JSONObject)12 HttpDelete (org.apache.http.client.methods.HttpDelete)7 ArrayList (java.util.ArrayList)4 Request (org.apache.stanbol.commons.testing.http.Request)4 JSONArray (org.codehaus.jettison.json.JSONArray)4 HttpPut (org.apache.http.client.methods.HttpPut)3 InputStream (java.io.InputStream)2 List (java.util.List)2 Set (java.util.Set)2 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)2 DbpediaQueryTest (org.apache.stanbol.entityhub.it.query.DbpediaQueryTest)2 Before (org.junit.Before)2 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 TreeSet (java.util.TreeSet)1 ZipEntry (java.util.zip.ZipEntry)1