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 edu.sampleu.travel.options.TripType;
022import org.junit.Test;
023import org.kuali.rice.core.api.util.type.KualiDecimal;
024import org.kuali.rice.krad.UserSession;
025import org.kuali.rice.krad.data.PersistenceOption;
026import org.kuali.rice.krad.document.Document;
027import org.kuali.rice.krad.service.KRADServiceLocator;
028import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
029import org.kuali.rice.krad.test.KRADTestCase;
030import org.kuali.rice.krad.util.GlobalVariables;
031import org.kuali.rice.krad.util.MessageMap;
032import org.kuali.rice.test.BaselineTestCase;
033
034import java.math.BigDecimal;
035import java.text.SimpleDateFormat;
036
037import static org.junit.Assert.assertEquals;
038import static org.junit.Assert.assertNotNull;
039import static org.junit.Assert.assertTrue;
040
041/**
042 * Tests basic {@code TravelAuthorizationDocument} persistence.
043 *
044 * @author Kuali Rice Team (rice.collab@kuali.org)
045 */
046@BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
047public class TravelAuthorizationDocumentTest extends KRADTestCase {
048
049    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
050
051    private static final String DOCUMENT_DESCRIPTION = "Test Travel Authorization Document";
052    private static final String TRIP_BEGIN = "2010-01-01";
053    private static final String TRIP_END = "2010-02-01";
054    private static final String TRIP_DESCRIPTION = "Test Trip";
055    private static final String TRAVEL_TYPE_CODE = TripType.OS.getCode();
056    private static final KualiDecimal EXPENSE_LIMIT = new KualiDecimal(10000);
057    private static final String CELL_PHONE_NUMBER = "555-555-5555";
058
059    private static String TRAVELER_DETAIL_ID;
060    private static final String PRINCIPAL_ID = "admin";
061
062    private static String TRAVEL_DESTINATION_ID;
063    private static final String DESTINATION_NAME = PostalStateCode.CA.getLabel();
064    private static final String COUNTRY_CODE = PostalCountryCode.US.getCode();
065    private static final String STATE_CODE = PostalStateCode.CA.getCode();
066
067    private static String MILEAGE_RATE_ID;
068    private static final String MILEAGE_RATE_CODE = "DO";
069    private static final String MILEAGE_RATE_NAME = "Domestic";
070    private static final BigDecimal MILEAGE_RATE = new BigDecimal("1994.88");
071
072    private static final String PER_DIEM_DATE = "2010-01-01";
073    private static final BigDecimal BREAKFAST_VALUE = new BigDecimal("15.00");
074    private static final BigDecimal LUNCH_VALUE = new BigDecimal("30.00");
075    private static final BigDecimal DINNER_VALUE = new BigDecimal("45.00");
076    private static final BigDecimal INCIDENTALS_VALUE = new BigDecimal("15.00");
077    private static final BigDecimal ESTIMATED_MILEAGE = new BigDecimal("50");
078
079    private static final String EXPENSE_TYPE = ExpenseType.A.getCode();
080    private static final String EXPENSE_DESCRIPTION = ExpenseType.A.getLabel();
081    private static final String TRAVEL_COMPANY_NAME = "Zorba's Travel";
082    private static final String EXPENSE_DATE = "2010-01-01";
083    private static final BigDecimal EXPENSE_AMOUNT = new BigDecimal("1236.49");
084
085    @Override
086    public void setUp() throws Exception {
087        super.setUp();
088        GlobalVariables.setMessageMap(new MessageMap());
089        GlobalVariables.setUserSession(new UserSession("admin"));
090
091        TravelerDetail newTravelerDetail = new TravelerDetail();
092        newTravelerDetail.setPrincipalId(PRINCIPAL_ID);
093        TRAVELER_DETAIL_ID = KRADServiceLocator.getDataObjectService().save(
094                newTravelerDetail, PersistenceOption.FLUSH).getId();
095
096        TravelDestination newTravelDestination = new TravelDestination();
097        newTravelDestination.setTravelDestinationName(DESTINATION_NAME);
098        newTravelDestination.setCountryCd(COUNTRY_CODE);
099        newTravelDestination.setStateCd(STATE_CODE);
100        TRAVEL_DESTINATION_ID = KRADServiceLocator.getDataObjectService().save(
101                newTravelDestination, PersistenceOption.FLUSH).getTravelDestinationId();
102
103        TravelMileageRate newTravelMileageRate = new TravelMileageRate();
104        newTravelMileageRate.setMileageRateCd(MILEAGE_RATE_CODE);
105        newTravelMileageRate.setMileageRateName(MILEAGE_RATE_NAME);
106        newTravelMileageRate.setMileageRate(MILEAGE_RATE);
107        MILEAGE_RATE_ID = KRADServiceLocator.getDataObjectService().save(
108                newTravelMileageRate, PersistenceOption.FLUSH).getMileageRateId();
109    }
110
111    @Override
112    public void tearDown() throws Exception {
113        GlobalVariables.setMessageMap(new MessageMap());
114        GlobalVariables.setUserSession(null);
115        super.tearDown();
116    }
117
118    /**
119     * Tests basic {@code TravelAuthorizationDocument} persistence by saving it, reloading it, and checking the data.
120     *
121     * @throws java.lang.Exception for any exceptions occurring during creation
122     */
123    @Test
124    public void testTravelExpenseItem() throws Exception {
125        assertTrue(TravelExpenseItem.class.getName() + " is not mapped in JPA",
126                KRADServiceLocator.getDataObjectService().supports(TravelAuthorizationDocument.class));
127
128        String id = createTravelAuthorizationDocument();
129
130        TravelAuthorizationDocument document = (TravelAuthorizationDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(id);
131        String documentNumber = document.getDocumentNumber();
132        assertNotNull("Travel Authorization Document is null", document);
133        assertNotNull("Travel Authorization Document ID is null", documentNumber);
134        assertEquals("Travel Authorization Document trip begin is incorrect", DATE_FORMAT.parse(TRIP_BEGIN), document.getTripBegin());
135        assertEquals("Travel Authorization Document trip end is incorrect", DATE_FORMAT.parse(TRIP_END), document.getTripEnd());
136        assertEquals("Travel Authorization Document trip description is incorrect", TRIP_DESCRIPTION, document.getTripDescription());
137        assertEquals("Travel Authorization Document trip destination ID is incorrect", TRAVEL_DESTINATION_ID, document.getTripDestinationId());
138        assertEquals("Travel Authorization Document traveler detail ID is incorrect", TRAVELER_DETAIL_ID, document.getTravelerDetailId());
139        assertEquals("Travel Authorization Document travel type code is incorrect", TRAVEL_TYPE_CODE, document.getTravelTypeCode());
140        assertEquals("Travel Authorization Document expense limit is incorrect", EXPENSE_LIMIT, document.getExpenseLimit());
141        assertEquals("Travel Authorization Document cell phone number is incorrect", CELL_PHONE_NUMBER, document.getCellPhoneNumber());
142
143        assertTrue("Travel Authorization Document daily expense estimates is empty", !document.getDailyExpenseEstimates().isEmpty());
144        TravelPerDiemExpense travelPerDiemExpense = document.getDailyExpenseEstimates().get(0);
145        String travelPerDiemExpenseDocumentNumber = travelPerDiemExpense.getTravelAuthorizationDocumentId();
146        assertEquals("Travel Per Diem Expense document ID is incorrect", travelPerDiemExpenseDocumentNumber, documentNumber);
147
148        assertTrue("Travel Authorization Document actual expense items is empty", !document.getActualExpenseItems().isEmpty());
149        TravelExpenseItem travelExpenseItem = document.getActualExpenseItems().get(0);
150        String travelExpenseItemDocumentNumber = travelExpenseItem.getTravelAuthorizationDocumentId();
151        assertEquals("Travel Expense Item document ID is incorrect", travelExpenseItemDocumentNumber, documentNumber);
152    }
153
154    private String createTravelAuthorizationDocument() throws Exception {
155        Document newDocument = KRADServiceLocatorWeb.getDocumentService().getNewDocument(TravelAuthorizationDocument.class);
156        newDocument.getDocumentHeader().setDocumentDescription(DOCUMENT_DESCRIPTION);
157        TravelAuthorizationDocument newTravelAuthorizationDocument = (TravelAuthorizationDocument) newDocument;
158        newTravelAuthorizationDocument.setTripBegin(new java.sql.Date(DATE_FORMAT.parse(TRIP_BEGIN).getTime()));
159        newTravelAuthorizationDocument.setTripEnd(new java.sql.Date(DATE_FORMAT.parse(TRIP_END).getTime()));
160        newTravelAuthorizationDocument.setTripDescription(TRIP_DESCRIPTION);
161        newTravelAuthorizationDocument.setTripDestinationId(TRAVEL_DESTINATION_ID);
162        newTravelAuthorizationDocument.setTravelerDetailId(TRAVELER_DETAIL_ID);
163        newTravelAuthorizationDocument.setTravelTypeCode(TRAVEL_TYPE_CODE);
164        newTravelAuthorizationDocument.setExpenseLimit(EXPENSE_LIMIT);
165        newTravelAuthorizationDocument.setCellPhoneNumber(CELL_PHONE_NUMBER);
166
167        TravelPerDiemExpense travelPerDiemExpense = new TravelPerDiemExpense();
168        travelPerDiemExpense.setTravelAuthorizationDocumentId(newTravelAuthorizationDocument.getDocumentNumber());
169        travelPerDiemExpense.setTravelDestinationId(TRAVEL_DESTINATION_ID);
170        travelPerDiemExpense.setPerDiemDate(DATE_FORMAT.parse(PER_DIEM_DATE));
171        travelPerDiemExpense.setBreakfastValue(BREAKFAST_VALUE);
172        travelPerDiemExpense.setLunchValue(LUNCH_VALUE);
173        travelPerDiemExpense.setDinnerValue(DINNER_VALUE);
174        travelPerDiemExpense.setIncidentalsValue(INCIDENTALS_VALUE);
175        travelPerDiemExpense.setMileageRateId(MILEAGE_RATE_ID);
176        travelPerDiemExpense.setEstimatedMileage(ESTIMATED_MILEAGE);
177        newTravelAuthorizationDocument.getDailyExpenseEstimates().add(travelPerDiemExpense);
178
179        TravelExpenseItem travelExpenseItem = new TravelExpenseItem();
180        travelExpenseItem.setTravelAuthorizationDocumentId(newTravelAuthorizationDocument.getDocumentNumber());
181        travelExpenseItem.setTravelExpenseTypeCd(EXPENSE_TYPE);
182        travelExpenseItem.setExpenseDesc(EXPENSE_DESCRIPTION);
183        travelExpenseItem.setTravelCompanyName(TRAVEL_COMPANY_NAME);
184        travelExpenseItem.setExpenseDate(DATE_FORMAT.parse(EXPENSE_DATE));
185        travelExpenseItem.setExpenseAmount(EXPENSE_AMOUNT);
186        travelExpenseItem.setReimbursable(true);
187        travelExpenseItem.setTaxable(false);
188        newTravelAuthorizationDocument.getActualExpenseItems().add(travelExpenseItem);
189
190        return KRADServiceLocatorWeb.getDocumentService().saveDocument(newTravelAuthorizationDocument).getDocumentNumber();
191    }
192}