Skip to content

Commit

Permalink
Sorting connections tabel by type
Browse files Browse the repository at this point in the history
  • Loading branch information
pz2 committed Jul 15, 2024
1 parent 7f8fe14 commit 21a397b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class HofundConnectionsTable {
Expand All @@ -14,10 +15,13 @@ public class HofundConnectionsTable {

public HofundConnectionsTable(List<HofundConnectionsProvider> connectionsProviders) {
List<HofundConnection> connections = new ArrayList<>();

for (HofundConnectionsProvider connectionsProvider : connectionsProviders) {
connections.addAll(connectionsProvider.getConnections());
}

connections.sort((d1, d2) -> d2.getType().compareTo(d1.getType()));

this.connections = connections;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<HofundConnection> 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)
);
}
}

0 comments on commit 21a397b

Please sign in to comment.