]> git.basschouten.com Git - openhab-addons.git/blob
92a9c23c512f28d3cb0854ac267bca9392f71256
[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.Instant;
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.junit.jupiter.params.ParameterizedTest;
27 import org.junit.jupiter.params.provider.ValueSource;
28 import org.mockito.junit.jupiter.MockitoExtension;
29
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32 import com.google.gson.JsonParseException;
33
34 /**
35  * Tests for {@link InstantDeserializer}.
36  * 
37  * @author Jacob Laursen - Initial contribution
38  */
39 @NonNullByDefault
40 @ExtendWith(MockitoExtension.class)
41 public class InstantDeserializerTest {
42
43     private final Gson gson = new GsonBuilder().registerTypeAdapter(Instant.class, new InstantDeserializer()).create();
44
45     @Test
46     void instantWhenInvalidShouldThrowJsonParseException() {
47         assertThrows(JsonParseException.class, () -> {
48             gson.fromJson("\"invalid\"", Instant.class);
49         });
50     }
51
52     @ParameterizedTest
53     @ValueSource(strings = { "\"2023-04-17T20:38:01Z\"", "\"2023-04-17T20:38:01\"" })
54     void instantWhenValidShouldParse(String input) {
55         assertThat((@Nullable Instant) gson.fromJson(input, Instant.class),
56                 is(equalTo(Instant.ofEpochSecond(1681763881))));
57     }
58 }