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.models.Container;
020import org.fcrepo.kernel.api.services.ContainerService;
021import org.fcrepo.kernel.api.utils.iterators.RdfStream;
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.RdfLexicon.REPOSITORY_NAMESPACE;
044import static org.junit.Assert.assertEquals;
045import static org.junit.Assert.assertNotNull;
046import static org.junit.Assert.assertTrue;
047
048
049/**
050 * <p>LDPathServiceIT class.</p>
051 *
052 * @author cbeer
053 */
054@RunWith(SpringJUnit4ClassRunner.class)
055@ContextConfiguration({"/spring-test/master.xml"})
056@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
057public class LDPathServiceIT {
058
059    @Inject
060    Repository repo;
061
062    @Inject
063    ContainerService containerService;
064    private LDPathTransform testObj;
065
066    @Before
067    public void setUp() {
068
069    }
070
071    @Test
072    public void shouldDoStuff() throws RepositoryException {
073        final Session session = repo.login();
074
075        final Container object = containerService.findOrCreate(session, "/testObject");
076        object.getNode().setProperty("dc:title", "some-title");
077
078        final String s = "@prefix dces : <http://purl.org/dc/elements/1.1/>\n" +
079                       "@prefix fcrepo : <" + REPOSITORY_NAMESPACE + ">\n" +
080                           "id      = . :: xsd:string ;\n" +
081                           "title = dc:title :: xsd:string ;";
082        final InputStream stringReader = new ByteArrayInputStream(s.getBytes());
083
084        testObj = new LDPathTransform(stringReader);
085
086        final DefaultIdentifierTranslator subjects = new DefaultIdentifierTranslator(session);
087        final Resource topic = subjects.reverse().convert(object);
088        final RdfStream triples = object.getTriples(subjects, PropertiesRdfContext.class)
089                                        .topic(topic.asNode());
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}