Search in sources :

Example 1 with Request

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

the class ReasonersTestBase method pingSingleJob.

/** 
     * Check for the status of a job, pinging it each 0.5s until it is ready.
     * It does **not** invoke the result.
     * Asks for the application/json representation of the job.
     * 
     * We assert that:
     * - The job must exists (response code 200)
     * - The Content-type header returns JSON
     * - The content contains valid JSON
     * 
     * @param location
     * @throws Exception
     */
protected void pingSingleJob(String location) throws Exception {
    log.info("Start pinging {} ... ", location);
    boolean waiting = true;
    while (waiting) {
        Request req = builder.buildOtherRequest(new HttpGet(location));
        req.withHeader("Accept", "application/json");
        log.info("Ping method: {}", req.getRequest().getMethod());
        log.info("Ping location: {}", req.getRequest().getURI());
        req.getRequest().setHeader("Accept", "application/json");
        log.info("headers:");
        for (Header h : req.getRequest().getAllHeaders()) {
            log.info("{}: {}", h.getName(), h.getValue());
        }
        log.info("Request line:\n\n {} \n\n", req.getRequest().getRequestLine().toString());
        try {
            String content = executor.execute(req).assertStatus(200).assertContentType("application/json").getContent();
            log.info("JSON content:\n\n {} \n\n", content);
            JSONObject json = new JSONObject(content);
            String status = json.getString("status");
            if (status.equals(JobInfo.RUNNING)) {
                log.info(" ... still working (wait for 0.5s)");
                Thread.sleep(500);
            } else {
                waiting = false;
                log.info(" ... done!");
            }
        } catch (Exception e) {
            log.error("An error occurred", e);
            assertTrue(false);
        }
    }
}
Also used : Header(org.apache.http.Header) JSONObject(org.codehaus.jettison.json.JSONObject) HttpGet(org.apache.http.client.methods.HttpGet) Request(org.apache.stanbol.commons.testing.http.Request) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest)

Example 2 with Request

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

the class EntityhubTest method testEntityDelete.

private void testEntityDelete() throws IOException {
    String stanbolProjectUri = "http://stanbol.apache.org";
    Request request = builder.buildOtherRequest(new HttpDelete(builder.buildUrl("/entityhub/entity", "id", stanbolProjectUri)));
    RequestExecutor re = executor.execute(request);
    re.assertStatus(200);
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) Request(org.apache.stanbol.commons.testing.http.Request) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) RequestExecutor(org.apache.stanbol.commons.testing.http.RequestExecutor)

Example 3 with Request

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

the class EntityhubTest method buildImportRdfData.

/**
     * Imports/updates RDF data of the file to the entityhub with the possibility
     * to restrict imports/updates to the parsed uri
     * @param file the file with the RDF data (needs to be in the classpath)
     * @param create if <code>true</code> the data are created (POST) otherwise
     * updated (PUT). 
     * @param uri if not <code>null</code> only data of this URI are imported by
     * specifying the id parameter
     */
protected Request buildImportRdfData(InputStream in, String contentType, boolean create, String uri) {
    Assert.assertNotNull(in);
    Assert.assertNotNull(contentType);
    Request request;
    String path;
    if (uri != null) {
        path = builder.buildUrl("/entityhub/entity", "id", uri);
    } else {
        path = builder.buildUrl("/entityhub/entity");
    }
    if (create) {
        request = builder.buildOtherRequest(new HttpPost(path));
    } else {
        request = builder.buildOtherRequest(new HttpPut(path));
    }
    //set the HttpEntity (both PUT and POST are HttpEntityEnclosingRequests)
    ((HttpEntityEnclosingRequest) request.getRequest()).setEntity(new InputStreamEntity(in, -1));
    //finally set the correct content-type of the provided data
    //currently fixed to "application/rdf+xml"
    request.getRequest().setHeader("Content-Type", contentType);
    return request;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) Request(org.apache.stanbol.commons.testing.http.Request) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpPut(org.apache.http.client.methods.HttpPut) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 4 with Request

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

the class MultiThreadedTestBase method performTest.

