001 /** 002 * Copyright 2004-2013 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 */ 016 package org.kuali.hr.time.timeblock; 017 018 import java.math.BigDecimal; 019 import java.sql.Timestamp; 020 import java.util.ArrayList; 021 import java.util.Collections; 022 import java.util.List; 023 024 import org.joda.time.DateTime; 025 import org.joda.time.DateTimeZone; 026 import org.joda.time.Interval; 027 import org.junit.Assert; 028 import org.junit.Test; 029 import org.kuali.hr.test.KPMETestCase; 030 import org.kuali.hr.time.calendar.Calendar; 031 import org.kuali.hr.time.calendar.CalendarEntries; 032 import org.kuali.hr.time.util.TKUtils; 033 import org.kuali.hr.time.util.TkTimeBlockAggregate; 034 035 public class TimeBlockTest extends KPMETestCase { 036 037 @Test 038 public void testTimeBlockComparison() throws Exception { 039 TimeBlock timeBlock = new TimeBlock(); 040 timeBlock.setJobNumber(2L); 041 timeBlock.setWorkArea(1234L); 042 timeBlock.setTask(1L); 043 timeBlock.setEarnCode("REG"); 044 Timestamp beginTimestamp = new Timestamp(System.currentTimeMillis()); 045 timeBlock.setBeginTimestamp(beginTimestamp); 046 Timestamp endTimestamp = new Timestamp(System.currentTimeMillis()); 047 timeBlock.setEndTimestamp(endTimestamp); 048 TimeHourDetail timeHourDetail = new TimeHourDetail(); 049 timeHourDetail.setEarnCode("REG"); 050 timeHourDetail.setHours(new BigDecimal(2.0)); 051 timeBlock.getTimeHourDetails().add(timeHourDetail); 052 053 TimeBlock timeBlock2 = new TimeBlock(); 054 timeBlock2.setJobNumber(2L); 055 timeBlock2.setWorkArea(1234L); 056 timeBlock2.setTask(1L); 057 timeBlock2.setEarnCode("REG"); 058 timeBlock2.setBeginTimestamp(beginTimestamp); 059 timeBlock2.setEndTimestamp(endTimestamp); 060 TimeHourDetail timeHourDetail2 = new TimeHourDetail(); 061 timeHourDetail2.setEarnCode("REG"); 062 timeHourDetail2.setHours(new BigDecimal(2.0)); 063 timeBlock2.getTimeHourDetails().add(timeHourDetail); 064 065 Assert.assertTrue("Timeblock has been equal", timeBlock.equals(timeBlock2)); 066 } 067 068 @Test 069 public void testTimeBlockBuilding() throws Exception { 070 CalendarEntries payCalendarEntry = new CalendarEntries(); 071 java.util.Date beginDateTime = new java.util.Date((new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()); 072 java.util.Date endDateTime = new java.util.Date((new DateTime(2010, 1, 15, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()); 073 payCalendarEntry.setBeginPeriodDateTime(beginDateTime); 074 payCalendarEntry.setEndPeriodDateTime(endDateTime); 075 076 List<Interval> dayInterval = TKUtils.getDaySpanForCalendarEntry(payCalendarEntry); 077 Timestamp beginTimeStamp = new Timestamp((new DateTime(2010, 1, 1, 13, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()); 078 Timestamp endTimeStamp = new Timestamp((new DateTime(2010, 1, 2, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()); 079 080 Interval firstDay = null; 081 List<TimeBlock> lstTimeBlocks = new ArrayList<TimeBlock>(); 082 for(Interval dayInt : dayInterval){ 083 //on second day of span so safe to assume doesnt go furthur than this 084 if(firstDay != null){ 085 TimeBlock tb = new TimeBlock(); 086 tb.setBeginTimestamp(new Timestamp(dayInt.getStartMillis())); 087 tb.setEndTimestamp(endTimeStamp); 088 lstTimeBlocks.add(tb); 089 break; 090 } 091 if(dayInt.contains(beginTimeStamp.getTime()) ){ 092 firstDay = dayInt; 093 if(dayInt.contains(endTimeStamp.getTime())){ 094 //create one timeblock if contained in one day interval 095 TimeBlock tb = new TimeBlock(); 096 tb.setBeginTimestamp(beginTimeStamp); 097 tb.setEndTimestamp(endTimeStamp); 098 lstTimeBlocks.add(tb); 099 break; 100 } else { 101 //create a timeblock that wraps the 24 hr day 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 //on second day of span so safe to assume doesnt go furthur than this 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 //create one timeblock if contained in one day interval 130 TimeBlock tb = new TimeBlock(); 131 tb.setBeginTimestamp(beginTimeStamp); 132 tb.setEndTimestamp(endTimeStamp); 133 lstTimeBlocks.add(tb); 134 break; 135 } else { 136 //create a timeblock that wraps the 24 hr day 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 // time block with 2010 time 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 //time block with 2009 time 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 // after sort 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 //on second day of span so safe to assume doesnt go furthur than this 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 //create one timeblock if contained in one day interval 207 TimeBlock tb = new TimeBlock(); 208 tb.setBeginTimestamp(beginTimeStamp); 209 tb.setEndTimestamp(endTimeStamp); 210 lstTimeBlocks.add(tb); 211 break; 212 } else { 213 //create a timeblock that wraps the 24 hr day 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 //on second day of span so safe to assume doesnt go furthur than this 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 //create one timeblock if contained in one day interval 242 TimeBlock tb = new TimeBlock(); 243 tb.setBeginTimestamp(beginTimeStamp); 244 tb.setEndTimestamp(endTimeStamp); 245 lstTimeBlocks.add(tb); 246 break; 247 } else { 248 //create a timeblock that wraps the 24 hr day 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 }