diff --git a/changelog/unreleased/000001-sorting_connections_tabel_by_type.yml b/changelog/unreleased/000001-sorting_connections_tabel_by_type.yml new file mode 100644 index 0000000..0701886 --- /dev/null +++ b/changelog/unreleased/000001-sorting_connections_tabel_by_type.yml @@ -0,0 +1,8 @@ +title: Sorting connections tabel by type +authors: + - name: Peter Zmilczak + nick: marwin1991 + url: https://github.com/marwin1991 +type: changed #[added/changed/deprecated/removed/fixed/security/other] +merge_requests: + - 45 \ No newline at end of file diff --git a/hofund-core/src/main/java/dev/logchange/hofund/connection/HofundConnectionsTable.java b/hofund-core/src/main/java/dev/logchange/hofund/connection/HofundConnectionsTable.java index 989e123..966ef1c 100644 --- a/hofund-core/src/main/java/dev/logchange/hofund/connection/HofundConnectionsTable.java +++ b/hofund-core/src/main/java/dev/logchange/hofund/connection/HofundConnectionsTable.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; public class HofundConnectionsTable { @@ -14,10 +15,13 @@ public class HofundConnectionsTable { public HofundConnectionsTable(List connectionsProviders) { List connections = new ArrayList<>(); + for (HofundConnectionsProvider connectionsProvider : connectionsProviders) { connections.addAll(connectionsProvider.getConnections()); } + connections.sort((d1, d2) -> d2.getType().compareTo(d1.getType())); + this.connections = connections; } diff --git a/hofund-core/src/test/java/dev/logchange/hofund/connection/HofundConnectionsTableTest.java b/hofund-core/src/test/java/dev/logchange/hofund/connection/HofundConnectionsTableTest.java new file mode 100644 index 0000000..ff34840 --- /dev/null +++ b/hofund-core/src/test/java/dev/logchange/hofund/connection/HofundConnectionsTableTest.java @@ -0,0 +1,57 @@ +package dev.logchange.hofund.connection; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class HofundConnectionsTableTest { + + @Test + void testSortByType() { + // given: + TastableHofundConnectionsProvider provider = new TastableHofundConnectionsProvider(); + HofundConnectionsTable table = new HofundConnectionsTable(Collections.singletonList(provider)); + String expected = + "+----------+---------+--------+------+\n" + + "| TYPE | NAME | STATUS | URL |\n" + + "+----------+---------+--------+------+\n" + + "| HTTP | target1 | UP | fake |\n" + + "| HTTP | target3 | UP | fake |\n" + + "| DATABASE | target2 | UP | fake |\n" + + "| DATABASE | target4 | UP | fake |\n" + + "+----------+---------+--------+------+\n"; + + // when: + String result = table.print(); + + // then: + assertEquals(expected, result); + } + + private static class TastableHofundConnectionsProvider implements HofundConnectionsProvider { + + public List getConnections() { + return Arrays.asList( + getHofundConnection("target1", Type.HTTP), + getHofundConnection("target2", Type.DATABASE), + getHofundConnection("target3", Type.HTTP), + getHofundConnection("target4", Type.DATABASE) + ); + } + + } + + private static HofundConnection getHofundConnection(String target, Type type) { + return new HofundConnection( + target, + "fake", + type, + new AtomicReference<>(() -> Status.UP) + ); + } +} \ No newline at end of file