2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.deutschebahn.internal;
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.*;
18 import java.util.function.Consumer;
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;
29 * Tests Mapping from {@link TripLabel} attribute values to openhab state values.
31 * @author Sönke Küper - initial contribution.
34 @SuppressWarnings("unchecked")
35 public class TripLabelAttributeTest {
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()));
51 // Create a trip label and set the attribute value.
52 final TripLabel labelWithValueSet = new TripLabel();
53 setValue.accept(labelWithValueSet);
55 // then try get value and state.
56 assertThat(attribute.getValue(labelWithValueSet), is(expectedValue));
57 assertThat(attribute.getState(labelWithValueSet), is(expectedState));
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));
68 public void testGetNonExistingChannel() {
69 assertThat(TripLabelAttribute.getByChannelName("unkownChannel"), is(nullValue()));
73 public void testCategory() {
74 final String category = "ICE";
75 doTestTripAttribute("category", null, (TripLabel e) -> e.setC(category), category, new StringType(category),
80 public void testNumber() {
81 final String number = "4567";
82 doTestTripAttribute("number", null, (TripLabel e) -> e.setN(number), number, new StringType(number), true);
86 public void testOwner() {
87 final String owner = "W3";
88 doTestTripAttribute("owner", null, (TripLabel e) -> e.setO(owner), owner, new StringType(owner), true);
92 public void testFilterFlages() {
93 final String filter = "a";
94 doTestTripAttribute("filter-flags", null, (TripLabel e) -> e.setF(filter), filter, new StringType(filter),
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);