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 org.kuali.rice.krad.service;
017
018import org.junit.Test;
019import org.kuali.rice.core.api.CoreApiServiceLocator;
020import org.kuali.rice.core.api.datetime.DateTimeService;
021import org.kuali.rice.krad.bo.InactivatableFromTo;
022import org.kuali.rice.krad.bo.TravelAccountUseRate;
023import org.kuali.rice.krad.test.KRADTestCase;
024import org.kuali.rice.krad.util.KRADPropertyConstants;
025import org.kuali.rice.test.BaselineTestCase;
026import org.kuali.rice.test.data.PerTestUnitTestData;
027import org.kuali.rice.test.data.UnitTestData;
028import org.kuali.rice.test.data.UnitTestFile;
029import org.kuali.rice.test.data.UnitTestSql;
030
031import java.sql.Date;
032import java.sql.Timestamp;
033import java.util.ArrayList;
034import java.util.Collection;
035import java.util.HashMap;
036import java.util.List;
037import java.util.Map;
038
039import static org.junit.Assert.assertEquals;
040import static org.junit.Assert.assertTrue;
041
042
043/**
044 * Test case for InactivateableFromToService. Note on usage that the tests sets the activeAsOfDate so that the results
045 * will be consistent over time. However this is not necessary and the service will default to using the current date if
046 * not given.
047 * 
048 * @see org.kuali.rice.krad.service.InactivateableFromToService
049 */
050@PerTestUnitTestData(
051        value = @UnitTestData(
052                order = {UnitTestData.Type.SQL_STATEMENTS, UnitTestData.Type.SQL_FILES},
053                sqlStatements = {
054                        @UnitTestSql("delete from trv_acct_use_rt_t")
055                },
056                sqlFiles = {
057                        @UnitTestFile(filename = "classpath:testAccountUseRate.sql", delimiter = ";")
058                }
059        ),
060        tearDown = @UnitTestData(
061                sqlStatements = {
062                        @UnitTestSql("delete from trv_acct_use_rt_t")
063                }
064       )
065)
066@BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
067public class InactivateableFromToServiceTest extends KRADTestCase {
068    // Methods which can be overridden by subclasses to reuse test cases. Here we are just testing the LookupService
069    protected <T> Collection<T> findCollectionBySearchUnbounded(Class<T> clazz, Map<String, String> formProps) {
070        return KRADServiceLocatorWeb.getLookupService().findCollectionBySearchUnbounded(clazz, formProps);
071    }
072
073    protected InactivateableFromToService getInactivateableFromToService() {
074        return KRADServiceLocator.getInactivateableFromToService();
075    }
076
077        /**
078         * Test finding active records
079         *
080         * @see InactivateableFromToService#findMatchingActive(Class, Map)
081         */
082        @Test
083        public void testFindMatchingActive() throws Exception {
084                InactivateableFromToService inactivateableFromToService = getInactivateableFromToService();
085
086                Map fieldValues = new HashMap();
087                fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, "04/01/2010");
088                fieldValues.put("number", "a2");
089
090                List<InactivatableFromTo> results = inactivateableFromToService.findMatchingActive(TravelAccountUseRate.class,
091                                fieldValues);
092                assertEquals(2, results.size());
093
094                TravelAccountUseRate useRate = (TravelAccountUseRate) results.get(0);
095                assertTrue("Incorrect active records returned, do not match expected ids",
096                                "2".equals(useRate.getId()) || "3".equals(useRate.getId()));
097
098                useRate = (TravelAccountUseRate) results.get(1);
099                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}