]> git.basschouten.com Git - openhab-addons.git/blob
985cb80214bab21f96789cfed8f238cb410b44c3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.deutschebahn.internal.filter;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.util.regex.Pattern;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.deutschebahn.internal.EventAttribute;
22 import org.openhab.binding.deutschebahn.internal.EventAttributeSelection;
23 import org.openhab.binding.deutschebahn.internal.EventType;
24 import org.openhab.binding.deutschebahn.internal.TripLabelAttribute;
25 import org.openhab.binding.deutschebahn.internal.timetable.dto.Event;
26 import org.openhab.binding.deutschebahn.internal.timetable.dto.TimetableStop;
27 import org.openhab.binding.deutschebahn.internal.timetable.dto.TripLabel;
28
29 /**
30  * Tests for {@link TimetableStopByStringEventAttributeFilter}
31  * 
32  * @author Sönke Küper - Initial contribution.
33  */
34 @NonNullByDefault
35 public final class TimetableByStringEventAttributeFilterTest {
36
37     @Test
38     public void testFilterTripLabelAttribute() {
39         final TimetableStopByStringEventAttributeFilter filter = new TimetableStopByStringEventAttributeFilter(
40                 TripLabelAttribute.C, Pattern.compile("IC.*"));
41         final TimetableStop stop = new TimetableStop();
42
43         // TripLabel is not set -> does not match
44         assertFalse(filter.test(stop));
45
46         final TripLabel label = new TripLabel();
47         stop.setTl(label);
48
49         // Attribute is not set -> does not match
50         assertFalse(filter.test(stop));
51
52         // Set attribute -> matches depending on value
53         label.setC("RE");
54         assertFalse(filter.test(stop));
55         label.setC("ICE");
56         assertTrue(filter.test(stop));
57         label.setC("IC");
58         assertTrue(filter.test(stop));
59     }
60
61     @Test
62     public void testFilterEventAttribute() {
63         final EventAttributeSelection eventAttribute = new EventAttributeSelection(EventType.DEPARTURE,
64                 EventAttribute.L);
65         final TimetableStopByStringEventAttributeFilter filter = new TimetableStopByStringEventAttributeFilter(
66                 eventAttribute, Pattern.compile("RE.*"));
67         final TimetableStop stop = new TimetableStop();
68
69         // Event is not set -> does not match
70         assertFalse(filter.test(stop));
71
72         Event event = new Event();
73         stop.setDp(event);
74
75         // Attribute is not set -> does not match
76         assertFalse(filter.test(stop));
77
78         // Set attribute -> matches depending on value
79         event.setL("S5");
80         assertFalse(filter.test(stop));
81         event.setL("5");
82         assertFalse(filter.test(stop));
83         event.setL("RE60");
84         assertTrue(filter.test(stop));
85
86         // Set wrong event
87         stop.setAr(event);
88         stop.setDp(null);
89         assertFalse(filter.test(stop));
90     }
91
92     @Test
93     public void testFilterEventAttributeList() {
94         final EventAttributeSelection eventAttribute = new EventAttributeSelection(EventType.DEPARTURE,
95                 EventAttribute.PPTH);
96         final TimetableStopByStringEventAttributeFilter filter = new TimetableStopByStringEventAttributeFilter(
97                 eventAttribute, Pattern.compile("Hannover.*"));
98         final TimetableStop stop = new TimetableStop();
99         Event event = new Event();
100         stop.setDp(event);
101
102         event.setPpth("Hannover Hbf|Hannover-Kleefeld|Hannover Karl-Wiechert-Allee|Hannover Anderten-Misburg|Ahlten");
103         assertTrue(filter.test(stop));
104         event.setPpth(
105                 "Ahlten|Hannover Hbf|Hannover-Kleefeld|Hannover Karl-Wiechert-Allee|Hannover Anderten-Misburg|Ahlten");
106         assertTrue(filter.test(stop));
107         event.setPpth(
108                 "Wolfsburg Hbf|Fallersleben|Calberlah|Gifhorn|Leiferde(b Gifhorn)|Meinersen|Dedenhausen|Dollbergen|Immensen-Arpke");
109         assertFalse(filter.test(stop));
110     }
111 }