View Javadoc

1   /**
2    * Copyright 2004-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.kpme.tklm.time.block.history;
17  
18  import java.math.BigDecimal;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.joda.time.DateTime;
24  import org.joda.time.Interval;
25  import org.junit.Assert;
26  import org.junit.Test;
27  import org.kuali.kpme.core.IntegrationTest;
28  import org.kuali.kpme.core.calendar.Calendar;
29  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
30  import org.kuali.kpme.core.util.TKUtils;
31  import org.kuali.kpme.tklm.TKLMIntegrationTestCase;
32  import org.kuali.kpme.tklm.time.timeblock.TimeBlock;
33  import org.kuali.kpme.tklm.time.timehourdetail.TimeHourDetail;
34  import org.kuali.kpme.tklm.time.util.TkTimeBlockAggregate;
35  
36  @IntegrationTest
37  public class TimeBlockTest extends TKLMIntegrationTestCase {
38  	
39  	@Test
40  	public void testTimeBlockComparison() throws Exception {
41  		DateTime beginDateTime = new DateTime();
42  		DateTime endDateTime = new DateTime();
43  		
44  		TimeBlock timeBlock = new TimeBlock();
45  		timeBlock.setJobNumber(2L);
46  		timeBlock.setWorkArea(1234L);
47  		timeBlock.setTask(1L);
48  		timeBlock.setEarnCode("REG");
49  		timeBlock.setBeginDateTime(beginDateTime);
50  		timeBlock.setEndDateTime(endDateTime);
51  		TimeHourDetail timeHourDetail = new TimeHourDetail();
52  		timeHourDetail.setEarnCode("REG");
53  		timeHourDetail.setHours(new BigDecimal(2.0));
54  		timeBlock.getTimeHourDetails().add(timeHourDetail);
55  		
56  		TimeBlock timeBlock2 = new TimeBlock();
57  		timeBlock2.setJobNumber(2L);
58  		timeBlock2.setWorkArea(1234L);
59  		timeBlock2.setTask(1L);
60  		timeBlock2.setEarnCode("REG");
61  		timeBlock2.setBeginDateTime(beginDateTime);
62  		timeBlock2.setEndDateTime(endDateTime);
63  		TimeHourDetail timeHourDetail2 = new TimeHourDetail();
64  		timeHourDetail2.setEarnCode("REG");
65  		timeHourDetail2.setHours(new BigDecimal(2.0));
66  		timeBlock2.getTimeHourDetails().add(timeHourDetail);
67  		
68  		Assert.assertTrue("Timeblock has been equal", timeBlock.equals(timeBlock2));
69  	}
70  	
71  	@Test
72  	public void testTimeBlockBuilding() throws Exception {
73  		CalendarEntry payCalendarEntry = new CalendarEntry();
74  		DateTime beginPeriodDateTime = new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone());
75  		DateTime endPeriodDateTime = new DateTime(2010, 1, 15, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone());
76  		payCalendarEntry.setBeginPeriodFullDateTime(beginPeriodDateTime);
77  		payCalendarEntry.setEndPeriodFullDateTime(endPeriodDateTime);
78  		
79  		List<Interval> dayInterval = TKUtils.getDaySpanForCalendarEntry(payCalendarEntry);
80  		DateTime beginDateTime = new DateTime(2010, 1, 1, 13, 0, 0, 0, TKUtils.getSystemDateTimeZone());
81  		DateTime endDateTime = new DateTime(2010, 1, 2, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
82  		
83  		Interval firstDay = null;
84  		List<TimeBlock> lstTimeBlocks = new ArrayList<TimeBlock>();
85  		for(Interval dayInt : dayInterval){
86  			//on second day of span so safe to assume doesnt go furthur than this
87  			if(firstDay != null){
88  				TimeBlock tb = new TimeBlock();
89  				tb.setBeginDateTime(dayInt.getStart());
90  				tb.setEndDateTime(endDateTime);
91  				lstTimeBlocks.add(tb);
92  				break;
93  			}
94  			if(dayInt.contains(beginDateTime) ){
95  				firstDay = dayInt;
96  				if(dayInt.contains(endDateTime)){
97  					//create one timeblock if contained in one day interval
98  					TimeBlock tb = new TimeBlock();
99  					tb.setBeginDateTime(beginDateTime);
100 					tb.setEndDateTime(endDateTime);
101 					lstTimeBlocks.add(tb);
102 					break;
103 				} else {
104 					//create a timeblock that wraps the 24 hr day
105 					TimeBlock tb = new TimeBlock();
106 					tb.setBeginDateTime(beginDateTime);
107 					tb.setEndDateTime(firstDay.getEnd());
108 					lstTimeBlocks.add(tb);
109 				}
110 			}
111 		}
112 		Assert.assertTrue("Two timeblocks created", lstTimeBlocks.size() == 2);
113 		
114 		lstTimeBlocks.clear();
115 		
116 		beginDateTime = new DateTime(2010, 1, 1, 13, 0, 0, 0, TKUtils.getSystemDateTimeZone());
117 		endDateTime = new DateTime(2010, 1, 1, 15, 0, 0, 0, TKUtils.getSystemDateTimeZone());
118 		
119 		firstDay = null;
120 		for(Interval dayInt : dayInterval){
121 			//on second day of span so safe to assume doesnt go furthur than this
122 			if(firstDay != null){
123 				TimeBlock tb = new TimeBlock();
124 				tb.setBeginDateTime(dayInt.getStart());
125 				tb.setEndDateTime(endDateTime);
126 				lstTimeBlocks.add(tb);
127 				break;
128 			}
129 			if(dayInt.contains(beginDateTime) ){
130 				firstDay = dayInt;
131 				if(dayInt.contains(endDateTime)){
132 					//create one timeblock if contained in one day interval
133 					TimeBlock tb = new TimeBlock();
134 					tb.setBeginDateTime(beginDateTime);
135 					tb.setEndDateTime(endDateTime);
136 					lstTimeBlocks.add(tb);
137 					break;
138 				} else {
139 					//create a timeblock that wraps the 24 hr day
140 					TimeBlock tb = new TimeBlock();
141 					tb.setBeginDateTime(beginDateTime);
142 					tb.setEndDateTime(firstDay.getEnd());
143 					lstTimeBlocks.add(tb);
144 				}
145 			}
146 		}
147 		Assert.assertTrue("One time block creation", lstTimeBlocks.size() == 1);
148 	}
149 	
150 	@Test
151 	public void testTimeBlockAggregate() throws Exception {
152 		DateTime beginTime = new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone());
153 		DateTime endTime = new DateTime(2010, 1, 16, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone());
154 		
155 		Calendar payCalendar = new Calendar();
156 		
157 		CalendarEntry payCalendarEntry = new CalendarEntry();
158 		payCalendarEntry.setBeginPeriodFullDateTime(beginTime);
159 		payCalendarEntry.setEndPeriodFullDateTime(endTime);
160 		
161 		List<TimeBlock> lstTimeBlocks = setupTimeBlocks(beginTime, endTime, payCalendarEntry);
162 		TkTimeBlockAggregate tkTimeBlockAggregate = new TkTimeBlockAggregate(lstTimeBlocks, payCalendarEntry, payCalendar);
163 		Assert.assertTrue("Aggregate built correctly ", tkTimeBlockAggregate!= null && tkTimeBlockAggregate.getWeekTimeBlocks(0).size() == 7);
164 		Assert.assertTrue("Total number of days is correct",tkTimeBlockAggregate.getDayTimeBlockList().size()==15);
165 	}
166 	
167 	@Test
168 	public void testTimeBlockSorting() throws Exception {
169 		List<TimeBlock> tbList = new ArrayList<TimeBlock>();
170 		TimeBlock tb1 = new TimeBlock();
171 		// time block with 2010 time
172 		tb1.setBeginDateTime(new DateTime(2010, 1, 1, 13, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
173 		tb1.setEndDateTime(new DateTime(2010, 1, 2, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
174 		tbList.add(tb1);
175 		//time block with 2009 time
176 		TimeBlock tb2 = new TimeBlock();
177 		tb2.setBeginDateTime(new DateTime(2009, 1, 1, 13, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
178 		tb2.setEndDateTime(new DateTime(2009, 1, 2, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone()));
179 		tbList.add(tb2);
180 		
181 		Assert.assertTrue(tbList.get(0) == tb1);
182 		Assert.assertTrue(tbList.get(1) == tb2);
183 		// after sort
184 		Collections.sort(tbList);
185 		Assert.assertTrue(tbList.get(0) == tb2);
186 		Assert.assertTrue(tbList.get(1) == tb1);
187 	}
188 	private List<TimeBlock> setupTimeBlocks(DateTime startTime, DateTime endTime, CalendarEntry payCalendarEntry){
189 		List<Interval> dayInterval = TKUtils.getDaySpanForCalendarEntry(payCalendarEntry);
190 		DateTime beginDateTime = new DateTime(2010, 1, 1, 13, 0, 0, 0, TKUtils.getSystemDateTimeZone());
191 		DateTime endDateTime = new DateTime(2010, 1, 2, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
192 		
193 		Interval firstDay = null;
194 		List<TimeBlock> lstTimeBlocks = new ArrayList<TimeBlock>();
195 		for(Interval dayInt : dayInterval){
196 			//on second day of span so safe to assume doesnt go furthur than this
197 			if(firstDay != null){
198 				TimeBlock tb = new TimeBlock();
199 				tb.setBeginDateTime(dayInt.getStart());
200 				tb.setEndDateTime(endDateTime);
201 				lstTimeBlocks.add(tb);
202 				break;
203 			}
204 			if(dayInt.contains(beginDateTime) ){
205 				firstDay = dayInt;
206 				if(dayInt.contains(endDateTime)){
207 					//create one timeblock if contained in one day interval
208 					TimeBlock tb = new TimeBlock();
209 					tb.setBeginDateTime(beginDateTime);
210 					tb.setEndDateTime(endDateTime);
211 					lstTimeBlocks.add(tb);
212 					break;
213 				} else {
214 					//create a timeblock that wraps the 24 hr day
215 					TimeBlock tb = new TimeBlock();
216 					tb.setBeginDateTime(beginDateTime);
217 					tb.setEndDateTime(firstDay.getEnd());
218 					lstTimeBlocks.add(tb);
219 				}
220 			}
221 		}
222 		Assert.assertTrue("Two timeblocks created", lstTimeBlocks.size() == 2);
223 		
224 		lstTimeBlocks.clear();
225 		
226 		beginDateTime = new DateTime(2010, 1, 1, 13, 0, 0, 0, TKUtils.getSystemDateTimeZone());
227 		endDateTime = new DateTime(2010, 1, 1, 15, 0, 0, 0, TKUtils.getSystemDateTimeZone());
228 		
229 		firstDay = null;
230 		for(Interval dayInt : dayInterval){
231 			//on second day of span so safe to assume doesnt go furthur than this
232 			if(firstDay != null){
233 				TimeBlock tb = new TimeBlock();
234 				tb.setBeginDateTime(dayInt.getStart());
235 				tb.setEndDateTime(endDateTime);
236 				lstTimeBlocks.add(tb);
237 				break;
238 			}
239 			if(dayInt.contains(beginDateTime) ){
240 				firstDay = dayInt;
241 				if(dayInt.contains(endDateTime)){
242 					//create one timeblock if contained in one day interval
243 					TimeBlock tb = new TimeBlock();
244 					tb.setBeginDateTime(beginDateTime);
245 					tb.setEndDateTime(endDateTime);
246 					lstTimeBlocks.add(tb);
247 					break;
248 				} else {
249 					//create a timeblock that wraps the 24 hr day
250 					TimeBlock tb = new TimeBlock();
251 					tb.setBeginDateTime(beginDateTime);
252 					tb.setEndDateTime(firstDay.getEnd());
253 					lstTimeBlocks.add(tb);
254 				}
255 			}
256 		}
257 		return lstTimeBlocks;
258 	}
259 }