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.File;
029import java.io.IOException;
030import java.io.UncheckedIOException;
031import java.nio.file.Files;
032import java.nio.file.Path;
033
034/**
035 * A basic suite of integration tests to test certain interaction patterns (and code) against the an OCFL version of
036 * Fedora.
037 * ..using the "ResumePidListManager"
038 *
039 * @author awoods
040 * @since 2019-11-11
041 */
042public class ResumePidListManagerIT {
043
044    private ConfigurableApplicationContext context;
045    private Migrator migrator;
046    private File storage;
047    private File staging;
048    private File pidDir;
049
050    @Before
051    public void setup() throws Exception {
052        // Create directories expected in this test (based on `spring/ocfl-pid-it-setup.xml`)
053        storage = new File("target/test/ocfl/pid-it/storage");
054        staging = new File("target/test/ocfl/pid-it/staging");
055        pidDir = new File("target/test/ocfl/pid-it/pid");
056
057        if (storage.exists()) {
058            FileUtils.forceDelete(storage);
059        }
060        if (staging.exists()) {
061            FileUtils.forceDelete(staging);
062        }
063        if (pidDir.exists()) {
064            FileUtils.forceDelete(pidDir);
065        }
066
067        storage.mkdirs();
068        staging.mkdirs();
069        pidDir.mkdirs();
070
071        context = new ClassPathXmlApplicationContext("spring/ocfl-pid-it-setup.xml");
072        migrator = (Migrator) context.getBean("migrator");
073    }
074
075    @After
076    public void tearDown() {
077        context.close();
078    }
079
080    @Test
081    public void testMigrate() throws Exception {
082        final boolean acceptAll = false;
083        final ResumePidListManager manager = new ResumePidListManager(pidDir, acceptAll);
084
085        // There are three test OCFL objects: example%3a1, example%3a2, example%3a3
086        migrator.setResumePidListManager(manager);
087        migrator.run();
088
089        Assert.assertEquals(3, countDirectories(storage.toPath()));
090    }
091
092    @Test
093    public void testMigrateIncremental() throws Exception {
094        final boolean acceptAll = false;
095
096        // There are three test OCFL objects: example%3a1, example%3a2, example%3a3
097        migrator.setResumePidListManager(new ResumePidListManager(pidDir, acceptAll));
098        // Only migrate 2 of 3 objects
099        migrator.setLimit(2);
100        migrator.run();
101        context.close();
102
103        Assert.assertEquals(2, countDirectories(storage.toPath()));
104
105        // Remove the previously exported objects, and resume the migration
106        FileUtils.forceDelete(storage);
107        FileUtils.forceDelete(staging);
108        storage.mkdirs();
109        staging.mkdirs();
110
111        context = new ClassPathXmlApplicationContext("spring/ocfl-pid-it-setup.xml");
112        migrator = (Migrator) context.getBean("migrator");
113        migrator.setResumePidListManager(new ResumePidListManager(pidDir, acceptAll));
114        migrator.setLimit(-1); // migrate all
115        migrator.run();
116
117        Assert.assertEquals(1, 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}