Skip to content

Commit

Permalink
Migrate ImpactedTestEngine mocks to kotlin mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Avanatiker committed Dec 13, 2024
1 parent c7c9ee5 commit 4d962e2
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 184 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.teamscale.test_impacted.engine.executor

import com.teamscale.client.ClusteredTestDetails
import com.teamscale.client.CommitDescriptor
import com.teamscale.client.PrioritizableTestCluster
import com.teamscale.client.TeamscaleClient
import com.teamscale.test_impacted.commons.LoggerUtils.createLogger
import retrofit2.Response
import java.io.IOException
import java.util.logging.Level

/**
* Class for retrieving the impacted [PrioritizableTestCluster]s corresponding to [ClusteredTestDetails]
* available for test execution.
*/
open class ImpactedTestsProvider(
private val client: TeamscaleClient,
private val baseline: String,
private val baselineRevision: String,
private val endCommit: CommitDescriptor,
private val endRevision: String,
private val repository: String,
@JvmField val partition: String,
private val includeNonImpacted: Boolean,
private val includeAddedTests: Boolean,
private val includeFailedAndSkipped: Boolean
) {
private val logger = createLogger()

/** Queries Teamscale for impacted tests. */
fun getImpactedTestsFromTeamscale(
availableTestDetails: List<ClusteredTestDetails>
): List<PrioritizableTestCluster> {
try {
logger.info { "Getting impacted tests..." }
val response = client
.getImpactedTests(
availableTestDetails, baseline, baselineRevision, endCommit, endRevision, repository,
listOf(partition),
includeNonImpacted, includeAddedTests, includeFailedAndSkipped
)

if (response.isSuccessful) {
val testClusters = response.body()
if (testClusters != null && testCountIsPlausible(testClusters, availableTestDetails)) {
return testClusters
}
logger.severe(
"""
Teamscale was not able to determine impacted tests:
${response.body()}
""".trimIndent()
)
} else {
logger.severe(
"Retrieval of impacted tests failed: ${response.code()} ${response.message()}\n${
getErrorBody(response)
}"
)
}
} catch (e: IOException) {
logger.log(
Level.SEVERE, e
) { "Retrieval of impacted tests failed." }
}
return emptyList()
}

/**
* Checks that the number of tests returned by Teamscale matches the number of available tests when running with
* [.includeNonImpacted].
*/
private fun testCountIsPlausible(
testClusters: List<PrioritizableTestCluster>,
availableTestDetails: List<ClusteredTestDetails>
): Boolean {
val returnedTests = testClusters.stream().mapToLong {
it.tests!!.size.toLong()
}.sum()
if (!includeNonImpacted) {
logger.info { "Received $returnedTests impacted tests of ${availableTestDetails.size} available tests." }
return true
}
if (returnedTests == availableTestDetails.size.toLong()) {
return true
} else {
logger.severe {
"Retrieved $returnedTests tests from Teamscale, but expected ${availableTestDetails.size}."
}
return false
}
}

