]> git.basschouten.com Git - openhab-addons.git/blob
b582e1d3a96dc43fcb8f2c503fbdcaa9d6d06add
[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.mybmw.internal.dto;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.mybmw.internal.MyBMWConstants.*;
17
18 import java.util.List;
19
20 import javax.measure.quantity.Energy;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.mybmw.internal.dto.charge.ChargeStatisticsContainer;
25 import org.openhab.binding.mybmw.internal.utils.Converter;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.types.StringType;
29 import org.openhab.core.library.unit.Units;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.types.State;
32
33 /**
34  * The {@link ChargeStatisticWrapper} tests stored fingerprint responses from BMW API
35  *
36  * @author Bernd Weymann - Initial contribution
37  */
38 @NonNullByDefault
39 @SuppressWarnings("null")
40 public class ChargeStatisticWrapper {
41     private ChargeStatisticsContainer chargeStatisticContainer;
42
43     public ChargeStatisticWrapper(String content) {
44         ChargeStatisticsContainer fromJson = Converter.getGson().fromJson(content, ChargeStatisticsContainer.class);
45         if (fromJson != null) {
46             chargeStatisticContainer = fromJson;
47         } else {
48             chargeStatisticContainer = new ChargeStatisticsContainer();
49         }
50     }
51
52     /**
53      * Test results auctomatically against json values
54      *
55      * @param channels
56      * @param states
57      * @return
58      */
59     public boolean checkResults(@Nullable List<ChannelUID> channels, @Nullable List<State> states) {
60         assertNotNull(channels);
61         assertNotNull(states);
62         assertTrue(channels.size() == states.size(), "Same list sizes");
63         for (int i = 0; i < channels.size(); i++) {
64             checkResult(channels.get(i), states.get(i));
65         }
66         return true;
67     }
68
69     @SuppressWarnings({ "unchecked", "rawtypes" })
70     private void checkResult(ChannelUID channelUID, State state) {
71         String cUid = channelUID.getIdWithoutGroup();
72         String gUid = channelUID.getGroupId();
73         StringType st;
74         DecimalType dt;
75         QuantityType<Energy> qte;
76         switch (cUid) {
77             case TITLE:
78                 assertTrue(state instanceof StringType);
79                 st = (StringType) state;
80                 switch (gUid) {
81                     case CHANNEL_GROUP_CHARGE_STATISTICS:
82                         assertEquals(chargeStatisticContainer.description, st.toString(), "Statistics name");
83                         break;
84                     default:
85                         assertFalse(true, "Channel " + channelUID + " " + state + " not found");
86                         break;
87                 }
88                 break;
89             case SESSIONS:
90                 assertTrue(state instanceof DecimalType);
91                 dt = ((DecimalType) state);
92                 assertEquals(chargeStatisticContainer.statistics.numberOfChargingSessions, dt.intValue(),
93                         "Charge Sessions");
94                 break;
95             case ENERGY:
96                 assertTrue(state instanceof QuantityType);
97                 qte = ((QuantityType) state);
98                 assertEquals(Units.KILOWATT_HOUR, qte.getUnit(), "kwh");
99                 assertEquals(chargeStatisticContainer.statistics.totalEnergyCharged, qte.intValue(), "Energy");
100                 break;
101             default:
102                 // fail in case of unknown update
103                 assertFalse(true, "Channel " + channelUID + " " + state + " not found");
104                 break;
105         }
106     }
107 }