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