Skip to content

Commit

Permalink
OpenAPI: Generate test cases with concrete input values (#102)
Browse files Browse the repository at this point in the history
Fixes issue #85
  • Loading branch information
kerrykimbrough authored Apr 4, 2020
1 parent f299e6c commit ff0eec8
Show file tree
Hide file tree
Showing 295 changed files with 47,009 additions and 1,420 deletions.
334 changes: 295 additions & 39 deletions tcases-cli/src/main/java/org/cornutum/tcases/openapi/ApiCommand.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.cornutum.tcases.io.SystemInputResources;
import org.cornutum.tcases.io.SystemTestResources;
import org.cornutum.tcases.openapi.ApiCommand.Options;
import org.cornutum.tcases.openapi.resolver.ResolverException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -461,6 +462,254 @@ public void run_8() throws Exception
assertThat( "Output model created", outFile.exists(), is( true));
}

/**
* Tests {@link ApiCommand#run run()} using the following inputs.
* <P>
* <TABLE border="1" cellpadding="8">
* <TR align="left"><TH colspan=2> 9. run (Success) </TH></TR>
* <TR align="left"><TH> Input Choice </TH> <TH> Value </TH></TR>
* <TR><TD> Perspective </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Model-Type </TD> <TD> Default </TD> </TR>
* <TR><TD> Condition-Handler </TD> <TD> Log </TD> </TR>
* <TR><TD> Output-File.Defined </TD> <TD> No </TD> </TR>
* <TR><TD> Output-File.Path </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Output-Dir.Defined </TD> <TD> No </TD> </TR>
* <TR><TD> Output-Dir.Exists </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Read-Only-Enforced </TD> <TD> Yes </TD> </TR>
* <TR><TD> Write-Only-Enforced </TD> <TD> No </TD> </TR>
* <TR><TD> Output-Transformer </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Request-Cases.Selected </TD> <TD> Yes </TD> </TR>
* <TR><TD> Request-Cases.Random-Seed </TD> <TD> Defined </TD> </TR>
* <TR><TD> Request-Cases.Max-Tries </TD> <TD> Default </TD> </TR>
* <TR><TD> Request-Cases.Condition-Handler </TD> <TD> Fail </TD> </TR>
* <TR><TD> Api-Spec.Defined </TD> <TD> No </TD> </TR>
* <TR><TD> Api-Spec.Path </TD> <TD> (not applicable) </TD> </TR>
* </TABLE>
* </P>
*/
@Test
public void run_9() throws Exception
{
// Given...
File apiFile = getResourceFile( "api-run-9.json");
StringBuffer outFile = new StringBuffer();

String[] args =
{
"-D",
"-c", "log,fail",
"-R",
"-r", "1234567"
};

expectFailure( ResolverException.class)
.when( () -> runWithStdIO( new Options( args), apiFile, outFile))
.then( failure -> assertThat( "Failure", failure.getMessage(), is( "Error processing RequestCaseDef[3,POST,/array,SUCCESS], param0, value, unique item[1] of 3")));
}

/**
* Tests {@link ApiCommand#run run()} using the following inputs.
* <P>
* <TABLE border="1" cellpadding="8">
* <TR align="left"><TH colspan=2> 10. run (Success) </TH></TR>
* <TR align="left"><TH> Input Choice </TH> <TH> Value </TH></TR>
* <TR><TD> Perspective </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Model-Type </TD> <TD> Test </TD> </TR>
* <TR><TD> Condition-Handler </TD> <TD> Ignore </TD> </TR>
* <TR><TD> Output-File.Defined </TD> <TD> Yes </TD> </TR>
* <TR><TD> Output-File.Path </TD> <TD> Absolute </TD> </TR>
* <TR><TD> Output-Dir.Defined </TD> <TD> No </TD> </TR>
* <TR><TD> Output-Dir.Exists </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Read-Only-Enforced </TD> <TD> No </TD> </TR>
* <TR><TD> Write-Only-Enforced </TD> <TD> Yes </TD> </TR>
* <TR><TD> Output-Transformer </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Request-Cases.Selected </TD> <TD> Yes </TD> </TR>
* <TR><TD> Request-Cases.Random-Seed </TD> <TD> Default </TD> </TR>
* <TR><TD> Request-Cases.Max-Tries </TD> <TD> Defined </TD> </TR>
* <TR><TD> Request-Cases.Condition-Handler </TD> <TD> Default </TD> </TR>
* <TR><TD> Api-Spec.Defined </TD> <TD> No </TD> </TR>
* <TR><TD> Api-Spec.Path </TD> <TD> (not applicable) </TD> </TR>
* </TABLE>
* </P>
*/
@Test
public void run_10() throws Exception
{
// Given...
File apiFile = getResourceFile( "api-run-10.json");
File outFile = new File( apiFile.getParentFile(), "api-run-10-Output.json");

outFile.delete();

String[] args =
{
"-D",
"-c", "ignore",
"-f", outFile.getPath(),
"-W",
"-m", "123"
};

// When...
runWithStdIO( new Options( args), apiFile, null);

// Then...
assertThat( "Output model created", outFile.exists(), is( true));
}