protected void performTest(TestSettings settings) throws Exception {
    Iterator<String> testDataIterator = initTestData(settings);
    if (settings.getChain() != null) {
        setEndpoint(getChainEndpoint(settings.getChain()), ENABLE_EXECUTION_METADATA);
    }
    log.info("Start Multi Thread testing of max. {} requests using {} threads " + "on Endpoint {}", new Object[] { settings.getMaxRequests(), settings.getNumThreads(), getEndpoint() });
    ExcutionTracker tracker = new ExcutionTracker(Executors.newFixedThreadPool(settings.getNumThreads()), Math.max(100, settings.getNumThreads() * 5));
    String rdfFormat = System.getProperty(PROPERTY_RDF_FORMAT, DEFAULT_RDF_FORMAT);
    int testNum;
    for (testNum = 0; testDataIterator.hasNext() && testNum < settings.getMaxRequests(); testNum++) {
        String test = testDataIterator.next();
        if (StringUtils.isNotBlank(test)) {
            Request request = builder.buildPostRequest(getEndpoint()).withHeader("Accept", rdfFormat).withContent(test);
            tracker.register(request, test);
            if (testNum % 100 == 0) {
                log.info("  ... sent {} Requests ({} finished, {} pending, {} failed", new Object[] { testNum, tracker.getNumCompleted(), tracker.getNumPending(), tracker.getFailed().size() });
            }
        } else {
            log.warn(" - TestDataIterator returned empty or NULL content (igonred)");
            testNum--;
        }
    }
    log.info("> All {} requests sent!", testNum);
    log.info("  ... wait for all requests to complete");
    while (tracker.getNumPending() > 0) {
        tracker.wait(3);
        log.info("  ... {} finished, {} pending, {} failed", new Object[] { tracker.getNumCompleted(), tracker.getNumPending(), tracker.getFailed().size() });
    }
    log.info("Multi Thread testing of {} requests (failed: {}) using {} threads completed", new Object[] { tracker.getNumCompleted(), tracker.getFailed().size(), settings.getNumThreads() });
    tracker.printStatistics();
    log.warn("Content(s) of Faild tests:");
    int i = 1;
    for (Entry<RequestExecutor, String> failed : tracker.getFailed().entrySet()) {
        log.warn("Failed ({}):", i);
        log.warn("  > Request: {}" + failed.getKey().getRequest());
        log.warn("  > Response: {}" + failed.getKey().getResponse());
        if (failed.getKey().getResponse() != null) {
            log.warn("    - Status: {}", failed.getKey().getResponse().getStatusLine());
        }
        log.warn("  > Content: {}", failed.getValue());
        i++;
    }
    Assert.assertTrue(tracker.getFailed().size() + "/" + settings.getMaxRequests() + " failed", tracker.getFailed().isEmpty());
    tracker = null;
}
Also used : Request(org.apache.stanbol.commons.testing.http.Request) RequestExecutor(org.apache.stanbol.commons.testing.http.RequestExecutor)

Example 5 with Request

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

the class QueryTestBase method executeQuery.

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     *  Utility Methods
     * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     */
/**
     * Executes a {@link QueryTestCase} by sending the 
     * {@link QueryTestCase#getContent() query} as an POST request to the 
     * <code>{@link #endpointPath}/{@link QueryTestCase#getServicePath()}</Code>.
     * @param path the path to perform the field query. "/query" is added to the
     * parsed value
     * @param test the field query test
     * @return the result executor used for the test
     * @throws IOException on any exception while connecting to the entityhub
     * @throws JSONException if the returned results are not valid JSON
     */
protected RequestExecutor executeQuery(QueryTestCase test) throws IOException, JSONException {
    Request request = builder.buildPostRequest(endpointPath + test.getServicePath());
    for (Entry<String, String> header : test.getHeaders().entrySet()) {
        request.withHeader(header.getKey(), header.getValue());
    }
    request.withContent(test.getContent());
    RequestExecutor re = executor.execute(request);
    assertQueryResults(re, test);
    return re;
}
Also used : Request(org.apache.stanbol.commons.testing.http.Request) RequestExecutor(org.apache.stanbol.commons.testing.http.RequestExecutor)

Aggregations

Request (org.apache.stanbol.commons.testing.http.Request)8 RequestExecutor (org.apache.stanbol.commons.testing.http.RequestExecutor)4 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)3 HttpDelete (org.apache.http.client.methods.HttpDelete)2 ReasonersOfflineTest (org.apache.stanbol.reasoners.it.offline.ReasonersOfflineTest)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Header (org.apache.http.Header)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpPut (org.apache.http.client.methods.HttpPut)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1 JSONObject (org.codehaus.jettison.json.JSONObject)1