pw.println(executor.getRequest().getMethod()); pw.print("URI: "); pw.println(executor.getRequest().getURI()); final Header[] allHeaders = executor.getRequest().getAllHeaders(); if (allHeaders != null && allHeaders.length > 0) { pw.println("Headers:"); if (executor.getRequest() instanceof HttpEntityEnclosingRequestBase) { final HttpEntityEnclosingRequestBase heb = (HttpEntityEnclosingRequestBase) executor.getRequest(); if (heb.getEntity() != null) { pw.print("Content-Type:"); pw.println(" ==="); pw.print("Content-Type:"); pw.println(executor.getResponse().getEntity().getContentType().getValue()); pw.println("Content:"); pw.println(executor.getContent());
executor.execute( builder.buildGetRequest("/entityhub") .withHeader("Accept", "text/html") .assertStatus(200) .assertContentType("text/html"); RequestExecutor re = executor.execute( builder.buildGetRequest("/entityhub/sites/referenced") .withHeader("Accept", "application/json")); re.assertStatus(200); re.assertContentType("application/json"); re.assertContentRegexp(String.format( "http:\\\\/\\\\/.*\\\\/entityhub\\\\/site\\\\/%s\\\\/", referencedSite)); re = executor.execute( builder.buildGetRequest("/entityhub/site/"+referencedSite) .withHeader("Accept", "application/json")); //check JSON serializer re.assertStatus(200); re.assertContentType("application/json"); re = executor.execute(builder.buildGetRequest("/entityhub/site/"+referencedSite) .withHeader("Accept", "application/rdf+xml"));//check RDF serializer re.assertStatus(200); re.assertContentType("application/rdf+xml");
public void close() { clear(); try { httpClient.close(); } catch (IOException e) { log.error("Error while closing Http Client", e); } } }
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);
@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); }
log.debug("Assert Query Results for test {}",test.getContent()); re.assertStatus(test.getExpectedStatus()); re.assertContentType("application/json"); //currently only application/json is supported if(!test.expectsSuccess()){ return null; //no further checks for tests that expect failure JSONObject jso = new JSONObject(re.getContent()); if(log.isDebugEnabled()){ log.debug("Assert Results: {}",jso.toString(2));
/** * For each supplied regexp, fail unless content contains at * least one line that matches. * Regexps are automatically prefixed/suffixed with .* so as * to have match partial lines. */ public RequestExecutor assertContentRegexp(String... regexp) { return assertContentRegexp(true,regexp); } /**
/** * 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; } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@After public void closeExecutor(){ executor.close(); }
httpClient = HttpClients.createDefault(); executor = new RequestExecutor(httpClient);
@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', "+ "}]"+ "}", false); //success but no result //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); } @Test
@After public void closeExecutor(){ executor.close(); }
httpClient = HttpClients.createDefault(); executor = new RequestExecutor(httpClient);
@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', "+ "}]"+ "}", false); //success but no result //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); } @Test
clear(); request = r.getRequest();
@Test public void testDefaultRangeConstraintDatatypeProperty() throws IOException, JSONException { FieldQueryTestCase test = new FieldQueryTestCase( "{"+ "'selected': [ 'http:\\/\\/www.test.org\\/test#field'], " + "'constraints': [{ " + "'type': 'range', " + "'field': 'http:\\/\\/www.test.org\\/test#field', " + "'lowerBound': 1000," + "'inclusive': true," + "}]" + "}", false); //expect BadRequest //now execute the test RequestExecutor re = executeQuery(test); JSONObject jQuery = assertResponseQuery(re.getContent()); JSONArray jConstraints = jQuery.optJSONArray("constraints"); assertNotNull("Result Query does not contain the constraints Array",jConstraints); assertTrue("Result Query Constraint Array does not contain the expected Constraint", jConstraints.length() == 1); JSONObject jConstraint = jConstraints.optJSONObject(0); assertNotNull("Constraint Array does not contain JSONObjects",jConstraint); assertTrue("Returned Query does not contain the default data type",jConstraint.has("datatype")); } }
@Test public void testValueConstraintDefaultDataType() throws IOException, JSONException { FieldQueryTestCase test = new FieldQueryTestCase( "{"+ "'selected': [ 'http:\\/\\/www.test.org\\/test#field'], " + "'constraints': [{ " + "'type': 'value'," + "'value': 'Paris'," + "'field': 'http:\\/\\/www.test.org\\/test#field'" + "}]," + "}", false); //expect BadRequest //now execute the test RequestExecutor re = executeQuery(test); JSONObject jQuery = assertResponseQuery(re.getContent()); JSONArray jConstraints = jQuery.optJSONArray("constraints"); assertNotNull("Result Query does not contain the constraints Array",jConstraints); assertTrue("Result Query Constraint Array does not contain the expected Constraint", jConstraints.length() == 1); JSONObject jConstraint = jConstraints.optJSONObject(0); assertNotNull("Constraint Array does not contain JSONObjects",jConstraint); assertTrue("Returned Query does not contain the default data type",jConstraint.has("datatype")); } @Test
@Test public void testDefaultTextConstraintPatternTypeProperty() throws IOException, JSONException { FieldQueryTestCase test = new FieldQueryTestCase( "{"+ "'selected': [ 'http:\\/\\/www.test.org\\/test#field'], " + "'constraints': [{ " + "'type': 'text', " + "'text': 'Paris', " + "'field': 'http:\\/\\/www.test.org\\/test#field', " + "}]" + "}", false); //expect BadRequest //now execute the test RequestExecutor re = executeQuery(test); JSONObject jQuery = assertResponseQuery(re.getContent()); JSONArray jConstraints = jQuery.optJSONArray("constraints"); assertNotNull("Result Query does not contain the constraints Array",jConstraints); assertTrue("Result Query Constraint Array does not contain the expected Constraint", jConstraints.length() == 1); JSONObject jConstraint = jConstraints.optJSONObject(0); assertNotNull("Constraint Array does not contain JSONObjects",jConstraint); assertTrue("The 'patternType' property MUST BE set for returned TextConstraints", jConstraint.has("patternType")); assertEquals("Default for patternType MUST BE 'none'", "none", jConstraint.getString("patternType")); } @Test
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);