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 */
016
017package org.fcrepo.migration.pidlist;
018
019import org.apache.commons.io.FileUtils;
020import org.fcrepo.migration.Migrator;
021import org.junit.After;
022import org.junit.Assert;
023import org.junit.Before;
024import org.junit.Test;
025import org.springframework.context.ConfigurableApplicationContext;
026import org.springframework.context.support.ClassPathXmlApplicationContext;
027
028import java.io.BufferedWriter;
029import java.io.File;
030import java.io.FileWriter;
031import java.io.IOException;
032import java.io.UncheckedIOException;
033import java.nio.file.Files;
034import java.nio.file.Path;
035
036/**
037 * A basic suite of integration tests to test certain interaction patterns (and code) against the an OCFL version of
038 * Fedora.
039 * ..using the "UserProvidePidListManager"
040 *
041 * @author awoods
042 * @since 2019-11-11
043 */
044public class UserProvidedPidListManagerIT {
045
046    private ConfigurableApplicationContext context;
047    private Migrator migrator;
048    private File storage;
049    private File staging;
050    private File pidFile;
051
052    @Before
053    public void setup() throws Exception {
054        // Create directories expected in this test (based on `spring/ocfl-user-it-setup.xml`)
055        storage = new File("target/test/ocfl/user-it/storage");
056        staging = new File("target/test/ocfl/user-it/staging");
057        pidFile = new File("target/test/ocfl/user-it/pidlist.txt");
058
059        if (storage.exists()) {
060            FileUtils.forceDelete(storage);
061        }
062        if (staging.exists()) {
063            FileUtils.forceDelete(staging);
064        }
065        if (pidFile.exists()) {
066            FileUtils.forceDelete(pidFile);
067        }
068
069        storage.mkdirs();
070        staging.mkdirs();
071
072        context = new ClassPathXmlApplicationContext("spring/ocfl-user-it-setup.xml");
073        migrator = (Migrator) context.getBean("migrator");
074    }
075
076    @After
077    public void tearDown() {
078        context.close();
079    }
080
081    @Test
082    public void testMigrate() throws Exception {
083        // Create PID-list file
084        final BufferedWriter writer = new BufferedWriter(new FileWriter(pidFile));
085        writer.write("example:3");
086        writer.newLine();
087        writer.write("example:2");
088        writer.newLine();
089        writer.write("example:1");
090        writer.flush();
091        writer.close();
092
093        final UserProvidedPidListManager manager = new UserProvidedPidListManager(pidFile);
094
095        // There are three test OCFL objects: example%3a1, example%3a2, example%3a3
096        migrator.setUserProvidedPidListManager(manager);
097        migrator.run();
098
099        Assert.assertEquals(3, countDirectories(storage.toPath())) ;
100    }
101
102    @Test
103    public void testMigrateIncremental() throws Exception {
104        // Create PID-list file
105        final BufferedWriter writer = new BufferedWriter(new FileWriter(pidFile));
106        writer.write("example:3");
107        writer.newLine();
108        writer.write("example:1");
109        writer.flush();
110        writer.close();
111
112        // There are three test OCFL objects: example%3a1, example%3a2, example%3a3
113        migrator.setUserProvidedPidListManager(new UserProvidedPidListManager(pidFile));
114        migrator.run();
115        context.close();
116
117        Assert.assertEquals(2, countDirectories(storage.toPath()));
118    }
119
120    private long countDirectories(final Path path) {
121        try (final var list = Files.list(path)) {
122            return list.filter(Files::isDirectory)
123                    .filter(dir -> !"extensions".equals(dir.getFileName().toString()))
124                    .count();
125        } catch (IOException e) {
126            throw new UncheckedIOException(e);
127        }
128    }
129
130}