View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package edu.sampleu.travel.dataobject;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.math.BigDecimal;
23  import java.text.SimpleDateFormat;
24  
25  import org.junit.Test;
26  import org.kuali.rice.core.api.util.type.KualiDecimal;
27  import org.kuali.rice.krad.UserSession;
28  import org.kuali.rice.krad.data.PersistenceOption;
29  import org.kuali.rice.krad.document.Document;
30  import org.kuali.rice.krad.service.KRADServiceLocator;
31  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
32  import org.kuali.rice.krad.test.KRADTestCase;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  import org.kuali.rice.krad.util.MessageMap;
35  import org.kuali.rice.test.BaselineTestCase;
36  
37  import edu.sampleu.travel.options.ExpenseType;
38  import edu.sampleu.travel.options.PostalCountryCode;
39  import edu.sampleu.travel.options.PostalStateCode;
40  import edu.sampleu.travel.options.TripType;
41  
42  /**
43   * Tests basic {@code TravelAuthorizationDocument} persistence.
44   *
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   */
47  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
48  //@PerTestUnitTestData(
49  //        value = @UnitTestData(
50  //        sqlStatements = {
51  //                @UnitTestSql("create table TRVL_AUTH_DOC_EXT_T ( DOC_HDR_ID VARCHAR(14), ANOTHER_PROP VARCHAR(10) )")
52  //        }
53  //    ),
54  //    tearDown = @UnitTestData(
55  //            sqlStatements = {
56  //                    @UnitTestSql("drop table TRVL_AUTH_DOC_EXT_T")
57  //            }
58  //            )
59  //)
60  public class TravelAuthorizationDocumentTest extends KRADTestCase {
61  
62      private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
63  
64      private static final String DOCUMENT_DESCRIPTION = "Test Travel Authorization Document";
65      private static final String TRIP_BEGIN = "2010-01-01";
66      private static final String TRIP_END = "2010-02-01";
67      private static final String TRIP_DESCRIPTION = "Test Trip";
68      private static final String TRAVEL_TYPE_CODE = TripType.OS.getCode();
69      private static final KualiDecimal EXPENSE_LIMIT = new KualiDecimal(10000);
70      private static final String CELL_PHONE_NUMBER = "555-555-5555";
71  
72      private static String TRAVELER_DETAIL_ID;
73      private static final String PRINCIPAL_ID = "admin";
74  
75      private static String TRAVEL_DESTINATION_ID;
76      private static final String DESTINATION_NAME = PostalStateCode.CA.getLabel();
77      private static final String COUNTRY_CODE = PostalCountryCode.US.getCode();
78      private static final String STATE_CODE = PostalStateCode.CA.getCode();
79  
80      private static String MILEAGE_RATE_ID;
81      private static final String MILEAGE_RATE_CODE = "DO";
82      private static final String MILEAGE_RATE_NAME = "Domestic";
83      private static final BigDecimal MILEAGE_RATE = new BigDecimal("1994.88");
84  
85      private static final String PER_DIEM_DATE = "2010-01-01";
86      private static final BigDecimal BREAKFAST_VALUE = new BigDecimal("15.00");
87      private static final BigDecimal LUNCH_VALUE = new BigDecimal("30.00");
88      private static final BigDecimal DINNER_VALUE = new BigDecimal("45.00");
89      private static final BigDecimal INCIDENTALS_VALUE = new BigDecimal("15.00");
90      private static final BigDecimal ESTIMATED_MILEAGE = new BigDecimal("50");
91  
92      private static final String EXPENSE_TYPE = ExpenseType.A.getCode();
93      private static final String EXPENSE_DESCRIPTION = ExpenseType.A.getLabel();
94      private static final String TRAVEL_COMPANY_NAME = "Zorba's Travel";
95      private static final String EXPENSE_DATE = "2010-01-01";
96      private static final BigDecimal EXPENSE_AMOUNT = new BigDecimal("1236.49");
97  
98      @Override
99      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 }