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