Skip to content

Commit

Permalink
toward supporting [elton ls] in --prov-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorrit Poelen committed Dec 24, 2024
1 parent 9c5316f commit 372ab0a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
31 changes: 26 additions & 5 deletions src/main/java/org/globalbioticinteractions/elton/cmd/CmdList.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.globalbioticinteractions.elton.cmd;

import org.eol.globi.util.ResourceServiceRemote;

import org.globalbioticinteractions.dataset.DatasetRegistry;
import org.globalbioticinteractions.dataset.DatasetRegistryException;
import org.globalbioticinteractions.dataset.DatasetRegistryGitHubArchive;
import org.globalbioticinteractions.dataset.DatasetRegistryProxy;
import org.globalbioticinteractions.dataset.DatasetRegistryZenodo;
import org.globalbioticinteractions.elton.store.AccessLogger;
import org.globalbioticinteractions.elton.store.ActivityListener;
import org.globalbioticinteractions.elton.store.LocalPathToHashIRI;
import org.globalbioticinteractions.elton.util.DatasetRegistryUtil;
import picocli.CommandLine;

import java.io.File;
Expand Down Expand Up @@ -37,21 +40,39 @@ public String getDescription() {
public void run(PrintStream out) {
InputStreamFactoryLogging inputStreamFactory = createInputStreamFactory();

DatasetRegistry registryLocal = getDatasetRegistry(inputStreamFactory, getActivityListener());
DatasetRegistry registryLocal = getDatasetRegistryWithProv();

ActivityListener activityListener =
getEnableProvMode()
? getActivityListenerWithProv()
: new AccessLogger(DatasetRegistryUtil.NAMESPACE_LOCAL, getProvDir());


File cacheDir = new File(getDataDir());

DatasetRegistryUtil.ResourceServiceListening resourceServiceRemote
= new DatasetRegistryUtil.ResourceServiceListening(
getActivityIdFactory(),
activityListener,
getActivityContext(),
new ResourceServiceRemote(inputStreamFactory, cacheDir),
new LocalPathToHashIRI(new File(getDataDir()))
);


List<DatasetRegistry> onlineAndOffline = Arrays.asList(
new DatasetRegistryZenodo(new ResourceServiceRemote(inputStreamFactory, cacheDir)),
new DatasetRegistryGitHubArchive(new ResourceServiceRemote(inputStreamFactory, cacheDir)),
new DatasetRegistryZenodo(resourceServiceRemote),
new DatasetRegistryGitHubArchive(resourceServiceRemote),
registryLocal
);

List<DatasetRegistry> registries = isOnline() ? onlineAndOffline : Collections.singletonList(registryLocal);

PrintStream dataSink = getDataSink(out);

DatasetRegistry registry = new DatasetRegistryProxy(registries);
try {
registry.findNamespaces(out::println);
registry.findNamespaces(dataSink::println);
} catch (
DatasetRegistryException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static DatasetRegistry forCacheOrLocalDir(String dataDir,
ResourceService resourceServiceRemote,
ContentPathFactory contentPathFactory,
ProvenancePathFactory provenancePathFactory,
ActivityListener dereferenceListener,
ActivityListener activityListener,
ActivityContext ctx,
Supplier<IRI> activityIdFactory) {
DatasetRegistry registry = forCache(
Expand All @@ -147,7 +147,7 @@ private static DatasetRegistry forCacheOrLocalDir(String dataDir,
contentPathFactory,
dataDir,
provDir,
dereferenceListener,
activityListener,
ctx,
activityIdFactory
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.globalbioticinteractions.elton.cmd;

import org.apache.commons.lang3.StringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -9,10 +10,13 @@
import java.io.IOException;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.util.Arrays;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;

public class CmdListTest {

Expand All @@ -22,27 +26,46 @@ public class CmdListTest {
@Test
public void listOffline() throws URISyntaxException, IOException {
String cacheDir = CmdTestUtil.cacheDirTest(tmpFolder);
ByteArrayOutputStream out = runCmd(cacheDir);
ByteArrayOutputStream out = runCmd(cacheDir, false);
assertThat(out.toString(), startsWith("globalbioticinteractions/template-dataset"));
}

@Test
public void listOfflineWithProv() throws URISyntaxException, IOException {
String cacheDir = CmdTestUtil.cacheDirTest(tmpFolder);
ByteArrayOutputStream out = runCmd(cacheDir, true);
String actual = out.toString();
assertThat(actual, not(startsWith("globalbioticinteractions/template-dataset")));

String[] lines = StringUtils.split(actual, '\n');
assertThat(lines.length, is(19));
assertThat(lines[0], startsWith("<https://globalbioticinteractions.org/elton> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/prov#SoftwareAgent>"));
assertThat(lines[lines.length - 1], containsString("<http://www.w3.org/ns/prov#endedAtTime>"));

long numberOfWasDerivedFromCount = Arrays.stream(lines).filter(line -> StringUtils.contains(line, "wasDerivedFrom")).count();

assertThat(numberOfWasDerivedFromCount, not(is(0L)));
}

@Test
public void listOfflineNonExistingCacheDir() throws URISyntaxException {
String cacheDirNonExisting = "this/does/not/exist";
assertThat(new File(cacheDirNonExisting).exists(), is(false));
ByteArrayOutputStream out = runCmd(cacheDirNonExisting);
ByteArrayOutputStream out = runCmd(cacheDirNonExisting, false);
assertThat(out.toString(), is("local\n"));
assertThat(new File(cacheDirNonExisting).exists(), is(false));
}

private ByteArrayOutputStream runCmd(String cacheDir) {
private ByteArrayOutputStream runCmd(String cacheDir, boolean enableProvMode) {
CmdList cmd = new CmdList();
cmd.setDataDir(cacheDir);
cmd.setProvDir(cacheDir);
cmd.setEnableProvMode(enableProvMode);

ByteArrayOutputStream out1 = new ByteArrayOutputStream();
PrintStream out = new PrintStream(out1);
cmd.run(out);
cmd.setStdout(out);
cmd.run();
return out1;
}

Expand Down

0 comments on commit 372ab0a

Please sign in to comment.