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.mybmw.internal.dto;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.mybmw.internal.MyBMWConstants.*;
18 import java.util.List;
20 import javax.measure.quantity.Energy;
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;
34 * The {@link ChargeStatisticWrapper} tests stored fingerprint responses from BMW API
36 * @author Bernd Weymann - Initial contribution
39 @SuppressWarnings("null")
40 public class ChargeStatisticWrapper {
41 private ChargeStatisticsContainer chargeStatisticContainer;
43 public ChargeStatisticWrapper(String content) {
44 ChargeStatisticsContainer fromJson = Converter.getGson().fromJson(content, ChargeStatisticsContainer.class);
45 if (fromJson != null) {
46 chargeStatisticContainer = fromJson;
48 chargeStatisticContainer = new ChargeStatisticsContainer();
53 * Test results auctomatically against json values
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));
69 @SuppressWarnings({ "unchecked", "rawtypes" })
70 private void checkResult(ChannelUID channelUID, State state) {
71 String cUid = channelUID.getIdWithoutGroup();
72 String gUid = channelUID.getGroupId();
75 QuantityType<Energy> qte;
78 assertTrue(state instanceof StringType);
79 st = (StringType) state;
81 case CHANNEL_GROUP_CHARGE_STATISTICS:
82 assertEquals(chargeStatisticContainer.description, st.toString(), "Statistics name");
85 assertFalse(true, "Channel " + channelUID + " " + state + " not found");
90 assertTrue(state instanceof DecimalType);
91 dt = ((DecimalType) state);
92 assertEquals(chargeStatisticContainer.statistics.numberOfChargingSessions, dt.intValue(),
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");
102 // fail in case of unknown update
103 assertFalse(true, "Channel " + channelUID + " " + state + " not found");