added json module
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 10m33s
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 10m33s
This commit is contained in:
parent
7d01a51fc8
commit
054aa67d34
6 changed files with 166 additions and 0 deletions
|
@ -0,0 +1,80 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.json;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@QuarkusTest
|
||||
class JsonServiceImplTest {
|
||||
|
||||
public static final TypeReference<List<TestRecord>> TYPE_REFERENCE = new TypeReference<>() {
|
||||
};
|
||||
|
||||
record TestRecord(String name, int age) {
|
||||
}
|
||||
|
||||
@Inject
|
||||
JsonServiceImpl jsonService;
|
||||
|
||||
@Test
|
||||
void toJson() {
|
||||
var expected = "{\"name\":\"John Doe\",\"age\":30}";
|
||||
|
||||
var actual = jsonService.toJson(new TestRecord("John Doe", 30));
|
||||
|
||||
assertThat(actual)
|
||||
.as("Json should match expected value")
|
||||
.isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromJsonWithClass() {
|
||||
var json = "{\"name\":\"John Doe\",\"age\":30}";
|
||||
var expected = new TestRecord("John Doe", 30);
|
||||
|
||||
var actual = jsonService.fromJson(json, TestRecord.class);
|
||||
|
||||
assertThat(actual)
|
||||
.as("Deserialized object should match expected value")
|
||||
.isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromJsonWithClassWhenInputIsInvalid() {
|
||||
var json = "{\"name\":\"John Doe\",\"age\":\"30\"";
|
||||
|
||||
assertThatThrownBy(() -> jsonService.fromJson(json, TestRecord.class))
|
||||
.as("Should throw IllegalArgumentException when input is invalid")
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("Unable to read object of class [ch.phoenixtechnologies.quarkus.commons.json.JsonServiceImplTest$TestRecord] from json String: {\"name\":\"John Doe\",\"age\":\"30\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromJsonWithTypeReference() {
|
||||
var json = "[{\"name\":\"John Doe\",\"age\":30},{\"name\":\"Jane Doe\",\"age\":25}]";
|
||||
var expected = List.of(new TestRecord("John Doe", 30), new TestRecord("Jane Doe", 25));
|
||||
|
||||
var actual = jsonService.fromJson(json, TYPE_REFERENCE);
|
||||
|
||||
Assertions.assertThat(actual)
|
||||
.as("Deserialized object should match expected value")
|
||||
.isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromJsonWithTypeReferenceWhenInputIsInvalid() {
|
||||
var json = "{\"name\":\"John Doe\",\"age\":30},{\"name\":\"Jane Doe\",\"age\":\"25\"}";
|
||||
|
||||
assertThatThrownBy(() -> jsonService.fromJson(json, TYPE_REFERENCE))
|
||||
.as("Should throw IllegalArgumentException when input is invalid")
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("Unable to read object of type [java.util.List<ch.phoenixtechnologies.quarkus.commons.json.JsonServiceImplTest$TestRecord>] from json String: {\"name\":\"John Doe\",\"age\":30},{\"name\":\"Jane Doe\",\"age\":\"25\"}");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue