Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#181] Add new test for Rdf4jDeployModule #187

Merged
merged 7 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -65,6 +65,21 @@ private static Property getParameter(final String name) {

static final Property P_RDF4J_REPOSITORY_USERNAME = getParameter("p-rdf4j-secured-username-variable");
private String rdf4jSecuredUsernameVariable;
private RepositoryManager repositoryManager;
private RepositoryConnection connection;
private Repository repository;

public void setRepositoryManager(RepositoryManager repositoryManager) {
this.repositoryManager = repositoryManager;
}

public void setConnection(RepositoryConnection connection) {
blcham marked this conversation as resolved.
Show resolved Hide resolved
this.connection = connection;
}

public void setRepository(Repository repository) {
blcham marked this conversation as resolved.
Show resolved Hide resolved
this.repository = repository;
}

static final Property P_RDF4J_REPOSITORY_PASSWORD = getParameter("p-rdf4j-secured-password-variable");
private String rdf4jSecuredPasswordVariable;
Expand Down Expand Up @@ -109,24 +124,13 @@ public void setReplaceContext(boolean replaceContext) {

@Override
ExecutionContext executeSelf() {
RepositoryConnection connection = null;
Repository repository = null;
LOG.debug("Deploying data into {} of rdf4j server repository {}/{}.",
isRdf4jContextIRIDefined() ? "context " + rdf4jContextIRI : "default context",
rdf4jServerURL,
rdf4jRepositoryName);
String username = getConfigurationVariable(rdf4jSecuredUsernameVariable);
String password = getConfigurationVariable(rdf4jSecuredPasswordVariable);

try {
RepositoryManager repositoryManager = RepositoryProvider.getRepositoryManager(rdf4jServerURL);

if (username != null && password != null) {
RemoteRepositoryManager remoteRepositoryManager = (RemoteRepositoryManager) repositoryManager;
remoteRepositoryManager.setUsernameAndPassword(username, password);
}

repository = repositoryManager.getRepository(rdf4jRepositoryName);
if (repository == null) {
LOG.info("Creating new repository {} within rdf4j server {} ...",
rdf4jServerURL, rdf4jRepositoryName);
Expand Down Expand Up @@ -194,6 +198,15 @@ public void loadConfiguration() {
rdf4jSecuredPasswordVariable = Optional.ofNullable(
getEffectiveValue(P_RDF4J_REPOSITORY_PASSWORD)).map(n -> n.asLiteral().getString()
).orElse(null);
repositoryManager = RepositoryProvider.getRepositoryManager(rdf4jServerURL);
blcham marked this conversation as resolved.
Show resolved Hide resolved
String username = getConfigurationVariable(rdf4jSecuredUsernameVariable);
String password = getConfigurationVariable(rdf4jSecuredPasswordVariable);
if (username != null && password != null) {
RemoteRepositoryManager remoteRepositoryManager = (RemoteRepositoryManager) repositoryManager;
remoteRepositoryManager.setUsernameAndPassword(username, password);
}

repository = repositoryManager.getRepository(rdf4jRepositoryName);
}
private static @Nullable String getConfigurationVariable(String variableName) {
if (variableName == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,56 @@
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.manager.RepositoryManager;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

//import info.aduna.webapp.util.HttpServerUtil;
import java.io.IOException;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;


@ExtendWith(MockitoExtension.class)
public class Rdf4jDeployModuleTest {

@Mock
RepositoryManager repositoryManager;
@Mock
Repository repository;
@Mock
RepositoryConnection connection;

@Test
void executeSelfDeployEmpty() throws IOException {
given(repository.getConnection()).willReturn(connection);

final ExecutionContext inputExecutionContext = ExecutionContextFactory.createEmptyContext();
final Rdf4jDeployModule moduleRdf4j = new Rdf4jDeployModule();
moduleRdf4j.setInputContext(inputExecutionContext);
moduleRdf4j.setRdf4jServerURL("http://localhost:18080/rdf4j-server");
moduleRdf4j.setRdf4jRepositoryName("test-s-pipes");
moduleRdf4j.setRdf4jContextIRI("");
blcham marked this conversation as resolved.
Show resolved Hide resolved
moduleRdf4j.setConnection(connection);
moduleRdf4j.setRepository(repository);
moduleRdf4j.setRepositoryManager(repositoryManager);

moduleRdf4j.executeSelf();
blcham marked this conversation as resolved.
Show resolved Hide resolved

verify(repositoryManager,times(0)).getRepository(anyString());
verify(connection,times(1)).commit();
blcham marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
@Disabled
public void testDeployEmpty() throws Exception {
public void testDeployEmpty() {
blcham marked this conversation as resolved.
Show resolved Hide resolved
final Rdf4jDeployModule moduleRdf4j = new Rdf4jDeployModule();

final Model deployModel = ModelFactory.createDefaultModel();
Expand All @@ -38,4 +78,5 @@ public void testDeployEmpty() throws Exception {
moduleRdf4j.setInputContext(executionContext);
moduleRdf4j.execute();
}

}
Loading