]> git.basschouten.com Git - openhab-addons.git/blob
e7d08eba6a3aaa26b1abada90ae167d76f725379
[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.vehicle;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.junit.jupiter.api.Assertions.assertNotNull;
17
18 import java.util.List;
19
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.mybmw.internal.handler.backend.JsonStringDeserializer;
23 import org.openhab.binding.mybmw.internal.util.FileReader;
24 import org.openhab.binding.mybmw.internal.utils.Constants;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.google.gson.Gson;
29
30 import ch.qos.logback.classic.Level;
31
32 /**
33  * 
34  * checks the vehicleBase response
35  * 
36  * @author Martin Grassl - Initial contribution
37  */
38 public class VehicleBaseTest {
39
40     private Logger logger = LoggerFactory.getLogger(VehicleBaseTest.class);
41
42     @BeforeEach
43     public void setupLogger() {
44         Logger root = LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
45
46         if ("debug".equals(System.getenv("LOG_LEVEL"))) {
47             ((ch.qos.logback.classic.Logger) root).setLevel(Level.DEBUG);
48         } else if ("trace".equals(System.getenv("LOG_LEVEL"))) {
49             ((ch.qos.logback.classic.Logger) root).setLevel(Level.TRACE);
50         }
51
52         logger.trace("tracing enabled");
53         logger.debug("debugging enabled");
54         logger.info("info enabled");
55     }
56
57     @Test
58     public void testVehicleBaseDeserializationByGson() {
59         String vehicleBaseJson = FileReader.fileToString("responses/MILD_HYBRID/vehicles_base.json");
60         Gson gson = new Gson();
61
62         VehicleBase[] vehicleBaseArray = gson.fromJson(vehicleBaseJson, VehicleBase[].class);
63
64         assertNotNull(vehicleBaseArray);
65     }
66
67     @Test
68     public void testVehicleBaseDeserializationByConverter() {
69         String vehicleBaseJson = FileReader.fileToString("responses/MILD_HYBRID/vehicles_base.json");
70
71         List<VehicleBase> vehicleBaseList = JsonStringDeserializer.getVehicleBaseList(vehicleBaseJson);
72
73         assertNotNull(vehicleBaseList);
74
75         assertEquals(1, vehicleBaseList.size(), "Number of Vehicles");
76         VehicleBase vehicle = vehicleBaseList.get(0);
77         assertEquals(Constants.ANONYMOUS + "MILD_HYBRID", vehicle.getVin(), "VIN");
78         assertEquals("M340i xDrive", vehicle.getAttributes().getModel(), "Model");
79         assertEquals(Constants.DRIVETRAIN_MILD_HYBRID, vehicle.getAttributes().getDriveTrain(), "DriveTrain");
80         assertEquals("bmw", vehicle.getAttributes().getBrand(), "Brand");
81         assertEquals(2022, vehicle.getAttributes().getYear(), "Year of Construction");
82     }
83 }