Skip to content

Commit

Permalink
moved test
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-pol committed Aug 29, 2024
1 parent d970c64 commit 44f4799
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import nl.knaw.dans.lib.dataverse.DataverseClient;
import nl.knaw.dans.lib.dataverse.DataverseException;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;
Expand All @@ -29,6 +31,8 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

public class AbstractSubcommandContainerTest extends AbstractCapturingTest {
private static final Logger log = LoggerFactory.getLogger(AbstractSubcommandContainerTest.class);

private static class TestCmd extends AbstractSubcommandContainer<Object> {

public TestCmd(String targets) {
Expand All @@ -44,6 +48,55 @@ protected List<Pair<String, Object>> getItems() throws IOException {
}
}

@Test
public void datasetCmd_with_dir_as_targets_file_throws() throws Exception {

var cmd = new DatasetCmd(new DataverseClient(null)){
@Override
public void doCall() throws IOException {
getItems();
}
};

// set private field
var targetField = AbstractSubcommandContainer.class.getDeclaredField("targets");
targetField.setAccessible(true);
targetField.set(cmd, "src/test/resources");

assertThatThrownBy(cmd::doCall)
.isInstanceOf(IOException.class)
.hasMessage("src/test/resources is not a regular file");

assertThat(logged.list).isEmpty();
assertThat(stdout.toString()).isEqualTo("");
assertThat(stderr.toString()).isEqualTo("");
}

@Test
public void collectionCmd_with_dir_as_targets_file_throws() throws Exception {

var cmd = new CollectionCmd(new DataverseClient(null)) {

@Override
public void doCall() throws IOException {
log.debug("doCall");
getItems();
}
};

// set private field
var targetField = AbstractSubcommandContainer.class.getDeclaredField("targets");
targetField.setAccessible(true);
targetField.set(cmd, "src/test/resources");

assertThatThrownBy(cmd::doCall)
.isInstanceOf(IOException.class)
.hasMessage("src/test/resources is not a regular file");

assertThat(stdout.toString()).isEqualTo("DEBUG doCall\n");
assertThat(stderr.toString()).isEqualTo("");
}

@Test
public void call_should_return_one_on_a_dataverseException_by_doCall() throws Exception {
var cmd = new TestCmd("1") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@
import org.mockito.Mockito;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -145,31 +143,16 @@ public void doCall_with_dir_as_json_file_fails() throws Exception {
""");
}

@Test
public void doCall_with_dir_as_ids_file_throws() throws Exception {

var metadataKeys = new HashMap<String, String>();
var client = new DataverseClient(new DataverseClientConfig(null));

// command under test

var target = "src/test/resources";
var cmd = getCmd(target, metadataKeys, "do-not-care-about-json-file", client);
assertThatThrownBy(cmd::doCall)
.isInstanceOf(IOException.class)
.hasMessage("src/test/resources is not a regular file");
}

private static CollectionCreateDataset getCmd(String target, HashMap<String, String> metadataKeys, String json, final DataverseClient client)
throws NoSuchFieldException, IllegalAccessException {

// set private fields with reflection

var cmd = new CollectionCmd(client);

var targetField = AbstractSubcommandContainer.class.getDeclaredField("targets");
targetField.setAccessible(true);
targetField.set(cmd, target);
var targetsField = AbstractSubcommandContainer.class.getDeclaredField("targets");
targetsField.setAccessible(true);
targetsField.set(cmd, target);

var subCmd = new CollectionCreateDataset();

Expand Down

0 comments on commit 44f4799

Please sign in to comment.