sop-java/sop-java-picocli/src/test/java/sop/cli/picocli/commands/ArmorCmdTest.java

73 lines
2.0 KiB
Java
Raw Normal View History

2022-01-11 13:46:05 +01:00
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.cli.picocli.commands;
import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
import com.ginsberg.junit.exit.FailOnSystemExit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import sop.Ready;
import sop.SOP;
import sop.cli.picocli.SopCLI;
import sop.exception.SOPGPException;
import sop.operation.Armor;
2023-11-15 15:26:06 +01:00
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
2022-01-11 13:46:05 +01:00
public class ArmorCmdTest {
private Armor armor;
private SOP sop;
@BeforeEach
2022-06-10 16:16:37 +02:00
public void mockComponents() throws SOPGPException.BadData, IOException {
2022-01-11 13:46:05 +01:00
armor = mock(Armor.class);
sop = mock(SOP.class);
when(sop.armor()).thenReturn(armor);
when(armor.data((InputStream) any())).thenReturn(nopReady());
SopCLI.setSopInstance(sop);
}
@Test
2022-06-10 16:16:37 +02:00
public void assertDataIsAlwaysCalled() throws SOPGPException.BadData, IOException {
2022-01-11 13:46:05 +01:00
SopCLI.main(new String[] {"armor"});
verify(armor, times(1)).data((InputStream) any());
}
@Test
@ExpectSystemExitWithStatus(SOPGPException.BadData.EXIT_CODE)
2022-06-10 16:16:37 +02:00
public void ifBadDataExit41() throws SOPGPException.BadData, IOException {
2022-01-11 13:46:05 +01:00
when(armor.data((InputStream) any())).thenThrow(new SOPGPException.BadData(new IOException()));
SopCLI.main(new String[] {"armor"});
}
@Test
@FailOnSystemExit
public void ifNoErrorsNoExit() {
when(sop.armor()).thenReturn(armor);
SopCLI.main(new String[] {"armor"});
}
private static Ready nopReady() {
return new Ready() {
@Override
2023-11-15 15:26:06 +01:00
public void writeTo(@Nonnull OutputStream outputStream) {
2022-01-11 13:46:05 +01:00
}
};
}
}