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