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.ld.businessobject.lookup;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Properties;
26  
27  import org.kuali.kfs.gl.Constant;
28  import org.kuali.kfs.gl.web.TestDataGenerator;
29  import org.kuali.kfs.module.ld.LaborConstants;
30  import org.kuali.kfs.module.ld.businessobject.AccountStatusCurrentFunds;
31  import org.kuali.kfs.module.ld.businessobject.LedgerBalance;
32  import org.kuali.kfs.module.ld.service.LaborInquiryOptionsService;
33  import org.kuali.kfs.sys.ConfigureContext;
34  import org.kuali.kfs.sys.KFSPropertyConstants;
35  import org.kuali.kfs.sys.ObjectUtil;
36  import org.kuali.kfs.sys.businessobject.lookup.LookupableSpringContext;
37  import org.kuali.kfs.sys.context.KualiTestBase;
38  import org.kuali.kfs.sys.context.SpringContext;
39  import org.kuali.kfs.sys.context.TestUtils;
40  import org.kuali.rice.kns.lookup.LookupableHelperService;
41  import org.kuali.rice.krad.service.BusinessObjectService;
42  import org.kuali.rice.krad.service.PersistenceService;
43  
44  /**
45   * This class contains test cases that can be applied to methods in Account Status Current Funds class.
46   */
47  @ConfigureContext
48  public class CurrentFundsLookupableHelperServiceTest extends KualiTestBase {
49      private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(CurrentFundsLookupableHelperServiceTest.class);
50      private BusinessObjectService businessObjectService;
51      private LookupableHelperService lookupableHelperService;
52      private PersistenceService persistenceService;
53  
54      private Properties properties;
55      private String fieldNames, documentFieldNames;
56      private String deliminator;
57      private int currentFundsNumberOfTestData;
58      private int currentFundsExpectedInsertion;
59  
60      /**
61       * Get ready for the test
62       */
63      @Override
64      protected void setUp() throws Exception {
65          super.setUp();
66  
67          businessObjectService = SpringContext.getBean(BusinessObjectService.class);
68  
69          lookupableHelperService = LookupableSpringContext.getLookupableHelperService(LaborConstants.CURRENT_FUNDS_LOOKUP_HELPER_SRVICE_NAME);
70          lookupableHelperService.setBusinessObjectClass(AccountStatusCurrentFunds.class);
71  
72          // Clear up the data so that any existing data cannot affact your test result
73          Map keys = new HashMap();
74          keys.put(KFSPropertyConstants.ACCOUNT_NUMBER, "6044906");
75          keys.put(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, TestUtils.getFiscalYearForTesting().toString());
76          keys.put(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, "BA");
77          businessObjectService.deleteMatching(LedgerBalance.class, keys);
78      }
79  
80      /**
81       *
82       * This method will run the current funds balance inquiry to test that the CurrentFundsLookupableHelperService
83       * is returning data correctly.
84       * @throws Exception
85       */
86      public void testGetSearchResults() throws Exception {
87          insertCurrentFundsRecords();
88          AccountStatusCurrentFunds accountStatusCurrentFunds = new AccountStatusCurrentFunds();
89          accountStatusCurrentFunds.setAccountNumber("6044906");
90          accountStatusCurrentFunds.setUniversityFiscalYear(TestUtils.getFiscalYearForTesting());
91          accountStatusCurrentFunds.setChartOfAccountsCode("BA");
92  
93          // test the search results before the specified entry is inserted
94          Map fieldValues = buildFieldValues(accountStatusCurrentFunds, this.getLookupFields(false));
95  
96          // Tells the lookupable I want detailed results
97          getInquiryOptionsService().getConsolidationField(lookupableHelperService.getRows()).setPropertyValue(Constant.DETAIL);
98          fieldValues.put(Constant.CONSOLIDATION_OPTION, Constant.DETAIL);
99  
100         List<String> groupByList = new ArrayList<String>();
101         List<AccountStatusCurrentFunds> searchResults = (List<AccountStatusCurrentFunds>) lookupableHelperService.getSearchResults(fieldValues);
102 
103         // Make sure the basic search parameters are returned from the inquiry
104         for (AccountStatusCurrentFunds accountStatusCurrentFundsReturn : searchResults) {
105               assertTrue((accountStatusCurrentFundsReturn.getAccountNumber().equals(accountStatusCurrentFunds.getAccountNumber()) &&
106               accountStatusCurrentFundsReturn.getUniversityFiscalYear().equals(accountStatusCurrentFunds.getUniversityFiscalYear()) &&
107               accountStatusCurrentFundsReturn.getChartOfAccountsCode().equals(accountStatusCurrentFunds.getChartOfAccountsCode())));
108         }
109 
110         if (searchResults != null) {
111             System.out.println("Results Size:" + searchResults.size());
112         }
113 
114         // compare the search results with the expected and see if they match with each other
115         assertEquals(this.currentFundsExpectedInsertion,searchResults.size());
116     }
117 
118     /**
119      *
120      * This method will run the current funds balance inquiry to test that the CurrentFundsLookupableHelperService
121      * is returning data correctly.
122      * @throws Exception
123      */
124     public void testGetSearchResultsConsolidated() throws Exception {
125         insertCurrentFundsRecords();
126         AccountStatusCurrentFunds accountStatusCurrentFunds = new AccountStatusCurrentFunds();
127         accountStatusCurrentFunds.setAccountNumber("6044906");
128         accountStatusCurrentFunds.setUniversityFiscalYear(TestUtils.getFiscalYearForTesting());
129         accountStatusCurrentFunds.setChartOfAccountsCode("BA");
130 
131         // test the search results before the specified entry is inserted
132         Map fieldValues = buildFieldValues(accountStatusCurrentFunds, this.getLookupFields(false));
133 
134         // Tells the lookupable I want consolidated results
135         getInquiryOptionsService().getConsolidationField(lookupableHelperService.getRows()).setPropertyValue(Constant.CONSOLIDATION);
136         fieldValues.put(Constant.CONSOLIDATION_OPTION, Constant.CONSOLIDATION);
137 
138         List<String> groupByList = new ArrayList<String>();
139         List<AccountStatusCurrentFunds> searchResults = (List<AccountStatusCurrentFunds>) lookupableHelperService.getSearchResults(fieldValues);
140 
141         // Make sure the basic search parameters are returned from the inquiry
142         for (AccountStatusCurrentFunds accountStatusCurrentFundsReturn : searchResults) {
143               assertFalse(!(accountStatusCurrentFundsReturn.getAccountNumber().equals(accountStatusCurrentFunds.getAccountNumber()) &&
144               accountStatusCurrentFundsReturn.getUniversityFiscalYear().equals(accountStatusCurrentFunds.getUniversityFiscalYear()) &&
145               accountStatusCurrentFundsReturn.getChartOfAccountsCode().equals(accountStatusCurrentFunds.getChartOfAccountsCode())));
146         }
147 
148         if (searchResults != null) {
149             LOG.debug("Results Size:" + searchResults.size());
150         }
151 
152         // compare the search results with the expected and see if they match with each other
153         assertEquals(this.currentFundsExpectedInsertion,searchResults.size());
154     }
155 
156     /**
157      *
158      * This method uses property file parameters to create insert datacurrent records for this test
159      * @param accountStatusCurrentFunds
160      * @param lookupFields
161      * @return
162      */
163     private Map<String, String> buildFieldValues(AccountStatusCurrentFunds accountStatusCurrentFunds, List<String> lookupFields) {
164         Map<String, String> fieldValues = new HashMap<String, String>();
165 
166         Map<String, Object> tempFieldValues = ObjectUtil.buildPropertyMap(accountStatusCurrentFunds, lookupFields);
167         for (String key : tempFieldValues.keySet()) {
168             fieldValues.put(key, tempFieldValues.get(key).toString());
169         }
170         return fieldValues;
171     }
172 
173     /**
174      *
175      * This method adds property constatants for future lookups
176      * @param isExtended
177      * @return
178      */
179     private List<String> getLookupFields(boolean isExtended) {
180         List<String> lookupFields = new ArrayList<String>();
181 
182         lookupFields.add(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
183         lookupFields.add(KFSPropertyConstants.ACCOUNT_NUMBER);
184         lookupFields.add(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
185 
186         return lookupFields;
187     }
188 
189     /**
190      * This method will add temporary test data to the Ledger Balance table
191      */
192     protected void insertCurrentFundsRecords() {
193         String messageFileName    = "org/kuali/kfs/module/ld/testdata/message.properties";
194         String propertiesFileName = "org/kuali/kfs/module/ld/testdata/accountStatusCurrentFunds.properties";
195 
196         properties = (new TestDataGenerator(propertiesFileName, messageFileName)).getProperties();
197         fieldNames = properties.getProperty("fieldNames");
198         documentFieldNames = properties.getProperty("fieldNames");
199         deliminator = properties.getProperty("deliminator");
200 
201         TestDataGenerator testDataGenerator = new TestDataGenerator(propertiesFileName, messageFileName);
202 
203         businessObjectService = SpringContext.getBean(BusinessObjectService.class);
204         persistenceService = SpringContext.getBean(PersistenceService.class);
205 
206         int numberOfDocuments = Integer.valueOf(properties.getProperty("getAccountStatusCurrentFunds.numOfData"));
207         List<LedgerBalance> inputDataList = new ArrayList<LedgerBalance>();
208         for (int i = 1; i <= numberOfDocuments; i++) {
209             String propertyKey = "getAccountStatusCurrentFunds.testData" + i;
210             LedgerBalance inputData = new LedgerBalance();
211             ObjectUtil.populateBusinessObject(inputData, properties, propertyKey, documentFieldNames, deliminator);
212             inputData.setUniversityFiscalYear(TestUtils.getFiscalYearForTesting());
213             inputDataList.add(inputData);
214         }
215         String testTarget = "getAccountStatusCurrentFunds.";
216         this.currentFundsNumberOfTestData = Integer.valueOf(properties.getProperty(testTarget + "numOfData"));
217         this.currentFundsExpectedInsertion = Integer.valueOf(properties.getProperty(testTarget + "expectedInsertion"));
218         businessObjectService.save(inputDataList);
219 
220         System.out.println("*** RECORDS INSERTED!!!!");
221     }
222 
223     private LaborInquiryOptionsService getInquiryOptionsService() {
224         return SpringContext.getBean(LaborInquiryOptionsService.class);
225     }
226 }