001/**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 edu.sampleu.travel.dataobject;
017
018import edu.sampleu.travel.options.ExpenseType;
019import edu.sampleu.travel.options.PostalCountryCode;
020import edu.sampleu.travel.options.PostalStateCode;
021import org.junit.Test;
022import org.kuali.rice.krad.UserSession;
023import org.kuali.rice.krad.data.PersistenceOption;
024import org.kuali.rice.krad.document.Document;
025import org.kuali.rice.krad.service.KRADServiceLocator;
026import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
027import org.kuali.rice.krad.test.KRADTestCase;
028import org.kuali.rice.krad.util.GlobalVariables;
029import org.kuali.rice.krad.util.MessageMap;
030import org.kuali.rice.test.BaselineTestCase;
031
032import java.math.BigDecimal;
033import java.text.SimpleDateFormat;
034
035import static org.junit.Assert.assertEquals;
036import static org.junit.Assert.assertFalse;
037import static org.junit.Assert.assertNotNull;
038import static org.junit.Assert.assertTrue;
039
040/**
041 * Tests basic {@code TravelExpenseItem} persistence.
042 *
043 * @author Kuali Rice Team (rice.collab@kuali.org)
044 */
045@BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
046public class TravelExpenseItemTest extends KRADTestCase {
047
048    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
049
050    private static String DOCUMENT_NUMBER;
051    private static final String DOCUMENT_DESCRIPTION = "Test Travel Authorization Document";
052    private static final String CELL_PHONE_NUMBER = "555-555-5555";
053
054    private static String TRAVEL_DESTINATION_ID;
055    private static final String TRAVEL_COMPANY_NAME = "Zorba's Travel";
056    private static final String DESTINATION_NAME = PostalStateCode.CA.getLabel();
057    private static final String COUNTRY_CODE = PostalCountryCode.US.getCode();
058    private static final String STATE_CODE = PostalStateCode.CA.getCode();
059
060    private static final String EXPENSE_TYPE = ExpenseType.A.getCode();
061    private static final String EXPENSE_DESCRIPTION = ExpenseType.A.getLabel();
062    private static final String EXPENSE_DATE = "2010-01-01";
063    private static final BigDecimal EXPENSE_AMOUNT = new BigDecimal("1236.49");
064
065    @Override
066    public void setUp() throws Exception {
067        super.setUp();
068        GlobalVariables.setMessageMap(new MessageMap());
069        GlobalVariables.setUserSession(new UserSession("admin"));
070
071        TravelDestination newTravelDestination = new TravelDestination();
072        newTravelDestination.setTravelDestinationName(DESTINATION_NAME);
073        newTravelDestination.setCountryCd(COUNTRY_CODE);
074        newTravelDestination.setStateCd(STATE_CODE);
075        TRAVEL_DESTINATION_ID = KRADServiceLocator.getDataObjectService().save(
076                newTravelDestination, PersistenceOption.FLUSH).getTravelDestinationId();
077
078        Document newDocument = KRADServiceLocatorWeb.getDocumentService().getNewDocument(TravelAuthorizationDocument.class);
079        newDocument.getDocumentHeader().setDocumentDescription(DOCUMENT_DESCRIPTION);
080        TravelAuthorizationDocument newTravelAuthorizationDocument = (TravelAuthorizationDocument) newDocument;
081        newTravelAuthorizationDocument.setCellPhoneNumber(CELL_PHONE_NUMBER);
082        newTravelAuthorizationDocument.setTripDestinationId(TRAVEL_DESTINATION_ID);
083        DOCUMENT_NUMBER = KRADServiceLocatorWeb.getDocumentService().saveDocument(
084                newTravelAuthorizationDocument).getDocumentNumber();
085    }
086
087    @Override
088    public void tearDown() throws Exception {
089        GlobalVariables.setMessageMap(new MessageMap());
090        GlobalVariables.setUserSession(null);
091        super.tearDown();
092    }
093
094    /**
095     * Tests basic {@code TravelExpenseItem} persistence by saving it, reloading it, and checking the data.
096     *
097     * @throws java.lang.Exception for any exceptions occurring during creation
098     */
099    @Test
100    public void testTravelExpenseItem() throws Exception {
101        assertTrue(TravelExpenseItem.class.getName() + " is not mapped in JPA",
102                KRADServiceLocator.getDataObjectService().supports(TravelExpenseItem.class));
103
104        String id = createTravelExpenseItem();
105
106        TravelExpenseItem travelExpenseItem = KRADServiceLocator.getDataObjectService().find(TravelExpenseItem.class, id);
107        assertNotNull("Travel Expense Item ID is null", travelExpenseItem.getTravelExpenseItemId());
108        assertEquals("Travel Expense Item document ID is incorrect", DOCUMENT_NUMBER, travelExpenseItem.getTravelAuthorizationDocumentId());
109        assertEquals("Travel Expense Item type is incorrect", EXPENSE_TYPE, travelExpenseItem.getTravelExpenseTypeCd());
110        assertEquals("Travel Company name is incorrect", TRAVEL_COMPANY_NAME, travelExpenseItem.getTravelCompanyName());
111        assertEquals("Travel Expense Item description is incorrect", EXPENSE_DESCRIPTION, travelExpenseItem.getExpenseDesc());
112        assertEquals("Travel Expense Item date is incorrect", DATE_FORMAT.parse(EXPENSE_DATE), travelExpenseItem.getExpenseDate());
113        assertEquals("Travel Expense Item amount is incorrect", EXPENSE_AMOUNT, travelExpenseItem.getExpenseAmount());
114        assertTrue("Travel Expense Item is not reimbursable", travelExpenseItem.isReimbursable());
115        assertFalse("Travel Expense Item is taxable", travelExpenseItem.isTaxable());
116    }
117
118    private String createTravelExpenseItem() throws Exception {
119        TravelExpenseItem travelExpenseItem = new TravelExpenseItem();
120        travelExpenseItem.setTravelAuthorizationDocumentId(DOCUMENT_NUMBER);
121        travelExpenseItem.setTravelExpenseTypeCd(EXPENSE_TYPE);
122        travelExpenseItem.setExpenseDesc(EXPENSE_DESCRIPTION);
123        travelExpenseItem.setTravelCompanyName(TRAVEL_COMPANY_NAME);
124        travelExpenseItem.setExpenseDate(DATE_FORMAT.parse(EXPENSE_DATE));
125        travelExpenseItem.setExpenseAmount(EXPENSE_AMOUNT);
126        travelExpenseItem.setReimbursable(true);
127        travelExpenseItem.setTaxable(false);
128
129        return KRADServiceLocator.getDataObjectService().save(travelExpenseItem, PersistenceOption.FLUSH).getTravelExpenseItemId();
130    }
131}