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