]> git.basschouten.com Git - openhab-addons.git/blob
116a8a87a79d0cb0864b54299e69b9daa2037fca
[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.energidataservice.internal.api.serialization;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.equalTo;
17 import static org.hamcrest.Matchers.is;
18 import static org.junit.jupiter.api.Assertions.assertThrows;
19
20 import java.time.LocalDateTime;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.junit.jupiter.MockitoExtension;
27
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.JsonParseException;
31
32 /**
33  * Tests for {@link LocalDateDeserializer}.
34  * 
35  * @author Jacob Laursen - Initial contribution
36  */
37 @NonNullByDefault
38 @ExtendWith(MockitoExtension.class)
39 public class LocalDateDeserializerTest {
40
41     private final Gson gson = new GsonBuilder()
42             .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeDeserializer()).create();
43
44     @Test
45     void localDateTimeWhenInvalidShouldThrowJsonParseException() {
46         assertThrows(JsonParseException.class, () -> {
47             gson.fromJson("\"invalid\"", LocalDateTime.class);
48         });
49     }
50
51     @Test
52     void instantWhenValidShouldParse() {
53         assertThat((@Nullable LocalDateTime) gson.fromJson("\"2023-04-17T20:38:01\"", LocalDateTime.class),
54                 is(equalTo(LocalDateTime.of(2023, 4, 17, 20, 38, 1, 0))));
55     }
56 }