001/* 002 * Copyright 2015 DuraSpace, Inc. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.fcrepo.integration; 017 018import com.hp.hpl.jena.rdf.model.Resource; 019import org.fcrepo.kernel.api.RdfStream; 020import org.fcrepo.kernel.api.models.Container; 021import org.fcrepo.kernel.api.services.ContainerService; 022import org.fcrepo.kernel.modeshape.rdf.impl.DefaultIdentifierTranslator; 023import org.fcrepo.kernel.modeshape.rdf.impl.PropertiesRdfContext; 024import org.fcrepo.transform.transformations.LDPathTransform; 025import org.junit.Before; 026import org.junit.Test; 027import org.junit.runner.RunWith; 028import org.springframework.test.annotation.DirtiesContext; 029import org.springframework.test.annotation.DirtiesContext.ClassMode; 030import org.springframework.test.context.ContextConfiguration; 031import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 032 033import javax.inject.Inject; 034import javax.jcr.Repository; 035import javax.jcr.RepositoryException; 036import javax.jcr.Session; 037import java.io.ByteArrayInputStream; 038import java.io.InputStream; 039import java.util.Collection; 040import java.util.Map; 041import java.util.List; 042 043import static org.fcrepo.kernel.api.RequiredRdfContext.PROPERTIES; 044import static org.fcrepo.kernel.api.RdfLexicon.REPOSITORY_NAMESPACE; 045import static org.junit.Assert.assertEquals; 046import static org.junit.Assert.assertNotNull; 047import static org.junit.Assert.assertTrue; 048 049 050/** 051 * <p>LDPathServiceIT class.</p> 052 * 053 * @author cbeer 054 */ 055@RunWith(SpringJUnit4ClassRunner.class) 056@ContextConfiguration({"/spring-test/master.xml"}) 057@DirtiesContext(classMode = ClassMode.AFTER_CLASS) 058public class LDPathServiceIT { 059 060 @Inject 061 Repository repo; 062 063 @Inject 064 ContainerService containerService; 065 private LDPathTransform testObj; 066 067 @Before 068 public void setUp() { 069 070 } 071 072 @Test 073 public void shouldDoStuff() throws RepositoryException { 074 final Session session = repo.login(); 075 076 final Container object = containerService.findOrCreate(session, "/testObject"); 077 object.getNode().setProperty("dc:title", "some-title"); 078 079 final String s = "@prefix dces : <http://purl.org/dc/elements/1.1/>\n" + 080 "@prefix fcrepo : <" + REPOSITORY_NAMESPACE + ">\n" + 081 "id = . :: xsd:string ;\n" + 082 "title = dc:title :: xsd:string ;"; 083 final InputStream stringReader = new ByteArrayInputStream(s.getBytes()); 084 085 testObj = new LDPathTransform(stringReader); 086 087 final DefaultIdentifierTranslator subjects = new DefaultIdentifierTranslator(session); 088 final Resource topic = subjects.reverse().convert(object); 089 final RdfStream triples = object.getTriples(subjects, PROPERTIES); 090 final List<Map<String, Collection<Object>>> list = testObj.apply(triples); 091 092 assertNotNull("Failed to retrieve results!", list); 093 094 assertTrue("List didn't contain a result", list.size() == 1); 095 096 final Map<String, Collection<Object>> stuff = list.get(0); 097 098 assertTrue("Results didn't contain an identifier!", stuff.containsKey("id")); 099 assertTrue("Results didn't contain a title!", stuff.containsKey("title")); 100 101 assertEquals("Received more than one identifier!", 1, stuff.get("id").size()); 102 assertEquals("Got wrong subject in identifier!", subjects.toDomain("/testObject").getURI(), stuff.get( 103 "id").iterator().next()); 104 assertEquals("Got wrong title!", "some-title", stuff.get("title").iterator().next()); 105 } 106}