Skip to content

Commit

Permalink
Parsed source "type" property in specmatic.json (sources section).
Browse files Browse the repository at this point in the history
Used enum strings in API Coverage JSON report for coverage status
Defaulted responseCode and count = 0 in OpenApiCoverageOperation so that they will be skipped during json generation if they are set as 0.
  • Loading branch information
jaydeepk authored and harikrishnan83 committed Sep 13, 2023
1 parent d729162 commit dc15c27
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import `in`.specmatic.core.log.logger
import java.io.File

sealed class ContractSource {
abstract val type:String?
abstract val testContracts: List<String>
abstract val stubContracts: List<String>
abstract fun pathDescriptor(path: String): String
Expand All @@ -21,7 +22,8 @@ data class GitRepo(
val gitRepositoryURL: String,
val branchName: String?,
override val testContracts: List<String>,
override val stubContracts: List<String>
override val stubContracts: List<String>,
override val type: String?
) : ContractSource() {
val repoName = gitRepositoryURL.split("/").last().removeSuffix(".git")
override fun pathDescriptor(path: String): String {
Expand Down Expand Up @@ -80,7 +82,7 @@ data class GitRepo(
}

return selector.select(this).map {
ContractPathData(repoDir.path, repoDir.resolve(it).path, "git", gitRepositoryURL, branchName, it)
ContractPathData(repoDir.path, repoDir.resolve(it).path, type, gitRepositoryURL, branchName, it)
}
}

Expand Down Expand Up @@ -135,7 +137,8 @@ data class GitRepo(
(sourceGit.workingDirectoryIsGitRepo() && sourceGit.getRemoteUrl() != this.gitRepositoryURL && sourceDir.listFiles()?.isEmpty() == true)
}

data class GitMonoRepo(override val testContracts: List<String>, override val stubContracts: List<String>) : ContractSource() {
data class GitMonoRepo(override val testContracts: List<String>, override val stubContracts: List<String>,
override val type: String?) : ContractSource() {
override fun pathDescriptor(path: String): String = path
override fun install(workingDirectory: File) {
println("Checking list of mono repo paths...")
Expand Down Expand Up @@ -167,7 +170,7 @@ data class GitMonoRepo(override val testContracts: List<String>, override val st
val configFileLocation = File(configFilePath).absoluteFile.parentFile

return selector.select(this).map {
ContractPathData(monoRepoBaseDir.canonicalPath, configFileLocation.resolve(it).canonicalPath)
ContractPathData(monoRepoBaseDir.canonicalPath, configFileLocation.resolve(it).canonicalPath, provider = type, specificationPath = it)
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/kotlin/in/specmatic/core/utilities/Utilities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ fun loadSources(specmaticConfigJson: SpecmaticConfigJson): List<ContractSource>
val testPaths = source.test ?: emptyList()

when (source.repository) {
null -> GitMonoRepo(testPaths, stubPaths)
else -> GitRepo(source.repository, source.branch, testPaths, stubPaths)
null -> GitMonoRepo(testPaths, stubPaths, source.provider.toString())
else -> GitRepo(source.repository, source.branch, testPaths, stubPaths, source.provider.toString())
}
}
}
Expand All @@ -199,10 +199,11 @@ fun loadSources(configJson: JSONObjectValue): List<ContractSource> {

val stubPaths = jsonArray(source, "stub")
val testPaths = jsonArray(source, "test")
val type = nativeString(source.jsonObject, "provider")

when (repositoryURL) {
null -> GitMonoRepo(testPaths, stubPaths)
else -> GitRepo(repositoryURL, branch, testPaths, stubPaths)
null -> GitMonoRepo(testPaths, stubPaths, type)
else -> GitRepo(repositoryURL, branch, testPaths, stubPaths, type)
}
}
else -> throw ContractException("Provider ${nativeString(source.jsonObject, "provider")} not recognised in $globalConfigFileName")
Expand Down
37 changes: 19 additions & 18 deletions core/src/test/kotlin/utilities/UtilitiesTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utilities

import `in`.specmatic.core.CONTRACT_EXTENSION
import `in`.specmatic.core.SourceProvider
import `in`.specmatic.core.git.GitCommand
import `in`.specmatic.core.git.SystemGit
import `in`.specmatic.core.git.checkout
Expand Down Expand Up @@ -30,7 +31,7 @@ internal class UtilitiesTest {

@Test
fun `contractFilePathsFrom sources when contracts repo dir does not exist`() {
val sources = listOf(GitRepo("https://repo1", null, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION")))
val sources = listOf(GitRepo("https://repo1", null, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION"), SourceProvider.git.toString()))
File(".spec").deleteRecursively()

mockkStatic("in.specmatic.core.utilities.Utilities")
Expand All @@ -54,7 +55,7 @@ internal class UtilitiesTest {
fun `contractFilePathsFrom sources with branch when contracts repo dir does not exist`() {
val branchName = "featureBranch"
val sources = listOf(GitRepo("https://repo1",
branchName, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION")))
branchName, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION"), SourceProvider.git.toString()))
File(".spec").deleteRecursively()

mockkStatic("in.specmatic.core.utilities.Utilities")
Expand All @@ -77,7 +78,7 @@ internal class UtilitiesTest {

@Test
fun `contractFilePathsFrom sources when contracts repo dir exists and is clean`() {
val sources = listOf(GitRepo("https://repo1", null, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION")))
val sources = listOf(GitRepo("https://repo1", null, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION"), SourceProvider.git.toString()))
File(".spec").deleteRecursively()
File(".spec/repos/repo1").mkdirs()

Expand All @@ -101,7 +102,7 @@ internal class UtilitiesTest {

@Test
fun `contractFilePathsFrom sources when contracts repo dir exists and is not clean`() {
val sources = listOf(GitRepo("https://repo1", null, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION")))
val sources = listOf(GitRepo("https://repo1", null, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION"), SourceProvider.git.toString()))
File(".spec").deleteRecursively()
File(".spec/repos/repo1").mkdirs()

Expand All @@ -128,7 +129,7 @@ internal class UtilitiesTest {

@Test
fun `contractFilePathsFrom sources when contracts repo dir exists and is behind remote`() {
val sources = listOf(GitRepo("https://repo1", null, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION")))
val sources = listOf(GitRepo("https://repo1", null, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION"), SourceProvider.git.toString()))
File(".spec").deleteRecursively()
File(".spec/repos/repo1").mkdirs()

Expand Down Expand Up @@ -171,11 +172,11 @@ internal class UtilitiesTest {
val testPaths = contractFilePathsFrom(configFilePath, ".$CONTRACT_EXTENSION") { source -> source.testContracts }
val stubPaths = contractFilePathsFrom(configFilePath, ".$CONTRACT_EXTENSION") { source -> source.stubContracts }
val expectedStubPaths = listOf(
ContractPathData("/path/to/monorepo", "$currentPath/monorepo/a/1.$CONTRACT_EXTENSION"),
ContractPathData("/path/to/monorepo", "$currentPath/monorepo/b/1.$CONTRACT_EXTENSION")
ContractPathData("/path/to/monorepo", "$currentPath/monorepo/a/1.$CONTRACT_EXTENSION", provider = SourceProvider.git.toString(), specificationPath = "../a/1.spec"),
ContractPathData("/path/to/monorepo", "$currentPath/monorepo/b/1.$CONTRACT_EXTENSION", provider = SourceProvider.git.toString(), specificationPath = "../b/1.spec")
)
val expectedTestPaths = listOf(
ContractPathData("/path/to/monorepo", "$currentPath/monorepo/c/1.$CONTRACT_EXTENSION"),
ContractPathData("/path/to/monorepo", "$currentPath/monorepo/c/1.$CONTRACT_EXTENSION", provider=SourceProvider.git.toString(), specificationPath = "../c/1.spec"),
)

assertThat(stubPaths == expectedStubPaths).isTrue
Expand All @@ -189,7 +190,7 @@ internal class UtilitiesTest {
val qontractJson = "{\"sources\": [{\"provider\": \"git\",\"repository\": \"https://repo1\",\"stub\": [\"a/1.$CONTRACT_EXTENSION\",\"b/1.$CONTRACT_EXTENSION\",\"c/1.$CONTRACT_EXTENSION\"]}]}"
val configJson = parsedJSON(qontractJson) as JSONObjectValue
val sources = loadSources(configJson)
val expectedSources = listOf(GitRepo("https://repo1", null, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION")))
val expectedSources = listOf(GitRepo("https://repo1", null, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION"), SourceProvider.git.toString()))
assertThat(sources == expectedSources).isTrue
}

Expand All @@ -200,8 +201,8 @@ internal class UtilitiesTest {
val configJson = parsedJSON(qontractJson) as JSONObjectValue
val sources = loadSources(configJson)
val expectedSources = listOf(
GitRepo("https://repo1",null, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION")),
GitRepo("https://repo2",null, listOf(), listOf("c/1.$CONTRACT_EXTENSION"))
GitRepo("https://repo1",null, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION"), SourceProvider.git.toString()),
GitRepo("https://repo2",null, listOf(), listOf("c/1.$CONTRACT_EXTENSION"), SourceProvider.git.toString())
)
assertThat(sources == expectedSources).isTrue
}
Expand All @@ -211,7 +212,7 @@ internal class UtilitiesTest {
val qontractJson = "{\"sources\": [{\"provider\": \"git\",\"stub\": [\"a/1.$CONTRACT_EXTENSION\",\"b/1.$CONTRACT_EXTENSION\",\"c/1.$CONTRACT_EXTENSION\"]}]}"
val configJson = parsedJSON(qontractJson) as JSONObjectValue
val sources = loadSources(configJson)
val expectedSources = listOf(GitMonoRepo(listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION")))
val expectedSources = listOf(GitMonoRepo(listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION"), SourceProvider.git.toString()))
assertThat(sources == expectedSources).isTrue
}

Expand All @@ -220,7 +221,7 @@ internal class UtilitiesTest {
val qontractJson = "{\"sources\": [{\"provider\": \"git\",\"test\": [\"a/1.$CONTRACT_EXTENSION\",\"b/1.$CONTRACT_EXTENSION\",\"c/1.$CONTRACT_EXTENSION\"]}]}"
val configJson = parsedJSON(qontractJson) as JSONObjectValue
val sources = loadSources(configJson)
val expectedSources = listOf(GitMonoRepo(listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION"), listOf()))
val expectedSources = listOf(GitMonoRepo(listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION", "c/1.$CONTRACT_EXTENSION"), listOf(), SourceProvider.git.toString()))
assertThat(sources == expectedSources).isTrue
}

Expand All @@ -229,7 +230,7 @@ internal class UtilitiesTest {
val qontractJson = "{\"sources\": [{\"provider\": \"git\",\"test\": [\"a/1.$CONTRACT_EXTENSION\",\"b/1.$CONTRACT_EXTENSION\"],\"stub\": [\"c/1.$CONTRACT_EXTENSION\"]}]}"
val configJson = parsedJSON(qontractJson) as JSONObjectValue
val sources = loadSources(configJson)
val expectedSources = listOf(GitMonoRepo(listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION"), listOf("c/1.$CONTRACT_EXTENSION")))
val expectedSources = listOf(GitMonoRepo(listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION"), listOf("c/1.$CONTRACT_EXTENSION"), SourceProvider.git.toString()))
assertThat(sources == expectedSources).isTrue
}

Expand All @@ -240,8 +241,8 @@ internal class UtilitiesTest {
val configJson = parsedJSON(qontractJson) as JSONObjectValue
val sources = loadSources(configJson)
val expectedSources = listOf(
GitMonoRepo(listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION")),
GitMonoRepo(listOf(), listOf("c/1.$CONTRACT_EXTENSION"))
GitMonoRepo(listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION"), SourceProvider.git.toString()),
GitMonoRepo(listOf(), listOf("c/1.$CONTRACT_EXTENSION"), SourceProvider.git.toString())
)
assertThat(sources == expectedSources).isTrue
}
Expand All @@ -253,8 +254,8 @@ internal class UtilitiesTest {
val configJson = parsedJSON(qontractJson) as JSONObjectValue
val sources = loadSources(configJson)
val expectedSources = listOf(
GitRepo("https://repo1", null, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION")),
GitMonoRepo(listOf(), listOf("c/1.$CONTRACT_EXTENSION"))
GitRepo("https://repo1", null, listOf(), listOf("a/1.$CONTRACT_EXTENSION", "b/1.$CONTRACT_EXTENSION"), SourceProvider.git.toString()),
GitMonoRepo(listOf(), listOf("c/1.$CONTRACT_EXTENSION"), SourceProvider.git.toString())
)
assertThat(sources == expectedSources).isTrue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class OpenApiCoverageReportInput(
method = operationGroup.second,
responseCode = operationGroup.third,
count = operationRows.count{it.includeForCoverage},
coverageStatus = getRemarks(operationRows)
coverageStatus = getRemarks(operationRows).toString()
)
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package `in`.specmatic.test.reports.coverage.json

import `in`.specmatic.test.reports.coverage.console.Remarks
import kotlinx.serialization.Serializable

@Serializable
data class OpenApiCoverageOperation(
val path: String,
val method: String,
val responseCode: Int,
val count: Int,
val coverageStatus: Remarks
val responseCode: Int = 0,
val count: Int = 0,
val coverageStatus: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ class ApiCoverageReportInputTest {
"in/specmatic/examples/store/route1.yaml",
"HTTP",
listOf(
OpenApiCoverageOperation("/route1", "GET",200, 1, Remarks.Covered),
OpenApiCoverageOperation( "/route1", "POST",200, 1, Remarks.Covered)
OpenApiCoverageOperation("/route1", "GET",200, 1, Remarks.Covered.toString()),
OpenApiCoverageOperation( "/route1", "POST",200, 1, Remarks.Covered.toString())
)
),
OpenApiCoverageJsonRow(
Expand All @@ -264,15 +264,15 @@ class ApiCoverageReportInputTest {
"in/specmatic/examples/store/route2.yaml",
"HTTP",
listOf(
OpenApiCoverageOperation( "/route2", "GET",200, 1, Remarks.Covered),
OpenApiCoverageOperation( "/route2", "POST",200, 0, Remarks.NotImplemented)
OpenApiCoverageOperation( "/route2", "GET",200, 1, Remarks.Covered.toString()),
OpenApiCoverageOperation( "/route2", "POST",200, 0, Remarks.NotImplemented.toString())
)
),
OpenApiCoverageJsonRow(
serviceType = "HTTP",
operations = listOf(
OpenApiCoverageOperation( "/route3/{route_id}", "GET",0, 0, Remarks.Missed),
OpenApiCoverageOperation( "/route3/{route_id}", "POST",0, 0, Remarks.Missed)
OpenApiCoverageOperation( "/route3/{route_id}", "GET",0, 0, Remarks.Missed.toString()),
OpenApiCoverageOperation( "/route3/{route_id}", "POST",0, 0, Remarks.Missed.toString() )
)
)
)
Expand Down

0 comments on commit dc15c27

Please sign in to comment.