]> git.basschouten.com Git - openhab-addons.git/blob
e3b472c125bd5cfd0a59045fb1b95b6600d35828
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.bmwconnecteddrive.internal.dto;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.bmwconnecteddrive.internal.ConnectedDriveConstants.*;
17
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
22 import javax.measure.Unit;
23 import javax.measure.quantity.Length;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.bmwconnecteddrive.internal.ConnectedDriveConstants.VehicleType;
28 import org.openhab.binding.bmwconnecteddrive.internal.dto.statistics.LastTrip;
29 import org.openhab.binding.bmwconnecteddrive.internal.dto.statistics.LastTripContainer;
30 import org.openhab.binding.bmwconnecteddrive.internal.utils.Constants;
31 import org.openhab.binding.bmwconnecteddrive.internal.utils.Converter;
32 import org.openhab.core.library.types.DateTimeType;
33 import org.openhab.core.library.types.QuantityType;
34 import org.openhab.core.library.unit.ImperialUnits;
35 import org.openhab.core.library.unit.Units;
36 import org.openhab.core.thing.ChannelUID;
37 import org.openhab.core.types.State;
38
39 import com.google.gson.Gson;
40
41 /**
42  * The {@link TripWrapper} Test json responses from ConnectedDrive Portal
43  *
44  * @author Bernd Weymann - Initial contribution
45  */
46 @NonNullByDefault
47 @SuppressWarnings("null")
48 public class TripWrapper {
49     private static final Gson GSON = new Gson();
50     private static final Unit<Length> MILES = ImperialUnits.MILE;
51
52     private LastTrip lastTrip;
53     private boolean imperial;
54     private boolean isElectric;
55     private boolean hasFuel;
56     private boolean isHybrid;
57
58     private Map<String, State> specialHandlingMap = new HashMap<String, State>();
59
60     public TripWrapper(String type, boolean imperial, String statusJson) {
61         this.imperial = imperial;
62         hasFuel = type.equals(VehicleType.CONVENTIONAL.toString()) || type.equals(VehicleType.PLUGIN_HYBRID.toString())
63                 || type.equals(VehicleType.ELECTRIC_REX.toString());
64         isElectric = type.equals(VehicleType.PLUGIN_HYBRID.toString())
65                 || type.equals(VehicleType.ELECTRIC_REX.toString()) || type.equals(VehicleType.ELECTRIC.toString());
66         isHybrid = hasFuel && isElectric;
67         LastTripContainer container = GSON.fromJson(statusJson, LastTripContainer.class);
68         assertNotNull(container);
69         assertNotNull(container.lastTrip);
70         lastTrip = container.lastTrip;
71     }
72
73     /**
74      * Test results auctomatically against json values
75      *
76      * @param channels
77      * @param states
78      * @return
79      */
80     public boolean checkResults(@Nullable List<ChannelUID> channels, @Nullable List<State> states) {
81         assertNotNull(channels);
82         assertNotNull(states);
83         assertTrue(channels.size() == states.size(), "Same list sizes");
84         for (int i = 0; i < channels.size(); i++) {
85             checkResult(channels.get(i), states.get(i));
86         }
87         return true;
88     }
89
90     /**
91      * Add a specific check for a value e.g. hard coded "Upcoming Service" in order to check the right ordering
92      *
93      * @param specialHand
94      * @return
95      */
96     public TripWrapper append(Map<String, State> compareMap) {
97         specialHandlingMap.putAll(compareMap);
98         return this;
99     }
100
101     @SuppressWarnings({ "unchecked", "rawtypes" })
102     private void checkResult(ChannelUID channelUID, State state) {
103         String cUid = channelUID.getIdWithoutGroup();
104         QuantityType<Length> qt;
105         DateTimeType dtt;
106         switch (cUid) {
107             case DATE:
108                 assertTrue(state instanceof DateTimeType);
109                 dtt = ((DateTimeType) state);
110                 DateTimeType expected = DateTimeType.valueOf(Converter.getLocalDateTimeWithoutOffest(lastTrip.date));
111                 assertEquals(expected.toString(), dtt.toString(), "Trip Date");
112                 break;
113             case DURATION:
114                 assertTrue(state instanceof QuantityType);
115                 qt = ((QuantityType) state);
116                 assertEquals(Units.MINUTE, qt.getUnit(), "Minute");
117                 assertEquals(lastTrip.duration, qt.floatValue(), 0.1, "Duration");
118                 break;
119             case DISTANCE:
120                 assertTrue(state instanceof QuantityType);
121                 qt = ((QuantityType) state);
122                 if (imperial) {
123                     assertEquals(MILES, qt.getUnit(), "Miles");
124                     assertEquals(lastTrip.totalDistance / Converter.MILES_TO_KM_RATIO, qt.floatValue(), 0.1,
125                             "Distance");
126
127                 } else {
128                     assertEquals(Constants.KILOMETRE_UNIT, qt.getUnit(), "KM");
129                     assertEquals(lastTrip.totalDistance, qt.floatValue(), 0.1, "Distance");
130                 }
131                 break;
132             case AVG_CONSUMPTION:
133                 assertTrue(state instanceof QuantityType);
134                 qt = ((QuantityType) state);
135                 assertEquals(Units.KILOWATT_HOUR, qt.getUnit(), "kw/h");
136                 if (imperial) {
137                     assertEquals(lastTrip.avgElectricConsumption * Converter.MILES_TO_KM_RATIO, qt.floatValue(), 0.1,
138                             "Avg Consumption");
139                 } else {
140                     assertEquals(lastTrip.avgElectricConsumption, qt.floatValue(), 0.1, "Avg Consumption");
141                 }
142                 break;
143             case AVG_COMBINED_CONSUMPTION:
144                 assertTrue(isHybrid, "Is Hybrid");
145                 assertTrue(state instanceof QuantityType);
146                 qt = ((QuantityType) state);
147                 assertEquals(Units.LITRE, qt.getUnit(), "Liter");
148                 if (imperial) {
149                     assertEquals(Converter.round(lastTrip.avgCombinedConsumption * Converter.MILES_TO_KM_RATIO),
150                             qt.floatValue(), 0.01, "Percent");
151                 } else {
152                     assertEquals(Converter.round(lastTrip.avgCombinedConsumption), qt.floatValue(), 0.01, "Percent");
153                 }
154                 break;
155             case AVG_RECUPERATION:
156                 assertTrue(state instanceof QuantityType);
157                 qt = ((QuantityType) state);
158                 assertEquals(Units.KILOWATT_HOUR, qt.getUnit(), "kw/h");
159                 if (imperial) {
160                     assertEquals(lastTrip.avgRecuperation * Converter.MILES_TO_KM_RATIO, qt.floatValue(), 0.1,
161                             "Avg Recuperation");
162                 } else {
163                     assertEquals(lastTrip.avgRecuperation, qt.floatValue(), 0.1, "Avg Recuperation");
164                 }
165                 break;
166             default:
167                 // fail in case of unknown update
168                 assertFalse(true, "Channel " + channelUID + " " + state + " not found");
169                 break;
170         }
171     }
172 }