aboutsummaryrefslogtreecommitdiffstats
path: root/config-model-api/src/test/java/com/yahoo/config/application/api/TimeWindowTest.java
blob: 59482bf64901875f48b8526a8442a364e1ebc0a1 (plain) (blame)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.application.api;

import org.junit.Test;

import java.time.DayOfWeek;
import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

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 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
            assertOutside(tw, i0);
            assertOutside(tw, i1);
            assertInside(tw, i2);
            assertInside(tw, i3);
            assertOutside(tw, i4);
            assertOutside(tw, 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");
            assertOutside(tw, i0);
            assertOutside(tw, i1);
            assertInside(tw, i2);
            assertInside(tw, i3);
            assertOutside(tw, i4);
            assertOutside(tw, i5);
        }
        {
            TimeWindow tw = TimeWindow.from("mon-sun", "0-23", "CET", "2022-01-15", "2022-02-15");
            Instant i0 = Instant.parse("2022-01-14T12:00:00.00Z"); // Before window
            Instant i1 = Instant.parse("2022-01-14T23:00:00.00Z"); // Inside window because of time zone offset
            Instant i2 = Instant.parse("2022-02-05T12:00:00.00Z");
            Instant i3 = Instant.parse("2022-02-14T23:00:00.00Z");
            Instant i4 = Instant.parse("2022-02-15T23:00:00.00Z"); // After window because of time zone offset
            Instant i5 = Instant.parse("2022-02-16T12:00:00.00Z"); // After window
            assertOutside(tw, i0);
            assertInside(tw, i1);
            assertInside(tw, i2);
            assertInside(tw, i3);
            assertOutside(tw, i4);
            assertOutside(tw, i5);

            TimeWindow tw2 = TimeWindow.from("sun", "1", "CET", "2022-01-01", "2022-01-02");
            Instant i6 = Instant.parse("2022-01-01T00:00:00.00Z"); // Wrong day
            Instant i7 = Instant.parse("2022-01-02T01:00:00.00Z"); // Wrong hour because of time zone offset
            Instant i8 = Instant.parse("2022-01-02T00:00:00.00Z");
            assertOutside(tw2, i6);
            assertOutside(tw2, i7);
            assertInside(tw2, i8);

            TimeWindow tw3 = TimeWindow.from("", "", "CET", "2022-01-02", "");
            Instant i9 = Instant.parse("2022-02-15T00:00:00.00Z");
            assertOutside(tw3, i6);
            assertInside(tw3, i7);
            assertInside(tw3, i8);
            assertInside(tw3, i9);
        }
    }

    @Test
    public void validWindows() {
        {
            TimeWindow tw = TimeWindow.from("fri", "8,17-19", "UTC", "", "");
            assertEquals(List.of(FRIDAY), tw.days());
            assertEquals(List.of(8, 17, 18, 19), tw.hours());
        }
        {
            TimeWindow tw = TimeWindow.from("sat,", "8,17-19", "UTC", "", "");
            assertEquals(List.of(SATURDAY), tw.days());
            assertEquals(List.of(8, 17, 18, 19), tw.hours());
        }
        {
            TimeWindow tw = TimeWindow.from("tue,sat", "0,3,7,10", "UTC", "", "");
            assertEquals(List.of(TUESDAY, SATURDAY), tw.days());
            assertEquals(List.of(0, 3, 7, 10), tw.hours());
        }
        {
            TimeWindow tw = TimeWindow.from("mon,wed-thu", "0,17-19", "UTC", "", "");
            assertEquals(List.of(MONDAY, WEDNESDAY, THURSDAY), tw.days());
            assertEquals(List.of(0, 17, 18, 19), tw.hours());
        }
        { // Empty results in default values
            TimeWindow tw = TimeWindow.from("", "", "", "", "");
            assertEquals(List.of(DayOfWeek.values()), tw.days());
            assertEquals(IntStream.rangeClosed(0, 23).boxed().toList(), tw.hours());
            assertEquals("UTC", tw.zone().getId());
        }
        {
            // Full day names is allowed
            TimeWindow tw = TimeWindow.from("monday,wednesday-thursday", "0,17-19", "UTC", "", "");
            assertEquals(List.of(MONDAY, WEDNESDAY, THURSDAY), tw.days());
            assertEquals(List.of(0, 17, 18, 19), tw.hours());
        }
        {
            // Duplicate day and overlapping range is allowed
            TimeWindow tw = TimeWindow.from("mon,wed-thu,mon", "3,1-4", "UTC", "", "");
            assertEquals(List.of(MONDAY, WEDNESDAY, THURSDAY), tw.days());
            assertEquals(List.of(1, 2, 3, 4), tw.hours());
        }
        {   // Default to days contained in the date range
            TimeWindow tw = TimeWindow.from("", "", "", "2022-01-11", "2022-01-14");
            assertEquals(List.of(TUESDAY, WEDNESDAY, THURSDAY, FRIDAY), tw.days());
            TimeWindow tw2 = TimeWindow.from("", "", "", "2022-01-01", "2100-01-01");
            assertEquals(List.of(DayOfWeek.values()), tw2.days());
        }
    }

    @Test
    public void invalidWindows() {
        // Invalid time zone
        assertInvalidZone("foo", "Invalid time zone 'foo'");

        // Malformed day input
        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("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'");

        // Invalid date range
        assertInvalidDateRange("", "foo", "bar", "Could not parse date range 'foo' and 'bar'");
        assertInvalidDateRange("", "2022-01-15", "2022-01-01", "Invalid date range: start date 2022-01-15 is after end date 2022-01-01");
        assertInvalidDateRange("wed", "2022-01-06", "2022-01-09", "Invalid day: date range [2022-01-06, 2022-01-09] does not contain WEDNESDAY");
        assertInvalidDateRange("mon-sun", "2022-01-03", "2022-01-07", "Invalid day: date range [2022-01-03, 2022-01-07] does not contain SATURDAY");
    }

    private static void assertOutside(TimeWindow window, Instant instant) {
        assertFalse("Instant " + instant + " is not in window", window.includes(instant));
    }

    private static void assertInside(TimeWindow window, Instant instant) {
        assertTrue("Instant " + instant + " is in window", window.includes(instant));
    }

    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());
        }
    }

    private static void assertInvalidDateRange(String daySpec, String startDate, String endDate, String message) {
        try {
            TimeWindow.from(daySpec, "", "UTC", startDate, endDate);
            fail("Expected exception");
        } catch (IllegalArgumentException e) {
            assertEquals(message, e.getMessage());
        }
    }

}