/**
* Tests {@link ApiCommand#run run()} using the following inputs.
* <P>
* <TABLE border="1" cellpadding="8">
* <TR align="left"><TH colspan=2> 11. run (Success) </TH></TR>
* <TR align="left"><TH> Input Choice </TH> <TH> Value </TH></TR>
* <TR><TD> Perspective </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Model-Type </TD> <TD> Default </TD> </TR>
* <TR><TD> Condition-Handler </TD> <TD> Default </TD> </TR>
* <TR><TD> Output-File.Defined </TD> <TD> No </TD> </TR>
* <TR><TD> Output-File.Path </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Output-Dir.Defined </TD> <TD> Yes </TD> </TR>
* <TR><TD> Output-Dir.Exists </TD> <TD> Yes </TD> </TR>
* <TR><TD> Read-Only-Enforced </TD> <TD> Yes </TD> </TR>
* <TR><TD> Write-Only-Enforced </TD> <TD> No </TD> </TR>
* <TR><TD> Output-Transformer </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Request-Cases.Selected </TD> <TD> Yes </TD> </TR>
* <TR><TD> Request-Cases.Random-Seed </TD> <TD> Defined </TD> </TR>
* <TR><TD> Request-Cases.Max-Tries </TD> <TD> Default </TD> </TR>
* <TR><TD> Request-Cases.Condition-Handler </TD> <TD> Log </TD> </TR>
* <TR><TD> Api-Spec.Defined </TD> <TD> Yes </TD> </TR>
* <TR><TD> Api-Spec.Path </TD> <TD> Relative </TD> </TR>
* </TABLE>
* </P>
*/
@Test
public void run_11() throws Exception
{
// Given...
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()
};

// When...
ApiCommand.run( new Options( args));

// Then...
assertThat( "Output model created", outFile.exists(), is( true));
}

/**
* Tests {@link ApiCommand#run run()} using the following inputs.
* <P>
* <TABLE border="1" cellpadding="8">
* <TR align="left"><TH colspan=2> 12. run (Success) </TH></TR>
* <TR align="left"><TH> Input Choice </TH> <TH> Value </TH></TR>
* <TR><TD> Perspective </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Model-Type </TD> <TD> Default </TD> </TR>
* <TR><TD> Condition-Handler </TD> <TD> Fail </TD> </TR>
* <TR><TD> Output-File.Defined </TD> <TD> Yes </TD> </TR>
* <TR><TD> Output-File.Path </TD> <TD> Relative </TD> </TR>
* <TR><TD> Output-Dir.Defined </TD> <TD> Yes </TD> </TR>
* <TR><TD> Output-Dir.Exists </TD> <TD> No </TD> </TR>
* <TR><TD> Read-Only-Enforced </TD> <TD> No </TD> </TR>
* <TR><TD> Write-Only-Enforced </TD> <TD> Yes </TD> </TR>
* <TR><TD> Output-Transformer </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Request-Cases.Selected </TD> <TD> Yes </TD> </TR>
* <TR><TD> Request-Cases.Random-Seed </TD> <TD> Default </TD> </TR>
* <TR><TD> Request-Cases.Max-Tries </TD> <TD> Defined </TD> </TR>
* <TR><TD> Request-Cases.Condition-Handler </TD> <TD> Ignore </TD> </TR>
* <TR><TD> Api-Spec.Defined </TD> <TD> Yes </TD> </TR>
* <TR><TD> Api-Spec.Path </TD> <TD> Absolute </TD> </TR>
* </TABLE>
* </P>
*/
@Test
public void run_12() throws Exception
{
// Given...
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()
};

