1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
package com.yahoo.config.application.api;
import org.junit.Test;
import java.time.Instant;
import static java.time.DayOfWeek.FRIDAY;
import static java.time.DayOfWeek.MONDAY;
import static java.time.DayOfWeek.SATURDAY;
import static java.time.DayOfWeek.THURSDAY;
import static java.time.DayOfWeek.TUESDAY;
import static java.time.DayOfWeek.WEDNESDAY;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author mpolden
*/
public class TimeWindowTest {
@Test
public void includesInstant() {
{
TimeWindow tw = TimeWindow.from("mon", "10,11", "UTC");
Instant i0 = Instant.parse("2017-09-17T11:15:30.00Z"); // Wrong day
Instant i1 = Instant.parse("2017-09-18T09:15:30.00Z"); // Wrong hour
Instant i2 = Instant.parse("2017-09-18T10:15:30.00Z");
Instant i3 = Instant.parse("2017-09-18T11:15:30.00Z");
Instant i4 = Instant.parse("2017-09-18T12:15:30.00Z"); // Wrong hour
Instant i5 = Instant.parse("2017-09-19T11:15:30.00Z"); // Wrong day
assertFalse("Instant " + i0 + " is not in window", tw.includes(i0));
assertFalse("Instant " + i1 + " is not in window", tw.includes(i1));
assertTrue("Instant " + i2 + " is in window", tw.includes(i2));
assertTrue("Instant " + i3 + " is in window", tw.includes(i3));
assertFalse("Instant " + i4 + " is not in window", tw.includes(i4));
assertFalse("Instant " + i5 + " is not in window", tw.includes(i5));
}
{
TimeWindow tw = TimeWindow.from("mon", "12,13", "CET");
Instant i0 = Instant.parse("2017-09-17T11:15:30.00Z");
Instant i1 = Instant.parse("2017-09-18T09:15:30.00Z");
Instant i2 = Instant.parse("2017-09-18T10:15:30.00Z"); // Including offset this matches hour 12
Instant i3 = Instant.parse("2017-09-18T11:15:30.00Z"); // Including offset this matches hour 13
Instant i4 = Instant.parse("2017-09-18T12:15:30.00Z");
Instant i5 = Instant.parse("2017-09-19T11:15:30.00Z");
assertFalse("Instant " + i0 + " is not in window", tw.includes(i0));
assertFalse("Instant " + i1 + " is not in window", tw.includes(i1));
assertTrue("Instant " + i2 + " is in window", tw.includes(i2));
assertTrue("Instant " + i3 + " is in window", tw.includes(i3));
assertFalse("Instant " + i4 + " is not in window", tw.includes(i4));
assertFalse("Instant " + i5 + " is not in window", tw.includes(i5));
}
}
@Test
public void validWindows() {
{
TimeWindow fz = TimeWindow.from("fri", "8,17-19", "UTC");
assertEquals(asList(FRIDAY), fz.days());
assertEquals(asList(8, 17, 18, 19), fz.hours());
}
{
TimeWindow fz = TimeWindow.from("sat,", "8,17-19", "UTC");
assertEquals(asList(SATURDAY), fz.days());
assertEquals(asList(8, 17, 18, 19), fz.hours());
}
{
TimeWindow fz = TimeWindow.from("tue,sat", "0,3,7,10", "UTC");
assertEquals(asList(TUESDAY, SATURDAY), fz.days());
assertEquals(asList(0, 3, 7, 10), fz.hours());
}
{
TimeWindow fz = TimeWindow.from("mon,wed-thu", "0,17-19", "UTC");
assertEquals(asList(MONDAY, WEDNESDAY, THURSDAY), fz.days());
assertEquals(asList(0, 17, 18, 19), fz.hours());
}
{
// Full day names is allowed
TimeWindow fz = TimeWindow.from("monday,wednesday-thursday", "0,17-19", "UTC");
assertEquals(asList(MONDAY, WEDNESDAY, THURSDAY), fz.days());
assertEquals(asList(0, 17, 18, 19), fz.hours());
}
{
// Duplicate day and overlapping range is allowed
TimeWindow fz = TimeWindow.from("mon,wed-thu,mon", "3,1-4", "UTC");
assertEquals(asList(MONDAY, WEDNESDAY, THURSDAY), fz.days());
assertEquals(asList(1, 2, 3, 4), fz.hours());
}
}
@Test
public void invalidWindows() {
// Invalid time zone
assertInvalidZone("foo", "Invalid time zone 'foo'");
// Malformed day input
assertInvalidDays("", "Invalid day ''");
assertInvalidDays("foo-", "Invalid range 'foo-'");
assertInvalidDays("foo", "Invalid day 'foo'");
assertInvalidDays("f", "Invalid day 'f'");
// Window crossing week boundary is disallowed
assertInvalidDays("fri-tue", "Invalid day range 'fri-tue'");
// Malformed hour input
assertInvalidHours("", "Invalid hour ''");
assertInvalidHours("24", "Invalid hour '24'");
assertInvalidHours("-1-9", "Invalid range '-1-9'");
// Window crossing day boundary is disallowed
assertInvalidHours("23-1", "Invalid hour range '23-1'");
}
private static void assertInvalidZone(String zoneSpec, String exceptionMessage) {
try {
TimeWindow.from("mon", "1", zoneSpec);
fail("Expected exception");
} catch (IllegalArgumentException e) {
assertEquals(exceptionMessage, e.getMessage());
}
}
private static void assertInvalidDays(String daySpec, String exceptionMessage) {
try {
TimeWindow.from(daySpec, "1", "UTC");
fail("Expected exception");
} catch (IllegalArgumentException e) {
assertEquals(exceptionMessage, e.getMessage());
}
}
private static void assertInvalidHours(String hourSpec, String exceptionMessage) {
try {
TimeWindow.from("mon", hourSpec, "UTC");
fail("Expected exception");
} catch (IllegalArgumentException e) {
assertEquals(exceptionMessage, e.getMessage());
}
}
}
|