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