Skip to content

Commit 3033750

Browse files
committed
Add unit test for id, name and description being null.
1 parent 9f74812 commit 3033750

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorBuilder.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,30 @@ public SmartConnector create() {
1616
}
1717

1818
public static SmartConnectorBuilder newSmartConnector(KnowledgeBase knowledgeBase) {
19+
20+
checkNull(knowledgeBase);
21+
1922
return new SmartConnectorBuilder(knowledgeBase);
2023
}
2124

25+
private static void checkNull(KnowledgeBase knowledgeBase) {
26+
String message = "The KB ";
27+
boolean allNonNull = true;
28+
if (knowledgeBase.getKnowledgeBaseId() == null) {
29+
allNonNull = false;
30+
message += "id";
31+
} else if (knowledgeBase.getKnowledgeBaseName() == null) {
32+
allNonNull = false;
33+
message += "name";
34+
} else if (knowledgeBase.getKnowledgeBaseDescription() == null) {
35+
allNonNull = false;
36+
message += "description";
37+
}
38+
39+
message += " should be non-null.";
40+
41+
if (!allNonNull)
42+
throw new NullPointerException(message);
43+
}
44+
2245
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package eu.knowledge.engine.smartconnector.api;
2+
3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
5+
import java.net.URI;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
import eu.knowledge.engine.smartconnector.impl.SmartConnectorBuilder;
10+
11+
/**
12+
* Giving null values for id, name or description is incorrect and this unit
13+
* tests makes sure we throw a null pointer exception when constructing the SC.
14+
*/
15+
class KnowledgeBaseNullTest {
16+
17+
private MyKnowledgeBase kb = new MyKnowledgeBase("kb1");
18+
19+
private static class MyKnowledgeBase implements KnowledgeBase {
20+
21+
private String id;
22+
private String name;
23+
private String desc;
24+
private boolean idIsNull = false;
25+
private boolean nameIsNull = false;
26+
private boolean descIsNull = false;
27+
28+
public MyKnowledgeBase(String aName) {
29+
this.id = "https://www.example.org/" + aName;
30+
this.name = aName;
31+
this.desc = aName + " description";
32+
}
33+
34+
@Override
35+
public URI getKnowledgeBaseId() {
36+
if (idIsNull)
37+
return null;
38+
else
39+
return URI.create(this.id);
40+
}
41+
42+
@Override
43+
public String getKnowledgeBaseName() {
44+
if (nameIsNull)
45+
return null;
46+
else
47+
return this.name;
48+
}
49+
50+
@Override
51+
public String getKnowledgeBaseDescription() {
52+
if (descIsNull)
53+
return null;
54+
else
55+
return this.desc;
56+
}
57+
58+
@Override
59+
public void smartConnectorReady(SmartConnector aSC) {
60+
}
61+
62+
@Override
63+
public void smartConnectorConnectionLost(SmartConnector aSC) {
64+
}
65+
66+
@Override
67+
public void smartConnectorConnectionRestored(SmartConnector aSC) {
68+
}
69+
70+
@Override
71+
public void smartConnectorStopped(SmartConnector aSC) {
72+
}
73+
74+
public void setNull(boolean anIdIsNull, boolean aNameIsNull, boolean aDescIsNull) {
75+
this.idIsNull = anIdIsNull;
76+
this.nameIsNull = aNameIsNull;
77+
this.descIsNull = aDescIsNull;
78+
}
79+
}
80+
81+
@Test
82+
void testNullId() {
83+
kb.setNull(true, false, false);
84+
assertThrows(NullPointerException.class, () -> {
85+
SmartConnectorBuilder.newSmartConnector(kb).create();
86+
});
87+
}
88+
89+
@Test
90+
void testNullName() {
91+
kb.setNull(false, true, false);
92+
assertThrows(NullPointerException.class, () -> {
93+
SmartConnectorBuilder.newSmartConnector(kb).create();
94+
});
95+
}
96+
97+
@Test
98+
void testNullDesc() {
99+
kb.setNull(false, false, true);
100+
assertThrows(NullPointerException.class, () -> {
101+
SmartConnectorBuilder.newSmartConnector(kb).create();
102+
});
103+
}
104+
105+
}

0 commit comments

Comments
 (0)