View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.tem.document.service;
20  
21  import static org.kuali.kfs.sys.fixture.UserNameFixture.khuntley;
22  
23  import java.sql.Timestamp;
24  import java.util.List;
25  
26  import org.apache.log4j.Logger;
27  import org.junit.After;
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.kuali.kfs.module.ar.businessobject.Customer;
31  import org.kuali.kfs.module.tem.TemConstants;
32  import org.kuali.kfs.module.tem.businessobject.TravelPayment;
33  import org.kuali.kfs.module.tem.businessobject.TravelerDetail;
34  import org.kuali.kfs.module.tem.document.TravelEntertainmentDocument;
35  import org.kuali.kfs.module.tem.pdf.Coversheet;
36  import org.kuali.kfs.sys.ConfigureContext;
37  import org.kuali.kfs.sys.DocumentTestUtils;
38  import org.kuali.kfs.sys.context.KualiTestBase;
39  import org.kuali.kfs.sys.context.SpringContext;
40  import org.kuali.kfs.sys.document.validation.event.AccountingDocumentSaveWithNoLedgerEntryGenerationEvent;
41  import org.kuali.rice.kew.api.exception.WorkflowException;
42  import org.kuali.rice.krad.service.DocumentService;
43  
44  @ConfigureContext(session = khuntley)
45  public class TravelEntertainmentServiceTest extends KualiTestBase {
46      private static final int EXPENSE_AMOUNT = 100;
47      private TravelEntertainmentDocument ent = null;
48      private TravelerDetail traveler = null;
49  
50      private TravelEntertainmentDocumentService entservice;
51      private TravelDocumentService service;
52      private DocumentService documentService;
53  
54      private static final Logger LOG = Logger.getLogger(TravelEntertainmentServiceTest.class);
55  
56      @Override
57      @Before
58      public void setUp() throws Exception {
59          super.setUp();
60          // setup services
61          entservice = SpringContext.getBean(TravelEntertainmentDocumentService.class);
62          documentService = SpringContext.getBean(DocumentService.class);
63  
64          ent = DocumentTestUtils.createDocument(documentService, TravelEntertainmentDocument.class);
65          documentService.prepareWorkflowDocument(ent);
66  
67          // setup traveler
68          traveler = new TravelerDetail() {
69              @Override
70              public void refreshReferenceObject(String referenceObjectName) {
71                  // do nothing
72              }
73          };
74          traveler.setTravelerTypeCode(TemConstants.EMP_TRAVELER_TYP_CD);
75          traveler.setCustomer(new Customer());
76          ent.setTraveler(traveler);
77          ent.setHostAsPayee(true);
78          ent.setTravelPayment(new TravelPayment());
79          ent.getTravelPayment().setDocumentationLocationCode("N");
80      }
81  
82      @Override
83      @After
84      public void tearDown() throws Exception {
85          entservice = null;
86          super.tearDown();
87      }
88  
89      /**
90       * This method test {@link TravelEntertainmentDocumentService#findByTravelId(Integer)} using travelDocumentIdentifier
91       */
92      @Test
93      public void testFindByTravelDocumentIdentifier() throws WorkflowException {
94          documentService.saveDocument(ent, AccountingDocumentSaveWithNoLedgerEntryGenerationEvent.class);
95  
96          // test find for non existent travelDocumentIdentifier
97          List<TravelEntertainmentDocument> result = (List<TravelEntertainmentDocument>) entservice.findByTravelId("-1");
98          assertNotNull(result);
99          assertTrue(result.isEmpty());
100 
101         // test find for existing travelDocument
102         result = (List<TravelEntertainmentDocument>) entservice.findByTravelId(ent.getTravelDocumentIdentifier());
103         assertNotNull(result);
104         assertFalse(result.isEmpty());
105     }
106 
107     /**
108      * This method test {@link TravelEntertainmentDocumentService#find(String)} using documentNumber
109      */
110     @Test
111     public void testFindByDocumentNumber() throws WorkflowException {
112         // test find for non existent documentNumber
113         assertNull(entservice.find(ent.getDocumentHeader().getDocumentNumber()));
114 
115         // test find for existent documentNumber
116         documentService.saveDocument(ent, AccountingDocumentSaveWithNoLedgerEntryGenerationEvent.class);
117         assertNotNull(entservice.find(ent.getDocumentHeader().getDocumentNumber()));
118     }
119 
120     /**
121      * This method tests {@link TravelEntertainmentDocumentService#addListenersTo(TravelEntertainmentDocument)}
122      */
123     @Test
124     public void testAddListenersTo() {
125         boolean success = false;
126 
127         try {
128             entservice.addListenersTo(ent);
129             success = true;
130         }
131         catch (NullPointerException e) {
132             success = false;
133             LOG.warn("NPE.", e);
134         }
135 
136         assertTrue(success);
137     }
138 
139 
140 
141     /**
142      * This method tests {@link TravelEntertainmentDocumentService#calculateTotalsFor(TravelEntertainmentDocument)}
143      *
144      * @throws WorkflowException
145 
146     @Test
147     public void testCalculateTotalsFor() throws WorkflowException {
148         ent = new TravelEntertainmentDocument();
149 
150         traveler.setTravelerTypeCode(TemConstants.EMP_TRAVELER_TYP_CD);
151         ent.setTraveler(traveler);
152 
153         List<ActualExpense> oteList = new ArrayList<ActualExpense>();
154         ent.setActualExpenses(oteList);
155         ent.setPerDiemExpenses(new ArrayList<PerDiemExpense>());
156 
157         // test with empty actualExpenseList
158         Map<String, Object> resultMap = service.calculateTotalsFor(ent);
159         assertTrue(((KualiDecimal) resultMap.get(TemConstants.NON_REIMBURSABLE_ATTRIBUTE)).equals(KualiDecimal.ZERO));
160 
161         // Override refreshReferenceObject - setting travelExpenseTypeCode manually
162         ActualExpense ote = new ActualExpense() {
163             public void refreshReferenceObject(String referenceObjectName) {
164                 // do nothing;
165             }
166         };
167         ote.setExpenseAmount(new KualiDecimal(EXPENSE_AMOUNT));
168         ote.setNonReimbursable(true);
169         //ote.setTravelExpenseTypeCode(new TemTravelExpenseTypeCode());
170         oteList.add(ote);
171 
172         ent.setActualExpenses(oteList);
173 
174         // test with non-reimbursable other expense
175         resultMap = service.calculateTotalsFor(ent);
176         assertTrue(((KualiDecimal) resultMap.get(TemConstants.NON_REIMBURSABLE_ATTRIBUTE)).equals(new KualiDecimal(EXPENSE_AMOUNT)));
177     }
178      */
179 
180 
181     /**
182      * This method tests {@link TravelEntertainmentDocumentService#generateCoversheetFor(TravelEntertainmentDocument)}
183      *
184      * @throws Exception
185      */
186     @Test
187     public void testGenerateCoversheetFor() throws Exception {
188         Coversheet cover = null;
189 
190         try {
191             cover = entservice.generateCoversheetFor(new TravelEntertainmentDocument());
192         }
193         catch (RuntimeException e) {
194             LOG.warn("Workflow doc is null.", e);
195         }
196 
197         assertNull(cover);
198 
199         ent.setTripBegin(new Timestamp(new java.util.Date().getTime()));
200         ent.setTripEnd(new Timestamp(new java.util.Date().getTime()));
201 
202         cover = entservice.generateCoversheetFor(ent);
203         assertNotNull(cover);
204     }
205 }