Skip to content

Commit e717ae4

Browse files
committed
Address copilot reviews
1 parent bd5d8f9 commit e717ae4

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

utils/src/main/java/org/apache/cloudstack/utils/usage/UsageUtils.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,15 @@ protected static Date getJobExecutionTime(TimeZone usageTimeZone, String jobExec
4646
logger.warn("Unable to parse configuration 'usage.stats.job.exec.time'.");
4747
return null;
4848
}
49-
int hourOfDay = Integer.parseInt(execTimeSegments[0]);
50-
int minutes = Integer.parseInt(execTimeSegments[1]);
49+
int hourOfDay;
50+
int minutes;
51+
try {
52+
hourOfDay = Integer.parseInt(execTimeSegments[0]);
53+
minutes = Integer.parseInt(execTimeSegments[1]);
54+
} catch (NumberFormatException e) {
55+
logger.warn("Unable to parse configuration 'usage.stats.job.exec.time' due to non-numeric values in [{}].", jobExecTimeConfig, e);
56+
return null;
57+
}
5158

5259
Date currentDate = DateUtil.currentGMTTime();
5360
Calendar jobExecTime = Calendar.getInstance(usageTimeZone);
@@ -58,9 +65,9 @@ protected static Date getJobExecutionTime(TimeZone usageTimeZone, String jobExec
5865
jobExecTime.set(Calendar.MILLISECOND, 0);
5966

6067
if (next && jobExecTime.getTime().before(currentDate)) {
61-
jobExecTime.roll(Calendar.DAY_OF_YEAR, true);
68+
jobExecTime.add(Calendar.DAY_OF_YEAR, 1);
6269
} else if (!next && jobExecTime.getTime().after(currentDate)) {
63-
jobExecTime.roll(Calendar.DAY_OF_YEAR, false);
70+
jobExecTime.add(Calendar.DAY_OF_YEAR, -1);
6471
}
6572

6673
return jobExecTime.getTime();

utils/src/test/java/org/apache/cloudstack/utils/usage/UsageUtilsTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,34 @@ public void getJobExecutionTimeTestReturnsExpectedDateWhenNextIsFalseAndExecutio
102102
}
103103
}
104104

105+
@Test
106+
public void getJobExecutionTimeTestReturnsExpectedDateWhenNextExecutionIsOnNextYear() {
107+
Date currentDate = new Date();
108+
currentDate.setTime(1767236340000L);
109+
110+
try (MockedStatic<DateUtil> dateUtilMockedStatic = Mockito.mockStatic(DateUtil.class)) {
111+
dateUtilMockedStatic.when(DateUtil::currentGMTTime).thenReturn(currentDate);
112+
113+
Date result = UsageUtils.getJobExecutionTime(usageTimeZone, "00:00", true);
114+
115+
Assert.assertNotNull(result);
116+
Assert.assertEquals(1767236400000L, result.getTime());
117+
}
118+
}
119+
120+
@Test
121+
public void getJobExecutionTimeTestReturnsExpectedDateWhenPreviousExecutionWasOnPreviousYear() {
122+
Date currentDate = new Date();
123+
currentDate.setTime(1767236460000L);
124+
125+
try (MockedStatic<DateUtil> dateUtilMockedStatic = Mockito.mockStatic(DateUtil.class)) {
126+
dateUtilMockedStatic.when(DateUtil::currentGMTTime).thenReturn(currentDate);
127+
128+
Date result = UsageUtils.getJobExecutionTime(usageTimeZone, "23:59", false);
129+
130+
Assert.assertNotNull(result);
131+
Assert.assertEquals(1767236340000L, result.getTime());
132+
}
133+
}
134+
105135
}

0 commit comments

Comments
 (0)