Skip to content

Commit

Permalink
Add exception throws to functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
theViz343 committed Nov 3, 2023
1 parent ef22510 commit 640ea3c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/main/java/GraphData.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,18 @@ public boolean addNodes(String[] labels) {
return result;
}

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

if (existing) {
graphObject.removeVertex(label);
return true;
}
else {
System.out.println("Node with label "+label+" does not exist!");
return false;
throw new Exception("Node with label "+label+" does not exist!");
}
}

public void removeNodes(String[] labels) {
public void removeNodes(String[] labels) throws Exception {
for(String label: labels) {
removeNode(label);
}
Expand Down
18 changes: 12 additions & 6 deletions src/test/java/GraphDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,34 @@ public void TestAddNodesIfExistAlready(){

@Test
@DisplayName("Test removeNode if node exists.")
public void TestRemoveNode(){
assertTrue(graphApi.removeNode("A"));
public void TestRemoveNode() throws Exception {
graphApi.removeNode("A");
assertFalse(graphApi.graphObject.vertexSet().stream().anyMatch(v -> (v.equals("A"))));
}

@Test
@DisplayName("Test removeNode if node does not exist.")
public void TestRemoveNodeIfExists(){
graphApi.removeNode("Z");
assertFalse(graphApi.removeNode("Z"));
public void TestRemoveNodeIfExists() {
assertThrows(Exception.class, () -> graphApi.removeNode("Z"));
}

@Test
@DisplayName("Test removal of multiple nodes")
public void TestRemoveNodes(){
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))));
}
}

@Test
@DisplayName("Test removal of multiple nodes which do not exist")
public void TestRemoveNodesIfNotExist() throws Exception {
String[] nodes = {"X", "Y", "Z"};
assertThrows(Exception.class, () -> graphApi.removeNodes(nodes));
}


@Test
@DisplayName("Test addition of edge")
Expand Down

0 comments on commit 640ea3c

Please sign in to comment.