Class SmithyDiffTestSuite
- java.lang.Object
-
- software.amazon.smithy.diff.testrunner.SmithyDiffTestSuite
-
public final class SmithyDiffTestSuite extends java.lang.Object
Runs diff test cases against corresponding model `a`, model `b`, and validation `events` files.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SmithyDiffTestSuite.Error
Thrown when errors are encountered in the test runner.static class
SmithyDiffTestSuite.Result
Value result of executing the test suite.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description SmithyDiffTestSuite
addTestCase(SmithyDiffTestCase testCase)
Adds a test case to the test suite.SmithyDiffTestSuite
addTestCasesFromDirectory(java.nio.file.Path modelDirectory)
Adds test cases by crawling a directory and looking for events files that end with ".events".SmithyDiffTestSuite
addTestCasesFromUrl(java.net.URL url)
Convenience method for supplying a directory using a class loader.static java.util.stream.Stream<java.lang.Object[]>
defaultParameterizedTestSource(java.lang.Class<?> contextClass)
Factory method used to easily create a JUnit 5ParameterizedTest
MethodSource
based on the givenClass
.java.util.stream.Stream<java.lang.Object[]>
parameterizedTestSource()
Factory method used to create a JUnit 5ParameterizedTest
MethodSource
.SmithyDiffTestSuite.Result
run()
Executes the test runner.SmithyDiffTestSuite.Result
run(java.util.concurrent.ExecutorService executorService)
Executes the test runner with a specificExecutorService
.static SmithyDiffTestSuite
runner()
Creates a new Smithy diff test suite.SmithyDiffTestSuite
setModelAssemblerFactory(java.util.function.Supplier<ModelAssembler> modelAssemblerFactory)
Sets a customModelAssembler
factory to use to create aModelAssembler
for each test case.java.util.stream.Stream<java.util.concurrent.Callable<SmithyDiffTestCase.Result>>
testCaseCallables()
Creates aStream
ofCallable
objects that can be used to execute each test case.
-
-
-
Method Detail
-
runner
public static SmithyDiffTestSuite runner()
Creates a new Smithy diff test suite.- Returns:
- Returns the created test suite.
-
defaultParameterizedTestSource
public static java.util.stream.Stream<java.lang.Object[]> defaultParameterizedTestSource(java.lang.Class<?> contextClass)
Factory method used to easily create a JUnit 5ParameterizedTest
MethodSource
based on the givenClass
.This method assumes that there is a resource named
diffs
relative to the given class that contains test cases. It also assumes validators and traits should be loaded using theClassLoader
of the givencontextClass
, and that model discovery should be used using the givencontextClass
.Each returned
Object[]
contains the filename of the test as the first argument, followed by aCallable<SmithyDiffTestCase.Result>
as the second argument. All a parameterized test needs to do is callcall
on the providedCallable
to execute the test and fail if the test case is invalid.For example, the following can be used as a unit test:
import java.util.concurrent.Callable; import java.util.stream.Stream; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import software.amazon.smithy.diff.testrunner.SmithyDiffTestCase; import software.amazon.smithy.diff.testrunner.SmithyDiffTestSuite; public class TestRunnerTest { \@ParameterizedTest(name = "\{0\}") \@MethodSource("source") public void testRunner(String filename, Callable<SmithyDiffTestCase.Result> callable) throws Exception { callable.call(); } public static Stream<?> source() { return SmithyDiffTestSuite.defaultParameterizedTestSource(TestRunnerTest.class); } }
- Parameters:
contextClass
- The class to use for loading diffs and model discovery.- Returns:
- Returns the Stream that should be used as a JUnit 5
MethodSource
return value.
-
parameterizedTestSource
public java.util.stream.Stream<java.lang.Object[]> parameterizedTestSource()
Factory method used to create a JUnit 5ParameterizedTest
MethodSource
.Test cases need to be added to the test suite before calling this, for example by using
addTestCasesFromDirectory(Path)
.Each returned
Object[]
contains the name of the test as the first argument, followed by aCallable<SmithyDiffTestCase.Result>
as the second argument. All a parameterized test needs to do is callcall
on the providedCallable
to execute the test and fail if the test case is invalid.For example, the following can be used as a unit test:
import java.util.concurrent.Callable; import java.util.stream.Stream; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import software.amazon.smithy.diff.testrunner.SmithyDiffTestCase; import software.amazon.smithy.diff.testrunner.SmithyDiffTestSuite; public class TestRunnerTest { \@ParameterizedTest(name = "\{0\}") \@MethodSource("source") public void testRunner(String filename, Callable<SmithyDiffTestCase.Result> callable) throws Exception { callable.call(); } public static Stream<?> source() { ModelAssembler assembler = Model.assembler(TestRunnerTest.class.getClassLoader()); return SmithyDiffTestSuite.runner() .setModelAssemblerFactory(assembler::copy) .addTestCasesFromUrl(TestRunnerTest.class.getResource("errorfiles")) .parameterizedTestSource(); } }
- Returns:
- Returns the Stream that should be used as a JUnit 5
MethodSource
return value.
-
addTestCase
public SmithyDiffTestSuite addTestCase(SmithyDiffTestCase testCase)
Adds a test case to the test suite.- Parameters:
testCase
- Test case to add.- Returns:
- Returns the test suite.
-
addTestCasesFromDirectory
public SmithyDiffTestSuite addTestCasesFromDirectory(java.nio.file.Path modelDirectory)
Adds test cases by crawling a directory and looking for events files that end with ".events". Corresponding ".a.(json|smithy)" and ".b.(json|smithy)" files are expected to be found for each found events file.See
SmithyDiffTestCase.from(java.nio.file.Path, java.lang.String)
for a description of how the events file is expected to be formatted.- Parameters:
modelDirectory
- Directory that contains diff models.- Returns:
- Returns the test suite.
- See Also:
SmithyDiffTestCase.from(java.nio.file.Path, java.lang.String)
-
addTestCasesFromUrl
public SmithyDiffTestSuite addTestCasesFromUrl(java.net.URL url)
Convenience method for supplying a directory using a class loader.- Parameters:
url
- URL that contains diff models.- Returns:
- Returns the test suite.
- Throws:
java.lang.IllegalArgumentException
- if a non-file scheme URL is provided.- See Also:
addTestCasesFromDirectory(java.nio.file.Path)
-
setModelAssemblerFactory
public SmithyDiffTestSuite setModelAssemblerFactory(java.util.function.Supplier<ModelAssembler> modelAssemblerFactory)
Sets a customModelAssembler
factory to use to create aModelAssembler
for each test case.The supplier must return a new instance of a Model assembler each time it is called. Model assemblers are mutated and execute in parallel.
- Parameters:
modelAssemblerFactory
- Model assembler factory to use.- Returns:
- Returns the test suite.
-
testCaseCallables
public java.util.stream.Stream<java.util.concurrent.Callable<SmithyDiffTestCase.Result>> testCaseCallables()
Creates aStream
ofCallable
objects that can be used to execute each test case.The
SmithyDiffTestCase.Result.unwrap()
method must be called on the result of each callable in order to actually assert that the test case result is OK.- Returns:
- Returns a stream of test case callables.
-
run
public SmithyDiffTestSuite.Result run()
Executes the test runner.- Returns:
- Returns the test case result object on success.
- Throws:
SmithyDiffTestSuite.Error
- if the validation events do not match expectations.
-
run
public SmithyDiffTestSuite.Result run(java.util.concurrent.ExecutorService executorService)
Executes the test runner with a specificExecutorService
.Tests ideally should use JUnit 5's ParameterizedTest as described in
parameterizedTestSource()
. However, this method can be used to run tests in parallel in other scenarios (like if you aren't using JUnit, or not running tests cases during unit tests).- Parameters:
executorService
- Executor service to execute tests with.- Returns:
- Returns the test case result object on success.
- Throws:
SmithyDiffTestSuite.Error
- if the validation events do not match expectations.
-
-