Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,30 @@ public SmartConnector create() {
}

public static SmartConnectorBuilder newSmartConnector(KnowledgeBase knowledgeBase) {

checkNull(knowledgeBase);

return new SmartConnectorBuilder(knowledgeBase);
}

private static void checkNull(KnowledgeBase knowledgeBase) {
String message = "The KB ";
boolean allNonNull = true;
if (knowledgeBase.getKnowledgeBaseId() == null) {
allNonNull = false;
message += "id";
} else if (knowledgeBase.getKnowledgeBaseName() == null) {
allNonNull = false;
message += "name";
} else if (knowledgeBase.getKnowledgeBaseDescription() == null) {
allNonNull = false;
message += "description";
}

message += " should be non-null.";

if (!allNonNull)
throw new NullPointerException(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package eu.knowledge.engine.smartconnector.api;

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

import java.net.URI;

import org.junit.jupiter.api.Test;

import eu.knowledge.engine.smartconnector.impl.SmartConnectorBuilder;

/**
* Giving null values for id, name or description is incorrect and this unit
* tests makes sure we throw a null pointer exception when constructing the SC.
*/
class KnowledgeBaseNullTest {

private MyKnowledgeBase kb = new MyKnowledgeBase("kb1");

private static class MyKnowledgeBase implements KnowledgeBase {

private String id;
private String name;
private String desc;
private boolean idIsNull = false;
private boolean nameIsNull = false;
private boolean descIsNull = false;

public MyKnowledgeBase(String aName) {
this.id = "https://www.example.org/" + aName;
this.name = aName;
this.desc = aName + " description";
}

@Override
public URI getKnowledgeBaseId() {
if (idIsNull)
return null;
else
return URI.create(this.id);
}

@Override
public String getKnowledgeBaseName() {
if (nameIsNull)
return null;
else
return this.name;
}

@Override
public String getKnowledgeBaseDescription() {
if (descIsNull)
return null;
else
return this.desc;
}

@Override
public void smartConnectorReady(SmartConnector aSC) {
}

@Override
public void smartConnectorConnectionLost(SmartConnector aSC) {
}

@Override
public void smartConnectorConnectionRestored(SmartConnector aSC) {
}

@Override
public void smartConnectorStopped(SmartConnector aSC) {
}

public void setNull(boolean anIdIsNull, boolean aNameIsNull, boolean aDescIsNull) {
this.idIsNull = anIdIsNull;
this.nameIsNull = aNameIsNull;
this.descIsNull = aDescIsNull;
}
}

@Test
void testNullId() {
kb.setNull(true, false, false);
assertThrows(NullPointerException.class, () -> {
SmartConnectorBuilder.newSmartConnector(kb).create();
});
}

@Test
void testNullName() {
kb.setNull(false, true, false);
assertThrows(NullPointerException.class, () -> {
SmartConnectorBuilder.newSmartConnector(kb).create();
});
}

@Test
void testNullDesc() {
kb.setNull(false, false, true);
assertThrows(NullPointerException.class, () -> {
SmartConnectorBuilder.newSmartConnector(kb).create();
});
}

}
Loading