All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.openapiprocessor.test.TestSetRunner.groovy Maven / Gradle / Ivy

There is a newer version: 2024.6.1
Show newest version
/*
 * Copyright 2020 https://github.com/openapi-processor/openapi-processor-base
 * PDX-License-Identifier: Apache-2.0
 */

package io.openapiprocessor.test
/**
 * used to execute test sets.
 *
 * "expected" represents the files from the "outputs" folder of the test case
 * "generated" represents the files generated by the processor
 */
class TestSetRunner {

    enum ResolveType {
        PATH_GENERATED, PATH_DIFF
    }

    Test test
    TestSet testSet

    TestSetRunner(Test test, TestSet testSet) {
        this.test = test
        this.testSet = testSet
    }

    /**
     * runs test set on the native file system
     *
     * @return true on success, false on failure, i.e. if there were any differences
     */
    boolean runOnNativeFileSystem () {
        test.init()

        def mapping = test.mapping
        def options = [
            parser: test.parser,
            apiPath: test.apiPath.toString(),
            targetDir: test.targetDir.toString(),
            mapping: mapping.yaml
        ]

        when:
        testSet.processor.run (options)

        then:
        def expectedFiles = test.expectedFiles
        def generatedFiles = getGeneratedFiles(expectedFiles)

        // check expected file names match generated file names
        def expectedFileKeys = expectedFiles.keySet()
        def generatedFileKeys = generatedFiles.keySet()
        def expectedFileNames = test.resolveModelTypeInTarget(expectedFileKeys)
        assert expectedFileNames == generatedFileKeys

        // compare expected files with the generated files
        def success = true
        expectedFiles.each {
            def expectedFilePath = test.getExpectedFilePath(it.key, it.value)
            def generatedFilePath = test.getGeneratedFilePath(it.key, it.value)
            def equalContent = !test.printUnifiedDiff (expectedFilePath, generatedFilePath)
            success &= equalContent
        }

        success
    }

    /**
     * runs test set on the given file system
     *
     * @param fs the file system
     * @return true on success, false on failure, ie. if there were any differences
     */
    boolean runOnCustomFileSystem () {
        test.init()

        def mapping = test.mapping
        def options = [
            parser: test.parser,
            apiPath: test.apiPath.toString(),
            targetDir: test.targetDir.toString(),
            mapping: mapping.yaml
        ]

        when:
        testSet.processor.run (options)

        then:
        def expectedFiles = test.expectedFiles
        def generatedFiles = getGeneratedFiles(expectedFiles)

        // check expected file names match generated file names
        def expectedFileKeys = expectedFiles.keySet()
        def generatedFileKeys = generatedFiles.keySet()
        def expectedFileNames = test.resolveModelTypeInTarget(expectedFileKeys)
        assert expectedFileNames == generatedFileKeys

        // compare expected files with the generated files
        def success = true
        expectedFiles.each {
            def expectedFilePath = test.getExpectedFilePath(it.key, it.value)
            def generatedFilePath = test.getGeneratedFilePath(it.key, it.value)
            def equalContent = !test.printUnifiedDiff (expectedFilePath, generatedFilePath)
            success &= equalContent
        }

        success
    }

    private Map getGeneratedFiles(Map expectedFiles) {
        def generatedFilesAll = test.generatedSourceFiles
        generatedFilesAll.addAll(test.generatedResourceFiles)

        def generatedFiles = filterUnexpectedFiles(
                expectedFiles,
                generatedFilesAll, [
                "support/Generated.java",
                "validation/Values.java",
                "validation/ValueValidator.java"
        ] as Set)

        return generatedFiles
    }

    private static Map filterUnexpectedFiles(
            Map expectedFiles,
            Set generatedFiles,
            Set unexpectedFiles
    ) {
        def generated = new TreeMap()
        generatedFiles.each {
            if (!expectedFiles.containsKey(it) && unexpectedFiles.contains(it)) {
                return
            }

            def value = expectedFiles.get(it)
            generated.put(it, value)
        }
        return generated
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy