Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApiTestCommand: Add option to generate separate test files for each API resource path #173

Merged
merged 2 commits into from
May 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import static java.util.Collections.singleton;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toSet;

@@ -215,6 +216,19 @@ public class ApiTestCommand
*  
* </TD>
* <TD>
* <NOBR>-S</NOBR>
* </TD>
* <TD>
* If specified, a separate test file is generated for each of the API resource paths specified by the <I>P</I> option,
* each containing tests for a single path. Otherwise, a single test file is generated containing tests for all paths.
* </TD>
* </TR>
*
* <TR valign="top">
* <TD>
* &nbsp;
* </TD>
* <TD>
* <NOBR>-O operations </NOBR>
* </TD>
* <TD>
@@ -495,6 +509,11 @@ else if( arg.equals( "-P"))
setPaths( Arrays.asList( args[i].split( " *, *")));
}

else if( arg.equals( "-S"))
{
setByPath( true);
}

else if( arg.equals( "-O"))
{
i++;
@@ -670,6 +689,10 @@ protected void printUsage()
" option must be a comma-separated list of resource paths defined in the apiSpec. If",
" omitted, tests are generated for all resource paths.",
"",
" -S If specified, a separate test file is generated for each of the API resource paths",
" specified by the -P option, each containing tests for a single path. Otherwise, a",
" single test file is generated containing tests for all paths.",
"",
" -O operations If defined, tests are generated only for the specified HTTP methods. The operations",
" option must be a comma-separated list of path operations defined in the apiSpec. If",
" omitted, tests are generated for all operations.",
@@ -918,6 +941,22 @@ public Set<String> getPaths()
return paths_;
}

/**
* Returns true if generating a separate test file for each request path.
*/
public void setByPath( boolean byPath)
{
byPath_ = byPath;
}

/**
* Returns true if generating a separate test file for each request path.
*/
public boolean isByPath()
{
return byPath_;
}

/**
* Changes request path operations for which tests are generated.
*/
@@ -1304,7 +1343,8 @@ public String toString()
Optional.ofNullable( getOutFile()).ifPresent( file -> builder.append( " -f ").append( file.getPath()));
Optional.ofNullable( getOutDir()).ifPresent( dir -> builder.append( " -o ").append( dir.getPath()));
Optional.ofNullable( getTimeout()).ifPresent( timeout -> builder.append( " -u ").append( timeout));
Optional.ofNullable( getMocoTestConfig()).ifPresent( moco -> builder.append( " -M ").append( moco.getPath()));
Optional.ofNullable( getMocoTestConfig()).ifPresent( moco -> builder.append( " -M ").append( moco.getPath()));
if( isByPath()) builder.append( " -S");
Optional.ofNullable( getPaths()).ifPresent( paths -> builder.append( " -P ").append( paths.stream().collect( joining( ","))));
Optional.ofNullable( getOperations()).ifPresent( operations -> builder.append( " -O ").append( operations.stream().collect( joining( ","))));
builder.append( " -c ").append( String.format( "%s,%s", getModelOptions().getConditionNotifier(), getResolverContext().getNotifier()));
@@ -1331,6 +1371,7 @@ public String toString()
private File outFile_;
private Long timeout_;
private File mocoTestConfig_;
private boolean byPath_;
private Set<String> paths_;
private Set<String> operations_;
private String contentType_;
@@ -1419,6 +1460,17 @@ public Builder paths( String... paths)
return this;
}

public Builder byPath()
{
return byPath( true);
}

public Builder byPath( boolean byPath)
{
options_.setByPath( byPath);
return this;
}

public Builder operations( String... operations)
{
options_.setOperations( Arrays.asList( operations));
@@ -1561,8 +1613,34 @@ public static void run( Options options) throws Exception
}

logger_.info( "Writing API test using {} and {}", testWriter, testCaseWriter);
logger_.info( "Writing API test to {}", Objects.toString( getTestFile( testWriter, testSource, testTarget), "standard output"));
writeTest( testWriter, testSource, testTarget);
if( options.isByPath())
{
String testBaseName = getTestName( testWriter, testSource, testTarget);
File testBaseFile = getTestFile( testWriter, testSource, testTarget);

testTarget.setFile( (File)null);
testTarget.setDir(
Optional.ofNullable( testTarget.getDir())
.orElse(
Optional.ofNullable( testBaseFile)
.map( File::getParentFile)
.orElse( null)));

Set<String> testPaths = Optional.ofNullable( options.getPaths()).orElse( testSource.getTestDef().getPaths());
for( String testPath : testPaths)
{
testSource.setPaths( singleton( testPath));
testTarget.setName( String.format( "%s_%s", testBaseName, testPath));

logger_.info( "Writing API tests for {} to {}", testPath, Objects.toString( getTestFile( testWriter, testSource, testTarget), "standard output"));
writeTest( testWriter, testSource, testTarget);
}
}
else
{
logger_.info( "Writing all API tests to {}", Objects.toString( getTestFile( testWriter, testSource, testTarget), "standard output"));
writeTest( testWriter, testSource, testTarget);
}
}
}

@@ -1599,6 +1677,39 @@ private static File getTestFile( TestWriter<?,?> testWriter, TestSource testSour
return testFile;
}

/**
* Returns the {@link TestWriter#getTestName test name} for the given {@link TestWriter}.
*/
private static String getTestName( TestWriter<?,?> testWriter, TestSource testSource, TestTarget testTarget)
{
Throwable failure = null;
String testName = null;

try
{
testName =
(String)
testWriter.getClass()
.getMethod( "getTestName", TestSource.class, TestTarget.class)
.invoke( testWriter, testSource, testTarget);
}
catch( InvocationTargetException ite)
{
failure = ite.getCause();
}
catch( Exception e)
{
failure = e;
}

if( failure != null)
{
throw new TestWriterException( String.format( "Can't get test name for %s", testWriter), failure);
}

return testName;
}

/**
* Applies the given {@link TestWriter} to write a test using the given source and target.
*/
Loading