diff --git a/src/main/java/org/globalbioticinteractions/elton/cmd/CmdList.java b/src/main/java/org/globalbioticinteractions/elton/cmd/CmdList.java index a8669e5..fb178e7 100644 --- a/src/main/java/org/globalbioticinteractions/elton/cmd/CmdList.java +++ b/src/main/java/org/globalbioticinteractions/elton/cmd/CmdList.java @@ -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; @@ -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 onlineAndOffline = Arrays.asList( - new DatasetRegistryZenodo(new ResourceServiceRemote(inputStreamFactory, cacheDir)), - new DatasetRegistryGitHubArchive(new ResourceServiceRemote(inputStreamFactory, cacheDir)), + new DatasetRegistryZenodo(resourceServiceRemote), + new DatasetRegistryGitHubArchive(resourceServiceRemote), registryLocal ); List 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); diff --git a/src/main/java/org/globalbioticinteractions/elton/util/DatasetRegistryUtil.java b/src/main/java/org/globalbioticinteractions/elton/util/DatasetRegistryUtil.java index fd1ea88..599c766 100644 --- a/src/main/java/org/globalbioticinteractions/elton/util/DatasetRegistryUtil.java +++ b/src/main/java/org/globalbioticinteractions/elton/util/DatasetRegistryUtil.java @@ -130,7 +130,7 @@ private static DatasetRegistry forCacheOrLocalDir(String dataDir, ResourceService resourceServiceRemote, ContentPathFactory contentPathFactory, ProvenancePathFactory provenancePathFactory, - ActivityListener dereferenceListener, + ActivityListener activityListener, ActivityContext ctx, Supplier activityIdFactory) { DatasetRegistry registry = forCache( @@ -147,7 +147,7 @@ private static DatasetRegistry forCacheOrLocalDir(String dataDir, contentPathFactory, dataDir, provDir, - dereferenceListener, + activityListener, ctx, activityIdFactory ); diff --git a/src/test/java/org/globalbioticinteractions/elton/cmd/CmdListTest.java b/src/test/java/org/globalbioticinteractions/elton/cmd/CmdListTest.java index fbb0b04..fca332a 100644 --- a/src/test/java/org/globalbioticinteractions/elton/cmd/CmdListTest.java +++ b/src/test/java/org/globalbioticinteractions/elton/cmd/CmdListTest.java @@ -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; @@ -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 { @@ -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(" ")); + assertThat(lines[lines.length - 1], containsString("")); + + 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; }