org.dataconservancy.ui.services.AtomDepositDocumentParserTest.java Source code

Java tutorial

Introduction

Here is the source code for org.dataconservancy.ui.services.AtomDepositDocumentParserTest.java

Source

/*
 * Copyright 2012 Johns Hopkins University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dataconservancy.ui.services;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.PrefixFileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.dataconservancy.model.builder.DcsModelBuilder;
import org.dataconservancy.model.builder.InvalidXmlException;
import org.dataconservancy.model.builder.xstream.DcsXstreamStaxModelBuilder;
import org.dataconservancy.model.dcp.Dcp;
import org.dataconservancy.model.dcs.DcsEntity;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Iterator;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 *
 */
public class AtomDepositDocumentParserTest {

    private static final String PRODUCTION_FEED_RESOURCE = "sampleDeposit/sample-sipDeposit-feed.xml";

    private static final String INMEMORY_FEED_RESOURCE = "inMemorySampleDeposit/sample-inMemorySipDeposit-feed.xml";

    /**
     * A sample Atom deposit feed from a production instance of the DCS
     */
    private InputStream productionFeedIn;

    /**
     * A sample Atom deposit feed generated by the InMemoryArchiveService
     */
    private InputStream inMemoryFeedIn;

    private AtomDepositDocumentParser underTest;

    private MockArchiveUtil productionMockArchiveUtil;

    private MockArchiveUtil inMemoryMockArchiveUtil;

    private DcsModelBuilder modelBuilder = new DcsXstreamStaxModelBuilder();

    /**
     * Seeds a {@code MockArchiveUtil} with entities, and constructs a {@code AtomDepositDocumentParser} for testing.
     * 
     * @throws URISyntaxException
     * @throws FileNotFoundException
     * @throws InvalidXmlException
     */
    @Before
    @SuppressWarnings("unchecked")
    public void setUp() throws URISyntaxException, FileNotFoundException, InvalidXmlException {

        // Obtain the valid, production, sample feed
        URL feedResource = this.getClass().getResource(PRODUCTION_FEED_RESOURCE);
        assertNotNull("Unable to resolve classpath resource " + PRODUCTION_FEED_RESOURCE);
        productionFeedIn = this.getClass().getResourceAsStream(PRODUCTION_FEED_RESOURCE);

        // Instantiate a MockArchiveUtil which we will pre-populate with expected DCS entities.
        productionMockArchiveUtil = new MockArchiveUtil();

        // Set the mapping strategy to entity ids only
        productionMockArchiveUtil.setMappingStrategy(MockArchiveUtil.ID_MAPPING_STRATEGY.ENTITY_ID);

        // Populate the MockArchiveUtil with the valid, expected, DCS entities.
        File feedBaseDir = new File(feedResource.toURI()).getParentFile();
        Iterator<File> serializedEntities = FileUtils.iterateFiles(feedBaseDir,
                new AndFileFilter(new PrefixFileFilter("4260"), new SuffixFileFilter(".xml")),
                DirectoryFileFilter.DIRECTORY);
        while (serializedEntities.hasNext()) {
            Dcp dcp = modelBuilder.buildSip(new FileInputStream(serializedEntities.next()));
            for (DcsEntity e : dcp) {
                productionMockArchiveUtil.addEntity(e);
            }
        }

        assertTrue("Error loading expected entities from " + feedBaseDir,
                productionMockArchiveUtil.getEntities().size() > 0);

        // Construct the DocumentParser under test
        underTest = new AtomDepositDocumentParser(productionMockArchiveUtil);
    }

    /**
     * Attempts to parse a sample feed (representing a UI DataItem deposit, in this case).  Verifies the expected root
     * DU of the deposit, and the identifier of the feed.
     *
     * @throws Exception
     */
    @Test
    public void testProductionParseOk() throws Exception {
        final String expectedFeedId = "http://dataconservancy.org/ingest/status/426025";
        final String expectedRootDuId = "http://bamboo.mse.jhu.edu:8086/dcs/entity/426029";

        DepositDocument result = underTest.parse(productionFeedIn);
        assertNotNull("Parser returned a null DepositDocument.", result);
        Assert.assertEquals("Expected the ingest to be complete", true, result.isComplete());
        Assert.assertEquals("Expected the ingest to be successful", true, result.isSuccessful());
        Assert.assertEquals("Expected the feed ID to be " + expectedFeedId, expectedFeedId, result.getId());
        assertNotNull("Parser did not find a root DU.", result.getRoot());
        Assert.assertEquals("Unexpected root DU id: " + result.getRoot().getId(), expectedRootDuId,
                result.getRoot().getId());
    }
}