Skip to content

Commit

Permalink
Merge pull request #1390 from znsio/stub-usage-report-append-reports
Browse files Browse the repository at this point in the history
Append stub usage reports
  • Loading branch information
harikrishnan83 authored Oct 28, 2024
2 parents c171dfb + 001684d commit a815e0c
Show file tree
Hide file tree
Showing 9 changed files with 441 additions and 10 deletions.
20 changes: 17 additions & 3 deletions core/src/main/kotlin/io/specmatic/stub/HttpStub.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import io.specmatic.core.route.modules.HealthCheckModule.Companion.isHealthCheck
import io.specmatic.core.utilities.*
import io.specmatic.core.value.*
import io.specmatic.mock.*
import io.specmatic.stub.report.StubEndpoint
import io.specmatic.stub.report.StubUsageReport
import io.specmatic.stub.report.*
import io.specmatic.test.HttpClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import kotlinx.serialization.SerializationException
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.ByteArrayOutputStream
Expand Down Expand Up @@ -594,7 +594,21 @@ class HttpStub(
val json = Json {
encodeDefaults = false
}
val reportJson = json.encodeToString(stubUsageReport.generate())
val generatedReport = stubUsageReport.generate()
val reportJson: String = File(JSON_REPORT_PATH).resolve(JSON_REPORT_FILE_NAME).let { reportFile ->
if (reportFile.exists()) {
try {
val existingReport = Json.decodeFromString<StubUsageReportJson>(reportFile.readText())
json.encodeToString(generatedReport.merge(existingReport))
} catch (exception: SerializationException) {
logger.log("The existing report file is not a valid Stub Usage Report. ${exception.message}")
json.encodeToString(generatedReport)
}
} else {
json.encodeToString(generatedReport)
}
}

saveJsonFile(reportJson, JSON_REPORT_PATH, JSON_REPORT_FILE_NAME)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,26 @@ import kotlinx.serialization.Serializable
data class StubUsageReportJson (
val specmaticConfigPath:String,
val stubUsage:List<StubUsageReportRow>
)
) {
fun merge(other: StubUsageReportJson): StubUsageReportJson {
val mergedStubUsageRows: MutableList<StubUsageReportRow> = mutableListOf<StubUsageReportRow>()

val allStubUsageRows = (other.stubUsage + this.stubUsage)

for (stubUsageReportRow in allStubUsageRows) {
val existingRow = mergedStubUsageRows.find { it.hasSameRowIdentifiers(stubUsageReportRow) }

if (existingRow == null) {
mergedStubUsageRows.add(stubUsageReportRow)
continue
}

mergedStubUsageRows[mergedStubUsageRows.indexOf(existingRow)] = existingRow.merge(stubUsageReportRow)
}

return StubUsageReportJson(
specmaticConfigPath = other.specmaticConfigPath,
stubUsage = mergedStubUsageRows
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ package io.specmatic.stub.report
import kotlinx.serialization.Serializable

@Serializable
data class StubUsageReportOperation (
data class StubUsageReportOperation(
val path: String?,
val method: String?,
val responseCode: Int,
val count: Int
)
) {
fun hasSameOperationIdentifiers(other: StubUsageReportOperation): Boolean {
return this.path.equals(other.path) && this.method.equals(other.method) && this.responseCode == other.responseCode
}

fun merge(other: StubUsageReportOperation): StubUsageReportOperation {
return this.copy(count = this.count + other.count)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,31 @@ data class StubUsageReportRow(
val specification: String? = null,
val serviceType: String? = null,
val operations: List<StubUsageReportOperation>
)
) {
fun hasSameRowIdentifiers(other: StubUsageReportRow): Boolean {
return type.equals(other.type) && repository.equals(other.repository)
&& branch.equals(other.branch) && specification.equals(other.specification)
&& serviceType.equals(other.serviceType)
}

fun merge(other: StubUsageReportRow): StubUsageReportRow {
val allOperations = this.operations + other.operations

val mergedOperations = mutableListOf<StubUsageReportOperation>()

for (operation in allOperations) {
val existingOperation = mergedOperations.find { it.hasSameOperationIdentifiers(operation) }

if (existingOperation != null) {
mergedOperations[mergedOperations.indexOf(existingOperation)] =
existingOperation.merge(operation)
} else {
mergedOperations.add(operation)
}
}

return copy(
operations = mergedOperations
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import io.specmatic.stub.report.StubUsageReportRow
import kotlinx.serialization.json.Json
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.springframework.web.client.RestTemplate
import java.io.File
import kotlin.concurrent.thread

class HttpStubStubCoverageTest {
class HttpStubUsageReportTest {
companion object {
private val stubUsageReportFile = File("./build/reports/specmatic/stub_usage_report.json")

@BeforeAll
@AfterAll
@JvmStatic
fun tearDownAll() {
fun cleanUpAnyExistingReports() {
stubUsageReportFile.delete()
}

Expand Down
Loading

0 comments on commit a815e0c

Please sign in to comment.