Skip to content

Commit

Permalink
Enabled HTML validation test using nu.validator.htmlparser
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Jul 4, 2013
1 parent 70c57e4 commit 7f34218
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 30 deletions.
14 changes: 10 additions & 4 deletions fcrepo-http-api/pom.xml
Expand Up @@ -124,10 +124,16 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<!-- This dependency is for compile-time: it keeps this module independent of
any given choice of JAX-RS implementation. It must be _after_ the test gear. Otherwise
it will get loaded during test phase, but because this is just an API, the tests
will not be able to execute. -->
<dependency>
<groupId>nu.validator.htmlparser</groupId>
<artifactId>htmlparser</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
<!-- This dependency is for compile-time: it keeps this module independent
of any given choice of JAX-RS implementation. It must be _after_ the test
gear. Otherwise it will get loaded during test phase, but because this is
just an API, the tests will not be able to execute. -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
Expand Down
Expand Up @@ -20,16 +20,20 @@
import static java.util.regex.Pattern.compile;
import static javax.ws.rs.core.Response.Status.NOT_MODIFIED;
import static javax.ws.rs.core.Response.Status.OK;
import static javax.ws.rs.core.Response.Status.CREATED;
import static nu.validator.htmlparser.common.XmlViolationPolicy.ALLOW;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.jcr.RepositoryException;
import javax.ws.rs.core.Response;
import nu.validator.htmlparser.sax.HtmlParser;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
Expand All @@ -42,6 +46,10 @@
import org.fcrepo.test.util.TestHelpers;
import org.fcrepo.utils.FedoraJcrTypes;
import org.junit.Test;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.Node;
Expand All @@ -55,7 +63,8 @@ public class FedoraNodesIT extends AbstractResourceIT {
public void testIngest() throws Exception {
final HttpPost method = postObjMethod("FedoraObjectsTest1");
final HttpResponse response = client.execute(method);
assertEquals(201, response.getStatusLine().getStatusCode());
assertEquals(CREATED.getStatusCode(), response.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(response.getEntity());
assertTrue("Response wasn't a PID", compile("[a-z]+").matcher(content)
.find());
Expand All @@ -70,22 +79,21 @@ public void testIngestWithNew() throws Exception {
final HttpResponse response = client.execute(method);
final String content = EntityUtils.toString(response.getEntity());
final int status = response.getStatusLine().getStatusCode();
if (201 != status) {
logger.error(content);
}
assertEquals(201, status);
assertEquals("Didn't get a CREATED response! Got content:\n" + content,
CREATED.getStatusCode(), status);
assertTrue("Response wasn't a PID", compile("[a-z]+").matcher(content)
.find());
assertTrue("new object did not mint a PID", !content
.endsWith("/fcr:new"));

assertEquals("Object wasn't created!", 200, getStatus(new HttpGet(
serverAddress + content)));
assertEquals("Object wasn't created!", OK.getStatusCode(),
getStatus(new HttpGet(serverAddress + content)));
}

@Test
public void testDeleteObject() throws Exception {
assertEquals(201, getStatus(postObjMethod("FedoraObjectsTest3")));
assertEquals(CREATED.getStatusCode(),
getStatus(postObjMethod("FedoraObjectsTest3")));
assertEquals(204, getStatus(new HttpDelete(serverAddress +
"objects/FedoraObjectsTest3")));
assertEquals("Object wasn't really deleted!", 404,
Expand All @@ -99,8 +107,8 @@ public void testGetDatastream() throws Exception {

assertEquals(404, getStatus(new HttpGet(serverAddress +
"objects/FedoraDatastreamsTest4/ds1")));
assertEquals(201, getStatus(postDSMethod("FedoraDatastreamsTest4",
"ds1", "foo")));
assertEquals(CREATED.getStatusCode(), getStatus(postDSMethod(
"FedoraDatastreamsTest4", "ds1", "foo")));
final HttpResponse response =
execute(new HttpGet(serverAddress +
"objects/FedoraDatastreamsTest4/ds1"));
Expand All @@ -114,12 +122,12 @@ public void testDeleteDatastream() throws Exception {

final HttpPost method =
postDSMethod("FedoraDatastreamsTest5", "ds1", "foo");
assertEquals(201, getStatus(method));
assertEquals(CREATED.getStatusCode(), getStatus(method));

final HttpGet method_2 =
new HttpGet(serverAddress +
"objects/FedoraDatastreamsTest5/ds1");
assertEquals(200, getStatus(method_2));
assertEquals(OK.getStatusCode(), getStatus(method_2));

final HttpDelete dmethod =
new HttpDelete(serverAddress +
Expand All @@ -137,7 +145,8 @@ public void testGetRepositoryGraph() throws Exception {
final HttpGet getObjMethod = new HttpGet(serverAddress + "");
getObjMethod.addHeader("Accept", "application/n-triples");
final HttpResponse response = client.execute(getObjMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final GraphStore graphStore =
TestHelpers.parseTriples(response.getEntity().getContent());
logger.debug("Retrieved repository graph:\n" + graphStore.toString());
Expand All @@ -155,7 +164,8 @@ public void testGetObjectGraphHtml() throws Exception {
new HttpGet(serverAddress + "objects/FedoraDescribeTestGraph");
getObjMethod.addHeader("Accept", "text/html");
final HttpResponse response = client.execute(getObjMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(response.getEntity());
logger.info(content);
}
Expand All @@ -167,7 +177,8 @@ public void testGetObjectGraph() throws Exception {
new HttpGet(serverAddress + "objects/FedoraDescribeTestGraph");
getObjMethod.addHeader("Accept", "application/n-triples");
final HttpResponse response = client.execute(getObjMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(response.getEntity());

logger.debug("Retrieved object graph:\n" + content);
Expand Down Expand Up @@ -199,7 +210,8 @@ public void testGetObjectGraphByUUID() throws Exception {
"objects/FedoraDescribeTestGraphByUuid");
getObjMethod.addHeader("Accept", "application/n3");
final HttpResponse response = client.execute(getObjMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final GraphStore graphStore =
TestHelpers.parseTriples(response.getEntity().getContent());
final Iterator<Quad> iterator =
Expand Down Expand Up @@ -281,7 +293,8 @@ public void testUpdateAndReplaceObjectGraph() throws Exception {

getObjMethod.addHeader("Accept", "application/n-triples");
final HttpResponse getResponse = client.execute(getObjMethod);
assertEquals(200, getResponse.getStatusLine().getStatusCode());
assertEquals(OK.getStatusCode(), getResponse.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(getResponse.getEntity());
logger.debug("Retrieved object graph:\n" + content);

Expand Down Expand Up @@ -316,7 +329,8 @@ public void testDescribeSize() throws Exception {
final HttpGet describeMethod = new HttpGet(serverAddress + "");
describeMethod.addHeader("Accept", "text/n3");
HttpResponse response = client.execute(describeMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
GraphStore graphStore =
TestHelpers.parseTriples(response.getEntity().getContent());
logger.debug("For testDescribeSize() first size retrieved repository graph:\n" +
Expand All @@ -329,11 +343,14 @@ public void testDescribeSize() throws Exception {
final Integer oldSize =
(Integer) iterator.next().getObject().getLiteralValue();

assertEquals(201, getStatus(postObjMethod("sizeNode")));
assertEquals(201, getStatus(postDSMethod("sizeNode", "asdf", "1234")));
assertEquals(CREATED.getStatusCode(),
getStatus(postObjMethod("sizeNode")));
assertEquals(CREATED.getStatusCode(), getStatus(postDSMethod(
"sizeNode", "asdf", "1234")));

response = client.execute(describeMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
graphStore =
TestHelpers.parseTriples(response.getEntity().getContent());
logger.debug("For testDescribeSize() new size retrieved repository graph:\n" +
Expand All @@ -357,7 +374,8 @@ public void testDescribeCount() throws Exception {
final HttpGet describeMethod = new HttpGet(serverAddress + "");
describeMethod.addHeader("Accept", "text/n3");
HttpResponse response = client.execute(describeMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
GraphStore graphStore =
TestHelpers.parseTriples(response.getEntity().getContent());
logger.debug("For testDescribeCount() first count retrieved repository graph:\n" +
Expand All @@ -370,11 +388,14 @@ public void testDescribeCount() throws Exception {
final Integer oldSize =
(Integer) iterator.next().getObject().getLiteralValue();

assertEquals(201, getStatus(postObjMethod("countNode")));
assertEquals(201, getStatus(postDSMethod("countNode", "asdf", "1234")));
assertEquals(CREATED.getStatusCode(),
getStatus(postObjMethod("countNode")));
assertEquals(CREATED.getStatusCode(), getStatus(postDSMethod(
"countNode", "asdf", "1234")));

response = client.execute(describeMethod);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
graphStore =
TestHelpers.parseTriples(response.getEntity().getContent());
logger.debug("For testDescribeCount() first count repository graph:\n" +
Expand Down Expand Up @@ -406,7 +427,8 @@ public void testGetProjectedNode() throws Exception {
method.setHeader("Accept", "text/n3");
final HttpResponse response = execute(method);

assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());

final String subjectURI = serverAddress + "files/FileSystem1";
final Graph result =
Expand Down Expand Up @@ -447,4 +469,44 @@ public void testDescribeRdfCached() throws RepositoryException, IOException {

}

@Test
public void testGetHTMLForObject() throws Exception {
client.execute(postObjMethod("FedoraDescribeTestGraphInHTML"));
final HttpGet getObjMethod =
new HttpGet(serverAddress +
"objects/FedoraDescribeTestGraphInHTML");
getObjMethod.addHeader("Accept", "text/html");
final HttpResponse response = client.execute(getObjMethod);
assertEquals(OK.getStatusCode(), response.getStatusLine()
.getStatusCode());
final String content = EntityUtils.toString(response.getEntity());
logger.debug("Retrieved object HTML view:\n" + content);

final HtmlParser htmlParser = new HtmlParser(ALLOW);
htmlParser.setErrorHandler(new ErrorHandler() {

@Override
public void warning(final SAXParseException e) throws SAXException {
logger.error("Problem with HTML!", e);
fail();
}

@Override
public void error(final SAXParseException e) throws SAXException {
logger.error("Problem with HTML!", e);
fail();
}

@Override
public void fatalError(final SAXParseException e)
throws SAXException {
logger.error("Problem with HTML!", e);
fail();
}
});

htmlParser.parse(new InputSource(content));
logger.info("HTML found to be valid.");
}

}

0 comments on commit 7f34218

Please sign in to comment.