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.energidataservice.internal.api.serialization;
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;
20 import java.time.Instant;
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;
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32 import com.google.gson.JsonParseException;
35 * Tests for {@link InstantDeserializer}.
37 * @author Jacob Laursen - Initial contribution
40 @ExtendWith(MockitoExtension.class)
41 public class InstantDeserializerTest {
43 private final Gson gson = new GsonBuilder().registerTypeAdapter(Instant.class, new InstantDeserializer()).create();
46 void instantWhenInvalidShouldThrowJsonParseException() {
47 assertThrows(JsonParseException.class, () -> {
48 gson.fromJson("\"invalid\"", Instant.class);
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))));