Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bobocode.exceptions;

public class IncorrectDistanceException extends RuntimeException {
public IncorrectDistanceException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bobocode.exceptions;

public class NodeNotFoundException extends RuntimeException {
public NodeNotFoundException(String message) {
super(message);
}
}
118 changes: 115 additions & 3 deletions dijkstra-algorythm/src/test/java/com/bobocode/GraphImplTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.bobocode;

import com.bobocode.exceptions.IncorrectDistanceException;
import com.bobocode.exceptions.NodeNotFoundException;
import com.bobocode.interfaces.Graph;
import org.junit.jupiter.api.Test;

import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

class GraphImplTest {
public static final String NODE_NOT_FOUND_MSG_PREFIX = "Cannot find Node with name=%s";
private Graph graph = new GraphImpl();

@Test
public void testAddNodesIntoEmptyGraph() {
public void testCalculateShortestDistanceBetweenNodes() {
graph.addNode("nodeA");
graph.addNode("nodeB");
graph.addNode("nodeC");
Expand All @@ -34,8 +38,116 @@ public void testAddNodesIntoEmptyGraph() {

assertEquals(graph.calculateShortestDistance("nodeA", "nodeF"), 7);
assertEquals(graph.calculateShortestDistance("nodeA", "nodeE"), 9);
}

@Test
public void testCalculateShortestPathBetweenNodes() {
graph.addNode("nodeA");
graph.addNode("nodeB");
graph.addNode("nodeC");
graph.addNode("nodeD");
graph.addNode("nodeE");
graph.addNode("nodeF");

graph.addDestination("nodeA", "nodeB", 2);
graph.addDestination("nodeA", "nodeC", 4);
graph.addDestination("nodeA", "nodeD", 5);

graph.addDestination("nodeB", "nodeE", 7);
graph.addDestination("nodeB", "nodeC", 1);

graph.addDestination("nodeC", "nodeD", 3);
graph.addDestination("nodeC", "nodeF", 4);

assertEquals(graph.calculateShortestPath("nodeA", "nodeF"), Set.of("nodeA", "nodeD", "NodeF"));
graph.addDestination("nodeD", "nodeF", 2);
graph.addDestination("nodeE", "nodeF", 8);

assertEquals(graph.calculateShortestPath("nodeA", "nodeF"), Set.of("nodeA", "nodeD", "nodeF"));
}

@Test
public void testCalculateShortestDistanceInEmptyGraph() {
graph.addNode("nodeA");
graph.addNode("nodeB");
graph.addNode("nodeC");
graph.addNode("nodeD");
graph.addNode("nodeE");
graph.addNode("nodeF");

assertEquals(graph.calculateShortestDistance("nodeA", "nodeF"), Integer.MAX_VALUE);
assertEquals(graph.calculateShortestDistance("nodeA", "nodeE"), Integer.MAX_VALUE);

assertNull(graph.calculateShortestPath("nodeA", "nodeF"));
}

@Test
public void testCalculateShortestDistanceInGraphWithoutNodes() {
try {
graph.calculateShortestDistance("nodeA", "nodeF");
fail("Should throw exception");
} catch (NodeNotFoundException e) {
assertEquals(String.format(NODE_NOT_FOUND_MSG_PREFIX, "nodeA"), e.getMessage());
} catch (Exception e) {
fail("Exception must be NodeNotFoundException");
}
}

@Test
public void testCalculateShortestPathInGraphWithoutNodes() {
try {
graph.calculateShortestPath("nodeA", "nodeF");
fail("Should throw exception");
} catch (NodeNotFoundException e) {
assertEquals(String.format(NODE_NOT_FOUND_MSG_PREFIX, "nodeA"), e.getMessage());
} catch (Exception e) {
fail("Exception must be NodeNotFoundException");
}
}

@Test
public void testNotConnectedNodeIntoGraph() {
graph.addNode("nodeA");
graph.addNode("nodeB");
graph.addNode("nodeC");
graph.addNode("nodeD");

graph.addDestination("nodeA", "nodeB", 3);
graph.addDestination("nodeA", "nodeC", 10);

graph.addDestination("nodeB", "nodeC", 6);
graph.addDestination("nodeC", "nodeB", 6);

assertEquals(graph.calculateShortestDistance("nodeA", "nodeC"), 9);
assertEquals(graph.calculateShortestDistance("nodeA", "nodeD"), Integer.MAX_VALUE);

assertEquals(graph.calculateShortestPath("nodeA", "nodeC"), Set.of("nodeA", "nodeB", "nodeC"));
}

@Test
public void testAddDistanceWithoutNode() {

try {
graph.addDestination("nodeA", "nodeB", -1);
fail("Should throw exception");
} catch (NodeNotFoundException e) {
assertEquals(String.format(NODE_NOT_FOUND_MSG_PREFIX, "nodeA"), e.getMessage());
} catch (Exception e) {
fail("Exception must be NodeNotFoundException");
}
}

@Test
public void testIncorrectDistanceIntoGraph() {
graph.addNode("nodeA");
graph.addNode("nodeB");

try {
graph.addDestination("nodeA", "nodeB", -1);
fail("Should throw exception");
} catch (IncorrectDistanceException e) {
// Correct situation
} catch (Exception e) {
fail("Exception must be IncorrectDistanceException");
}
}
}