| package com.apigee.security.oas; |
| |
| import static org.assertj.core.api.Assertions.assertThat; |
| import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| import static org.assertj.core.api.Assertions.catchThrowable; |
| |
| import com.beust.jcommander.ParameterException; |
| import com.google.common.io.Resources; |
| import com.google.inject.Guice; |
| import com.google.inject.Injector; |
| import java.io.FileNotFoundException; |
| import java.net.URISyntaxException; |
| import java.net.URL; |
| import org.junit.Before; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| import org.junit.runners.JUnit4; |
| |
| @RunWith(JUnit4.class) |
| public class CommandLineBaseParserTest { |
| private static final String VALID_FILE_NAME = "validOpenApi3DemoSpec.yaml"; |
| private static final URL VALID_FILE_URL = Resources.getResource(VALID_FILE_NAME); |
| private static final String INVALID_FILE_URI_PARAM = "demoOpenSpec.yaml"; |
| |
| private String validFileUriPath; |
| |
| private CommandLineParser commandLineParser; |
| |
| /** Creates a CommandLineParser Object and does other necessary initializations. */ |
| @Before |
| public void setup() throws URISyntaxException { |
| validFileUriPath = VALID_FILE_URL.toURI().getPath(); |
| |
| Injector injector = Guice.createInjector(new CommandLineModule()); |
| commandLineParser = injector.getInstance(CommandLineParser.class); |
| } |
| |
| @Test |
| public void parseArguments_noParameter_throwsParameterExceptionWithMessage() { |
| String[] testArgs = new String[] {}; |
| assertThatThrownBy(() -> commandLineParser.parseArguments(testArgs)) |
| .isInstanceOf(ParameterException.class) |
| .hasMessageContainingAll("option", "required"); |
| } |
| |
| @Test |
| public void parseArguments_helpParameterPassed_doesNotThrowException() { |
| String[] testArgs = new String[] {"--help"}; |
| |
| assertThat(catchThrowable(() -> commandLineParser.parseArguments(testArgs))) |
| .doesNotThrowAnyException(); |
| } |
| |
| @Test |
| public void parseArguments_validFilePath_shouldNotThrowException() { |
| String[] testArgs = new String[] {"--file", validFileUriPath}; |
| |
| assertThat(catchThrowable(() -> commandLineParser.parseArguments(testArgs))) |
| .doesNotThrowAnyException(); |
| } |
| |
| @Test |
| public void parseArguments_invalidFilePath_throwsParameterExceptionWithCauseAndMessage() { |
| String[] testArgs = new String[] {"--file", INVALID_FILE_URI_PARAM}; |
| assertThatThrownBy(() -> commandLineParser.parseArguments(testArgs)) |
| .isInstanceOf(ParameterException.class) |
| .hasRootCauseInstanceOf(FileNotFoundException.class) |
| .hasMessageContaining("not valid"); |
| } |
| |
| @Test |
| public void parseArguments_emptyFilePath_throwsParameterExceptionWithMessage() { |
| String[] testArgs = new String[] {"--file"}; |
| |
| assertThatThrownBy(() -> commandLineParser.parseArguments(testArgs)) |
| .isInstanceOf(ParameterException.class) |
| .hasMessageContainingAll("Expected", "value"); |
| } |
| } |