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.mcd.internal.handler;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.junit.jupiter.api.Assertions.assertNull;
18 import java.text.SimpleDateFormat;
19 import java.util.Date;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
24 import com.google.gson.Gson;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonObject;
29 * @author Dengler - Initial contribution
32 public class SensorThingHandlerTest {
34 private final Gson gson = new Gson();
37 public void getLatestValueFromJsonObjectTest() {
38 String arrayString = """
42 "LastName": "Mustermann",
47 "SerialNumber": "001",
48 "Name": "Test Sitzkissen",
52 "DateEntry": "2021-11-22T10:17:56.2866667"
60 JsonArray array = gson.fromJson(arrayString, JsonArray.class);
61 JsonObject object = SensorThingHandler.getLatestValueFromJsonArray(array);
62 String string = object != null ? object.toString() : null;
63 assertEquals("{\"EventDef\":\"Alarm\",\"DateEntry\":\"2021-11-22T10:17:56.2866667\"}", string);
68 "LastName": "Mustermann",
73 "SerialNumber": "001",
74 "Name": "Test Sitzkissen"
80 array = gson.fromJson(arrayString, JsonArray.class);
81 assertNull(SensorThingHandler.getLatestValueFromJsonArray(array));
85 public void dateFormatTest2() {
87 Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse("2021-11-22T14:00:09.9933333");
88 String dateString = new SimpleDateFormat("yyyy-MM-dd', 'HH:mm:ss").format(date);
89 assertEquals("2021-11-22, 14:00:09", dateString);
90 } catch (Exception e) {