ApiCommand { public static void run( Options options) throws Exception { if( options.showVersion()) { System.out.println( getVersion()); return; } logger_.info( "{}", getVersion()); File apiSpecFile = options.getApiSpec(); if( apiSpecFile != null && !apiSpecFile.isAbsolute()) { apiSpecFile = new File( options.getWorkingDir(), apiSpecFile.getPath()); } File inputDir = apiSpecFile==null ? options.getWorkingDir() : apiSpecFile.getParentFile(); logger_.info( "Reading API spec from {}", apiSpecFile==null? "standard input" : apiSpecFile); SystemInputDef inputDef = options.isServerTest() ? TcasesOpenApiIO.getRequestInputModel( apiSpecFile, options.getContentType(), options.getModelOptions()) : TcasesOpenApiIO.getResponseInputModel( apiSpecFile, options.getContentType(), options.getModelOptions()); if( inputDef == null) { logger_.warn( "No {} defined", options.isServerTest()? "requests" : "responses"); } else { File outputDir = options.getOutDir(); File outputFile = options.getOutFile(); if( outputFile == null && apiSpecFile != null) { boolean requestTestCases = options.isTests() && options.isRequestCases(); outputFile = new File( String.format( "%s%s-%s-%s.json", Optional.ofNullable( apiSpecFile.getParent()).map( p -> p + "/").orElse( ""), getBaseName( apiSpecFile.getName()), requestTestCases? "Request" : options.isServerTest()? "Requests" : "Responses", requestTestCases? "Cases" : options.isTests()? "Test" : "Input")); } if( outputFile != null) { if( outputDir == null) { outputDir = outputFile.isAbsolute() ? outputFile.getParentFile() : inputDir; } if( !outputDir.exists() && !outputDir.mkdirs()) { throw new RuntimeException( "Can't create output directory=" + outputDir); } outputFile = new File( outputDir, outputFile.getName()); } AbstractFilter transformer; if( !(options.isTests() && options.getTransformType() != null)) { transformer = null; } else if( options.getTransformType() == Options.TransformType.JUNIT) { transformer = new TestDefToJUnitFilter( MapBuilder.of( "system", (Object)inputDef.getName()).build()); outputFile = Optional.ofNullable( outputFile) .map( f -> new File( f.getParentFile(), getBaseName( f.getName()).replaceAll( "\\W+", "") + ".java")) .orElse( null); } else if( options.getTransformType() == Options.TransformType.HTML) { transformer = new TestDefToHtmlFilter(); outputFile = Optional.ofNullable( outputFile) .map( f -> new File( f.getParentFile(), getBaseName( f.getName()) + ".htm")) .orElse( null); } else { transformer = Optional.ofNullable( options.getTransformDef()) .map( transformDefFile -> !transformDefFile.isAbsolute()? new File( inputDir, transformDefFile.getPath()) : transformDefFile) .map( transformDefFile -> new TransformFilter( transformDefFile, options.getTransformParams())) .orElse( null); } if( transformer != null) { transformer.setTarget( outputFile); } OutputStream outputStream = null; try { outputStream = transformer != null? transformer.getSource() : outputFile != null? new FileOutputStream( outputFile) : null; } catch( Exception e) { throw new IllegalStateException( "Can't open output file=" + outputFile, e); } logger_.info( "Writing results to {}", outputFile==null? "standard output" : outputFile); if( !options.isTests()) { TcasesOpenApiIO.writeInputModel( inputDef, outputStream); } else if( transformer != null) { TcasesIO.writeTests( Tcases.getTests( inputDef, null, null), outputStream); } else if( options.isRequestCases()) { if( options.getRandomSeed() == null) { options.setRandomSeed( options.getDefaultRandomSeed()); } logger_.info( "Generating request test cases using random seed={}", options.getRandomSeed()); TcasesOpenApiIO.writeRequestCases( Tcases.getTests( inputDef, null, null), options.getResolverContext(), outputStream); } else { TcasesOpenApiIO.writeTests( Tcases.getTests( inputDef, null, null), outputStream); } } } private  ApiCommand(); static void main( String[] args); static void run( Options options); static String getVersion();  }
@Test public void run_0() throws Exception { File apiFile = getResourceFile( "api-run-0.json"); File outFile = new File( apiFile.getParentFile(), "api-run-0-Responses-Test.htm"); outFile.delete(); String[] args = { "-C", "-c", "fail", "-W", "-H", apiFile.getPath() }; ApiCommand.run( new Options( args)); assertThat( "Output model created", outFile.exists(), is( true)); apiFile = getResourceFile( "api-run-0-fail.json"); outFile = new File( apiFile.getParentFile(), "api-run-0-fail-Requests-Test.htm"); outFile.delete(); String[] failArgs = { "-S", "-c", "fail", "-W", "-H", apiFile.getPath() }; expectFailure( OpenApiException.class) .when( () -> ApiCommand.run( new Options( failArgs))) .then( failure -> assertThat( "Failure", failure.getMessage(), is( "Error processing AllOf, /allOf, POST, param0, allOf, oneOf, oneOf[0]"))); }
@Test public void run_3() throws Exception { File apiFile = getResourceFile( "api-run-3.yml"); String outFileName = "api-run-3-Test.json"; File outFile = new File( apiFile.getParentFile(), "apirun3Test.java"); outFile.delete(); String[] args = { "-C", "-f", outFileName, "-R", "-J", apiFile.getPath() }; ApiCommand.run( new Options( args)); assertThat( "Output model created", outFile.exists(), is( true)); }
@Test public void run_4() throws Exception { File apiFile = getResourceFile( "api-run-4.json"); File outFile = new File( apiFile.getParentFile(), "api-run-4-Requests-Test.json"); outFile.delete(); String[] args = { "-S", "-c", "fail", apiFile.getPath() }; ApiCommand.run( new Options( args)); assertThat( "Output model created", testResources_.readJson( outFile), is( notNullValue())); }
@Test public void run_6() throws Exception { File apiFile = getResourceFile( "api-run-6.yaml"); File outFile = new File( apiFile.getParentFile(), "api-run-6-Test.json"); outFile.delete(); String[] args = { "-C", "-c", "log", "-f", outFile.getName(), "-W", apiFile.getPath() }; ApiCommand.run( new Options( args)); assertThat( "Output model created", testResources_.readJson( outFile), is( notNullValue())); }
@Test public void run_8() throws Exception { File apiFile = getResourceFile( "api-run-8.json"); File outDir = new File( apiFile.getParent(), "transformed"); File outFile = new File( outDir, "api-run-8-Requests-Test.json"); File transformFile = getResourceFile( "api-run-8.xsl"); String[] args = { "-o", outDir.getPath(), "-x", transformFile.getName(), "-p", "system=Foo", apiFile.getPath() }; ApiCommand.run( new Options( args)); assertThat( "Output model created", outFile.exists(), is( true)); }
@Test public void run_11() throws Exception { File apiFile = getResourceFile( "api-run-11.json"); File outDir = new File( apiFile.getParentFile(), "11"); File outFile = new File( outDir, "api-run-11-Request-Cases.json"); FileUtils.deleteDirectory( outDir); String[] args = { "-D", "-c", ",log", "-o", outDir.getPath(), "-R", "-r", "1234567890", apiFile.getPath() }; ApiCommand.run( new Options( args)); assertThat( "Output model created", outFile.exists(), is( true)); }
@Test public void run_12() throws Exception { File apiFile = getResourceFile( "api-run-12.json"); File outDir = new File( apiFile.getParentFile(), "12"); File outFile = new File( outDir, "api-run-12-Output.json"); FileUtils.deleteDirectory( outDir); String[] args = { "-D", "-c", "fail,ignore", "-o", outDir.getPath(), "-f", outFile.getName(), "-W", apiFile.getPath() }; ApiCommand.run( new Options( args)); assertThat( "Output model created", outFile.exists(), is( true)); }
@Test public void run_13() throws Exception { File apiFile = getResourceFile( "api-run-13.json"); File outFile = new File( apiFile.getParentFile(), "api-run-13-Requests-Input.json"); outFile.delete(); String[] args = { "-D", "-I", "-c", " , ", apiFile.getPath() }; ApiCommand.run( new Options( args)); assertThat( "Output model created", outFile.exists(), is( true)); }
@Test public void run_1() throws Exception { File apiFile = stdApiSpec( "OpenApiTest"); File outDir = new File( getResourceDir(), "java/org/cornutum/moco"); File testConfigFile = new File( getResourceDir(), "mocoRestServer.json"); MocoTestConfig testConfig = MocoTestConfig.builder( "RestServer") .serverConfig( MocoServerConfig.file( new File( getResourceDir(), "myRestServerConfig.json")).build()) .build(); writeMocoTestConfig( testConfig, testConfigFile); String[] args = { "-t", "moco", "-b", "MyBaseClass", "-o", outDir.getPath(), "-M", testConfigFile.getPath(), "-P", "/post", "-c", "log,log", "-m", "123", apiFile.getPath() }; ApiTestCommand.run( new Options( args)); File testFile = new File( outDir, "OpenAPIRequestTestCasesTest.java"); String testFileResults = FileUtils.readFileToString( testFile, "UTF-8"); verifyTest( "api-test-1", testFileResults); }
@Test public void run_3() throws Exception { File apiFile = stdApiSpec( "OpenApiTest"); String[] args = { "-t", "testng", "-p", "org.cornutum.examples", "-b", "org.cornutum.testwriter.BaseTest", "-O", "TRACE", "-c", ",ignore", "-m", "10", apiFile.getPath() }; ApiTestCommand.run( new Options( args)); File testFile = new File( apiFile.getParentFile(), "OpenAPIRequestTestCasesTest.java"); String testFileResults = FileUtils.readFileToString( testFile, "UTF-8"); verifyTest( "api-test-3", testFileResults); }
@Test public void run_5() throws Exception { File apiFile = stdApiSpec( "OpenApiTest"); File outDir = new File( getResourceDir(), "java/org/cornutum/moco"); File testConfigFile = new File( getResourceDir(), "mocoHttpsServer.json"); MocoTestConfig testConfig = MocoTestConfig.builder( "HttpsServer") .serverConfig( MocoServerConfig.resource( "myMocoServerConfig").port( 9999).forEachTest().build()) .certConfig( new CertConfigResource( "myCertificate", "myCert.cks", "kss!", "css!")) .build(); writeMocoTestConfig( testConfig, testConfigFile); String[] args = { "-t", "moco", "-n", "org.cornutum.moco.MyMocoTest", "-o", outDir.getPath(), "-M", testConfigFile.getName(), "-P", "/post, /posts", "-O", "get, delete, put", "-c", "ignore,", "-m", "123", apiFile.getPath() }; ApiTestCommand.run( new Options( args)); File testFile = new File( outDir, "MyMocoTest.java"); String testFileResults = FileUtils.readFileToString( testFile, "UTF-8"); verifyTest( "api-test-5", testFileResults); }
@Test public void run_7() throws Exception { File apiFile = stdApiSpec( "OpenApiTest"); String[] args = { "-t", "testng", "-n", "Why Not?", "-p", "org.cornutum.examples", "-P", "/post", "-m", "1234", apiFile.getPath() }; ApiTestCommand.run( new Options( args)); File testFile = new File( apiFile.getParentFile(), "WhyNotTest.java"); String testFileResults = FileUtils.readFileToString( testFile, "UTF-8"); verifyTest( "api-test-7", testFileResults); }
@Test public void run_9() throws Exception { File apiFile = apiSpecFor( getResourceClass(), "read-only-enforced"); File outDir = new File( getResourceDir(), "java/org/cornutum/readonly"); String[] args = { "-n", "ReadOnlyTest", "-b", "MyBaseClass", "-o", outDir.getPath(), "-R", "-m", "1", apiFile.getPath() }; ApiTestCommand.run( new Options( args)); File testFile = new File( outDir, "ReadOnlyTest.java"); String testFileResults = FileUtils.readFileToString( testFile, "UTF-8"); verifyTest( "api-test-9", testFileResults); }
@Test public void run_10() throws Exception { File apiFile = apiSpecFor( getResourceClass(), "modelling-condition"); File outDir = new File( getResourceDir(), "conditions"); String[] args = { "-p", "org.cornutum", "-o", outDir.getPath(), "-c", "fail,ignore", apiFile.getPath() }; assertFailure( OpenApiException.class, () -> ApiTestCommand.run( new Options( args)), "Error processing Numbers, /numbers, POST, param0", "minimum=1000 is greater than maximum=99"); }
@Test public void run_12() throws Exception { File apiFile = apiSpecFor( getResourceClass(), "resolver-condition"); File outFile = new File( getResourceDir(), "java/org/cornutum/MyTest.java"); String[] args = { "-n", "Condition", "-f", outFile.getPath(), "-c", ",fail", apiFile.getPath() }; assertFailure( ResolverException.class, () -> ApiTestCommand.run( new Options( args)), "Error processing RequestCaseDef[3,param0.Items.Size='> 1',POST,/array,SUCCESS], param0, value, unique item[1] of 6", "Unable to resolve a value after 10000 tries"); }
@Test public void run_13() throws Exception { File apiFile = stdApiSpec( "OpenApiTest"); File outDir = new File( getResourceDir(), "java/org/cornutum/moco"); File testConfigFile = new File( getResourceDir(), "mocoHttpServer.json"); MocoTestConfig testConfig = MocoTestConfig.builder( "HttpServer") .serverConfig( MocoServerConfig.resource( "myMocoServerConfig").build()) .build(); writeMocoTestConfig( testConfig, testConfigFile); String[] args = { "-t", "moco", "-n", "MyMocoTest", "-b", "MyBaseClass", "-o", outDir.getPath(), "-O", "get", "-c", ",ignore", apiFile.getPath() }; assertTestWriterException( () -> ApiTestCommand.run( new Options( args)), "Can't create Moco test writer", "No Moco server test configuration defined"); }