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