001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.integration.rdf; 019 020import static javax.ws.rs.core.HttpHeaders.ACCEPT; 021import static org.apache.jena.rdf.model.ModelFactory.createDefaultModel; 022import static org.junit.Assert.assertEquals; 023import static org.junit.Assert.assertFalse; 024import static org.junit.Assert.assertTrue; 025import static org.apache.jena.rdf.model.ResourceFactory.createProperty; 026import static org.fcrepo.kernel.api.RdfLexicon.FEDORA_CONTAINER; 027import static org.fcrepo.kernel.api.RdfLexicon.RDF_NAMESPACE; 028 029import org.apache.http.client.methods.CloseableHttpResponse; 030import org.apache.http.client.methods.HttpGet; 031import org.apache.jena.rdf.model.Model; 032import org.apache.jena.rdf.model.Property; 033import org.apache.jena.rdf.model.Resource; 034import org.fcrepo.integration.http.api.AbstractResourceIT; 035import org.junit.Ignore; 036import org.junit.Test; 037 038/** 039 * @author bbpennel 040 */ 041@Ignore // TODO FIX THESE TESTS 042public class RdfNamespaceMappingIT extends AbstractResourceIT { 043 044 public static final Property RDF_TYPE = createProperty(RDF_NAMESPACE + "type"); 045 046 @Test 047 public void testUnregisteredNamespace() throws Exception { 048 final String pid = getRandomUniqueId(); 049 final String subjectURI = serverAddress + pid; 050 051 // create object with rdf that contains a namespace declaration 052 final String content = "@prefix asdf: <http://asdf.org/> . <> asdf:foo 'bar' ."; 053 try (final CloseableHttpResponse response = execute(putObjMethod(pid, "text/turtle", content))) { 054 assertEquals(201, response.getStatusLine().getStatusCode()); 055 } 056 057 // that namespace should come back without a prefix 058 final HttpGet httpGet = getObjMethod(pid); 059 httpGet.addHeader(ACCEPT, "text/turtle"); 060 final Model model = createDefaultModel(); 061 try (final CloseableHttpResponse getResponse = execute(httpGet)) { 062 model.read(getResponse.getEntity().getContent(), subjectURI, "TURTLE"); 063 064 assertFalse("Should not contain unregistered namespace mapping", 065 model.getNsPrefixMap().containsKey("asdf")); 066 067 final Resource resc = model.getResource(subjectURI); 068 assertTrue("Must return property from unregistered namespace", 069 resc.hasLiteral(createProperty("http://asdf.org/foo"), "bar")); 070 } 071 } 072 073 @Test 074 public void testRegisteredNamespace() throws Exception { 075 final String pid = getRandomUniqueId(); 076 final String subjectURI = serverAddress + pid; 077 createObject(pid); 078 079 final HttpGet httpGet = getObjMethod(pid); 080 httpGet.addHeader(ACCEPT, "text/turtle"); 081 final Model model = createDefaultModel(); 082 try (final CloseableHttpResponse response = execute(httpGet)) { 083 model.read(response.getEntity().getContent(), subjectURI, "TURTLE"); 084 085 assertTrue("Should contain fedora namespace prefix", 086 model.getNsPrefixMap().containsKey("fedora")); 087 assertTrue("Should contain rdf namespace prefix", 088 model.getNsPrefixMap().containsKey("rdf")); 089 090 final Resource resc = model.getResource(subjectURI); 091 assertTrue("Must contain property using register namespaces", 092 resc.hasProperty(RDF_TYPE, FEDORA_CONTAINER)); 093 } 094 } 095 096 @Test 097 public void testUnusedNamespaceNotReturned() throws Exception { 098 verifyUnusedNamespaceNotReturned("text/turtle", "TURTLE"); 099 } 100 101 // Testing a serialization that cannot be block streamed 102 @Test 103 public void testUnusedNamespaceNotReturnedRdfXML() throws Exception { 104 verifyUnusedNamespaceNotReturned("application/rdf+xml", "RDF/XML"); 105 } 106 107 private void verifyUnusedNamespaceNotReturned(final String acceptType, final String rdfLang) throws Exception { 108 final String pid = getRandomUniqueId(); 109 final String subjectURI = serverAddress + pid; 110 createObject(pid); 111 112 final HttpGet httpGet = getObjMethod(pid); 113 httpGet.addHeader(ACCEPT, acceptType); 114 final Model model = createDefaultModel(); 115 try (final CloseableHttpResponse response = execute(httpGet)) { 116 model.read(response.getEntity().getContent(), subjectURI, rdfLang); 117 118 assertTrue("Should contain fedora namespace prefix", 119 model.getNsPrefixMap().containsKey("fedora")); 120 121 assertFalse("Must not contain prefix for registered but unused namespace", 122 model.getNsPrefixMap().containsKey("unused")); 123 } 124 } 125 126 @Test 127 public void testUnprefixedSerialization() throws Exception { 128 final String pid = getRandomUniqueId(); 129 final String subjectURI = serverAddress + pid; 130 createObject(pid); 131 132 final HttpGet httpGet = getObjMethod(pid); 133 httpGet.addHeader(ACCEPT, "application/n-triples"); 134 final Model model = createDefaultModel(); 135 try (final CloseableHttpResponse response = execute(httpGet)) { 136 model.read(response.getEntity().getContent(), subjectURI, "NTRIPLES"); 137 138 assertTrue("No namespaces should be returned", 139 model.getNsPrefixMap().isEmpty()); 140 141 final Resource resc = model.getResource(subjectURI); 142 assertTrue("Must contain property using register namespaces", 143 resc.hasProperty(RDF_TYPE, FEDORA_CONTAINER)); 144 } 145 } 146}