companion object {
@Throws(IOException::class)
private fun getErrorBody(response: Response<*>): String {
response.errorBody().use { error ->
if (error != null) {
return error.string()
}
}
return ""
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class TestEngineOptions {
File(reportDirectory, "server-request.txt")
)
return ImpactedTestsProvider(
client, baseline, baselineRevision, endCommit, endRevision, repository, partition,
client, baseline!!, baselineRevision!!, endCommit!!, endRevision!!, repository!!, partition!!,
runAllTests, includeAddedTests, includeFailedAndSkipped
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import com.teamscale.test_impacted.engine.executor.TeamscaleAgentNotifier
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.platform.engine.*
import org.mockito.ArgumentMatchers
import org.mockito.Mockito
import org.mockito.Mockito.mock
import org.mockito.kotlin.*

/** Base class for testing specific scenarios in the impacted test engine. */
abstract class ImpactedTestEngineTestBase {
Expand All @@ -30,23 +29,23 @@ abstract class ImpactedTestEngineTestBase {
Assertions.assertThat(engineDescriptor.uniqueId)
.isEqualTo(UniqueId.forEngine(ImpactedTestEngine.ENGINE_ID))

Mockito.`when`(executionRequest.engineExecutionListener)
whenever(executionRequest.engineExecutionListener)
.thenReturn(executionListener)
Mockito.`when`(executionRequest.rootTestDescriptor)
whenever(executionRequest.rootTestDescriptor)
.thenReturn(engineDescriptor)
Mockito.`when`(impactedTestsProvider.getImpactedTestsFromTeamscale(ArgumentMatchers.any()))
whenever(impactedTestsProvider.getImpactedTestsFromTeamscale(any()))
.thenReturn(impactedTests)

testEngine.execute(executionRequest)

verifyCallbacks(executionListener)

// Ensure test data is written.
Mockito.verify(testDataWriter).dumpTestDetails(ArgumentMatchers.any())
Mockito.verify(testDataWriter).dumpTestExecutions(ArgumentMatchers.any())
verify(testDataWriter).dumpTestDetails(any())
verify(testDataWriter).dumpTestExecutions(any())

Mockito.verifyNoMoreInteractions(executionListener)
Mockito.verifyNoMoreInteractions(testDataWriter)
verifyNoMoreInteractions(executionListener)
verifyNoMoreInteractions(testDataWriter)
}

/** Returns the available engines that should be assumed by the impacted test engine. */
Expand All @@ -60,10 +59,10 @@ abstract class ImpactedTestEngineTestBase {

private fun createInternalImpactedTestEngine(engines: List<TestEngine>): InternalImpactedTestEngine {
engines.forEach { engine ->
Mockito.`when`(testEngineRegistry.getTestEngine(ArgumentMatchers.eq(engine.id)))
whenever(testEngineRegistry.getTestEngine(eq(engine.id)))
.thenReturn(engine)
}
Mockito.`when`<Iterator<TestEngine>>(testEngineRegistry.iterator()).thenReturn(engines.iterator())
whenever<Iterator<TestEngine>>(testEngineRegistry.iterator()).thenReturn(engines.iterator())

return InternalImpactedTestEngine(
ImpactedTestEngineConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.teamscale.test_impacted.engine.executor.DummyEngine
import com.teamscale.test_impacted.engine.executor.SimpleTestDescriptor
import com.teamscale.test_impacted.test_descriptor.JUnitJupiterTestDescriptorResolver
import org.junit.platform.engine.*
import org.mockito.Mockito
import org.mockito.kotlin.verify

/** Test setup for JUnit Jupiter dynamic tests. */
internal class ImpactedTestEngineWithDynamicTestsTest : ImpactedTestEngineTestBase() {
Expand Down Expand Up @@ -47,20 +47,20 @@ internal class ImpactedTestEngineWithDynamicTestsTest : ImpactedTestEngineTestBa

override fun verifyCallbacks(executionListener: EngineExecutionListener) {
// First the parents test descriptors are started in order.
Mockito.verify(executionListener).executionStarted(testRoot)
Mockito.verify(executionListener).executionStarted(dynamicTestClassCase)
Mockito.verify(executionListener).executionStarted(dynamicTestCase)
verify(executionListener).executionStarted(testRoot)
verify(executionListener).executionStarted(dynamicTestClassCase)
verify(executionListener).executionStarted(dynamicTestCase)

// Test case is added dynamically and executed.
dynamicTestCase.addChild(dynamicallyRegisteredTestCase)
Mockito.verify(executionListener).dynamicTestRegistered(dynamicallyRegisteredTestCase)
Mockito.verify(executionListener).executionStarted(dynamicallyRegisteredTestCase)
Mockito.verify(executionListener)
verify(executionListener).dynamicTestRegistered(dynamicallyRegisteredTestCase)
verify(executionListener).executionStarted(dynamicallyRegisteredTestCase)
verify(executionListener)
.executionFinished(dynamicallyRegisteredTestCase, TestExecutionResult.successful())

// Parent test descriptors are also finished.
Mockito.verify(executionListener).executionFinished(dynamicTestCase, TestExecutionResult.successful())
Mockito.verify(executionListener).executionFinished(dynamicTestClassCase, TestExecutionResult.successful())
Mockito.verify(executionListener).executionFinished(testRoot, TestExecutionResult.successful())
verify(executionListener).executionFinished(dynamicTestCase, TestExecutionResult.successful())
verify(executionListener).executionFinished(dynamicTestClassCase, TestExecutionResult.successful())
verify(executionListener).executionFinished(testRoot, TestExecutionResult.successful())
}
}
Loading

0 comments on commit 4d962e2

Please sign in to comment.