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 org.kuali.rice.krad.service;
17  
18  import org.junit.Test;
19  import org.kuali.rice.core.api.CoreApiServiceLocator;
20  import org.kuali.rice.core.api.datetime.DateTimeService;
21  import org.kuali.rice.krad.bo.InactivatableFromTo;
22  import org.kuali.rice.krad.bo.TravelAccountUseRate;
23  import org.kuali.rice.krad.test.KRADTestCase;
24  import org.kuali.rice.krad.util.KRADPropertyConstants;
25  import org.kuali.rice.test.BaselineTestCase;
26  import org.kuali.rice.test.data.PerTestUnitTestData;
27  import org.kuali.rice.test.data.UnitTestData;
28  import org.kuali.rice.test.data.UnitTestFile;
29  import org.kuali.rice.test.data.UnitTestSql;
30  
31  import java.sql.Date;
32  import java.sql.Timestamp;
33  import java.util.ArrayList;
34  import java.util.Collection;
35  import java.util.HashMap;
36  import java.util.List;
37  import java.util.Map;
38  
39  import static org.junit.Assert.assertEquals;
40  import static org.junit.Assert.assertTrue;
41  
42  
43  /**
44   * Test case for InactivateableFromToService. Note on usage that the tests sets the activeAsOfDate so that the results
45   * will be consistent over time. However this is not necessary and the service will default to using the current date if
46   * not given.
47   * 
48   * @see org.kuali.rice.krad.service.InactivateableFromToService
49   */
50  @PerTestUnitTestData(
51          value = @UnitTestData(
52                  order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
53                  sqlStatements = {
54                          @UnitTestSql("delete from trv_acct_use_rt_t")
55                  },
56                  sqlFiles = {
57                          @UnitTestFile(filename = "classpath:testAccountUseRate.sql", delimiter = ";")
58                  }
59          ),
60          tearDown = @UnitTestData(
61                  sqlStatements = {
62                  	@UnitTestSql("delete from trv_acct_use_rt_t")
63                  }
64         )
65  )
66  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
67  public class InactivateableFromToServiceTest extends KRADTestCase {
68      // Methods which can be overridden by subclasses to reuse test cases. Here we are just testing the LookupService
69      protected <T> Collection<T> findCollectionBySearchUnbounded(Class<T> clazz, Map<String, String> formProps) {
70          return KRADServiceLocatorWeb.getLookupService().findCollectionBySearchUnbounded(clazz, formProps);
71      }
72  
73      protected InactivateableFromToService getInactivateableFromToService() {
74          return KRADServiceLocator.getInactivateableFromToService();
75      }
76  
77  	/**
78  	 * Test finding active records
79  	 *
80  	 * @see InactivateableFromToService#findMatchingActive(Class, Map)
81  	 */
82  	@Test
83  	public void testFindMatchingActive() throws Exception {
84  		InactivateableFromToService inactivateableFromToService = getInactivateableFromToService();
85  
86  		Map fieldValues = new HashMap();
87  		fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, "04/01/2010");
88  		fieldValues.put("number", "a2");
89  
90  		List<InactivatableFromTo> results = inactivateableFromToService.findMatchingActive(TravelAccountUseRate.class,
91  				fieldValues);
92  		assertEquals(2, results.size());
93  
94  		TravelAccountUseRate useRate = (TravelAccountUseRate) results.get(0);
95  		assertTrue("Incorrect active records returned, do not match expected ids",
96  				"2".equals(useRate.getId()) || "3".equals(useRate.getId()));
97  
98  		useRate = (TravelAccountUseRate) results.get(1);
99  		assertTrue("Incorrect active records returned, do not match expected ids",
100 				"2".equals(useRate.getId()) || "3".equals(useRate.getId()));
101 
102 		fieldValues = new HashMap();
103 		fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, "04/01/2010");
104 		fieldValues.put("number", "a1");
105 
106 		results = inactivateableFromToService.findMatchingActive(TravelAccountUseRate.class, fieldValues);
107 		assertEquals(1, results.size());
108 
109 		useRate = (TravelAccountUseRate) results.get(0);
110 		assertTrue("Incorrect active record returned, does not match expected id", "1".equals(useRate.getId()));
111 
112 		fieldValues = new HashMap();
113 		fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, "01/01/2010 01:31 PM");
114 		fieldValues.put("number", "b1");
115 
116 		results = inactivateableFromToService.findMatchingActive(TravelAccountUseRate.class, fieldValues);
117 		assertEquals(1, results.size());
118 
119 		useRate = (TravelAccountUseRate) results.get(0);
120 		assertTrue("Incorrect active record returned, does not match expected id", "9".equals(useRate.getId()));
121 	}
122 
123 	/**
124 	 * Test finding inactive records through LookupService
125 	 */
126 	@Test
127 	public void testFindMatchingActive_inactive() throws Exception {
128 		Map fieldValues = new HashMap();
129 		fieldValues.put(KRADPropertyConstants.ACTIVE, "N");
130 		fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, "04/01/2010");
131 		fieldValues.put("number", "a2");
132 
133 		List<? extends InactivatableFromTo> results = (List<? extends InactivatableFromTo>) findCollectionBySearchUnbounded(TravelAccountUseRate.class,
134 				fieldValues);
135 		assertEquals(1, results.size());
136 
137 		TravelAccountUseRate useRate = (TravelAccountUseRate) results.get(0);
138 		assertTrue("Incorrect inactive record returned, does not match expected id",
139 				"4".equals(useRate.getId()));
140 
141 		fieldValues = new HashMap();
142 		fieldValues.put(KRADPropertyConstants.ACTIVE, "N");
143 		fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, "07/01/2010");
144 		fieldValues.put("number", "a3");
145 
146 		results = (List<? extends InactivatableFromTo>) findCollectionBySearchUnbounded(TravelAccountUseRate.class, fieldValues);
147 		assertEquals(1, results.size());
148 
149 		useRate = (TravelAccountUseRate) results.get(0);
150 		assertTrue("Incorrect inactive record returned, does not match expected id", "5".equals(useRate.getId()));
151 	}
152 
153 	/**
154 	 * Test query results when begin date is null
155 	 *
156 	 * @see InactivateableFromToService#findMatchingActive(Class, Map)
157 	 */
158 	@Test
159 	public void testFindMatchingActive_nullBeginDate() throws Exception {
160 		InactivateableFromToService inactivateableFromToService = getInactivateableFromToService();
161 
162 		Map fieldValues = new HashMap();
163 		fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, "04/01/2010");
164 		fieldValues.put("number", "a4");
165 
166 		List<? extends InactivatableFromTo> results = inactivateableFromToService.findMatchingActive(TravelAccountUseRate.class,
167 				fieldValues);
168 		assertEquals(1, results.size());
169 
170 		TravelAccountUseRate useRate = (TravelAccountUseRate) results.get(0);
171 		assertTrue("Incorrect inactive record returned, does not match expected id", "6".equals(useRate.getId()));
172 
173 		fieldValues = new HashMap();
174 		fieldValues.put(KRADPropertyConstants.ACTIVE, "N");
175 		fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, "04/01/2010");
176 		fieldValues.put("number", "a4");
177 
178 		results = (List<? extends InactivatableFromTo>) findCollectionBySearchUnbounded(TravelAccountUseRate.class, fieldValues);
179 		assertEquals(0, results.size());
180 	}
181 
182 	/**
183 	 * Test query results when end date is null
184 	 *
185 	 * @see InactivateableFromToService#findMatchingActive(Class, Map)
186 	 */
187 	@Test
188 	public void testFindMatchingActive_nullEndDate() throws Exception {
189 		InactivateableFromToService inactivateableFromToService = getInactivateableFromToService();
190 
191 		Map fieldValues = new HashMap();
192 		fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, "04/01/2030");
193 		fieldValues.put("number", "a5");
194 
195 		List<InactivatableFromTo> results = inactivateableFromToService.findMatchingActive(TravelAccountUseRate.class,
196 				fieldValues);
197 		assertEquals(1, results.size());
198 
199 		TravelAccountUseRate useRate = (TravelAccountUseRate) results.get(0);
200 		assertTrue("Incorrect active record returned, does not match expected id", "7".equals(useRate.getId()));
201 	}
202 
203 	/**
204 	 * Test query results when the begin and end date is null
205 	 *
206 	 * @see InactivateableFromToService#findMatchingActive(Class, Map)
207 	 */
208 	@Test
209 	public void testFindMatchingActive_nullBeginEndDate() throws Exception {
210 		InactivateableFromToService inactivateableFromToService = getInactivateableFromToService();
211 
212 		Map fieldValues = new HashMap();
213 		fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, "04/01/2010");
214 		fieldValues.put("number", "a6");
215 
216 		List<InactivatableFromTo> results = inactivateableFromToService.findMatchingActive(TravelAccountUseRate.class,
217 				fieldValues);
218 		assertEquals(1, results.size());
219 
220 		TravelAccountUseRate useRate = (TravelAccountUseRate) results.get(0);
221 		assertTrue("Incorrect active record returned, does not match expected id", "8".equals(useRate.getId()));
222 	}
223 
224 	/**
225 	 * Test for the filterOutNonActive method
226 	 *
227 	 * @see InactivateableFromToService#filterOutNonActive(List)
228 	 */
229 	@Test
230 	public void testFilterOutNonActive() throws Exception {
231 		InactivateableFromToService inactivateableFromToService = getInactivateableFromToService();
232 
233 		List<InactivatableFromTo> filterList = new ArrayList<InactivatableFromTo>();
234 		filterList.add(constructUseRate("1", "a1", "01/01/2010", "01/01/2011"));
235 		filterList.add(constructUseRate("2", "a1", "01/01/2012", "01/01/2013"));
236 		filterList.add(constructUseRate("3", "a2", "01/01/2009", "01/01/2010"));
237 		filterList.add(constructUseRate("4", "a3", "01/01/2010", "05/16/2010"));
238 		filterList.add(constructUseRate("5", "a4", null, "01/01/2011"));
239 
240 		Date activeAsOfDate = CoreApiServiceLocator.getDateTimeService().convertToSqlDate("06/01/2010");
241 		List<InactivatableFromTo> accessibleList = inactivateableFromToService.filterOutNonActive(filterList,
242 				activeAsOfDate);
243 		assertEquals(2, accessibleList.size());
244 
245 		TravelAccountUseRate useRate = (TravelAccountUseRate) accessibleList.get(0);
246 		assertTrue("Incorrect active records returned, do not match expected ids", "1".equals(useRate.getId()) || "5".equals(useRate.getId()));
247 
248 		useRate = (TravelAccountUseRate) accessibleList.get(1);
249 		assertTrue("Incorrect active records returned, do not match expected ids", "1".equals(useRate.getId()) || "5".equals(useRate.getId()));
250 	}
251 
252 	/**
253 	 * Test finding current records through the findMatchingCurrent method
254 	 *
255 	 * @see InactivateableFromToService#findMatchingCurrent(Class, Map)
256 	 */
257 	@Test
258 	public void testFindMatchingCurrent() throws Exception {
259 		InactivateableFromToService inactivateableFromToService = getInactivateableFromToService();
260 
261 		Map fieldValues = new HashMap();
262 		fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, "06/01/2010");
263 		fieldValues.put("number", "a2");
264 
265 		List<InactivatableFromTo> results = inactivateableFromToService.findMatchingCurrent(TravelAccountUseRate.class,
266 				fieldValues);
267 		assertEquals(1, results.size());
268 
269         // should find the record with the latest valid "from date"
270         TravelAccountUseRate result = (TravelAccountUseRate)results.get(0);
271         assertEquals("Incorrect current record returned, does not match expected id", "3", result.getId());
272 
273         fieldValues = new HashMap();
274         fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, "02/01/2010");
275         fieldValues.put("number", "a2");
276 
277         results = inactivateableFromToService.findMatchingCurrent(TravelAccountUseRate.class,
278                 fieldValues);
279         assertEquals(1, results.size());
280 
281         // since our record with id 3 has an active from date of 03/01/2010, it won't be returned so we should get the other one
282         result = (TravelAccountUseRate) results.get(0);
283 		assertEquals("Incorrect current record returned, does not match expected id", "2", result.getId());
284 	}
285 
286     /**
287      * Test that finding non-current records is the same as finding active with current filtered
288      *
289      * @see InactivateableFromToService#findMatchingCurrent(Class, Map)
290      */
291     @Test
292     public void testFindMatchingNonCurrent() throws Exception {
293         InactivateableFromToService inactivateableFromToService = getInactivateableFromToService();
294 
295         Map fieldValues = new HashMap();
296         fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, "04/01/2010");
297         fieldValues.put("number", "a2");
298 
299         List<InactivatableFromTo> results = inactivateableFromToService.findMatchingActive(TravelAccountUseRate.class,
300                 fieldValues);
301         assertEquals(2, results.size());
302 
303         TravelAccountUseRate useRate = (TravelAccountUseRate) results.get(0);
304         assertTrue("Incorrect active records returned, do not match expected ids",
305                 "2".equals(useRate.getId()) || "3".equals(useRate.getId()));
306 
307         useRate = (TravelAccountUseRate) results.get(1);
308         assertTrue("Incorrect active records returned, do not match expected ids",
309                 "2".equals(useRate.getId()) || "3".equals(useRate.getId()));
310 
311         List<InactivatableFromTo> nonCurrent = inactivateableFromToService.filterOutNonCurrent(results, CoreApiServiceLocator.getDateTimeService().convertToSqlDate("04/01/2010"));
312 
313         //
314         fieldValues.put(KRADPropertyConstants.ACTIVE, "true");
315         fieldValues.put(KRADPropertyConstants.CURRENT, "true");
316         Collection<TravelAccountUseRate> nonCurrentLookup = findCollectionBySearchUnbounded(TravelAccountUseRate.class, fieldValues);
317 
318         assertEquals(nonCurrent.size(), nonCurrentLookup.size());
319         List<TravelAccountUseRate> nonCurrentLookupList = new ArrayList<TravelAccountUseRate>(nonCurrentLookup);
320         for (int i = 0; i < nonCurrent.size(); i++) {
321             TravelAccountUseRate expected = (TravelAccountUseRate) nonCurrent.get(i);
322             TravelAccountUseRate actual = nonCurrentLookupList.get(i);
323 
324             assertEquals(expected.getId(), actual.getId());
325             assertEquals(expected.getRate(), actual.getRate());
326             assertEquals(expected.getActiveFromDate(), actual.getActiveFromDate());
327             assertEquals(expected.getActiveToDate(), actual.getActiveToDate());
328         }
329     }
330 
331 	/**
332 	 * Test for the filterOutNonCurrent method
333 	 *
334 	 * @see InactivateableFromToService#filterOutNonCurrent(List)
335 	 */
336 	@Test
337 	public void testFilterOutNonCurrent() throws Exception {
338 		InactivateableFromToService inactivateableFromToService = getInactivateableFromToService();
339 
340 		List<InactivatableFromTo> filterList = new ArrayList<InactivatableFromTo>();
341 		filterList.add(constructUseRate("1", "a1", "01/01/2010", "01/01/2011"));
342 		filterList.add(constructUseRate("2", "a1", "01/16/2010", "01/01/2011"));
343 		filterList.add(constructUseRate("3", "a1", "01/01/2012", "01/01/2013"));
344 		filterList.add(constructUseRate("4", "a2", "01/01/2009", "01/01/2010"));
345 		filterList.add(constructUseRate("5", "a3", "01/01/2010", "05/16/2011"));
346 		filterList.add(constructUseRate("6", "a3", "06/01/2010", "05/16/2011"));
347 
348 		Date activeAsOfDate = CoreApiServiceLocator.getDateTimeService().convertToSqlDate("06/10/2010");
349 		List<InactivatableFromTo> accessibleList = inactivateableFromToService.filterOutNonCurrent(filterList,
350 				activeAsOfDate);
351 		assertEquals(2, accessibleList.size());
352 
353 		TravelAccountUseRate useRate = (TravelAccountUseRate) accessibleList.get(0);
354 		assertTrue("Incorrect curren records returned, do not match expected ids",
355 				"2".equals(useRate.getId()) || "6".equals(useRate.getId()));
356 
357 		useRate = (TravelAccountUseRate) accessibleList.get(1);
358 		assertTrue("Incorrect curren records returned, do not match expected ids",
359 				"2".equals(useRate.getId()) || "6".equals(useRate.getId()));
360 	}
361 
362 	protected TravelAccountUseRate constructUseRate(String id, String number, String fromDate, String toDate)
363 			throws Exception {
364 		TravelAccountUseRate useRate = new TravelAccountUseRate();
365 
366 		DateTimeService dateTimeService = CoreApiServiceLocator.getDateTimeService();
367 		useRate.setId(id);
368 		useRate.setNumber(number);
369 		if (fromDate != null) {
370 			useRate.setActiveFromDate(new Timestamp(dateTimeService.convertToSqlDate(fromDate).getTime()));
371 		}
372 		if (toDate != null) {
373 			useRate.setActiveToDate(new Timestamp(dateTimeService.convertToSqlDate(toDate).getTime()));
374 		}
375 
376 		return useRate;
377 	}
378 }