// When...
ApiCommand.run( new Options( args));

// Then...
assertThat( "Output model created", outFile.exists(), is( true));
}

/**
* Tests {@link ApiCommand#run run()} using the following inputs.
* <P>
* <TABLE border="1" cellpadding="8">
* <TR align="left"><TH colspan=2> 13. run (Success) </TH></TR>
* <TR align="left"><TH> Input Choice </TH> <TH> Value </TH></TR>
* <TR><TD> Perspective </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Model-Type </TD> <TD> Input </TD> </TR>
* <TR><TD> Condition-Handler </TD> <TD> Default </TD> </TR>
* <TR><TD> Output-File.Defined </TD> <TD> No </TD> </TR>
* <TR><TD> Output-File.Path </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Output-Dir.Defined </TD> <TD> No </TD> </TR>
* <TR><TD> Output-Dir.Exists </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Read-Only-Enforced </TD> <TD> No </TD> </TR>
* <TR><TD> Write-Only-Enforced </TD> <TD> No </TD> </TR>
* <TR><TD> Output-Transformer </TD> <TD> (not applicable) </TD> </TR>
* <TR><TD> Request-Cases.Selected </TD> <TD> Yes </TD> </TR>
* <TR><TD> Request-Cases.Random-Seed </TD> <TD> Default </TD> </TR>
* <TR><TD> Request-Cases.Max-Tries </TD> <TD> Default </TD> </TR>
* <TR><TD> Request-Cases.Condition-Handler </TD> <TD> Default </TD> </TR>
* <TR><TD> Api-Spec.Defined </TD> <TD> Yes </TD> </TR>
* <TR><TD> Api-Spec.Path </TD> <TD> Relative </TD> </TR>
* </TABLE>
* </P>
*/
@Test
public void run_13() throws Exception
{
// Given...
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()
};

// When...
ApiCommand.run( new Options( args));

// Then...
assertThat( "Output model created", outFile.exists(), is( true));
}

/**
* Return the file for the given resource.
*/
Expand All @@ -475,7 +724,7 @@ private File getResourceFile( String resource)
* If <CODE>stdIn</CODE> is non-null, redirect standard input to read from the given file.
* If <CODE>stdOut</CODE> is non-null, redirect standard output to write to the given buffer.
*/
private void runWithStdIO( Options options, File stdIn, StringBuffer stdOut)
private void runWithStdIO( Options options, File stdIn, StringBuffer stdOut) throws Exception
{
InputStream prevIn = System.in;
PrintStream prevOut = System.out;
Expand All @@ -499,10 +748,6 @@ private void runWithStdIO( Options options, File stdIn, StringBuffer stdOut)

ApiCommand.run( options);
}
catch( Exception e)
{
throw new RuntimeException( "Can't run with options=" + options, e);
}
finally
{
IOUtils.closeQuietly( newIn);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"openapi": "3.0.0",
"info": {
"title": "String",
"version": "0.0.0"
},
"paths": {
"/string": {
"post": {
"parameters": [
{
"name": "param0",
"in": "cookie",
"schema": {
"type": "string",
"format": "password",
"default": "Change Me!",
"maxLength": 128,
"nullable": true
}
}
],
"responses": {
"200": {
"$ref": "#/components/responses/success"
},
"default": {
"$ref": "#/components/responses/failure"
}
}
}
}
},
"components": {
"parameters": {
"id": {
"name": "id",
"in": "query",
"schema": {
"type": "integer"
}
}
},
"requestBodies": {
"standard": {
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}
},
"responses": {
"success": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
},
"failure": {
"description": "Error",
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}
}
}
}
Loading

0 comments on commit ff0eec8

Please sign in to comment.