| package com.apigee.security.oas; |
| |
| import static org.assertj.core.api.Assertions.assertThat; |
| import static org.mockito.ArgumentMatchers.any; |
| import static org.mockito.Mockito.times; |
| import static org.mockito.Mockito.verify; |
| import static org.mockito.Mockito.when; |
| |
| import com.apigee.security.oas.providers.PrintWriterProvider; |
| import com.google.common.io.Resources; |
| import com.google.inject.Binder; |
| import com.google.inject.Guice; |
| import com.google.inject.Module; |
| import com.google.inject.util.Modules; |
| import java.io.PrintWriter; |
| import org.junit.Before; |
| import org.junit.Rule; |
| import org.junit.Test; |
| import org.junit.experimental.categories.Category; |
| import org.mockito.ArgumentCaptor; |
| import org.mockito.Captor; |
| import org.mockito.Mock; |
| import org.mockito.junit.MockitoJUnit; |
| import org.mockito.junit.MockitoRule; |
| import org.mockito.quality.Strictness; |
| |
| @Category(SlowTests.class) |
| public class CommandLineBaseRunnerIntegrationTest { |
| |
| public final class TestModule implements Module { |
| @Override |
| public void configure(Binder binder) { |
| binder.bind(PrintWriterProvider.class).toInstance(printWriterProvider); |
| } |
| } |
| |
| @Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); |
| @Captor private ArgumentCaptor<String> stringCaptor; |
| @Captor private ArgumentCaptor<Integer> intCaptor; |
| @Mock private PrintWriterProvider printWriterProvider; |
| @Mock private PrintWriter printWriter; |
| private CommandLineRunner commandLineRunner; |
| |
| private static String getResourcePath(String name) { |
| return Resources.getResource(name).getPath(); |
| } |
| |
| /** Overriding guice dependencies and setting up command line runner. */ |
| @Before |
| public void setup() { |
| when(printWriterProvider.get()).thenReturn(printWriter); |
| |
| commandLineRunner = |
| Guice.createInjector(Modules.override(new CommandLineModule()).with(new TestModule())) |
| .getInstance(CommandLineRunner.class); |
| } |
| |
| @Test |
| public void run_wrongFileType_printsParserException() { |
| commandLineRunner.run(new String[] {"--file", getResourcePath("Test_Text_File.txt")}); |
| |
| verify(printWriter, times(2)).printf(any(String.class), stringCaptor.capture()); |
| |
| assertThat(stringCaptor.getAllValues().toString().toLowerCase()).contains("failed", "parse"); |
| } |
| |
| @Test |
| public void run_emptyFile_printsParameterException() { |
| commandLineRunner.run(new String[] {"--file", getResourcePath("Empty_Json_File.json")}); |
| |
| verify(printWriter).printf(any(String.class), stringCaptor.capture()); |
| |
| assertThat(stringCaptor.getValue().toLowerCase()).contains("file", "empty"); |
| } |
| |
| @Test |
| public void run_validOasFile_printsZeroErrors() { |
| commandLineRunner.run(new String[] {"--file", getResourcePath("Valid_OAS.json")}); |
| |
| verify(printWriter).printf(stringCaptor.capture(), intCaptor.capture()); |
| |
| assertThat(intCaptor.getValue()).isEqualTo(0); |
| assertThat(stringCaptor.getValue().toLowerCase()).contains("found", "errors"); |
| } |
| |
| @Test |
| public void run_validOasFile_validSecurityExtensionUsage_printsZeroErrors() { |
| commandLineRunner.run( |
| new String[] {"--file", getResourcePath("Valid_OAS_Valid_Extensions.json")}); |
| |
| verify(printWriter).printf(stringCaptor.capture(), intCaptor.capture()); |
| |
| assertThat(intCaptor.getValue()).isEqualTo(0); |
| assertThat(stringCaptor.getValue().toLowerCase()).contains("found", "errors"); |
| } |
| |
| @Test |
| public void run_invalidOasFile() { |
| commandLineRunner.run(new String[] {"--file", getResourcePath("Invalid_OAS.json")}); |
| |
| verify(printWriter, times(2)).printf(any(String.class), stringCaptor.capture()); |
| |
| assertThat(stringCaptor.getAllValues().toString().toLowerCase()) |
| .contains("failed", "parse", "openapi3", "failure"); |
| } |
| |
| @Test |
| public void run_validOasFile_invalidSecurityExtensionSchema_printsErrors() { |
| commandLineRunner.run( |
| new String[] {"--file", getResourcePath("Valid_OAS_Invalid_Schema.json")}); |
| |
| verify(printWriter, times(4)).printf(any(String.class), stringCaptor.capture()); |
| assertThat(stringCaptor.getAllValues()).contains("INVALID_SCHEMA"); |
| } |
| |
| @Test |
| public void run_validOasFile_invalidSecurityExtensionScope_printsErrors() { |
| commandLineRunner.run(new String[] {"--file", getResourcePath("Valid_OAS_Invalid_Scope.json")}); |
| |
| verify(printWriter, times(5)).printf(any(String.class), stringCaptor.capture()); |
| assertThat(stringCaptor.getAllValues()).contains("INVALID_SCOPE"); |
| } |
| } |