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