Skip to content

Commit

Permalink
refactor: Encapsulate graphObject and create a getter function.
Browse files Browse the repository at this point in the history
This refactor commit aims to encapsulate the graphObject object by
making it private to the GraphData class. A getter function called
getGraph is now used to safely access the object.
  • Loading branch information
theViz343 committed Nov 25, 2023
1 parent 5d7cdc1 commit e45a2ce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/main/java/GraphData.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
import java.util.List;

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

enum Algorithm{
BFS,
DFS
}

public Graph<String, DefaultEdge> getGraph() {
return graphObject;
}

public boolean parseGraph(String filepath) {

// Import the graph from file
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/GraphDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public void TestParseGraph() {
nodes.add("B");
nodes.add("C");
nodes.add("D");
assertEquals(nodes, graphApi.graphObject.vertexSet());
assertEquals(nodes, graphApi.getGraph().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()) {
for (DefaultEdge e: graphApi.getGraph().edgeSet()) {
expected_edges.add(e.toString());
}
assertEquals(expected_edges, edges);
Expand Down Expand Up @@ -67,7 +67,7 @@ public void TestOutputGraph() throws IOException {
@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"))));
assertTrue(graphApi.getGraph().vertexSet().stream().anyMatch(v -> (v.equals("Z"))));
}

@Test
Expand All @@ -83,7 +83,7 @@ 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))));
assertTrue(graphApi.getGraph().vertexSet().stream().anyMatch(v -> (v.equals(node))));
}
}

Expand All @@ -98,7 +98,7 @@ public void TestAddNodesIfExistAlready(){
@DisplayName("Test removeNode if node exists.")
public void TestRemoveNode() throws Exception {
graphApi.removeNode("A");
assertFalse(graphApi.graphObject.vertexSet().stream().anyMatch(v -> (v.equals("A"))));
assertFalse(graphApi.getGraph().vertexSet().stream().anyMatch(v -> (v.equals("A"))));
}

@Test
Expand All @@ -113,7 +113,7 @@ public void TestRemoveNodes() throws Exception {
String[] nodes = {"A", "B", "C"};
graphApi.removeNodes(nodes);
for(String node:nodes) {
assertFalse(graphApi.graphObject.vertexSet().stream().anyMatch(v -> (v.equals(node))));
assertFalse(graphApi.getGraph().vertexSet().stream().anyMatch(v -> (v.equals(node))));
}
}

Expand Down

0 comments on commit e45a2ce

Please sign in to comment.