podd.util.WebappInitialisationUtilUnitTest.java Source code

Java tutorial

Introduction

Here is the source code for podd.util.WebappInitialisationUtilUnitTest.java

Source

/*
 * Copyright (c) 2009 - 2010. School of Information Technology and Electrical
 * Engineering, The University of Queensland.  This software is being developed
 * for the "Phenomics Ontoogy Driven Data Management Project (PODD)" project.
 * PODD is a National e-Research Architecture Taskforce (NeAT) project
 * co-funded by ANDS and ARCS.
 *
 * PODD is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PODD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PODD.  If not, see <http://www.gnu.org/licenses/>.
 */

package podd.util;

import fedora.server.types.gen.ComparisonOperator;
import fedora.server.types.gen.Condition;
import fedora.server.types.gen.ObjectFields;
import info.aduna.collections.iterators.CloseableIterator;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;

import podd.owl.OntologyHandlingException;
import podd.owl.OntologyRegistry;
import podd.repository.exceptions.RepositoryObjectHandlingException;
import podd.util.fedora.FedoraRepositoryUtil;

import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static podd.util.PoddWebappTestUtil.clearDB;
import static podd.util.common.vocabulary.PoddModelNamespace.PODD_MODEL;

/**
 * @author Yuan-Fang Li
 * @version $Id$
 */

public class WebappInitialisationUtilUnitTest {
    private static final PoddApplicationContext PODD_CONTEXT = PoddApplicationContext.getPoddContext();

    private FedoraRepositoryUtil repoUtil;
    private WebappInitialisationUtil initUtil;
    private OntologyRegistry ontologyRegistry;

    @Before
    public void setUp() throws RepositoryObjectHandlingException, OntologyHandlingException {
        repoUtil = PODD_CONTEXT.getFedoraUtil();
        ontologyRegistry = PODD_CONTEXT.getOntologyRegistry();
        ontologyRegistry.reset();

        initUtil = new WebappInitialisationUtil();
        initUtil.setToIngest(false);
        initUtil.setOntologyRegistry(ontologyRegistry);
        initUtil.setUserDao(PODD_CONTEXT.getUserDAO());
        initUtil.setRrDao(PODD_CONTEXT.getRepositoryRoleDAO());
        initUtil.setAdminSettingsDao(PODD_CONTEXT.getAdministrationSettingsDAO());
        initUtil.setConceptDao(PODD_CONTEXT.getPoddConceptDAO());
        initUtil.setEntityFactory(PODD_CONTEXT.getEntityFactory());
        initUtil.setOntologyHelper(PODD_CONTEXT.getOntologyHelper());
        initUtil.setRepoAdminUserResource(new ClassPathResource("admin.properties"));
        initUtil.setSessionFactory(PODD_CONTEXT.getSessionFactory());
        initUtil.setRepoWrapper(PODD_CONTEXT.getSesameRepoWrapper());
        initUtil.setOntologyDirectory(new ClassPathResource("/ontologies"));
        initUtil.setOntologyNames("podd_base.owl,poddModelPlant.owl");

        clearDB("PoddEntity");
        clearDB("User");
        deleteFCObjects(PODD_MODEL.prefix + ":*");
    }

    @After
    public void tearDown() throws OntologyHandlingException {
        PODD_CONTEXT.getOntologyRegistry().clear();
        ontologyRegistry = null;
        repoUtil = null;
        initUtil = null;
    }

    private void deleteFCObjects(String pattern) throws RepositoryObjectHandlingException {
        final Condition condition = new Condition("pid", ComparisonOperator.has, pattern);
        final CloseableIterator<ObjectFields> iterator = repoUtil.searchForObjectFields(20,
                Collections.singleton("pid"), condition);
        try {
            while (iterator.hasNext()) {
                repoUtil.ensureDeleted(iterator.next().getPid());
            }
        } finally {
            iterator.close();
        }
    }

    @Test
    public void testNotInitialize() throws Exception {
        initUtil.setToReset(false);
        initUtil.doInitialize();
        assertNull(initUtil.getRepoAdmin());
    }

    @Test
    public void testInitializeAndIngest() throws Exception {
        assertEquals(0, ontologyRegistry.getOntologies().size());

        initUtil.setToReset(true);
        initUtil.setToIngest(true);
        initUtil.doInitialize();

        assertEquals(2, ontologyRegistry.getOntologies().size());
        final CloseableIterator<String> iterator = repoUtil.searchForPIDs(PODD_MODEL.prefix + "*", 20);
        try {
            assertTrue(iterator.hasNext());
        } finally {
            iterator.close();
        }
    }

    @Test
    public void testInitializeButNotIngest() throws Exception {

        assertEquals(0, ontologyRegistry.getOntologies().size());

        final int initialSize = PODD_MODEL.getAllRDFNames().size();

        initUtil.setToReset(true);
        initUtil.setToIngest(false);
        initUtil.doInitialize();

        final int afterSize = PODD_MODEL.getAllRDFNames().size();
        assertTrue("original size " + initialSize + ", after size " + afterSize, afterSize >= initialSize);

        assertEquals(2, ontologyRegistry.getOntologies().size());
        final CloseableIterator<String> iterator = repoUtil.searchForPIDs(PODD_MODEL.prefix + "*", 20);
        try {
            assertFalse(iterator.hasNext());
        } finally {
            iterator.close();
        }
    }
}