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.ExpenseType;
19  import edu.sampleu.travel.options.PostalCountryCode;
20  import edu.sampleu.travel.options.PostalStateCode;
21  import org.junit.Test;
22  import org.kuali.rice.krad.UserSession;
23  import org.kuali.rice.krad.data.PersistenceOption;
24  import org.kuali.rice.krad.document.Document;
25  import org.kuali.rice.krad.service.KRADServiceLocator;
26  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
27  import org.kuali.rice.krad.test.KRADTestCase;
28  import org.kuali.rice.krad.util.GlobalVariables;
29  import org.kuali.rice.krad.util.MessageMap;
30  import org.kuali.rice.test.BaselineTestCase;
31  
32  import java.math.BigDecimal;
33  import java.text.SimpleDateFormat;
34  
35  import static org.junit.Assert.assertEquals;
36  import static org.junit.Assert.assertFalse;
37  import static org.junit.Assert.assertNotNull;
38  import static org.junit.Assert.assertTrue;
39  
40  /**
41   * Tests basic {@code TravelExpenseItem} persistence.
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
46  public class TravelExpenseItemTest extends KRADTestCase {
47  
48      private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
49  
50      private static String DOCUMENT_NUMBER;
51      private static final String DOCUMENT_DESCRIPTION = "Test Travel Authorization Document";
52      private static final String CELL_PHONE_NUMBER = "555-555-5555";
53  
54      private static String TRAVEL_DESTINATION_ID;
55      private static final String TRAVEL_COMPANY_NAME = "Zorba's Travel";
56      private static final String DESTINATION_NAME = PostalStateCode.CA.getLabel();
57      private static final String COUNTRY_CODE = PostalCountryCode.US.getCode();
58      private static final String STATE_CODE = PostalStateCode.CA.getCode();
59  
60      private static final String EXPENSE_TYPE = ExpenseType.A.getCode();
61      private static final String EXPENSE_DESCRIPTION = ExpenseType.A.getLabel();
62      private static final String EXPENSE_DATE = "2010-01-01";
63      private static final BigDecimal EXPENSE_AMOUNT = new BigDecimal("1236.49");
64  
65      @Override
66      public void setUp() throws Exception {
67          super.setUp();
68          GlobalVariables.setMessageMap(new MessageMap());
69          GlobalVariables.setUserSession(new UserSession("admin"));
70  
71          TravelDestination newTravelDestination = new TravelDestination();
72          newTravelDestination.setTravelDestinationName(DESTINATION_NAME);
73          newTravelDestination.setCountryCd(COUNTRY_CODE);
74          newTravelDestination.setStateCd(STATE_CODE);
75          TRAVEL_DESTINATION_ID = KRADServiceLocator.getDataObjectService().save(
76                  newTravelDestination, PersistenceOption.FLUSH).getTravelDestinationId();
77  
78          Document newDocument = KRADServiceLocatorWeb.getDocumentService().getNewDocument(TravelAuthorizationDocument.class);
79          newDocument.getDocumentHeader().setDocumentDescription(DOCUMENT_DESCRIPTION);
80          TravelAuthorizationDocument newTravelAuthorizationDocument = (TravelAuthorizationDocument) newDocument;
81          newTravelAuthorizationDocument.setCellPhoneNumber(CELL_PHONE_NUMBER);
82          newTravelAuthorizationDocument.setTripDestinationId(TRAVEL_DESTINATION_ID);
83          DOCUMENT_NUMBER = KRADServiceLocatorWeb.getDocumentService().saveDocument(
84                  newTravelAuthorizationDocument).getDocumentNumber();
85      }
86  
87      @Override
88      public void tearDown() throws Exception {
89          GlobalVariables.setMessageMap(new MessageMap());
90          GlobalVariables.setUserSession(null);
91          super.tearDown();
92      }
93  
94      /**
95       * Tests basic {@code TravelExpenseItem} persistence by saving it, reloading it, and checking the data.
96       *
97       * @throws java.lang.Exception for any exceptions occurring during creation
98       */
99      @Test
100     public void testTravelExpenseItem() throws Exception {
101         assertTrue(TravelExpenseItem.class.getName() + " is not mapped in JPA",
102                 KRADServiceLocator.getDataObjectService().supports(TravelExpenseItem.class));
103 
104         String id = createTravelExpenseItem();
105 
106         TravelExpenseItem travelExpenseItem = KRADServiceLocator.getDataObjectService().find(TravelExpenseItem.class, id);
107         assertNotNull("Travel Expense Item ID is null", travelExpenseItem.getTravelExpenseItemId());
108         assertEquals("Travel Expense Item document ID is incorrect", DOCUMENT_NUMBER, travelExpenseItem.getTravelAuthorizationDocumentId());
109         assertEquals("Travel Expense Item type is incorrect", EXPENSE_TYPE, travelExpenseItem.getTravelExpenseTypeCd());
110         assertEquals("Travel Company name is incorrect", TRAVEL_COMPANY_NAME, travelExpenseItem.getTravelCompanyName());
111         assertEquals("Travel Expense Item description is incorrect", EXPENSE_DESCRIPTION, travelExpenseItem.getExpenseDesc());
112         assertEquals("Travel Expense Item date is incorrect", DATE_FORMAT.parse(EXPENSE_DATE), travelExpenseItem.getExpenseDate());
113         assertEquals("Travel Expense Item amount is incorrect", EXPENSE_AMOUNT, travelExpenseItem.getExpenseAmount());
114         assertTrue("Travel Expense Item is not reimbursable", travelExpenseItem.isReimbursable());
115         assertFalse("Travel Expense Item is taxable", travelExpenseItem.isTaxable());
116     }
117 
118     private String createTravelExpenseItem() throws Exception {
119         TravelExpenseItem travelExpenseItem = new TravelExpenseItem();
120         travelExpenseItem.setTravelAuthorizationDocumentId(DOCUMENT_NUMBER);
121         travelExpenseItem.setTravelExpenseTypeCd(EXPENSE_TYPE);
122         travelExpenseItem.setExpenseDesc(EXPENSE_DESCRIPTION);
123         travelExpenseItem.setTravelCompanyName(TRAVEL_COMPANY_NAME);
124         travelExpenseItem.setExpenseDate(DATE_FORMAT.parse(EXPENSE_DATE));
125         travelExpenseItem.setExpenseAmount(EXPENSE_AMOUNT);
126         travelExpenseItem.setReimbursable(true);
127         travelExpenseItem.setTaxable(false);
128 
129         return KRADServiceLocator.getDataObjectService().save(travelExpenseItem, PersistenceOption.FLUSH).getTravelExpenseItemId();
130     }
131 }