From 21a397bc5c9f58d19bcd255e2d93b1c6eb098597 Mon Sep 17 00:00:00 2001 From: pz2 Date: Mon, 15 Jul 2024 09:59:33 +0200 Subject: [PATCH 1/2] Sorting connections tabel by type --- ...0001-sorting_connections_tabel_by_type.yml | 8 +++ .../connection/HofundConnectionsTable.java | 4 ++ .../HofundConnectionsTableTest.java | 57 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 changelog/unreleased/000001-sorting_connections_tabel_by_type.yml create mode 100644 hofund-core/src/test/java/dev/logchange/hofund/connection/HofundConnectionsTableTest.java 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..9f2a801 --- /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: + - 42 \ 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 From 60a76b390ed02316c42d3757f77fcd46f2b6e748 Mon Sep 17 00:00:00 2001 From: pz2 Date: Mon, 15 Jul 2024 10:00:47 +0200 Subject: [PATCH 2/2] Sorting connections tabel by type --- .../unreleased/000001-sorting_connections_tabel_by_type.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/000001-sorting_connections_tabel_by_type.yml b/changelog/unreleased/000001-sorting_connections_tabel_by_type.yml index 9f2a801..0701886 100644 --- a/changelog/unreleased/000001-sorting_connections_tabel_by_type.yml +++ b/changelog/unreleased/000001-sorting_connections_tabel_by_type.yml @@ -5,4 +5,4 @@ authors: url: https://github.com/marwin1991 type: changed #[added/changed/deprecated/removed/fixed/security/other] merge_requests: - - 42 \ No newline at end of file + - 45 \ No newline at end of file