Skip to content

Commit

Permalink
fix: Sort rows in Extension configuration status table by configurati…
Browse files Browse the repository at this point in the history
…on name (#150)

Refs: #148
  • Loading branch information
yurtsevich-sbb authored Sep 2, 2024
1 parent c681097 commit 75550d4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ConfigurationStatus {
public class ConfigurationStatus implements Comparable<ConfigurationStatus> {
private @NotNull String name;
private @NotNull Status status;
private @NotNull String details;

public ConfigurationStatus(@NotNull String name, @NotNull Status status) {
this(name, status, "");
}
}

@Override
public int compareTo(@NotNull ConfigurationStatus o) {
return name.compareTo(o.name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,27 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

@SuppressWarnings("unused")
public abstract class ConfigurationStatusProvider {

@SneakyThrows
public static List<ConfigurationStatus> getAllStatuses(String scope) {
public static Collection<ConfigurationStatus> getAllStatuses(String scope) {
Context context = new Context(scope);
Set<Class<? extends ConfigurationStatusProvider>> subTypes = ContextUtils.findSubTypes(ConfigurationStatusProvider.class);
List<ConfigurationStatus> statuses = new ArrayList<>();
Collection<ConfigurationStatus> statuses = new TreeSet<>();
for (Class<? extends ConfigurationStatusProvider> subType : subTypes) {
ConfigurationStatusProvider provider = subType.getConstructor().newInstance();
statuses.addAll(provider.getStatuses(context));
}
return statuses;
}

public @NotNull List<ConfigurationStatus> getStatuses(@NotNull Context context) {
public @NotNull Collection<ConfigurationStatus> getStatuses(@NotNull Context context) {
return List.of(getStatus(context));
}

Expand Down Expand Up @@ -56,4 +57,4 @@ private static boolean contains(@NotNull String input, @NotNull String regex) {
public static class Context {
private String scope;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<%@ page import="ch.sbb.polarion.extension.generic.rest.model.Context" %>
<%@ page import="ch.sbb.polarion.extension.generic.configuration.ConfigurationStatus" %>
<%@ page import="ch.sbb.polarion.extension.generic.configuration.ConfigurationStatusProvider" %>
<%@ page import="java.util.Collection" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
Expand Down Expand Up @@ -91,7 +92,7 @@
</tbody>
</table>

<% List<ConfigurationStatus> configurationStatuses = ConfigurationStatusProvider.getAllStatuses(request.getParameter("scope")); %>
<% Collection<ConfigurationStatus> configurationStatuses = ConfigurationStatusProvider.getAllStatuses(request.getParameter("scope")); %>
<% if (!configurationStatuses.isEmpty()) { %>
<h3>Extension configuration status</h3>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import java.util.Collection;
import java.util.List;
import java.util.Set;

Expand All @@ -19,13 +20,15 @@ class ConfigurationStatusProviderTest {
void testGetAllStatuses() {
try (MockedStatic<ContextUtils> contextUtilsMockedStatic = mockStatic(ContextUtils.class)) {
contextUtilsMockedStatic.when(() -> ContextUtils.findSubTypes(any())).thenReturn(Set.of(TestSingleProvider.class, TestMultipleProvider.class));
List<ConfigurationStatus> statuses = ConfigurationStatusProvider.getAllStatuses("some scope");
Collection<ConfigurationStatus> statuses = ConfigurationStatusProvider.getAllStatuses("some scope");

assertEquals(3, statuses.size());
assertTrue(statuses.containsAll(List.of(
new ConfigurationStatus("single", Status.OK, "single details"),
new ConfigurationStatus("multiple 1", Status.WARNING, "multiple 1 details"),
new ConfigurationStatus("multiple 2", Status.ERROR, "multiple 2 details")
)));

// Check alphabetical order of configurations
ConfigurationStatus[] statusesArray = statuses.toArray(new ConfigurationStatus[0]);
assertEquals(statusesArray[0], new ConfigurationStatus("multiple 1", Status.WARNING, "multiple 1 details"));
assertEquals(statusesArray[1], new ConfigurationStatus("multiple 2", Status.ERROR, "multiple 2 details"));
assertEquals(statusesArray[2], new ConfigurationStatus("single", Status.OK, "single details"));
}
}

Expand Down

0 comments on commit 75550d4

Please sign in to comment.