Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
theViz343 committed Oct 10, 2023
1 parent eb738c4 commit 5e1a861
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/main/java/GraphData.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
import java.awt.image.BufferedImage;
import java.io.*;
import java.io.File;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public class GraphData {
private Graph<String, DefaultEdge> graphObject = new DefaultDirectedGraph<>(DefaultEdge.class);
Graph<String, DefaultEdge> graphObject = new DefaultDirectedGraph<>(DefaultEdge.class);

public void parseGraph(String filepath) {
public boolean parseGraph(String filepath) {

// Import the graph from file
DOTImporter<String, DefaultEdge> dotImporter = new DOTImporter<>();
Expand All @@ -30,9 +29,11 @@ public void parseGraph(String filepath) {
String fileContent = Files.readString(Paths.get(filepath));
dotImporter.importGraph(graphObject, new StringReader(fileContent));
System.out.println("Graph successfully parsed!");
return true;
} catch (IOException e) {
System.out.println("Cannot read file " + filepath);
System.out.println(e);
return false;
}
}

Expand All @@ -53,24 +54,28 @@ public String toString() {
return opt;
}

public void outputGraph(String filepath) {
public boolean outputGraph(String filepath) {
String opt = toString();
try {
Files.writeString(Paths.get(filepath), opt, StandardCharsets.ISO_8859_1);
return true;
} catch (IOException e) {
System.out.println("Cannot write file at " + filepath);
System.out.println(e);
return false;
}
}

public void addNode(String label) {
public boolean addNode(String label) {
boolean existing = graphObject.vertexSet().stream().anyMatch(v -> Objects.equals(v, label));

if (existing) {
System.out.println("Node with label "+label+" already exists!");
return false;
}
else {
graphObject.addVertex(label);
return true;
}
}

Expand All @@ -80,32 +85,37 @@ public void addNodes(String[] labels) {
}
}

public void addEdge(String srcLabel, String dstLabel) {
public boolean addEdge(String srcLabel, String dstLabel) {
boolean srcnodeexisting = graphObject.vertexSet().stream().anyMatch(v -> Objects.equals(v, srcLabel));
boolean dstnodeexisting = graphObject.vertexSet().stream().anyMatch(v -> Objects.equals(v, dstLabel));
DefaultEdge edgeexisting = graphObject.getEdge(srcLabel, dstLabel);
if (edgeexisting!=null) {
System.out.println("Edge "+ edgeexisting + " already exists!");
return;
return false;
}
if (!srcnodeexisting) {
System.out.println("Node "+ srcLabel+" does not exist!");
return false;
} else if (!dstnodeexisting) {
System.out.println("Node "+ dstLabel+" does not exist!");
return false;
} else {
graphObject.addEdge(srcLabel, dstLabel);
return true;
}
}

public void outputDOTGraph(String path) {
public boolean outputDOTGraph(String path) {
DOTExporter<String, DefaultEdge> exporter = new DOTExporter<>(v -> v);
Writer writer = new StringWriter();
exporter.exportGraph(graphObject, writer);
try {
Files.writeString(Paths.get(path), writer.toString(), StandardCharsets.ISO_8859_1);
return true;
} catch (IOException e) {
System.out.println("Cannot write file at " + path);
System.out.println(e);
return false;
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/expected.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
strict digraph G {
A;
B;
C;
D;
Z;
A -> B;
A -> C;
A -> D;
Z -> C;
}
117 changes: 117 additions & 0 deletions src/test/java/GraphDataTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import org.jgrapht.graph.DefaultEdge;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;

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

public class GraphDataTest {
GraphData graphApi = new GraphData();
@BeforeEach
public void init_graph() {
assertTrue(graphApi.parseGraph("src/test/test_graph.dot"));
}

@Test
@DisplayName("Test initial graph parsing")
public void TestParseGraph() {
Set<String> nodes = new HashSet<String>();
nodes.add("A");
nodes.add("B");
nodes.add("C");
nodes.add("D");
assertEquals(nodes, graphApi.graphObject.vertexSet());
Set<String> edges = new HashSet<String>();
Set<String> expected_edges = new HashSet<String>();
edges.add("(A : B)");
edges.add("(A : C)");
edges.add("(A : D)");
for (DefaultEdge e: graphApi.graphObject.edgeSet()) {
expected_edges.add(e.toString());
}
assertEquals(expected_edges, edges);
}

@Test
@DisplayName("Test toString")
public void TestToString() {
String expected_value = "Number of nodes: 4\n" +
"Node labels: [A, B, C, D]\n" +
"Number of edges: 3\n" +
"Node and edge directions: (A -> B), (A -> C), (A -> D)\n";

assertEquals(expected_value, graphApi.toString());
}

@Test
@DisplayName("Test outputGraph")
public void TestOutputGraph() throws IOException {
String expected_value = """
Number of nodes: 4
Node labels: [A, B, C, D]
Number of edges: 3
Node and edge directions: (A -> B), (A -> C), (A -> D)
""";
assertTrue(graphApi.outputGraph("src/test/output.txt"));
assertEquals(expected_value, Files.readString(Paths.get("src/test/output.txt")));
}

@Test
@DisplayName("Test outputGraph if node does not exist already")
public void TestAddNode(){
assertTrue(graphApi.addNode("Z"));
assertTrue(graphApi.graphObject.vertexSet().stream().anyMatch(v -> (v.equals("Z"))));
}

@Test
@DisplayName("Test outputGraph if node exists already")
public void TestAddNodeIfExists(){
graphApi.addNode("Z");
assertFalse(graphApi.addNode("Z"));
}

@Test
@DisplayName("Test addition of multiple nodes")
public void TestAddNodes(){
String[] nodes = {"Z", "X", "Y"};
graphApi.addNodes(nodes);
for(String node:nodes) {
assertTrue(graphApi.graphObject.vertexSet().stream().anyMatch(v -> (v.equals(node))));
}
}

@Test
@DisplayName("Test addition of edge")
public void TestAddEdge(){
assertTrue(graphApi.addEdge("B","C"));
}

@Test
@DisplayName("Test addition of alreardy existing edge")
public void TestAddEdgeIfExists(){
assertFalse(graphApi.addEdge("A","C"));
}

@Test
@DisplayName("Test addition of edge")
public void TestAddEdgeIfNodeDoesNotExist(){
assertFalse(graphApi.addEdge("Z","C"));
}

@Test
@DisplayName("Test DOT graph generation")
public void TestOutputDOTGraph() throws IOException {
graphApi.addNode("Z");
graphApi.addEdge("Z","C");
String expected_value = Files.readString(Paths.get("src/test/expected.dot"));
assertTrue(graphApi.outputDOTGraph("src/test/gen_graph.dot"));
assertEquals(expected_value, Files.readString(Paths.get("src/test/gen_graph.dot")));
}

}
10 changes: 10 additions & 0 deletions src/test/test_graph.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
strict digraph D {

A
B
C

A -> B
A -> C
A -> D
}

0 comments on commit 5e1a861

Please sign in to comment.