Add AbstractSopCmdTest to test getInput method

This commit is contained in:
Paul Schaub 2022-08-01 18:21:41 +02:00
parent b7c1b4f1a1
commit fe729c4eb8
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 148 additions and 0 deletions

View file

@ -0,0 +1,126 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.cli.picocli.commands;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.opentest4j.TestAbortedException;
import sop.cli.picocli.TestFileUtil;
import sop.exception.SOPGPException;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class AbstractSopCmdTest {
private static AbstractSopCmd abstractCmd;
private static final TestEnvironmentVariableResolver resolver = new TestEnvironmentVariableResolver();
@BeforeAll
public static void setup() {
abstractCmd = new VersionCmd(); // Use Version as representative command
abstractCmd.setEnvironmentVariableResolver(resolver);
}
@Test
public void setEnvironmentVariableResolver_nullNPE() {
assertThrows(NullPointerException.class, () -> abstractCmd.setEnvironmentVariableResolver(null));
}
@Test
public void getInput_NullInvalid() {
assertThrows(IllegalArgumentException.class, () -> abstractCmd.getInput(null));
}
@Test
public void getInput_EmptyInvalid() {
assertThrows(IllegalArgumentException.class, () -> abstractCmd.getInput(""));
}
@Test
public void getInput_BlankInvalid() {
assertThrows(IllegalArgumentException.class, () -> abstractCmd.getInput(" "));
}
@Test
public void getInput_envNotSetIllegalArg() {
String envName = "@ENV:IS_NOT_SET";
assertThrows(IllegalArgumentException.class, () -> abstractCmd.getInput(envName));
}
@Test
public void getInput_envEmptyIllegalArg() {
String envName = "@ENV:IS_EMPTY";
resolver.addEnvironmentVariable("IS_EMPTY", "");
assertThrows(IllegalArgumentException.class, () -> abstractCmd.getInput(envName));
}
@Test
public void getInput_fromEnv() throws IOException {
resolver.addEnvironmentVariable("FOO", "BAR");
InputStream input = abstractCmd.getInput("@ENV:FOO");
String string = readStringFromInputStream(input);
assertEquals("BAR", string);
}
private static String readStringFromInputStream(InputStream input)
throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[512];
int read;
while ((read = input.read(buf)) > 0) {
output.write(buf, 0, read);
}
return output.toString();
}
@Test
public void getInput_envClashesWithExistingFile() throws IOException {
String env = "@ENV:existing.file";
File tempFile = new File(env);
if (!tempFile.createNewFile()) {
throw new TestAbortedException("Cannot create temporary file " + tempFile.getAbsolutePath());
}
tempFile.deleteOnExit();
resolver.addEnvironmentVariable("existing.file", "foo_bar");
assertThrows(SOPGPException.AmbiguousInput.class, () -> abstractCmd.getInput(env));
}
@Test
public void getInput_fdClashesWithExistingFile() throws IOException {
String env = "@FD:existing.file";
File tempFile = new File(env);
if (!tempFile.createNewFile()) {
throw new TestAbortedException("Cannot create temporary file " + tempFile.getAbsolutePath());
}
tempFile.deleteOnExit();
resolver.addEnvironmentVariable("existing.file", "foo_bar");
assertThrows(SOPGPException.AmbiguousInput.class, () -> abstractCmd.getInput(env));
}
@Test
public void getInput_missingFile() {
String missingFile = "missing.file";
assertThrows(SOPGPException.MissingInput.class, () -> abstractCmd.getInput(missingFile));
}
@Test
public void getInput_notAFile() throws IOException {
File directory = TestFileUtil.createTempDir();
directory.deleteOnExit();
assertThrows(SOPGPException.MissingInput.class, () -> abstractCmd.getInput(directory.getAbsolutePath()));
}
}

View file

@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.cli.picocli.commands;
import java.util.HashMap;
import java.util.Map;
public class TestEnvironmentVariableResolver implements AbstractSopCmd.EnvironmentVariableResolver {
private final Map<String, String> environment = new HashMap<>();
public void addEnvironmentVariable(String name, String value) {
this.environment.put(name, value);
}
@Override
public String resolveEnvironmentVariable(String name) {
return environment.get(name);
}
}