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