001/* 002 * The contents of this file are subject to the license and copyright 003 * detailed in the LICENSE and NOTICE files at the root of the source 004 * tree. 005 */ 006package org.fcrepo.integration.rdf; 007 008import static javax.ws.rs.core.HttpHeaders.ACCEPT; 009import static org.apache.jena.rdf.model.ModelFactory.createDefaultModel; 010import static org.junit.Assert.assertEquals; 011import static org.junit.Assert.assertFalse; 012import static org.junit.Assert.assertTrue; 013import static org.apache.jena.rdf.model.ResourceFactory.createProperty; 014import static org.fcrepo.kernel.api.RdfLexicon.FEDORA_CONTAINER; 015import static org.fcrepo.kernel.api.RdfLexicon.RDF_NAMESPACE; 016 017import org.apache.http.client.methods.CloseableHttpResponse; 018import org.apache.http.client.methods.HttpGet; 019import org.apache.jena.rdf.model.Model; 020import org.apache.jena.rdf.model.Property; 021import org.apache.jena.rdf.model.Resource; 022import org.fcrepo.integration.http.api.AbstractResourceIT; 023import org.junit.Ignore; 024import org.junit.Test; 025 026/** 027 * @author bbpennel 028 */ 029@Ignore // TODO FIX THESE TESTS 030public class RdfNamespaceMappingIT extends AbstractResourceIT { 031 032 public static final Property RDF_TYPE = createProperty(RDF_NAMESPACE + "type"); 033 034 @Test 035 public void testUnregisteredNamespace() throws Exception { 036 final String pid = getRandomUniqueId(); 037 final String subjectURI = serverAddress + pid; 038 039 // create object with rdf that contains a namespace declaration 040 final String content = "@prefix asdf: <http://asdf.org/> . <> asdf:foo 'bar' ."; 041 try (final CloseableHttpResponse response = execute(putObjMethod(pid, "text/turtle", content))) { 042 assertEquals(201, response.getStatusLine().getStatusCode()); 043 } 044 045 // that namespace should come back without a prefix 046 final HttpGet httpGet = getObjMethod(pid); 047 httpGet.addHeader(ACCEPT, "text/turtle"); 048 final Model model = createDefaultModel(); 049 try (final CloseableHttpResponse getResponse = execute(httpGet)) { 050 model.read(getResponse.getEntity().getContent(), subjectURI, "TURTLE"); 051 052 assertFalse("Should not contain unregistered namespace mapping", 053 model.getNsPrefixMap().containsKey("asdf")); 054 055 final Resource resc = model.getResource(subjectURI); 056 assertTrue("Must return property from unregistered namespace", 057 resc.hasLiteral(createProperty("http://asdf.org/foo"), "bar")); 058 } 059 } 060 061 @Test 062 public void testRegisteredNamespace() throws Exception { 063 final String pid = getRandomUniqueId(); 064 final String subjectURI = serverAddress + pid; 065 createObject(pid); 066 067 final HttpGet httpGet = getObjMethod(pid); 068 httpGet.addHeader(ACCEPT, "text/turtle"); 069 final Model model = createDefaultModel(); 070 try (final CloseableHttpResponse response = execute(httpGet)) { 071 model.read(response.getEntity().getContent(), subjectURI, "TURTLE"); 072 073 assertTrue("Should contain fedora namespace prefix", 074 model.getNsPrefixMap().containsKey("fedora")); 075 assertTrue("Should contain rdf namespace prefix", 076 model.getNsPrefixMap().containsKey("rdf")); 077 078 final Resource resc = model.getResource(subjectURI); 079 assertTrue("Must contain property using register namespaces", 080 resc.hasProperty(RDF_TYPE, FEDORA_CONTAINER)); 081 } 082 } 083 084 @Test 085 public void testUnusedNamespaceNotReturned() throws Exception { 086 verifyUnusedNamespaceNotReturned("text/turtle", "TURTLE"); 087 } 088 089 // Testing a serialization that cannot be block streamed 090 @Test 091 public void testUnusedNamespaceNotReturnedRdfXML() throws Exception { 092 verifyUnusedNamespaceNotReturned("application/rdf+xml", "RDF/XML"); 093 } 094 095 private void verifyUnusedNamespaceNotReturned(final String acceptType, final String rdfLang) throws Exception { 096 final String pid = getRandomUniqueId(); 097 final String subjectURI = serverAddress + pid; 098 createObject(pid); 099 100 final HttpGet httpGet = getObjMethod(pid); 101 httpGet.addHeader(ACCEPT, acceptType); 102 final Model model = createDefaultModel(); 103 try (final CloseableHttpResponse response = execute(httpGet)) { 104 model.read(response.getEntity().getContent(), subjectURI, rdfLang); 105 106 assertTrue("Should contain fedora namespace prefix", 107 model.getNsPrefixMap().containsKey("fedora")); 108 109 assertFalse("Must not contain prefix for registered but unused namespace", 110 model.getNsPrefixMap().containsKey("unused")); 111 } 112 } 113 114 @Test 115 public void testUnprefixedSerialization() throws Exception { 116 final String pid = getRandomUniqueId(); 117 final String subjectURI = serverAddress + pid; 118 createObject(pid); 119 120 final HttpGet httpGet = getObjMethod(pid); 121 httpGet.addHeader(ACCEPT, "application/n-triples"); 122 final Model model = createDefaultModel(); 123 try (final CloseableHttpResponse response = execute(httpGet)) { 124 model.read(response.getEntity().getContent(), subjectURI, "NTRIPLES"); 125 126 assertTrue("No namespaces should be returned", 127 model.getNsPrefixMap().isEmpty()); 128 129 final Resource resc = model.getResource(subjectURI); 130 assertTrue("Must contain property using register namespaces", 131 resc.hasProperty(RDF_TYPE, FEDORA_CONTAINER)); 132 } 133 } 134}