ApiMojo extends AbstractMojo { public void execute() throws MojoExecutionException { try { DirectoryScanner inputScanner = new DirectoryScanner(); Set<String> apiDefPatterns = getApiDefs(); if( !apiDefPatterns.isEmpty()) { } else if( !StringUtils.isBlank( getProject())) { apiDefPatterns.add( "**/" + getProject() + ".json"); apiDefPatterns.add( "**/" + getProject() + ".yaml"); apiDefPatterns.add( "**/" + getProject() + ".yml"); } else if( !StringUtils.isBlank( getApiDef())) { apiDefPatterns.add( getApiDef()); } else { apiDefPatterns.add( "***.yaml"); apiDefPatterns.add( "**/*.yml"); } inputScanner.setIncludes( apiDefPatterns.toArray( new String[0])); File inputRootDir = getInputDirFile(); inputScanner.setBasedir( inputRootDir); inputScanner.scan(); String[] apiDefs = inputScanner.getIncludedFiles(); for( int i = 0; i < apiDefs.length; i++) { String inputFile = apiDefs[i]; File apiDef = new File( inputRootDir, inputFile); Options options = new Options(); options.setApiSpec( apiDef); options.setContentType( getContentType()); options.setOutDir( new File( getOutDirFile(), getPath( inputFile))); if( isJunit()) { options.setTransformType( TransformType.JUNIT); } if( isHtml()) { options.setTransformType( TransformType.HTML); } if( getTransformDef() != null) { options.setTransformType( TransformType.CUSTOM); options.setTransformParams( getTransformParams()); } options.setTests( !isInputModels()); options.setOnModellingCondition( "log".equals( getOnModellingCondition())? getOnCondition() : getOnModellingCondition()); options.setReadOnlyEnforced( isReadOnlyEnforced()); options.setWriteOnlyEnforced( isWriteOnlyEnforced()); if( isRequestCases()) { options.setRequestCases( true); options.setOnResolverCondition( getOnResolverCondition()); options.setMaxTries( getMaxTries()); options.setRandomSeed( getRandom()); } else { options.setServerTest( true); options.setTransformDef( resolveTransformDefFile( apiDef, options.isServerTest())); options.setOutFile( resolveTransformOutFile( apiDef, options.isServerTest())); } ApiCommand.run( options); options.setServerTest( false); options.setTransformDef( resolveTransformDefFile( apiDef, options.isServerTest())); options.setOutFile( resolveTransformOutFile( apiDef, options.isServerTest())); ApiCommand.run( options); } } catch( Exception e) { throw new MojoExecutionException( "Can't generate requested models", e); } }  void execute(); void setApiDefs( Set<String> apiDefs); Set<String> getApiDefs(); void setApiDef( String apiDef); String getApiDef(); void setProject( String project); String getProject(); void setInputDir( String inputDir); String getInputDir(); File getInputDirFile(); void setContentType( String contentType); String getContentType(); void setOutDir( String outDir); String getOutDir(); File getOutDirFile(); void setJunit( boolean junit); boolean isJunit(); void setHtml( boolean html); boolean isHtml(); void setTransformDef( String transformDef); String getTransformDef(); void setTransformParams( Map<String,Object> params); Map<String,Object> getTransformParams(); void setTransformOutFile( String transformOutFile); String getTransformOutFile(); void setInputModels( boolean inputModels); boolean isInputModels(); void setOnCondition( String onCondition); String getOnCondition(); void setOnModellingCondition( String onCondition); String getOnModellingCondition(); void setOnResolverCondition( String onCondition); String getOnResolverCondition(); void setReadOnlyEnforced( boolean readOnlyEnforced); boolean isReadOnlyEnforced(); void setWriteOnlyEnforced( boolean writeOnlyEnforced); boolean isWriteOnlyEnforced(); void setRequestCases( boolean requestCases); boolean isRequestCases(); void setMaxTries( int maxTries); int getMaxTries(); void setRandom( Long random); Long getRandom();  }
@Test public void execute_6() throws Exception { File baseDirTest = getBaseDirTest( "api-project-6"); ApiMojo apiMojo = (ApiMojo) mojoHelper.lookupConfiguredMojo( baseDirTest, "api"); clean( apiMojo); expectFailure( MojoExecutionException.class) .when( () -> apiMojo.execute()) .then( failure -> { Stream.Builder<String> causes = Stream.builder(); for( Throwable cause = failure; cause != null; cause = cause.getCause()) { causes.add( cause.getMessage()); } assertThat( "Causes", causes.build().collect( toList()), listsMembers( "Can't generate requested models", "Error processing AllOf, /allOf, POST, param0, allOf, oneOf, oneOf[0]", "Ignoring this schema -- not applicable when only instance types=[string] can be valid")); }); }
@Test public void execute_12() throws Exception { File baseDirTest = getBaseDirTest( "api-project-12"); ApiMojo apiMojo = (ApiMojo) mojoHelper.lookupConfiguredMojo( baseDirTest, "api"); clean( apiMojo); expectFailure( MojoExecutionException.class) .when( () -> apiMojo.execute()) .then( failure -> { Stream.Builder<String> causes = Stream.builder(); for( Throwable cause = failure; cause != null; cause = cause.getCause()) { causes.add( cause.getMessage()); } assertThat( "Causes", causes.build().collect( toList()), listsMembers( "Can't generate requested models", "Error processing AllOf, /allOf, POST, param0, allOf, oneOf, oneOf[0]", "Ignoring this schema -- not applicable when only instance types=[string] can be valid")); }); }
@Test public void execute_13() throws Exception { File baseDirTest = getBaseDirTest( "api-project-13"); ApiMojo apiMojo = (ApiMojo) mojoHelper.lookupConfiguredMojo( baseDirTest, "api"); clean( apiMojo); expectFailure( MojoExecutionException.class) .when( () -> apiMojo.execute()) .then( failure -> { Stream.Builder<String> causes = Stream.builder(); for( Throwable cause = failure; cause != null; cause = cause.getCause()) { causes.add( cause.getMessage()); } assertThat( "Causes", causes.build().collect( toList()), listsMembers( "Can't generate requested models", "Error processing RequestCaseDef[3,param0.Items.Size='> 1',POST,/array,SUCCESS], param0, value, unique item[1] of 11", "Unable to resolve a value after 10000 tries")); }); }
@Test public void execute() throws Exception { File baseDirTest = getBaseDirTest( "api-test-project"); ApiTestMojo apiTestMojo = (ApiTestMojo) mojoHelper.lookupConfiguredMojo( baseDirTest, "api-test"); clean( apiTestMojo); apiTestMojo.execute(); File expectedInputDir = new File( baseDirTest, "src/test/tcases/openapi"); assertThat( "Input dir", apiTestMojo.getInputDirFile(), is( expectedInputDir)); File expectedOutDir = new File( baseDirTest, "target/generated-test-sources/java"); assertThat( "Out dir", apiTestMojo.getOutDirFile(), is( expectedOutDir)); List<String> expectedTests = Arrays.asList( "org/cornutum/api/MyTest.java"); assertThat( "Tests", Arrays.asList( findPathsMatching( expectedOutDir, "**/*")), containsMembers( expectedTests)); }