]> git.basschouten.com Git - openhab-addons.git/blob
c5507867274b0f2164714bdae7b831671880a39b
[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;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.*;
17
18 import java.util.function.Consumer;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.deutschebahn.internal.timetable.dto.TripLabel;
24 import org.openhab.binding.deutschebahn.internal.timetable.dto.TripType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.types.State;
27
28 /**
29  * Tests Mapping from {@link TripLabel} attribute values to openhab state values.
30  * 
31  * @author Sönke Küper - initial contribution.
32  */
33 @NonNullByDefault
34 @SuppressWarnings("unchecked")
35 public class TripLabelAttributeTest {
36
37     private <VALUE_TYPE, STATE_TYPE extends State> void doTestTripAttribute( //
38             String channelName, //
39             @Nullable String expectedChannelName, //
40             Consumer<TripLabel> setValue, //
41             VALUE_TYPE expectedValue, //
42             @Nullable STATE_TYPE expectedState, //
43             boolean performSetterTest) { //
44         final TripLabelAttribute<VALUE_TYPE, STATE_TYPE> attribute = (TripLabelAttribute<VALUE_TYPE, STATE_TYPE>) TripLabelAttribute
45                 .getByChannelName(channelName);
46         assertThat(attribute, is(not(nullValue())));
47         assertThat(attribute.getChannelTypeName(), is(expectedChannelName == null ? channelName : expectedChannelName));
48         assertThat(attribute.getValue(new TripLabel()), is(nullValue()));
49         assertThat(attribute.getState(new TripLabel()), is(nullValue()));
50
51         // Create a trip label and set the attribute value.
52         final TripLabel labelWithValueSet = new TripLabel();
53         setValue.accept(labelWithValueSet);
54
55         // then try get value and state.
56         assertThat(attribute.getValue(labelWithValueSet), is(expectedValue));
57         assertThat(attribute.getState(labelWithValueSet), is(expectedState));
58
59         // Try set Value in new Event
60         final TripLabel copyTarget = new TripLabel();
61         attribute.setValue(copyTarget, expectedValue);
62         if (performSetterTest) {
63             assertThat(attribute.getValue(copyTarget), is(expectedValue));
64         }
65     }
66
67     @Test
68     public void testGetNonExistingChannel() {
69         assertThat(TripLabelAttribute.getByChannelName("unkownChannel"), is(nullValue()));
70     }
71
72     @Test
73     public void testCategory() {
74         final String category = "ICE";
75         doTestTripAttribute("category", null, (TripLabel e) -> e.setC(category), category, new StringType(category),
76                 true);
77     }
78
79     @Test
80     public void testNumber() {
81         final String number = "4567";
82         doTestTripAttribute("number", null, (TripLabel e) -> e.setN(number), number, new StringType(number), true);
83     }
84
85     @Test
86     public void testOwner() {
87         final String owner = "W3";
88         doTestTripAttribute("owner", null, (TripLabel e) -> e.setO(owner), owner, new StringType(owner), true);
89     }
90
91     @Test
92     public void testFilterFlages() {
93         final String filter = "a";
94         doTestTripAttribute("filter-flags", null, (TripLabel e) -> e.setF(filter), filter, new StringType(filter),
95                 true);
96     }
97
98     @Test
99     public void testTripType() {
100         final TripType type = TripType.E;
101         doTestTripAttribute("trip-type", null, (TripLabel e) -> e.setT(type), type, new StringType("e"), true);
102     }
103 }