Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration item metadata ordering is not consistent #26230

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,26 +160,18 @@ private Object extractItemValue(Object value) {

private static class ItemMetadataComparator implements Comparator<ItemMetadata> {

private final Comparator<ItemMetadata> itemComparator = Comparator.comparing(this::isDeprecated)
.thenComparing(ItemMetadata::getName).thenComparing(ItemMetadata::getSourceType);

private final Comparator<ItemMetadata> groupComparator = Comparator.comparing(ItemMetadata::getName)
.thenComparing(ItemMetadata::getSourceType);

@Override
public int compare(ItemMetadata o1, ItemMetadata o2) {
if (o1.isOfItemType(ItemType.GROUP)) {
return compareGroup(o1, o2);
}
return compareProperty(o1, o2);
}

private int compareGroup(ItemMetadata o1, ItemMetadata o2) {
return o1.getName().compareTo(o2.getName());
}

private int compareProperty(ItemMetadata o1, ItemMetadata o2) {
if (isDeprecated(o1) && !isDeprecated(o2)) {
return 1;
}
if (isDeprecated(o2) && !isDeprecated(o1)) {
return -1;
return this.groupComparator.compare(o1, o2);
zeldigas marked this conversation as resolved.
Show resolved Hide resolved
}
return o1.getName().compareTo(o2.getName());
return this.itemComparator.compare(o1, o2);
zeldigas marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean isDeprecated(ItemMetadata item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void marshallOrderItems() throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonMarshaller marshaller = new JsonMarshaller();
marshaller.write(metadata, outputStream);
String json = new String(outputStream.toByteArray());
String json = outputStream.toString();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ide suggestion to simplify statement

assertThat(json).containsSubsequence("\"groups\"", "\"com.acme.alpha\"", "\"com.acme.bravo\"", "\"properties\"",
"\"com.example.alpha.ccc\"", "\"com.example.alpha.ddd\"", "\"com.example.bravo.aaa\"",
"\"com.example.bravo.bbb\"", "\"hints\"", "\"eee\"", "\"fff\"");
Expand All @@ -100,9 +100,43 @@ void marshallPutDeprecatedItemsAtTheEnd() throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonMarshaller marshaller = new JsonMarshaller();
marshaller.write(metadata, outputStream);
String json = new String(outputStream.toByteArray());
String json = outputStream.toString();
assertThat(json).containsSubsequence("\"properties\"", "\"com.example.alpha.ddd\"", "\"com.example.bravo.bbb\"",
"\"com.example.alpha.ccc\"", "\"com.example.bravo.aaa\"");
}

@Test
void orderingForSameGroupNames() throws IOException {
ConfigurationMetadata metadata = new ConfigurationMetadata();
metadata.add(ItemMetadata.newGroup("com.acme.alpha", null, "com.example.Foo", null));
metadata.add(ItemMetadata.newGroup("com.acme.alpha", null, "com.example.Bar", null));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonMarshaller marshaller = new JsonMarshaller();
marshaller.write(metadata, outputStream);
String json = outputStream.toString();
assertThat(json).containsSubsequence("\"groups\"", "\"name\": \"com.acme.alpha\"",
"\"sourceType\": \"com.example.Bar\"", "\"name\": \"com.acme.alpha\"",
"\"sourceType\": \"com.example.Foo\"");
}

@Test
void orderingForSamePropertyNames() throws IOException {
ConfigurationMetadata metadata = new ConfigurationMetadata();
metadata.add(ItemMetadata.newProperty("com.example.bravo", "aaa", "java.lang.Boolean", "com.example.Foo", null,
null, null, null));
metadata.add(ItemMetadata.newProperty("com.example.bravo", "aaa", "java.lang.Integer", "com.example.Bar", null,
null, null, null));
metadata.add(
ItemMetadata.newProperty("com.example.alpha", "ddd", null, "com.example.Bar", null, null, null, null));
metadata.add(
ItemMetadata.newProperty("com.example.alpha", "ccc", null, "com.example.Foo", null, null, null, null));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonMarshaller marshaller = new JsonMarshaller();
marshaller.write(metadata, outputStream);
String json = outputStream.toString();
assertThat(json).containsSubsequence("\"groups\"", "\"properties\"", "\"com.example.alpha.ccc\"",
"com.example.Foo", "\"com.example.alpha.ddd\"", "com.example.Bar", "\"com.example.bravo.aaa\"",
"com.example.Bar", "\"com.example.bravo.aaa\"", "com.example.Foo");
}

}