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