-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AbstractSopCmdTest to test getInput method
- Loading branch information
1 parent
b7c1b4f
commit fe729c4
Showing
2 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
sop-java-picocli/src/test/java/sop/cli/picocli/commands/AbstractSopCmdTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
sop-java-picocli/src/test/java/sop/cli/picocli/commands/TestEnvironmentVariableResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |