org.jasig.ssp.dao.PersonProgramStatusDaoTest.java Source code

Java tutorial

Introduction

Here is the source code for org.jasig.ssp.dao.PersonProgramStatusDaoTest.java

Source

/**
 * Licensed to Apereo under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Apereo licenses this file to you 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 the following location:
 *
 *   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.jasig.ssp.dao;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jasig.ssp.dao.reference.ProgramStatusChangeReasonDao;
import org.jasig.ssp.dao.reference.ProgramStatusDao;
import org.jasig.ssp.model.ObjectStatus;
import org.jasig.ssp.model.Person;
import org.jasig.ssp.model.PersonProgramStatus;
import org.jasig.ssp.service.ObjectNotFoundException;
import org.jasig.ssp.service.impl.SecurityServiceInTestEnvironment;
import org.jasig.ssp.util.sort.SortingAndPaging;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

/**
 * Tests for the {@link PersonProgramStatusDao} class.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("dao-testConfig.xml")
@TransactionConfiguration(defaultRollback = false)
@Transactional
public class PersonProgramStatusDaoTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(PersonProgramStatusDaoTest.class);

    @Autowired
    private transient PersonProgramStatusDao dao;

    @Autowired
    private transient ProgramStatusDao programStatusDao;

    @Autowired
    private transient ProgramStatusChangeReasonDao programStatusChangeReasonDao;

    @Autowired
    private transient SecurityServiceInTestEnvironment securityService;

    @Autowired
    private transient SessionFactory sessionFactory;

    /**
     * Initialize security and test data.
     */
    @Before
    public void setUp() {
        securityService.setCurrent(new Person(Person.SYSTEM_ADMINISTRATOR_ID));
    }

    /**
     * Test {@link PersonProgramStatusDao#save(PersonProgramStatus)} ,
     * {@link PersonProgramStatusDao#get(UUID)},
     * {@link PersonProgramStatusDao#getAll(ObjectStatus)}, and
     * {@link PersonProgramStatusDao#delete(PersonProgramStatus)}.
     * 
     * @throws ObjectNotFoundException
     *             If saved instance could not be reloaded.
     */
    @Test
    public void testSaveNew() throws ObjectNotFoundException {
        UUID saved;

        PersonProgramStatus obj = new PersonProgramStatus();
        obj.setObjectStatus(ObjectStatus.ACTIVE);
        obj.setPerson(securityService.currentUser().getPerson());
        obj.setProgramStatus(programStatusDao.get(UUID.fromString("b2d12527-5056-a51a-8054-113116baab88")));
        obj.setProgramStatusChangeReason(
                programStatusChangeReasonDao.get(UUID.fromString("b2d128f0-5056-a51a-803f-8cef57177aea")));
        obj.setEffectiveDate(new Date());
        obj = dao.save(obj);

        assertNotNull("Saved object should not have been null.", obj.getId());
        saved = obj.getId();

        // flush to storage, then clear out in-memory version
        final Session session = sessionFactory.getCurrentSession();
        session.flush();
        session.evict(obj);

        obj = dao.get(saved);
        LOGGER.debug("testSaveNew(): Saved " + obj.toString());
        assertNotNull("Reloaded object should not have been null.", obj);
        assertNotNull("Reloaded ID should not have been null.", obj.getId());

        final List<PersonProgramStatus> all = (List<PersonProgramStatus>) dao.getAll(ObjectStatus.ACTIVE).getRows();
        assertNotNull("GetAll list should not have been null.", all);
        assertFalse("GetAll list should not have been empty.", all.isEmpty());
        assertList(all);

        dao.delete(obj);
    }

    /**
     * Test that invalid identifiers to {@link PersonProgramStatusDao#get(UUID)}
     * correctly throw ObjectNotFound exception.
     * 
     * @throws ObjectNotFoundException
     *             Expected to be thrown
     */
    @Test(expected = ObjectNotFoundException.class)
    public void testNull() throws ObjectNotFoundException {
        dao.get(UUID.randomUUID());
        fail("Result of invalid get() should have thrown an exception.");
    }

    /**
     * Test that all results from getAll are not null
     */
    @Test
    public void getAllForPersonId() {
        assertList(dao.getAllForPersonId(UUID.randomUUID(), new SortingAndPaging(ObjectStatus.ACTIVE)).getRows());
    }

    private void assertList(final Collection<PersonProgramStatus> objects) {
        for (final PersonProgramStatus object : objects) {
            assertNotNull("Object in the list should not have been null.", object.getId());
        }
    }
}