View Javadoc

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