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.connector.file;
019
020import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
021import org.fcrepo.kernel.api.models.FedoraResource;
022import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
023import org.fcrepo.kernel.modeshape.rdf.impl.DefaultIdentifierTranslator;
024import org.junit.Test;
025
026import javax.jcr.PathNotFoundException;
027import javax.jcr.Property;
028import javax.jcr.RepositoryException;
029import javax.jcr.Session;
030
031import static com.hp.hpl.jena.graph.NodeFactory.createURI;
032import static org.fcrepo.kernel.api.RdfLexicon.REPOSITORY_NAMESPACE;
033import static org.fcrepo.kernel.api.RequiredRdfContext.PROPERTIES;
034import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.getJcrNode;
035import static org.junit.Assert.assertEquals;
036import static org.junit.Assert.assertNotNull;
037import static org.junit.Assert.assertTrue;
038
039/**
040 * @author Mike Durbin
041 */
042public class BasicReadWriteFedoraFileSystemConnectorIT extends AbstractFedoraFileSystemConnectorIT {
043
044    @Override
045    protected String federationName() {
046        return "federated";
047    }
048
049    @Override
050    protected String testFilePath() {
051        return "/" + federationName() + "/repository.json";
052    }
053
054    @Override
055    protected String testDirPath() {
056        return "/" + federationName();
057    }
058
059    @Override
060    protected String getFederationRoot() {
061        return getReadWriteFederationRoot();
062    }
063
064    @Test(expected = RepositoryRuntimeException.class)
065    public void testWriteProperty() throws RepositoryException {
066        final Session session = repo.login();
067        try {
068            final FedoraResource object = nodeService.find(session, testFilePath());
069            assertNotNull(object);
070
071            final String sparql = "PREFIX fedora: <" + REPOSITORY_NAMESPACE + "> " +
072                    "INSERT DATA { " +
073                    "<info:fedora" + testFilePath() + "> " +
074                    "fedora:name " +
075                    "'some-test-name' }";
076
077            // Write the properties
078            try (final DefaultRdfStream originalTriples =
079                    new DefaultRdfStream(createURI("info:fedora" + testFilePath()))) {
080                object.updateProperties(new DefaultIdentifierTranslator(session), sparql, originalTriples);
081            }
082
083            // Verify
084            final Property property = getJcrNode(object).getProperty("fedora:name");
085            assertNotNull(property);
086            assertEquals("some-test-name", property.getValues()[0].toString());
087
088            session.save();
089        } finally {
090            session.logout();
091        }
092    }
093
094    @Test(expected = RepositoryRuntimeException.class)
095    public void testRemoveProperty() throws RepositoryException {
096        final Session session = repo.login();
097        try {
098            final FedoraResource object = nodeService.find(session, testFilePath());
099            assertNotNull(object);
100
101            final String sparql = "PREFIX fedora: <" + REPOSITORY_NAMESPACE + "> " +
102                    "INSERT DATA { " +
103                    "<info:fedora" + testFilePath() + "> " +
104                    "fedora:remove " +
105                    "'some-property-to-remove' }";
106
107            // Write the properties
108            final DefaultIdentifierTranslator graphSubjects = new DefaultIdentifierTranslator(session);
109            try (final DefaultRdfStream originalTriples =
110                    new DefaultRdfStream(createURI("info:fedora" + testFilePath()))) {
111                object.updateProperties(graphSubjects, sparql, originalTriples);
112            }
113
114            // Verify property exists
115            final Property property = getJcrNode(object).getProperty("fedora:remove");
116            assertNotNull(property);
117            assertEquals("some-property-to-remove", property.getValues()[0].getString());
118
119            final String sparqlRemove = "PREFIX fedora: <" + REPOSITORY_NAMESPACE + "> " +
120                    "DELETE {" +
121                    "  <info:fedora" + testFilePath() + "> fedora:remove ?s " +
122                    "} WHERE { " +
123                    "  <info:fedora" + testFilePath() + "> fedora:remove ?s" +
124                    "}";
125
126            // Remove the properties
127            object.updateProperties(graphSubjects,
128                    sparqlRemove,
129                    object.getTriples(graphSubjects, PROPERTIES));
130
131            // Persist the object (although the propery will be removed from memory without this.)
132            session.save();
133
134            // Verify
135            boolean thrown = false;
136            try {
137                getJcrNode(object).getProperty("fedora:remove");
138            } catch (final PathNotFoundException e) {
139                thrown = true;
140            }
141            assertTrue("Exception expected - property should be missing", thrown);
142        } finally {
143            session.logout();
144        }
145    }
146}