]> git.basschouten.com Git - openhab-addons.git/blob
1e331c34500613bb47a3ff1f5887b0a724af5247
[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 = """
39                 [
40                   {
41                     "IdPatient": 1,
42                     "LastName": "Mustermann",
43                     "FirstName": "Max",
44                     "Devices": [
45                       {
46                         "IdDevice": 2,
47                         "SerialNumber": "001",
48                         "Name": "Test Sitzkissen",
49                         "Events": [
50                           {
51                             "EventDef": "Alarm",
52                             "DateEntry": "2021-11-22T10:17:56.2866667"
53                           }
54                         ]
55                       }
56                     ]
57                   }
58                 ]\
59                 """;
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);
64         arrayString = """
65                 [
66                   {
67                     "IdPatient": 1,
68                     "LastName": "Mustermann",
69                     "FirstName": "Max",
70                     "Devices": [
71                       {
72                         "IdDevice": 2,
73                         "SerialNumber": "001",
74                         "Name": "Test Sitzkissen"
75                       }
76                     ]
77                   }
78                 ]\
79                 """;
80         array = gson.fromJson(arrayString, JsonArray.class);
81         assertNull(SensorThingHandler.getLatestValueFromJsonArray(array));
82     }
83
84     @Test
85     public void dateFormatTest2() {
86         try {
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) {
91         }
92     }
93 }