Add tests for gson (#5182)

* Add tests for gson

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich
2025-12-15 22:33:25 +01:00
committed by GitHub
parent b45a3b1203
commit c3deec6d21
5 changed files with 249 additions and 0 deletions
@@ -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");
}
}
@@ -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 + '}';
}
}
@@ -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 + '}';
}
}
@@ -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 + '}';
}
}
@@ -0,0 +1,6 @@
{
"name": "John Doe",
"age": 30,
"email": "john.doe@example.com",
"active": true
}