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

io.specmatic.core.ContractFileWithExports.kt Maven / Gradle / Ivy

Go to download

Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.

There is a newer version: 2.0.37
Show newest version
package io.specmatic.core

import io.specmatic.core.pattern.ContractException
import io.specmatic.test.HttpClient
import java.io.File

data class ContractFileWithExports(val path: String, val relativeTo: RelativeTo = NoAnchor) {
    fun readFeatureForValue(valueName: String): Feature {
        return file.let {
            if(!it.exists())
                throw ContractException("$APPLICATION_NAME file $path does not exist, but is used as the source of variables in value $valueName")

            parseGherkinStringToFeature(it.readText(), it.canonicalPath)
        }
    }

    val file: File = relativeTo.resolve(path)

    val absolutePath: String = file.canonicalPath

    fun runContractAndExtractExports(valueName: String, baseURLs: Map, variables: Map): Map {
        val feature = readFeatureForValue(valueName)
            .copy(testVariables = variables, testBaseURLs = baseURLs)

        val client = HttpClient(
            baseURLs[path] ?: throw ContractException("Base URL for spec file $path was not supplied.")
        )

        val results = feature.executeTests(client)

        if (results.hasFailures()) {
            throw ContractException(
                "There were failures when running $path as a test against URL ${baseURLs[path]}:\n" + results.report(
                    PATH_NOT_RECOGNIZED_ERROR
                ).prependIndent("  ")
            )
        }

        return results.results.filterIsInstance().fold(mapOf()) { acc, result ->
            acc.plus(result.variables)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy