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 edu.sampleu.travel.options.PostalCountryCode;
19  import edu.sampleu.travel.options.PostalStateCode;
20  import org.junit.Test;
21  import org.kuali.rice.krad.UserSession;
22  import org.kuali.rice.krad.data.PersistenceOption;
23  import org.kuali.rice.krad.document.Document;
24  import org.kuali.rice.krad.service.KRADServiceLocator;
25  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
26  import org.kuali.rice.krad.test.KRADTestCase;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  import org.kuali.rice.krad.util.MessageMap;
29  import org.kuali.rice.test.BaselineTestCase;
30  
31  import java.math.BigDecimal;
32  import java.text.SimpleDateFormat;
33  
34  import static org.junit.Assert.assertEquals;
35  import static org.junit.Assert.assertNotNull;
36  import static org.junit.Assert.assertTrue;
37  
38  /**
39   * Tests basic {@code TravelPerDiemExpense} persistence.
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
44  public class TravelPerDiemExpenseTest extends KRADTestCase {
45  
46      private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
47  
48      private static String DOCUMENT_NUMBER;
49      private static final String DOCUMENT_DESCRIPTION = "Test Travel Authorization Document";
50      private static final String CELL_PHONE_NUMBER = "555-555-5555";
51  
52      private static String TRAVEL_DESTINATION_ID;
53      private static final String DESTINATION_NAME = PostalStateCode.CA.getLabel();
54      private static final String COUNTRY_CODE = PostalCountryCode.US.getCode();
55      private static final String STATE_CODE = PostalStateCode.CA.getCode();
56  
57      private static String MILEAGE_RATE_ID;
58      private static final String MILEAGE_RATE_CODE = "DO";
59      private static final String MILEAGE_RATE_NAME = "Domestic";
60      private static final BigDecimal MILEAGE_RATE = new BigDecimal("30");
61  
62      private static final String PER_DIEM_DATE = "2010-01-01";
63      private static final BigDecimal BREAKFAST_VALUE = new BigDecimal("15.00");
64      private static final BigDecimal LUNCH_VALUE = new BigDecimal("30.00");
65      private static final BigDecimal DINNER_VALUE = new BigDecimal("45.00");
66      private static final BigDecimal INCIDENTALS_VALUE = new BigDecimal("15.00");
67      private static final BigDecimal ESTIMATED_MILEAGE = new BigDecimal("50");
68  
69      @Override
70      public void setUp() throws Exception {
71          super.setUp();
72          GlobalVariables.setMessageMap(new MessageMap());
73          GlobalVariables.setUserSession(new UserSession("admin"));
74  
75          TravelDestination newTravelDestination = new TravelDestination();
76          newTravelDestination.setTravelDestinationName(DESTINATION_NAME);
77          newTravelDestination.setCountryCd(COUNTRY_CODE);
78          newTravelDestination.setStateCd(STATE_CODE);
79          TRAVEL_DESTINATION_ID = KRADServiceLocator.getDataObjectService().save(
80                  newTravelDestination, PersistenceOption.FLUSH).getTravelDestinationId();
81  
82          TravelMileageRate newTravelMileageRate = new TravelMileageRate();
83          newTravelMileageRate.setMileageRateCd(MILEAGE_RATE_CODE);
84          newTravelMileageRate.setMileageRateName(MILEAGE_RATE_NAME);
85          newTravelMileageRate.setMileageRate(MILEAGE_RATE);
86          MILEAGE_RATE_ID = KRADServiceLocator.getDataObjectService().save(
87                  newTravelMileageRate, PersistenceOption.FLUSH).getMileageRateId();
88  
89          Document newDocument = KRADServiceLocatorWeb.getDocumentService().getNewDocument(TravelAuthorizationDocument.class);
90          newDocument.getDocumentHeader().setDocumentDescription(DOCUMENT_DESCRIPTION);
91          TravelAuthorizationDocument newTravelAuthorizationDocument = (TravelAuthorizationDocument) newDocument;
92          newTravelAuthorizationDocument.setCellPhoneNumber(CELL_PHONE_NUMBER);
93          newTravelAuthorizationDocument.setTripDestinationId(TRAVEL_DESTINATION_ID);
94          DOCUMENT_NUMBER = KRADServiceLocatorWeb.getDocumentService().saveDocument(
95                  newTravelAuthorizationDocument).getDocumentNumber();
96      }
97  
98      @Override
99      public void tearDown() throws Exception {
100         GlobalVariables.setMessageMap(new MessageMap());
101         GlobalVariables.setUserSession(null);
102         super.tearDown();
103     }
104 
105     /**
106      * Tests basic {@code TravelPerDiemExpense} persistence by saving it, reloading it, and checking the data.
107      *
108      * @throws java.lang.Exception for any exceptions occurring during creation
109      */
110     @Test
111     public void testTravelPerDiemExpense() throws Exception {
112         assertTrue(TravelPerDiemExpense.class.getName() + " is not mapped in JPA",
113                 KRADServiceLocator.getDataObjectService().supports(TravelPerDiemExpense.class));
114 
115         String id = createTravelPerDiemExpense();
116 
117         TravelPerDiemExpense travelPerDiemExpense = KRADServiceLocator.getDataObjectService().find(TravelPerDiemExpense.class, id);
118 
119         assertNotNull("Travel Per Diem Expense ID is null", travelPerDiemExpense.getTravelPerDiemExpenseId());
120         assertEquals("Travel Per Diem Expense document ID is incorrect", DOCUMENT_NUMBER, travelPerDiemExpense.getTravelAuthorizationDocumentId());
121         assertEquals("Travel Per Diem Expense destination ID is incorrect", TRAVEL_DESTINATION_ID, travelPerDiemExpense.getTravelDestinationId());
122         assertEquals("Travel Per Diem Expense date is incorrect", DATE_FORMAT.parse(PER_DIEM_DATE), travelPerDiemExpense.getPerDiemDate());
123         assertEquals("Travel Per Diem Expense breakfast value is incorrect", BREAKFAST_VALUE, travelPerDiemExpense.getBreakfastValue());
124         assertEquals("Travel Per Diem Expense lunch value is incorrect", LUNCH_VALUE, travelPerDiemExpense.getLunchValue());
125         assertEquals("Travel Per Diem Expense dinner value is incorrect", DINNER_VALUE, travelPerDiemExpense.getDinnerValue());
126         assertEquals("Travel Per Diem Expense incidentals value is incorrect", INCIDENTALS_VALUE, travelPerDiemExpense.getIncidentalsValue());
127         assertEquals("Travel Per Diem Expense mileage rate ID is incorrect", MILEAGE_RATE_ID, travelPerDiemExpense.getMileageRateId());
128         assertEquals("Travel Per Diem Expense estimated mileage is incorrect", ESTIMATED_MILEAGE, travelPerDiemExpense.getEstimatedMileage());
129     }
130 
131     private String createTravelPerDiemExpense() throws Exception {
132         TravelPerDiemExpense travelPerDiemExpense = new TravelPerDiemExpense();
133         travelPerDiemExpense.setTravelAuthorizationDocumentId(DOCUMENT_NUMBER);
134         travelPerDiemExpense.setTravelDestinationId(TRAVEL_DESTINATION_ID);
135         travelPerDiemExpense.setPerDiemDate(DATE_FORMAT.parse(PER_DIEM_DATE));
136         travelPerDiemExpense.setBreakfastValue(BREAKFAST_VALUE);
137         travelPerDiemExpense.setLunchValue(LUNCH_VALUE);
138         travelPerDiemExpense.setDinnerValue(DINNER_VALUE);
139         travelPerDiemExpense.setIncidentalsValue(INCIDENTALS_VALUE);
140         travelPerDiemExpense.setMileageRateId(MILEAGE_RATE_ID);
141         travelPerDiemExpense.setEstimatedMileage(ESTIMATED_MILEAGE);
142 
143         return KRADServiceLocator.getDataObjectService().save(travelPerDiemExpense, PersistenceOption.FLUSH).getTravelPerDiemExpenseId();
144     }
145 }