org.trellisldp.rosid.file.CachedResourceTest.java Source code

Java tutorial

Introduction

Here is the source code for org.trellisldp.rosid.file.CachedResourceTest.java

Source

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.trellisldp.rosid.file;

import static java.time.Instant.now;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import static org.trellisldp.rosid.file.Constants.RESOURCE_CACHE;
import static org.trellisldp.rosid.file.Constants.RESOURCE_QUADS;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

import org.trellisldp.api.Resource;
import org.apache.commons.rdf.api.IRI;
import org.apache.commons.rdf.api.RDF;
import org.apache.commons.rdf.jena.JenaRDF;
import org.junit.Before;
import org.junit.Test;

/**
 * @author acoburn
 */
public class CachedResourceTest {

    private static final RDF rdf = new JenaRDF();

    private File file5, file3, readonly, readonly2, ldprs;
    private IRI identifier = rdf.createIRI("trellis:repository/resource");
    private IRI ldprsIri = rdf.createIRI("trellis:repository/ldprs");

    @Before
    public void setUp() throws Exception {
        ldprs = new File(getClass().getResource("/ldprs").toURI());
        file5 = new File(getClass().getResource("/res5").toURI());
        file3 = new File(getClass().getResource("/res3").toURI());
        readonly = new File(getClass().getResource("/readonly").toURI());
        readonly2 = new File(getClass().getResource("/readonly2").toURI());
    }

    @Test
    public void testNonExistent1() {
        final Optional<Resource> resource = CachedResource.find(null, identifier);
        assertFalse(resource.isPresent());
    }

    @Test
    public void testNonExistent2() {
        final Optional<Resource> resource = CachedResource.find(file5, identifier);
        assertTrue(resource.isPresent());
        assertFalse(resource.get().stream().findFirst().isPresent());
    }

    @Test
    public void testNonExistent3() {
        final Optional<Resource> resource = CachedResource.find(file3, identifier);
        assertFalse(resource.isPresent());
    }

    @Test
    public void testWriteNonExistent() {
        final File fileUnknown = new File(file3, "testing");
        assertFalse(CachedResource.write(fileUnknown, identifier, now()));
    }

    @Test
    public void testWriteOk() {
        assumeTrue(readonly.setWritable(true));
        assertTrue(CachedResource.write(readonly, identifier, now()));
    }

    @Test
    public void testWriteError() {
        assumeTrue(readonly.setWritable(false));
        assertFalse(CachedResource.write(readonly, identifier, now()));
        readonly.setWritable(true);
    }

    @Test
    public void testWriteErrorResource() throws IOException {
        final File cache = new File(readonly2, RESOURCE_CACHE);
        final File quads = new File(readonly2, RESOURCE_QUADS);

        readonly2.setWritable(true);
        cache.createNewFile();
        quads.createNewFile();

        assumeTrue(quads.setWritable(false));
        assertFalse(CachedResource.write(readonly2, ldprsIri, now()));

        assumeTrue(cache.setWritable(false));
        assertFalse(CachedResource.write(readonly2, ldprsIri, now()));

        quads.setWritable(true);
        cache.setWritable(true);
    }

    @Test
    public void testWriteError2() {
        final File resource = new File(readonly2, RESOURCE_QUADS);
        assumeTrue(readonly2.setWritable(true));
        assumeTrue(resource.setWritable(false));
        assertFalse(CachedResource.write(readonly2, identifier, now()));
        resource.setWritable(true);
    }

    @Test
    public void testReadError() {
        final Optional<Resource> res = CachedResource.find(readonly2, rdf.createIRI("trellis:repository/ldpnr"));
        assertTrue(res.isPresent());

        final File quads = new File(readonly2, RESOURCE_QUADS);
        assumeTrue(quads.setReadable(false));
        assertEquals(0L, res.get().stream().count());
        quads.setReadable(true);
    }
}