1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package edu.sampleu.travel.dataobject;
17
18 import edu.sampleu.travel.options.ExpenseType;
19 import edu.sampleu.travel.options.PostalCountryCode;
20 import edu.sampleu.travel.options.PostalStateCode;
21 import edu.sampleu.travel.options.TripType;
22 import org.junit.Test;
23 import org.kuali.rice.core.api.util.type.KualiDecimal;
24 import org.kuali.rice.krad.UserSession;
25 import org.kuali.rice.krad.data.PersistenceOption;
26 import org.kuali.rice.krad.document.Document;
27 import org.kuali.rice.krad.service.KRADServiceLocator;
28 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
29 import org.kuali.rice.krad.test.KRADTestCase;
30 import org.kuali.rice.krad.util.GlobalVariables;
31 import org.kuali.rice.krad.util.MessageMap;
32 import org.kuali.rice.test.BaselineTestCase;
33
34 import java.math.BigDecimal;
35 import java.text.SimpleDateFormat;
36
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertNotNull;
39 import static org.junit.Assert.assertTrue;
40
41
42
43
44
45
46 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
47 public class TravelAuthorizationDocumentTest extends KRADTestCase {
48
49 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
50
51 private static final String DOCUMENT_DESCRIPTION = "Test Travel Authorization Document";
52 private static final String TRIP_BEGIN = "2010-01-01";
53 private static final String TRIP_END = "2010-02-01";
54 private static final String TRIP_DESCRIPTION = "Test Trip";
55 private static final String TRAVEL_TYPE_CODE = TripType.OS.getCode();
56 private static final KualiDecimal EXPENSE_LIMIT = new KualiDecimal(10000);
57 private static final String CELL_PHONE_NUMBER = "555-555-5555";
58
59 private static String TRAVELER_DETAIL_ID;
60 private static final String PRINCIPAL_ID = "admin";
61
62 private static String TRAVEL_DESTINATION_ID;
63 private static final String DESTINATION_NAME = PostalStateCode.CA.getLabel();
64 private static final String COUNTRY_CODE = PostalCountryCode.US.getCode();
65 private static final String STATE_CODE = PostalStateCode.CA.getCode();
66
67 private static String MILEAGE_RATE_ID;
68 private static final String MILEAGE_RATE_CODE = "DO";
69 private static final String MILEAGE_RATE_NAME = "Domestic";
70 private static final BigDecimal MILEAGE_RATE = new BigDecimal("1994.88");
71
72 private static final String PER_DIEM_DATE = "2010-01-01";
73 private static final BigDecimal BREAKFAST_VALUE = new BigDecimal("15.00");
74 private static final BigDecimal LUNCH_VALUE = new BigDecimal("30.00");
75 private static final BigDecimal DINNER_VALUE = new BigDecimal("45.00");
76 private static final BigDecimal INCIDENTALS_VALUE = new BigDecimal("15.00");
77 private static final BigDecimal ESTIMATED_MILEAGE = new BigDecimal("50");
78
79 private static final String EXPENSE_TYPE = ExpenseType.A.getCode();
80 private static final String EXPENSE_DESCRIPTION = ExpenseType.A.getLabel();
81 private static final String TRAVEL_COMPANY_NAME = "Zorba's Travel";
82 private static final String EXPENSE_DATE = "2010-01-01";
83 private static final BigDecimal EXPENSE_AMOUNT = new BigDecimal("1236.49");
84
85 @Override
86 public void setUp() throws Exception {
87 super.setUp();
88 GlobalVariables.setMessageMap(new MessageMap());
89 GlobalVariables.setUserSession(new UserSession("admin"));
90
91 TravelerDetail newTravelerDetail = new TravelerDetail();
92 newTravelerDetail.setPrincipalId(PRINCIPAL_ID);
93 TRAVELER_DETAIL_ID = KRADServiceLocator.getDataObjectService().save(
94 newTravelerDetail, PersistenceOption.FLUSH).getId();
95
96 TravelDestination newTravelDestination = new TravelDestination();
97 newTravelDestination.setTravelDestinationName(DESTINATION_NAME);
98 newTravelDestination.setCountryCd(COUNTRY_CODE);
99 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
120
121
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 }