diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/gson/GsonTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/gson/GsonTest.java new file mode 100644 index 000000000..08c2bdefd --- /dev/null +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/gson/GsonTest.java @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2010-2025 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.core.gson; + +import static org.junit.jupiter.api.Assertions.*; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.junit.jupiter.api.Test; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +/** + * Gson deserialization, DTOs. + * + * @author Holger Friedrich - Initial contribution + */ +@NonNullByDefault +class GsonTest { + + private static final Gson GSON = new GsonBuilder().create(); + + /** + * Load the test JSON payload string from a file (copied from hue binding) + */ + private String load(String fileName) { + try (FileReader file = new FileReader(String.format("src/test/resources/%s.json", fileName)); + BufferedReader reader = new BufferedReader(file)) { + StringBuilder builder = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + builder.append(line).append("\n"); + } + return builder.toString(); + } catch (IOException e) { + fail(e.getMessage()); + } + return ""; + } + + @Test + void testLoadAndDeserializeJson() { + // Load JSON from file + String json = load("testdata"); + assertNotNull(json, "JSON content should not be null"); + assertFalse(json.isEmpty(), "JSON content should not be empty"); + + // Deserialize into GsonTestClass_DTO + GsonTestClass_DTO test = GSON.fromJson(json, GsonTestClass_DTO.class); + assertNotNull(test, "Deserialized object should not be null"); + + // Verify all fields are correctly deserialized + assertEquals("John Doe", test.name, "Name field should match"); + assertEquals(30, test.age, "Age field should match"); + assertEquals("john.doe@example.com", test.email, "Email field should match"); + assertTrue(test.active, "Active field should match"); + } + + @Test + void testLoadAndDeserializeAnnotatedJson() { + // Load JSON from file + String json = load("testdata"); + assertNotNull(json, "JSON content should not be null"); + assertFalse(json.isEmpty(), "JSON content should not be empty"); + + // Deserialize into GsonTestClassAnnotated_DTO + GsonTestClassAnnotated_DTO test = GSON.fromJson(json, GsonTestClassAnnotated_DTO.class); + assertNotNull(test, "Deserialized object should not be null"); + + // Verify all fields are correctly deserialized + assertEquals("John Doe", test.n, "Name field should match"); + assertEquals(30, test.age, "Age field should match"); + assertTrue(test.active, "Active field should match"); + assertEquals("john.doe@example.com", test.e, "Email field should match"); + } + + @Test + void testLoadAndDeserializeAnnotated2Json() { + // Load JSON from file + String json = load("testdata"); + assertNotNull(json, "JSON content should not be null"); + assertFalse(json.isEmpty(), "JSON content should not be empty"); + + // Deserialize into GsonTestClassAnnotated2_DTO + GsonTestClassAnnotated2_DTO test = GSON.fromJson(json, GsonTestClassAnnotated2_DTO.class); + assertNotNull(test, "Deserialized object should not be null"); + + // Verify all fields are correctly deserialized + assertEquals("John Doe", test.n, "Name field should match"); + assertEquals(30, test.age, "Age field should match"); + assertTrue(test.active, "Active field should match"); + assertEquals("john.doe@example.com", test.e, "Email field should match"); + } +} diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/gson/GsonTestClassAnnotated2_DTO.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/gson/GsonTestClassAnnotated2_DTO.java new file mode 100644 index 000000000..b40726959 --- /dev/null +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/gson/GsonTestClassAnnotated2_DTO.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2010-2025 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.core.gson; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + +import com.google.gson.annotations.SerializedName; + +/** + * Example Test for testing Gson deserialization. + * + * @author Holger Friedrich - Initial contribution + */ +@NonNullByDefault +public class GsonTestClassAnnotated2_DTO { + public @SerializedName("name") @Nullable String n; + public @Nullable @SerializedName("email") String e; + public int age; + public boolean active; + + public GsonTestClassAnnotated2_DTO() { + } + + public GsonTestClassAnnotated2_DTO(String name, int age, String email, boolean active) { + this.n = name; + this.e = email; + this.age = age; + this.active = active; + } + + @Override + public String toString() { + return "GsonTestClassAnnotated2_DTO{" + "name='" + n + '\'' + ", age=" + age + ", email='" + e + '\'' + + ", active=" + active + '}'; + } +} diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/gson/GsonTestClassAnnotated_DTO.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/gson/GsonTestClassAnnotated_DTO.java new file mode 100644 index 000000000..757686c23 --- /dev/null +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/gson/GsonTestClassAnnotated_DTO.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2010-2025 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.core.gson; + +import org.eclipse.jdt.annotation.NonNullByDefault; + +import com.google.gson.annotations.SerializedName; + +/** + * Example Test for testing Gson deserialization. + * + * @author Holger Friedrich - Initial contribution + */ +@NonNullByDefault +public class GsonTestClassAnnotated_DTO { + // adding annotations in different order is intended, please do not sort! + public @SerializedName("name") @NonNullByDefault({}) String n = null; + public @NonNullByDefault({}) @SerializedName("email") String e = null; + public int age; + public boolean active; + + public GsonTestClassAnnotated_DTO() { + } + + public GsonTestClassAnnotated_DTO(String name, int age, String email, boolean active) { + this.n = name; + this.e = email; + this.age = age; + this.active = active; + } + + @Override + public String toString() { + return "GsonTestClassAnnotated_DTO{" + "name='" + n + '\'' + ", age=" + age + ", email='" + e + '\'' + + ", active=" + active + '}'; + } +} diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/gson/GsonTestClass_DTO.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/gson/GsonTestClass_DTO.java new file mode 100644 index 000000000..5736b4f68 --- /dev/null +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/gson/GsonTestClass_DTO.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2010-2025 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.core.gson; + +/** + * Example Test for testing Gson deserialization. + * + * @author Holger Friedrich - Initial contribution + */ +public class GsonTestClass_DTO { + public String name; + public int age; + public String email; + public boolean active; + + public GsonTestClass_DTO() { + } + + public GsonTestClass_DTO(String name, int age, String email, boolean active) { + this.name = name; + this.age = age; + this.email = email; + this.active = active; + } + + @Override + public String toString() { + return "GsonTestClass{" + "name='" + name + '\'' + ", age=" + age + ", email='" + email + '\'' + ", active=" + + active + '}'; + } +} diff --git a/bundles/org.openhab.core/src/test/resources/testdata.json b/bundles/org.openhab.core/src/test/resources/testdata.json new file mode 100644 index 000000000..f165d1248 --- /dev/null +++ b/bundles/org.openhab.core/src/test/resources/testdata.json @@ -0,0 +1,6 @@ +{ + "name": "John Doe", + "age": 30, + "email": "john.doe@example.com", + "active": true +}