From 22006e52990bf9fb058d3c56604258b6068e6932 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sat, 20 Jul 2024 13:03:42 +0200 Subject: [PATCH] add tests --- pom.xml | 3 +- .../java/eu/m724/netsim/PathVisualizer.java | 22 ++++++++++++ src/test/java/netsim/PeeringDirectTest.java | 33 +++++++++++++++++ src/test/java/netsim/PeeringTest.java | 3 ++ src/test/java/netsim/PeeringTreeTest.java | 35 +++++++++++++++++++ 5 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 src/main/java/eu/m724/netsim/PathVisualizer.java create mode 100644 src/test/java/netsim/PeeringDirectTest.java create mode 100644 src/test/java/netsim/PeeringTreeTest.java diff --git a/pom.xml b/pom.xml index c509f00..1f2a099 100644 --- a/pom.xml +++ b/pom.xml @@ -6,8 +6,7 @@ 0.0.1-SNAPSHOT - 17 - 17 + 17 diff --git a/src/main/java/eu/m724/netsim/PathVisualizer.java b/src/main/java/eu/m724/netsim/PathVisualizer.java new file mode 100644 index 0000000..e63e49f --- /dev/null +++ b/src/main/java/eu/m724/netsim/PathVisualizer.java @@ -0,0 +1,22 @@ +package eu.m724.netsim; + +public class PathVisualizer { + public static String path(Machine from, Machine to, int limit) { + String out = from.id; + + Machine current = from; + int i = 0; + while (!current.equals(to)) { + RoutingInfo route = current.getRouteTo(to); + current = route.from(); + out += " - " + current.id; + + if (++i >= limit) { + out += " - x"; + break; + } + } + + return out; + } +} diff --git a/src/test/java/netsim/PeeringDirectTest.java b/src/test/java/netsim/PeeringDirectTest.java new file mode 100644 index 0000000..7818d1f --- /dev/null +++ b/src/test/java/netsim/PeeringDirectTest.java @@ -0,0 +1,33 @@ +package netsim; + +import org.junit.Test; + +import eu.m724.netsim.Machine; +import eu.m724.netsim.PathVisualizer; + +public class PeeringDirectTest { + @Test + public void testComplexPeering() { + // machine1 - machine2 - machine3 - machine4 + // \------------/ + // so machine1 should skip machine2 + + Machine machine1 = new Machine("machine1"); + Machine machine2 = new Machine("machine2"); + Machine machine3 = new Machine("machine3"); + Machine machine4 = new Machine("machine4"); + + machine1.peerWith(machine2); + machine2.peerWith(machine3); + machine3.peerWith(machine4); + machine1.peerWith(machine3); // that's the bottom arrow + + System.out.println("\nMachine 1:"); + System.out.println(machine1.getRouteTo(machine2).toString()); + System.out.println(machine1.getRouteTo(machine3).toString()); + System.out.println(machine1.getRouteTo(machine4).toString()); + System.out.println(PathVisualizer.path(machine1, machine4, 10)); + System.out.println("\nMachine 2:"); + System.out.println(machine2.getRouteTo(machine4).toString()); + } +} diff --git a/src/test/java/netsim/PeeringTest.java b/src/test/java/netsim/PeeringTest.java index 14139ea..76ac41c 100644 --- a/src/test/java/netsim/PeeringTest.java +++ b/src/test/java/netsim/PeeringTest.java @@ -3,6 +3,7 @@ package netsim; import org.junit.Test; import eu.m724.netsim.Machine; +import eu.m724.netsim.PathVisualizer; public class PeeringTest { @Test @@ -21,5 +22,7 @@ public class PeeringTest { System.out.println(machine1.getRouteTo(machine2).toString()); System.out.println(machine2.getRouteTo(machine3).toString()); System.out.println(machine1.getRouteTo(machine3).toString()); + + System.out.print(PathVisualizer.path(machine1, machine3, 10)); } } diff --git a/src/test/java/netsim/PeeringTreeTest.java b/src/test/java/netsim/PeeringTreeTest.java new file mode 100644 index 0000000..201dddf --- /dev/null +++ b/src/test/java/netsim/PeeringTreeTest.java @@ -0,0 +1,35 @@ +package netsim; + +import org.junit.Test; + +import eu.m724.netsim.Machine; +import eu.m724.netsim.PathVisualizer; + +public class PeeringTreeTest { + @Test + public void testTreePeering() { + // / machine4 + // / machine2 - machine5 + // machine1 + // \ machine3 - machine6 + // \ machine7 + // so machine1 should skip machine2 + + Machine machine1 = new Machine("machine1"); + Machine machine2 = new Machine("machine2"); + Machine machine3 = new Machine("machine3"); + Machine machine4 = new Machine("machine4"); + Machine machine5 = new Machine("machine5"); + Machine machine6 = new Machine("machine6"); + Machine machine7 = new Machine("machine7"); + + machine1.peerWith(machine2); + machine1.peerWith(machine3); + machine2.peerWith(machine4); + machine2.peerWith(machine5); + machine3.peerWith(machine6); + machine3.peerWith(machine7); + + System.out.println(PathVisualizer.path(machine4, machine7, 10)); + } +}