Skip to content

Commit ff0eec8

Browse files
OpenAPI: Generate test cases with concrete input values (#102)
Fixes issue #85
1 parent f299e6c commit ff0eec8

File tree

295 files changed

+47009
-1420
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

295 files changed

+47009
-1420
lines changed

tcases-cli/src/main/java/org/cornutum/tcases/openapi/ApiCommand.java

+295-39
Large diffs are not rendered by default.

tcases-cli/src/test/java/org/cornutum/tcases/openapi/ApiCommandTest.java

+250-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.cornutum.tcases.io.SystemInputResources;
1111
import org.cornutum.tcases.io.SystemTestResources;
1212
import org.cornutum.tcases.openapi.ApiCommand.Options;
13+
import org.cornutum.tcases.openapi.resolver.ResolverException;
1314

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

465+
/**
466+
* Tests {@link ApiCommand#run run()} using the following inputs.
467+
* <P>
468+
* <TABLE border="1" cellpadding="8">
469+
* <TR align="left"><TH colspan=2> 9. run (Success) </TH></TR>
470+
* <TR align="left"><TH> Input Choice </TH> <TH> Value </TH></TR>
471+
* <TR><TD> Perspective </TD> <TD> (not applicable) </TD> </TR>
472+
* <TR><TD> Model-Type </TD> <TD> Default </TD> </TR>
473+
* <TR><TD> Condition-Handler </TD> <TD> Log </TD> </TR>
474+
* <TR><TD> Output-File.Defined </TD> <TD> No </TD> </TR>
475+
* <TR><TD> Output-File.Path </TD> <TD> (not applicable) </TD> </TR>
476+
* <TR><TD> Output-Dir.Defined </TD> <TD> No </TD> </TR>
477+
* <TR><TD> Output-Dir.Exists </TD> <TD> (not applicable) </TD> </TR>
478+
* <TR><TD> Read-Only-Enforced </TD> <TD> Yes </TD> </TR>
479+
* <TR><TD> Write-Only-Enforced </TD> <TD> No </TD> </TR>
480+
* <TR><TD> Output-Transformer </TD> <TD> (not applicable) </TD> </TR>
481+
* <TR><TD> Request-Cases.Selected </TD> <TD> Yes </TD> </TR>
482+
* <TR><TD> Request-Cases.Random-Seed </TD> <TD> Defined </TD> </TR>
483+
* <TR><TD> Request-Cases.Max-Tries </TD> <TD> Default </TD> </TR>
484+
* <TR><TD> Request-Cases.Condition-Handler </TD> <TD> Fail </TD> </TR>
485+
* <TR><TD> Api-Spec.Defined </TD> <TD> No </TD> </TR>
486+
* <TR><TD> Api-Spec.Path </TD> <TD> (not applicable) </TD> </TR>
487+
* </TABLE>
488+
* </P>
489+
*/
490+
@Test
491+
public void run_9() throws Exception
492+
{
493+
// Given...
494+
File apiFile = getResourceFile( "api-run-9.json");
495+
StringBuffer outFile = new StringBuffer();
496+
497+
String[] args =
498+
{
499+
"-D",
500+
"-c", "log,fail",
501+
"-R",
502+
"-r", "1234567"
503+
};
504+
505+
expectFailure( ResolverException.class)
506+
.when( () -> runWithStdIO( new Options( args), apiFile, outFile))
507+
.then( failure -> assertThat( "Failure", failure.getMessage(), is( "Error processing RequestCaseDef[3,POST,/array,SUCCESS], param0, value, unique item[1] of 3")));
508+
}
509+
510+
/**
511+
* Tests {@link ApiCommand#run run()} using the following inputs.
512+
* <P>
513+
* <TABLE border="1" cellpadding="8">
514+
* <TR align="left"><TH colspan=2> 10. run (Success) </TH></TR>
515+
* <TR align="left"><TH> Input Choice </TH> <TH> Value </TH></TR>
516+
* <TR><TD> Perspective </TD> <TD> (not applicable) </TD> </TR>
517+
* <TR><TD> Model-Type </TD> <TD> Test </TD> </TR>
518+
* <TR><TD> Condition-Handler </TD> <TD> Ignore </TD> </TR>
519+
* <TR><TD> Output-File.Defined </TD> <TD> Yes </TD> </TR>
520+
* <TR><TD> Output-File.Path </TD> <TD> Absolute </TD> </TR>
521+
* <TR><TD> Output-Dir.Defined </TD> <TD> No </TD> </TR>
522+
* <TR><TD> Output-Dir.Exists </TD> <TD> (not applicable) </TD> </TR>
523+
* <TR><TD> Read-Only-Enforced </TD> <TD> No </TD> </TR>
524+
* <TR><TD> Write-Only-Enforced </TD> <TD> Yes </TD> </TR>
525+
* <TR><TD> Output-Transformer </TD> <TD> (not applicable) </TD> </TR>
526+
* <TR><TD> Request-Cases.Selected </TD> <TD> Yes </TD> </TR>
527+
* <TR><TD> Request-Cases.Random-Seed </TD> <TD> Default </TD> </TR>
528+
* <TR><TD> Request-Cases.Max-Tries </TD> <TD> Defined </TD> </TR>
529+
* <TR><TD> Request-Cases.Condition-Handler </TD> <TD> Default </TD> </TR>
530+
* <TR><TD> Api-Spec.Defined </TD> <TD> No </TD> </TR>
531+
* <TR><TD> Api-Spec.Path </TD> <TD> (not applicable) </TD> </TR>
532+
* </TABLE>
533+
* </P>
534+
*/
535+
@Test
536+
public void run_10() throws Exception
537+
{
538+
// Given...
539+
File apiFile = getResourceFile( "api-run-10.json");
540+
File outFile = new File( apiFile.getParentFile(), "api-run-10-Output.json");
541+
542+
outFile.delete();
543+
544+
String[] args =
545+
{
546+
"-D",
547+
"-c", "ignore",
548+
"-f", outFile.getPath(),
549+
"-W",
550+
"-m", "123"
551+
};
552+
553+
// When...
554+
runWithStdIO( new Options( args), apiFile, null);
555+
556+
// Then...
557+
assertThat( "Output model created", outFile.exists(), is( true));
558+
}
559+
560+
/**
561+
* Tests {@link ApiCommand#run run()} using the following inputs.
562+
* <P>
563+
* <TABLE border="1" cellpadding="8">
564+
* <TR align="left"><TH colspan=2> 11. run (Success) </TH></TR>
565+
* <TR align="left"><TH> Input Choice </TH> <TH> Value </TH></TR>
566+
* <TR><TD> Perspective </TD> <TD> (not applicable) </TD> </TR>
567+
* <TR><TD> Model-Type </TD> <TD> Default </TD> </TR>
568+
* <TR><TD> Condition-Handler </TD> <TD> Default </TD> </TR>
569+
* <TR><TD> Output-File.Defined </TD> <TD> No </TD> </TR>
570+
* <TR><TD> Output-File.Path </TD> <TD> (not applicable) </TD> </TR>
571+
* <TR><TD> Output-Dir.Defined </TD> <TD> Yes </TD> </TR>
572+
* <TR><TD> Output-Dir.Exists </TD> <TD> Yes </TD> </TR>
573+
* <TR><TD> Read-Only-Enforced </TD> <TD> Yes </TD> </TR>
574+
* <TR><TD> Write-Only-Enforced </TD> <TD> No </TD> </TR>
575+
* <TR><TD> Output-Transformer </TD> <TD> (not applicable) </TD> </TR>
576+
* <TR><TD> Request-Cases.Selected </TD> <TD> Yes </TD> </TR>
577+
* <TR><TD> Request-Cases.Random-Seed </TD> <TD> Defined </TD> </TR>
578+
* <TR><TD> Request-Cases.Max-Tries </TD> <TD> Default </TD> </TR>
579+
* <TR><TD> Request-Cases.Condition-Handler </TD> <TD> Log </TD> </TR>
580+
* <TR><TD> Api-Spec.Defined </TD> <TD> Yes </TD> </TR>
581+
* <TR><TD> Api-Spec.Path </TD> <TD> Relative </TD> </TR>
582+
* </TABLE>
583+
* </P>
584+
*/
585+
@Test
586+
public void run_11() throws Exception
587+
{
588+
// Given...
589+
File apiFile = getResourceFile( "api-run-11.json");
590+
File outDir = new File( apiFile.getParentFile(), "11");
591+
File outFile = new File( outDir, "api-run-11-Request-Cases.json");
592+
593+
FileUtils.deleteDirectory( outDir);
594+
595+
String[] args =
596+
{
597+
"-D",
598+
"-c", ",log",
599+
"-o", outDir.getPath(),
600+
"-R",
601+
"-r", "1234567890",
602+
apiFile.getPath()
603+
};
604+
605+
// When...
606+
ApiCommand.run( new Options( args));
607+
608+
// Then...
609+
assertThat( "Output model created", outFile.exists(), is( true));
610+
}
611+
612+
/**
613+
* Tests {@link ApiCommand#run run()} using the following inputs.
614+
* <P>
615+
* <TABLE border="1" cellpadding="8">
616+
* <TR align="left"><TH colspan=2> 12. run (Success) </TH></TR>
617+
* <TR align="left"><TH> Input Choice </TH> <TH> Value </TH></TR>
618+
* <TR><TD> Perspective </TD> <TD> (not applicable) </TD> </TR>
619+
* <TR><TD> Model-Type </TD> <TD> Default </TD> </TR>
620+
* <TR><TD> Condition-Handler </TD> <TD> Fail </TD> </TR>
621+
* <TR><TD> Output-File.Defined </TD> <TD> Yes </TD> </TR>
622+
* <TR><TD> Output-File.Path </TD> <TD> Relative </TD> </TR>
623+
* <TR><TD> Output-Dir.Defined </TD> <TD> Yes </TD> </TR>
624+
* <TR><TD> Output-Dir.Exists </TD> <TD> No </TD> </TR>
625+
* <TR><TD> Read-Only-Enforced </TD> <TD> No </TD> </TR>
626+
* <TR><TD> Write-Only-Enforced </TD> <TD> Yes </TD> </TR>
627+
* <TR><TD> Output-Transformer </TD> <TD> (not applicable) </TD> </TR>
628+
* <TR><TD> Request-Cases.Selected </TD> <TD> Yes </TD> </TR>
629+
* <TR><TD> Request-Cases.Random-Seed </TD> <TD> Default </TD> </TR>
630+
* <TR><TD> Request-Cases.Max-Tries </TD> <TD> Defined </TD> </TR>
631+
* <TR><TD> Request-Cases.Condition-Handler </TD> <TD> Ignore </TD> </TR>
632+
* <TR><TD> Api-Spec.Defined </TD> <TD> Yes </TD> </TR>
633+
* <TR><TD> Api-Spec.Path </TD> <TD> Absolute </TD> </TR>
634+
* </TABLE>
635+
* </P>
636+
*/
637+
@Test
638+
public void run_12() throws Exception
639+
{
640+
// Given...
641+
File apiFile = getResourceFile( "api-run-12.json");
642+
File outDir = new File( apiFile.getParentFile(), "12");
643+
File outFile = new File( outDir, "api-run-12-Output.json");
644+
645+
FileUtils.deleteDirectory( outDir);
646+
647+
String[] args =
648+
{
649+
"-D",
650+
"-c", "fail,ignore",
651+
"-o", outDir.getPath(),
652+
"-f", outFile.getName(),
653+
"-W",
654+
apiFile.getPath()
655+
};
656+
657+
// When...
658+
ApiCommand.run( new Options( args));
659+
660+
// Then...
661+
assertThat( "Output model created", outFile.exists(), is( true));
662+
}
663+
664+
/**
665+
* Tests {@link ApiCommand#run run()} using the following inputs.
666+
* <P>
667+
* <TABLE border="1" cellpadding="8">
668+
* <TR align="left"><TH colspan=2> 13. run (Success) </TH></TR>
669+
* <TR align="left"><TH> Input Choice </TH> <TH> Value </TH></TR>
670+
* <TR><TD> Perspective </TD> <TD> (not applicable) </TD> </TR>
671+
* <TR><TD> Model-Type </TD> <TD> Input </TD> </TR>
672+
* <TR><TD> Condition-Handler </TD> <TD> Default </TD> </TR>
673+
* <TR><TD> Output-File.Defined </TD> <TD> No </TD> </TR>
674+
* <TR><TD> Output-File.Path </TD> <TD> (not applicable) </TD> </TR>
675+
* <TR><TD> Output-Dir.Defined </TD> <TD> No </TD> </TR>
676+
* <TR><TD> Output-Dir.Exists </TD> <TD> (not applicable) </TD> </TR>
677+
* <TR><TD> Read-Only-Enforced </TD> <TD> No </TD> </TR>
678+
* <TR><TD> Write-Only-Enforced </TD> <TD> No </TD> </TR>
679+
* <TR><TD> Output-Transformer </TD> <TD> (not applicable) </TD> </TR>
680+
* <TR><TD> Request-Cases.Selected </TD> <TD> Yes </TD> </TR>
681+
* <TR><TD> Request-Cases.Random-Seed </TD> <TD> Default </TD> </TR>
682+
* <TR><TD> Request-Cases.Max-Tries </TD> <TD> Default </TD> </TR>
683+
* <TR><TD> Request-Cases.Condition-Handler </TD> <TD> Default </TD> </TR>
684+
* <TR><TD> Api-Spec.Defined </TD> <TD> Yes </TD> </TR>
685+
* <TR><TD> Api-Spec.Path </TD> <TD> Relative </TD> </TR>
686+
* </TABLE>
687+
* </P>
688+
*/
689+
@Test
690+
public void run_13() throws Exception
691+
{
692+
// Given...
693+
File apiFile = getResourceFile( "api-run-13.json");
694+
File outFile = new File( apiFile.getParentFile(), "api-run-13-Requests-Input.json");
695+
696+
outFile.delete();
697+
698+
String[] args =
699+
{
700+
"-D",
701+
"-I",
702+
"-c", " , ",
703+
apiFile.getPath()
704+
};
705+
706+
// When...
707+
ApiCommand.run( new Options( args));
708+
709+
// Then...
710+
assertThat( "Output model created", outFile.exists(), is( true));
711+
}
712+
464713
/**
465714
* Return the file for the given resource.
466715
*/
@@ -475,7 +724,7 @@ private File getResourceFile( String resource)
475724
* If <CODE>stdIn</CODE> is non-null, redirect standard input to read from the given file.
476725
* If <CODE>stdOut</CODE> is non-null, redirect standard output to write to the given buffer.
477726
*/
478-
private void runWithStdIO( Options options, File stdIn, StringBuffer stdOut)
727+
private void runWithStdIO( Options options, File stdIn, StringBuffer stdOut) throws Exception
479728
{
480729
InputStream prevIn = System.in;
481730
PrintStream prevOut = System.out;
@@ -499,10 +748,6 @@ private void runWithStdIO( Options options, File stdIn, StringBuffer stdOut)
499748

500749
ApiCommand.run( options);
501750
}
502-
catch( Exception e)
503-
{
504-
throw new RuntimeException( "Can't run with options=" + options, e);
505-
}
506751
finally
507752
{
508753
IOUtils.closeQuietly( newIn);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "String",
5+
"version": "0.0.0"
6+
},
7+
"paths": {
8+
"/string": {
9+
"post": {
10+
"parameters": [
11+
{
12+
"name": "param0",
13+
"in": "cookie",
14+
"schema": {
15+
"type": "string",
16+
"format": "password",
17+
"default": "Change Me!",
18+
"maxLength": 128,
19+
"nullable": true
20+
}
21+
}
22+
],
23+
"responses": {
24+
"200": {
25+
"$ref": "#/components/responses/success"
26+
},
27+
"default": {
28+
"$ref": "#/components/responses/failure"
29+
}
30+
}
31+
}
32+
}
33+
},
34+
"components": {
35+
"parameters": {
36+
"id": {
37+
"name": "id",
38+
"in": "query",
39+
"schema": {
40+
"type": "integer"
41+
}
42+
}
43+
},
44+
"requestBodies": {
45+
"standard": {
46+
"content": {
47+
"application/json": {
48+
"schema": {
49+
"type": "object"
50+
}
51+
}
52+
}
53+
}
54+
},
55+
"responses": {
56+
"success": {
57+
"description": "Success",
58+
"content": {
59+
"application/json": {
60+
"schema": {
61+
"type": "object"
62+
}
63+
}
64+
}
65+
},
66+
"failure": {
67+
"description": "Error",
68+
"content": {
69+
"application/json": {
70+
"schema": {
71+
"type": "object"
72+
}
73+
}
74+
}
75+
}
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)