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.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;
24 import java.util.List;
26 import javax.measure.quantity.Energy;
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;
40 * The {@link ChargingStatisticsWrapper} tests stored fingerprint responses from BMW API
42 * @author Bernd Weymann - Initial contribution
43 * @author Martin Grassl - small import change
46 @SuppressWarnings("null")
47 public class ChargingStatisticsWrapper {
48 private ChargingStatisticsContainer chargeStatisticContainer;
50 public ChargingStatisticsWrapper(String content) {
51 chargeStatisticContainer = JsonStringDeserializer.getChargingStatistics(content);
55 * Test results auctomatically against json values
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));
71 @SuppressWarnings({ "unchecked", "rawtypes" })
72 private void checkResult(ChannelUID channelUID, State state) {
73 String cUid = channelUID.getIdWithoutGroup();
74 String gUid = channelUID.getGroupId();
77 QuantityType<Energy> qte;
80 assertTrue(state instanceof StringType);
81 st = (StringType) state;
83 case CHANNEL_GROUP_CHARGE_STATISTICS:
84 assertEquals(chargeStatisticContainer.getDescription(), st.toString(), "Statistics name");
87 assertFalse(true, "Channel " + channelUID + " " + state + " not found");
92 assertTrue(state instanceof DecimalType);
93 dt = ((DecimalType) state);
94 assertEquals(chargeStatisticContainer.getStatistics().getNumberOfChargingSessions(), dt.intValue(),
98 assertTrue(state instanceof QuantityType);
99 qte = ((QuantityType) state);
100 assertEquals(Units.KILOWATT_HOUR, qte.getUnit(), "kwh");
101 assertEquals(chargeStatisticContainer.getStatistics().getTotalEnergyCharged(), qte.intValue(),
105 // fail in case of unknown update
106 assertFalse(true, "Channel " + channelUID + " " + state + " not found");