]> git.basschouten.com Git - openhab-addons.git/blob
b0204babcdeea3f7dc96648d98514cd17004b1e8
[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.mcd.internal.handler;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.junit.jupiter.api.Assertions.assertNull;
17
18 import java.text.SimpleDateFormat;
19 import java.util.Date;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23
24 import com.google.gson.Gson;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonObject;
27
28 /**
29  * @author Dengler - Initial contribution
30  */
31 @NonNullByDefault
32 public class SensorThingHandlerTest {
33
34     private final Gson gson = new Gson();
35
36     @Test
37     public void getLatestValueFromJsonObjectTest() {
38         String arrayString = "[\n" + "  {\n" + "    \"IdPatient\": 1,\n" + "    \"LastName\": \"Mustermann\",\n"
39                 + "    \"FirstName\": \"Max\",\n" + "    \"Devices\": [\n" + "      {\n" + "        \"IdDevice\": 2,\n"
40                 + "        \"SerialNumber\": \"001\",\n" + "        \"Name\": \"Test Sitzkissen\",\n"
41                 + "        \"Events\": [\n" + "          {\n" + "            \"EventDef\": \"Alarm\",\n"
42                 + "            \"DateEntry\": \"2021-11-22T10:17:56.2866667\"\n" + "          }\n" + "        ]\n"
43                 + "      }\n" + "    ]\n" + "  }\n" + "]";
44         JsonArray array = gson.fromJson(arrayString, JsonArray.class);
45         JsonObject object = SensorThingHandler.getLatestValueFromJsonArray(array);
46         String string = object != null ? object.toString() : null;
47         assertEquals("{\"EventDef\":\"Alarm\",\"DateEntry\":\"2021-11-22T10:17:56.2866667\"}", string);
48         arrayString = "[\n" + "  {\n" + "    \"IdPatient\": 1,\n" + "    \"LastName\": \"Mustermann\",\n"
49                 + "    \"FirstName\": \"Max\",\n" + "    \"Devices\": [\n" + "      {\n" + "        \"IdDevice\": 2,\n"
50                 + "        \"SerialNumber\": \"001\",\n" + "        \"Name\": \"Test Sitzkissen\"\n" + "      }\n"
51                 + "    ]\n" + "  }\n" + "]";
52         array = gson.fromJson(arrayString, JsonArray.class);
53         assertNull(SensorThingHandler.getLatestValueFromJsonArray(array));
54     }
55
56     @Test
57     public void dateFormatTest2() {
58         try {
59             Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse("2021-11-22T14:00:09.9933333");
60             String dateString = new SimpleDateFormat("yyyy-MM-dd', 'HH:mm:ss").format(date);
61             assertEquals("2021-11-22, 14:00:09", dateString);
62         } catch (Exception e) {
63         }
64     }
65 }