Fail if superfluous properties are used in property metadata

Closes gh-37597
This commit is contained in:
Moritz Halbritter
2023-11-30 14:29:19 +01:00
parent 970c226847
commit 25614710d5
2 changed files with 232 additions and 24 deletions

View File

@@ -20,12 +20,14 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* Tests for {@link JsonMarshaller}.
@@ -38,14 +40,15 @@ class JsonMarshallerTests {
@Test
void marshallAndUnmarshal() throws Exception {
ConfigurationMetadata metadata = new ConfigurationMetadata();
metadata.add(ItemMetadata.newProperty("a", "b", StringBuffer.class.getName(), InputStream.class.getName(),
"sourceMethod", "desc", "x", new ItemDeprecation("Deprecation comment", "b.c.d", "1.2.3")));
metadata.add(ItemMetadata.newProperty("a", "b", StringBuffer.class.getName(), InputStream.class.getName(), null,
"desc", "x", new ItemDeprecation("Deprecation comment", "b.c.d", "1.2.3")));
metadata.add(ItemMetadata.newProperty("b.c.d", null, null, null, null, null, null, null));
metadata.add(ItemMetadata.newProperty("c", null, null, null, null, null, 123, null));
metadata.add(ItemMetadata.newProperty("d", null, null, null, null, null, true, null));
metadata.add(ItemMetadata.newProperty("e", null, null, null, null, null, new String[] { "y", "n" }, null));
metadata.add(ItemMetadata.newProperty("f", null, null, null, null, null, new Boolean[] { true, false }, null));
metadata.add(ItemMetadata.newGroup("d", null, null, null));
metadata.add(ItemMetadata.newGroup("e", null, null, "sourceMethod"));
metadata.add(ItemHint.newHint("a.b"));
metadata.add(ItemHint.newHint("c", new ItemHint.ValueHint(123, "hey"), new ItemHint.ValueHint(456, null)));
metadata.add(new ItemHint("d", null,
@@ -66,6 +69,7 @@ class JsonMarshallerTests {
assertThat(read).has(Metadata.withProperty("e").withDefaultValue(new String[] { "y", "n" }));
assertThat(read).has(Metadata.withProperty("f").withDefaultValue(new Object[] { true, false }));
assertThat(read).has(Metadata.withGroup("d"));
assertThat(read).has(Metadata.withGroup("e").fromSourceMethod("sourceMethod"));
assertThat(read).has(Metadata.withHint("a.b"));
assertThat(read).has(Metadata.withHint("c").withValue(0, 123, "hey").withValue(1, 456, null));
assertThat(read).has(Metadata.withHint("d").withProvider("first", "target", "foo").withProvider("second"));
@@ -170,4 +174,159 @@ class JsonMarshallerTests {
"\"java.lang.Boolean\"", "\"com.example.bravo.aaa\"", "\"java.lang.Integer\"", "\"com.example.Bar");
}
@Test
void shouldCheckRootFields() {
String json = """
{
"groups": [], "properties": [], "hints": [], "dummy": []
}""";
assertThatException().isThrownBy(() -> read(json))
.withMessage("Expected only keys [groups, hints, properties], but found additional keys [dummy]. Path: .");
}
@Test
void shouldCheckGroupFields() {
String json = """
{
"groups": [
{
"name": "g",
"type": "java.lang.String",
"description": "Some description",
"sourceType": "java.lang.String",
"sourceMethod": "some()",
"dummy": "dummy"
}
], "properties": [], "hints": []
}""";
assertThatException().isThrownBy(() -> read(json))
.withMessage(
"Expected only keys [description, name, sourceMethod, sourceType, type], but found additional keys [dummy]. Path: .groups.[0]");
}
@Test
void shouldCheckPropertyFields() {
String json = """
{
"groups": [], "properties": [
{
"name": "name",
"type": "java.lang.String",
"description": "Some description",
"sourceType": "java.lang.String",
"defaultValue": "value",
"deprecation": {
"level": "warning",
"reason": "some reason",
"replacement": "name-new",
"since": "v17"
},
"deprecated": true,
"dummy": "dummy"
}
], "hints": []
}""";
assertThatException().isThrownBy(() -> read(json))
.withMessage(
"Expected only keys [defaultValue, deprecated, deprecation, description, name, sourceType, type], but found additional keys [dummy]. Path: .properties.[0]");
}
@Test
void shouldCheckPropertyDeprecationFields() {
String json = """
{
"groups": [], "properties": [
{
"name": "name",
"type": "java.lang.String",
"description": "Some description",
"sourceType": "java.lang.String",
"defaultValue": "value",
"deprecation": {
"level": "warning",
"reason": "some reason",
"replacement": "name-new",
"since": "v17",
"dummy": "dummy"
},
"deprecated": true
}
], "hints": []
}""";
assertThatException().isThrownBy(() -> read(json))
.withMessage(
"Expected only keys [level, reason, replacement, since], but found additional keys [dummy]. Path: .properties.[0].deprecation");
}
@Test
void shouldCheckHintFields() {
String json = """
{
"groups": [], "properties": [], "hints": [
{
"name": "name",
"values": [],
"providers": [],
"dummy": "dummy"
}
]
}""";
assertThatException().isThrownBy(() -> read(json))
.withMessage(
"Expected only keys [name, providers, values], but found additional keys [dummy]. Path: .hints.[0]");
}
@Test
void shouldCheckHintValueFields() {
String json = """
{
"groups": [], "properties": [], "hints": [
{
"name": "name",
"values": [
{
"value": "value",
"description": "some description",
"dummy": "dummy"
}
],
"providers": []
}
]
}""";
assertThatException().isThrownBy(() -> read(json))
.withMessage(
"Expected only keys [description, value], but found additional keys [dummy]. Path: .hints.[0].values.[0]");
}
@Test
void shouldCheckHintProviderFields() {
String json = """
{
"groups": [], "properties": [], "hints": [
{
"name": "name",
"values": [],
"providers": [
{
"name": "name",
"parameters": {
"target": "jakarta.servlet.http.HttpServlet"
},
"dummy": "dummy"
}
]
}
]
}""";
assertThatException().isThrownBy(() -> read(json))
.withMessage(
"Expected only keys [name, parameters], but found additional keys [dummy]. Path: .hints.[0].providers.[0]");
}
private void read(String json) throws Exception {
JsonMarshaller marshaller = new JsonMarshaller();
marshaller.read(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)));
}
}