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

io.specmatic.reports.CentralContractRepoReport.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.reports

import io.specmatic.conversions.OpenApiSpecification
import io.specmatic.conversions.convertPathParameterStyle
import io.specmatic.core.log.logger
import io.specmatic.core.utilities.exceptionCauseMessage
import io.specmatic.stub.isOpenAPI
import io.specmatic.stub.hasOpenApiFileExtension
import java.io.File

class CentralContractRepoReport {
    fun generate(currentWorkingDir: String = ""): CentralContractRepoReportJson {
        val searchPath = File(currentWorkingDir).canonicalPath
        logger.log("Searching for specification files at: $searchPath")
        val specifications = findSpecifications(searchPath)
        return CentralContractRepoReportJson(getSpecificationRows(specifications.sorted(), searchPath))
    }

    private fun getSpecificationRows(specifications: List, currentWorkingDir: String): List {
        val currentWorkingDirPath = File(currentWorkingDir).absoluteFile

        return specifications.mapNotNull {
            try {
                val feature = OpenApiSpecification.fromYAML(it.readText(), it.path).toFeature()
                Pair(it, feature)
            }
            catch (e:Throwable){
                logger.log("Could not parse ${it.path} due to the following error:")
                logger.log(exceptionCauseMessage(e))
                null
            }
        }
       .filter { (spec, feature) ->
            if (feature.scenarios.isEmpty()) {
                logger.log("Excluding specification: ${spec.path} as it does not have any paths ")
            }
            feature.scenarios.isNotEmpty()
        }.map { (spec, feature) ->
            SpecificationRow(
                spec.relativeTo(File("").canonicalFile).path,
                feature.serviceType,
                feature.scenarios.map {
                    SpecificationOperation(
                        convertPathParameterStyle(it.path),
                        it.method,
                        it.httpResponsePattern.status
                    )
                }
            )
        }
    }


    private fun findSpecifications(currentDirectoryPath: String): List {
        val currentDirectory = File(currentDirectoryPath)
        val specifications = mutableListOf()
        val allFiles = currentDirectory.listFiles() ?: emptyArray()
        for (file in allFiles) {
            if (file.isDirectory) {
                specifications.addAll(findSpecifications(file.canonicalPath))
            } else if (hasOpenApiFileExtension(file.canonicalPath) && isOpenAPI(file.canonicalPath)) {
                specifications.add(file)
            }
        }
        return specifications
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy