SwaggerGenMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException { getLog().debug("Creating output directory \"" + outputDirectory + "\"."); java.nio.file.Path outputDirectoryPath = Paths.get(outputDirectory.toURI()); if (!Files.exists(outputDirectoryPath)) { try { Files.createDirectories(outputDirectoryPath); } catch (IOException e) { throw new MojoExecutionException("Unable to create directory for output path \"" + outputDirectoryPath + "\".", e); } } Swagger swagger = getSwagger(); ModelClassFinder modelClassFinder = new ModelClassFinder(getLog(), modelJavaPackage, modelErrorClassName); RestControllerProcessor restControllerProcessor = new RestControllerProcessor(getLog(), swagger, restJavaPackage, tagPatternParameter, modelClassFinder.getModelErrorClass()); XsdParser xsdParser = null; if (xsdName != null) { xsdParser = new XsdParser(xsdName); } new DefinitionGenerator(getLog(), swagger, restControllerProcessor.getExampleClassNames(), modelClassFinder.getModelClasses(), xsdParser); if (applyOperationsFilter != null && applyOperationsFilter.equals(Boolean.TRUE)) { OperationsFilter specFilter = new OperationsFilter(includeOperations); swagger = new SpecFilter().filter(swagger, specFilter, null, null, null); } createYamlFile(swagger); }  @Override void execute();  }
@Test public void test_1() throws Exception { File tempDir = Files.createTempDir(); String outputFileName = "swagger.yaml"; SwaggerGenMojo swaggerGenMojo = new SwaggerGenMojo(); ReflectionTestUtils.setField(swaggerGenMojo, "outputDirectory", tempDir); ReflectionTestUtils.setField(swaggerGenMojo, "outputFilename", outputFileName); ReflectionTestUtils.setField(swaggerGenMojo, "restJavaPackage", "org.finra.herd.swaggergen.test.swaggerGenMojo.rest"); ReflectionTestUtils.setField(swaggerGenMojo, "modelJavaPackage", "org.finra.herd.swaggergen.test.swaggerGenMojo.model"); ReflectionTestUtils.setField(swaggerGenMojo, "modelErrorClassName", "ErrorResponse"); ReflectionTestUtils.setField(swaggerGenMojo, "tagPatternParameter", "(?<tag>.+?)RestController"); ReflectionTestUtils.setField(swaggerGenMojo, "title", "test_title"); ReflectionTestUtils.setField(swaggerGenMojo, "version", "test_version"); ReflectionTestUtils.setField(swaggerGenMojo, "basePath", "/test_basePath"); ReflectionTestUtils.setField(swaggerGenMojo, "schemeParameters", Arrays.asList("http", "https")); swaggerGenMojo.execute(); assertEquals(getResourceAsString("/yaml4.yaml"), getFileAsString(Paths.get(tempDir.getAbsolutePath(), outputFileName))); }
@Test public void test_2() throws Exception { File tempDir = Files.createTempDir(); tempDir = Paths.get(tempDir.toURI()).resolve("non_existent").toFile(); String outputFileName = "swagger.yaml"; SwaggerGenMojo swaggerGenMojo = new SwaggerGenMojo(); ReflectionTestUtils.setField(swaggerGenMojo, "outputDirectory", tempDir); ReflectionTestUtils.setField(swaggerGenMojo, "outputFilename", outputFileName); ReflectionTestUtils.setField(swaggerGenMojo, "restJavaPackage", "org.finra.herd.swaggergen.test.swaggerGenMojo.rest"); ReflectionTestUtils.setField(swaggerGenMojo, "modelJavaPackage", "org.finra.herd.swaggergen.test.swaggerGenMojo.model"); ReflectionTestUtils.setField(swaggerGenMojo, "modelErrorClassName", "ErrorResponse"); ReflectionTestUtils.setField(swaggerGenMojo, "tagPatternParameter", "(?<tag>.+?)RestController"); ReflectionTestUtils.setField(swaggerGenMojo, "title", "test_title"); ReflectionTestUtils.setField(swaggerGenMojo, "version", "test_version"); ReflectionTestUtils.setField(swaggerGenMojo, "basePath", "/test_basePath"); ReflectionTestUtils.setField(swaggerGenMojo, "schemeParameters", Arrays.asList("http", "https")); swaggerGenMojo.execute(); assertEquals(getResourceAsString("/yaml4.yaml"), getFileAsString(Paths.get(tempDir.getAbsolutePath(), outputFileName))); }
@Test public void test_3() throws Exception { File tempDir = Files.createTempDir(); String outputFileName = "swagger.yaml"; SwaggerGenMojo swaggerGenMojo = new SwaggerGenMojo(); ReflectionTestUtils.setField(swaggerGenMojo, "outputDirectory", tempDir); ReflectionTestUtils.setField(swaggerGenMojo, "outputFilename", outputFileName); ReflectionTestUtils.setField(swaggerGenMojo, "restJavaPackage", "org.finra.herd.swaggergen.test.swaggerGenMojo.rest"); ReflectionTestUtils.setField(swaggerGenMojo, "modelJavaPackage", "org.finra.herd.swaggergen.test.swaggerGenMojo.model"); ReflectionTestUtils.setField(swaggerGenMojo, "modelErrorClassName", "ErrorResponse"); ReflectionTestUtils.setField(swaggerGenMojo, "tagPatternParameter", "(?<tag>.+?)RestController"); ReflectionTestUtils.setField(swaggerGenMojo, "title", "test_title"); ReflectionTestUtils.setField(swaggerGenMojo, "version", "test_version"); ReflectionTestUtils.setField(swaggerGenMojo, "basePath", "/test_basePath"); swaggerGenMojo.execute(); assertEquals(getResourceAsString("/yaml5.yaml"), getFileAsString(Paths.get(tempDir.getAbsolutePath(), outputFileName))); }
@Test public void test_4() throws Exception { File tempDir = Files.createTempDir(); String outputFileName = "swagger.yaml"; SwaggerGenMojo swaggerGenMojo = new SwaggerGenMojo(); ReflectionTestUtils.setField(swaggerGenMojo, "outputDirectory", tempDir); ReflectionTestUtils.setField(swaggerGenMojo, "outputFilename", outputFileName); ReflectionTestUtils.setField(swaggerGenMojo, "restJavaPackage", "org.finra.herd.swaggergen.test.swaggerGenMojo.rest"); ReflectionTestUtils.setField(swaggerGenMojo, "modelJavaPackage", "org.finra.herd.swaggergen.test.swaggerGenMojo.model"); ReflectionTestUtils.setField(swaggerGenMojo, "modelErrorClassName", "ErrorResponse"); ReflectionTestUtils.setField(swaggerGenMojo, "tagPatternParameter", "(?<tag>.+?)RestController"); ReflectionTestUtils.setField(swaggerGenMojo, "title", "test_title"); ReflectionTestUtils.setField(swaggerGenMojo, "version", "test_version"); ReflectionTestUtils.setField(swaggerGenMojo, "basePath", "/test_basePath"); ReflectionTestUtils.setField(swaggerGenMojo, "schemeParameters", Arrays.asList("http", null)); try { swaggerGenMojo.execute(); fail(); } catch (Exception e) { assertEquals(MojoExecutionException.class, e.getClass()); assertEquals("Invalid scheme specified: null", e.getMessage()); } }
@Test public void test_5() throws Exception { File tempDir = Files.createTempDir(); String outputFileName = "swagger.yaml"; SwaggerGenMojo swaggerGenMojo = new SwaggerGenMojo(); ReflectionTestUtils.setField(swaggerGenMojo, "outputDirectory", tempDir); ReflectionTestUtils.setField(swaggerGenMojo, "outputFilename", outputFileName); ReflectionTestUtils.setField(swaggerGenMojo, "restJavaPackage", "org.finra.herd.swaggergen.test.swaggerGenMojo.rest"); ReflectionTestUtils.setField(swaggerGenMojo, "modelJavaPackage", "org.finra.herd.swaggergen.test.swaggerGenMojo.model"); ReflectionTestUtils.setField(swaggerGenMojo, "modelErrorClassName", "ErrorResponse"); ReflectionTestUtils.setField(swaggerGenMojo, "tagPatternParameter", "(?<tag>.+?)RestController"); ReflectionTestUtils.setField(swaggerGenMojo, "title", "test_title"); ReflectionTestUtils.setField(swaggerGenMojo, "version", "test_version"); ReflectionTestUtils.setField(swaggerGenMojo, "basePath", "/test_basePath"); ReflectionTestUtils.setField(swaggerGenMojo, "schemeParameters", Arrays.asList("http", "https")); ReflectionTestUtils.setField(swaggerGenMojo, "xsdName", "xsd3.xsd"); swaggerGenMojo.execute(); assertEquals(getResourceAsString("/yaml6.yaml"), getFileAsString(Paths.get(tempDir.getAbsolutePath(), outputFileName))); }
@Test public void test_GetOperationsFilterShouldBeApplied() throws Exception { File tempDir = Files.createTempDir(); String outputFileName = "swagger.yaml"; SwaggerGenMojo swaggerGenMojo = new SwaggerGenMojo(); ReflectionTestUtils.setField(swaggerGenMojo, "outputDirectory", tempDir); ReflectionTestUtils.setField(swaggerGenMojo, "outputFilename", outputFileName); ReflectionTestUtils.setField(swaggerGenMojo, "restJavaPackage", "org.finra.herd.swaggergen.test.swaggerGenMojo.rest"); ReflectionTestUtils.setField(swaggerGenMojo, "modelJavaPackage", "org.finra.herd.swaggergen.test.swaggerGenMojo.model"); ReflectionTestUtils.setField(swaggerGenMojo, "modelErrorClassName", "ErrorResponse"); ReflectionTestUtils.setField(swaggerGenMojo, "tagPatternParameter", "(?<tag>.+?)RestController"); ReflectionTestUtils.setField(swaggerGenMojo, "title", "test_title"); ReflectionTestUtils.setField(swaggerGenMojo, "version", "test_version"); ReflectionTestUtils.setField(swaggerGenMojo, "basePath", "/test_basePath"); ReflectionTestUtils.setField(swaggerGenMojo, "schemeParameters", Arrays.asList("http", "https")); ReflectionTestUtils.setField(swaggerGenMojo, "applyOperationsFilter", true); ReflectionTestUtils.setField(swaggerGenMojo, "includeOperations", new String[] {"Person.get"}); swaggerGenMojo.execute(); assertEquals(getResourceAsString("/yaml_GetOperationsFilter.yaml"), getFileAsString(Paths.get(tempDir.getAbsolutePath(), outputFileName))); }