Search in sources :

Example 11 with RequestExecutor

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

the class HttpQueryHeaderGetTest method testOverrideAccept.

@Test
public void testOverrideAccept() throws IOException {
    //first a normal request with application/rdf+xml
    String id = "http://dbpedia.org/resource/Paris";
    RequestExecutor re = executor.execute(builder.buildGetRequest(DBPEDIA_SITE_PATH + "/entity", "id", id, "header_Accept", //parse the rdf+nt format as query parameter
    "text/rdf+nt").withHeader("Accept", //MUST override the rdf+xml
    "application/rdf+xml"));
    re.assertStatus(200);
    re.assertContentType("text/rdf+nt");
    re.assertContentContains("<http://dbpedia.org/resource/Paris> " + "<http://www.w3.org/2000/01/rdf-schema#label> " + "\"Paris\"@en .");
}
Also used : RequestExecutor(org.apache.stanbol.commons.testing.http.RequestExecutor) Test(org.junit.Test)

Example 12 with RequestExecutor

use of org.apache.stanbol.commons.testing.http.RequestExecutor 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 13 with RequestExecutor

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

the class QueryTestBase method testDefaultsParameter.

/**
     * Tests the defaults for the text constraint used for find queries. This
     * includes: <ul>
     *   <li> limit set to an value > 0
     *   <li> offset set to 0
     *   <li> selected set to rdfs:label
     *   <li> constraint.patternType set to wildcard
     *   <li> constraint.field set to rdfs:label
     *   <li> constraint.type set to text
     * </ul>
     * @throws IOException
     * @throws JSONException
     */
@Test
public void testDefaultsParameter() throws IOException, JSONException {
    FindQueryTestCase test = new FindQueryTestCase("non_existant_" + UUID.randomUUID().toString(), false);
    RequestExecutor re = executeQuery(test);
    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);
    assertTrue("Result Query does not contain offset property", jQuery.has("offset"));
    assertTrue("Returned offset is != 0", jQuery.getInt("offset") == 0);
    assertSelectedField(jQuery, getDefaultFindQueryField());
    JSONArray jConstraints = jQuery.optJSONArray("constraints");
    assertNotNull("Result Query is missing the 'constraints' property", jConstraints);
    assertEquals("Result Query is expected to have a single constraint", 1, jConstraints.length());
    JSONObject constraint = jConstraints.optJSONObject(0);
    assertNotNull("'constraints' array does not contain a JSONObject but " + jConstraints.get(0), constraint);
    assertEquals("The 'type' of the Constraint is not 'text' but " + constraint.opt("type"), "text", constraint.optString("type"));
    assertEquals("The 'patternType' of the Constraint is not 'wildcard' but " + constraint.opt("patternType"), "wildcard", constraint.optString("patternType"));
    assertEquals("The 'field' of the Constraint is not " + getDefaultFindQueryField() + " but " + constraint.opt("field"), getDefaultFindQueryField(), constraint.optString("field"));
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) RequestExecutor(org.apache.stanbol.commons.testing.http.RequestExecutor) JSONArray(org.codehaus.jettison.json.JSONArray) Test(org.junit.Test)

Example 14 with RequestExecutor

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

the class QueryTestBase method testCustomFieldParameter.

@Test
public void testCustomFieldParameter() throws IOException, JSONException {
    FindQueryTestCase test = new FindQueryTestCase("non_existant_" + UUID.randomUUID().toString(), false);
    String testField = "http://www.test.org/test#test_" + UUID.randomUUID();
    test.setField(testField);
    RequestExecutor re = executeQuery(test);
    JSONObject jQuery = assertResponseQuery(re.getContent());
    assertSelectedField(jQuery, testField);
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) RequestExecutor(org.apache.stanbol.commons.testing.http.RequestExecutor) Test(org.junit.Test)

Example 15 with RequestExecutor

use of org.apache.stanbol.commons.testing.http.RequestExecutor 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

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