001/**
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 *
006 */
007package org.fcrepo.migration.handlers;
008
009import java.io.File;
010import java.io.FileOutputStream;
011import java.io.IOException;
012import java.util.ArrayList;
013import java.util.List;
014
015import javax.xml.stream.XMLStreamException;
016
017import org.apache.commons.io.IOUtils;
018import org.fcrepo.migration.DatastreamVersion;
019import org.fcrepo.migration.FedoraObjectVersionHandler;
020import org.fcrepo.migration.Migrator;
021import org.fcrepo.migration.ObjectVersionReference;
022import org.fcrepo.migration.ObjectInfo;
023import org.junit.Assert;
024import org.junit.Test;
025import org.springframework.context.ApplicationContext;
026import org.springframework.context.support.ClassPathXmlApplicationContext;
027/**
028 *
029 * @author mdurbin
030 *
031 */
032public class VersionAbstractionFedoraObjectHandlerTest {
033
034    @Test
035    public void testObjectProcessing() throws IOException, XMLStreamException {
036        final ApplicationContext context = new ClassPathXmlApplicationContext("spring/version-abstraction.xml");
037        final TestingFedoraObjectVersionHandler vh =
038                (TestingFedoraObjectVersionHandler) context.getBean("versionHandler");
039        final Migrator m = (Migrator) context.getBean("migrator");
040        m.run();
041
042        Assert.assertEquals("Six versions should have been gleaned.", 6, vh.versions.size());
043        Assert.assertEquals("2015-01-27T19:07:33.120Z", vh.versions.get(0).getVersionDate());
044        Assert.assertEquals("AUDIT.0", vh.versions.get(0).listChangedDatastreams().get(0).getVersionId());
045        Assert.assertEquals("DC1.0", vh.versions.get(0).listChangedDatastreams().get(1).getVersionId());
046
047        Assert.assertEquals("2015-01-27T19:08:43.701Z", vh.versions.get(1).getVersionDate());
048        Assert.assertEquals("DS1.0", vh.versions.get(1).listChangedDatastreams().get(0).getVersionId());
049
050        Assert.assertEquals("2015-01-27T19:09:18.112Z", vh.versions.get(2).getVersionDate());
051        Assert.assertEquals("DS2.0", vh.versions.get(2).listChangedDatastreams().get(0).getVersionId());
052
053        Assert.assertEquals("2015-01-27T19:14:05.948Z", vh.versions.get(3).getVersionDate());
054        Assert.assertEquals("DS3.0", vh.versions.get(3).listChangedDatastreams().get(0).getVersionId());
055
056        Assert.assertEquals("2015-01-27T19:14:38.999Z", vh.versions.get(4).getVersionDate());
057        Assert.assertEquals("DS4.0", vh.versions.get(4).listChangedDatastreams().get(0).getVersionId());
058
059        Assert.assertEquals("2015-01-27T19:20:40.678Z", vh.versions.get(5).getVersionDate());
060        Assert.assertEquals("DS1.1", vh.versions.get(5).listChangedDatastreams().get(0).getVersionId());
061    }
062
063    /**
064     * An implementation of FedoraObjectVersionHandler which is meant to test the processing
065     * of a well-known fedora object.
066     */
067    private static class TestingFedoraObjectVersionHandler implements FedoraObjectVersionHandler {
068
069        List<ObjectVersionReference> versions;
070
071        public TestingFedoraObjectVersionHandler() {
072            versions = new ArrayList<ObjectVersionReference>();
073        }
074
075        /**
076         * Tests that which is only testable within this method call and
077         * puts the reference on the versions list for later tests.
078         */
079        @Override
080        public void processObjectVersions(final Iterable<ObjectVersionReference> versions,
081                                          final ObjectInfo objectInfo) {
082            for (final ObjectVersionReference version : versions) {
083                this.versions.add(version);
084                for (final DatastreamVersion dsv : version.listChangedDatastreams()) {
085                    if (dsv.getDatastreamInfo().getControlGroup().equals("M")) {
086                        try {
087                            testDatastreamBinary(dsv);
088                        } catch (final IOException e) {
089                            throw new RuntimeException(e);
090                        }
091                    }
092                }
093            }
094
095        }
096
097        private void testDatastreamBinary(final DatastreamVersion v) throws IOException {
098            final File temp = File.createTempFile("temporary", ".file");
099            final FileOutputStream fos = new FileOutputStream(temp);
100            try {
101                IOUtils.copy(v.getContent(), fos);
102            } finally {
103                fos.close();
104            }
105            Assert.assertEquals(v.getSize(), temp.length());
106            temp.delete();
107        }
108    }
109
110}