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 edu.sampleu.travel.options.PostalCountryCode; 019import edu.sampleu.travel.options.PostalStateCode; 020import org.junit.Test; 021import org.kuali.rice.krad.UserSession; 022import org.kuali.rice.krad.data.PersistenceOption; 023import org.kuali.rice.krad.document.Document; 024import org.kuali.rice.krad.service.KRADServiceLocator; 025import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 026import org.kuali.rice.krad.test.KRADTestCase; 027import org.kuali.rice.krad.util.GlobalVariables; 028import org.kuali.rice.krad.util.MessageMap; 029import org.kuali.rice.test.BaselineTestCase; 030 031import java.math.BigDecimal; 032import java.text.SimpleDateFormat; 033 034import static org.junit.Assert.assertEquals; 035import static org.junit.Assert.assertNotNull; 036import static org.junit.Assert.assertTrue; 037 038/** 039 * Tests basic {@code TravelPerDiemExpense} persistence. 040 * 041 * @author Kuali Rice Team (rice.collab@kuali.org) 042 */ 043@BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB) 044public class TravelPerDiemExpenseTest extends KRADTestCase { 045 046 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); 047 048 private static String DOCUMENT_NUMBER; 049 private static final String DOCUMENT_DESCRIPTION = "Test Travel Authorization Document"; 050 private static final String CELL_PHONE_NUMBER = "555-555-5555"; 051 052 private static String TRAVEL_DESTINATION_ID; 053 private static final String DESTINATION_NAME = PostalStateCode.CA.getLabel(); 054 private static final String COUNTRY_CODE = PostalCountryCode.US.getCode(); 055 private static final String STATE_CODE = PostalStateCode.CA.getCode(); 056 057 private static String MILEAGE_RATE_ID; 058 private static final String MILEAGE_RATE_CODE = "DO"; 059 private static final String MILEAGE_RATE_NAME = "Domestic"; 060 private static final BigDecimal MILEAGE_RATE = new BigDecimal("30"); 061 062 private static final String PER_DIEM_DATE = "2010-01-01"; 063 private static final BigDecimal BREAKFAST_VALUE = new BigDecimal("15.00"); 064 private static final BigDecimal LUNCH_VALUE = new BigDecimal("30.00"); 065 private static final BigDecimal DINNER_VALUE = new BigDecimal("45.00"); 066 private static final BigDecimal INCIDENTALS_VALUE = new BigDecimal("15.00"); 067 private static final BigDecimal ESTIMATED_MILEAGE = new BigDecimal("50"); 068 069 @Override 070 public void setUp() throws Exception { 071 super.setUp(); 072 GlobalVariables.setMessageMap(new MessageMap()); 073 GlobalVariables.setUserSession(new UserSession("admin")); 074 075 TravelDestination newTravelDestination = new TravelDestination(); 076 newTravelDestination.setTravelDestinationName(DESTINATION_NAME); 077 newTravelDestination.setCountryCd(COUNTRY_CODE); 078 newTravelDestination.setStateCd(STATE_CODE); 079 TRAVEL_DESTINATION_ID = KRADServiceLocator.getDataObjectService().save( 080 newTravelDestination, PersistenceOption.FLUSH).getTravelDestinationId(); 081 082 TravelMileageRate newTravelMileageRate = new TravelMileageRate(); 083 newTravelMileageRate.setMileageRateCd(MILEAGE_RATE_CODE); 084 newTravelMileageRate.setMileageRateName(MILEAGE_RATE_NAME); 085 newTravelMileageRate.setMileageRate(MILEAGE_RATE); 086 MILEAGE_RATE_ID = KRADServiceLocator.getDataObjectService().save( 087 newTravelMileageRate, PersistenceOption.FLUSH).getMileageRateId(); 088 089 Document newDocument = KRADServiceLocatorWeb.getDocumentService().getNewDocument(TravelAuthorizationDocument.class); 090 newDocument.getDocumentHeader().setDocumentDescription(DOCUMENT_DESCRIPTION); 091 TravelAuthorizationDocument newTravelAuthorizationDocument = (TravelAuthorizationDocument) newDocument; 092 newTravelAuthorizationDocument.setCellPhoneNumber(CELL_PHONE_NUMBER); 093 newTravelAuthorizationDocument.setTripDestinationId(TRAVEL_DESTINATION_ID); 094 DOCUMENT_NUMBER = KRADServiceLocatorWeb.getDocumentService().saveDocument( 095 newTravelAuthorizationDocument).getDocumentNumber(); 096 } 097 098 @Override 099 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}