From 9dd4241a7093764d20ed71d712c1cf826aa8ab58 Mon Sep 17 00:00:00 2001 From: "Alexander.Likhachev" Date: Wed, 9 Aug 2023 18:09:05 +0200 Subject: [PATCH] [Build, IGS] Make the consent checker tests parameterized tests --- .../internal-gradle-setup/build.gradle.kts | 6 +- .../src/test/kotlin/ConsentManagerTest.kt | 82 ++++++++----------- 2 files changed, 41 insertions(+), 47 deletions(-) diff --git a/repo/gradle-settings-conventions/internal-gradle-setup/build.gradle.kts b/repo/gradle-settings-conventions/internal-gradle-setup/build.gradle.kts index 2befac5b261..7548ef73956 100644 --- a/repo/gradle-settings-conventions/internal-gradle-setup/build.gradle.kts +++ b/repo/gradle-settings-conventions/internal-gradle-setup/build.gradle.kts @@ -11,7 +11,11 @@ repositories { dependencies { implementation(libs.kotlinx.serialization.json) - implementation(kotlin("test-junit5")) + testImplementation(kotlin("test-junit5")) + testImplementation(platform(libs.junit.bom)) + testImplementation(libs.junit.jupiter.api) + testImplementation(libs.junit.jupiter.params) + testRuntimeOnly(libs.junit.jupiter.engine) } kotlin.jvmToolchain(8) diff --git a/repo/gradle-settings-conventions/internal-gradle-setup/src/test/kotlin/ConsentManagerTest.kt b/repo/gradle-settings-conventions/internal-gradle-setup/src/test/kotlin/ConsentManagerTest.kt index ba2e40925f7..ed2ea68d488 100644 --- a/repo/gradle-settings-conventions/internal-gradle-setup/src/test/kotlin/ConsentManagerTest.kt +++ b/repo/gradle-settings-conventions/internal-gradle-setup/src/test/kotlin/ConsentManagerTest.kt @@ -6,17 +6,25 @@ package org.jetbrains.kotlin.build import org.apache.tools.ant.filters.StringInputStream -import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Named.named import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.MethodSource import java.io.ByteArrayOutputStream import java.io.PrintStream import java.nio.file.Files import java.nio.file.Path +import java.util.stream.Stream import kotlin.test.Test import kotlin.test.assertEquals -import kotlin.test.assertNull import kotlin.test.assertTrue +data class ConsentConfiguration( + val globalConsent: Boolean?, + val localConsent: Boolean?, + val expected: Boolean?, +) + class ConsentManagerTest { @TempDir lateinit var workingDir: Path @@ -29,50 +37,17 @@ class ConsentManagerTest { LocalPropertiesModifier(localPropertiesFile.toFile()) } - @Test - @DisplayName("can check if there's no decision over the consent") - fun testNoConsentRead() { - assertNull(ConsentManager(modifier).getUserDecision()) - } - - @Test - @DisplayName("can check if there's agree to the consent") - fun testConsentAgreeRead() { - localPropertiesFile.toFile().writeText(USER_CONSENT_MARKER) - assertEquals(true, ConsentManager(modifier).getUserDecision()) - } - - @Test - @DisplayName("can check if there's refusal to the consent") - fun testConsentRefusalRead() { - localPropertiesFile.toFile().writeText(USER_REFUSAL_MARKER) - assertEquals(false, ConsentManager(modifier).getUserDecision()) - } - - @Test - @DisplayName("if there's no local consent, but the global one is given") - fun testGlobalConsentGiven() { - assertEquals(true, ConsentManager(modifier, true).getUserDecision()) - } - - @Test - @DisplayName("if there's no local consent, but the global one is refused") - fun testGlobalConsentRefusal() { - assertEquals(false, ConsentManager(modifier, false).getUserDecision()) - } - - @Test - @DisplayName("local given consent takes priority over the global one") - fun testLocalGivenConsentTakesPriority() { - localPropertiesFile.toFile().writeText(USER_CONSENT_MARKER) - assertEquals(true, ConsentManager(modifier, false).getUserDecision()) - } - - @Test - @DisplayName("local refused consent takes priority over the global one") - fun testLocalRefusedConsentTakesPriority() { - localPropertiesFile.toFile().writeText(USER_REFUSAL_MARKER) - assertEquals(false, ConsentManager(modifier, true).getUserDecision()) + @ParameterizedTest(name = "{0}") + @MethodSource("getConfigurationsForParameterizedConsentCheckingTest") + fun testConsentChecker(consentConfiguration: ConsentConfiguration) { + when (consentConfiguration.localConsent) { + true -> localPropertiesFile.toFile().writeText(USER_CONSENT_MARKER) + false -> localPropertiesFile.toFile().writeText(USER_REFUSAL_MARKER) + null -> { + // do nothing + } + } + assertEquals(consentConfiguration.expected, ConsentManager(modifier, consentConfiguration.globalConsent).getUserDecision()) } @Test @@ -117,4 +92,19 @@ class ConsentManagerTest { } } } + + companion object { + @JvmStatic + private fun getConfigurationsForParameterizedConsentCheckingTest() = Stream.of( + named("no decision is provided", ConsentConfiguration(null, null, null)), + named("local consent given with no global decision", ConsentConfiguration(null, true, true)), + named("local consent refused with no global decision", ConsentConfiguration(null, false, false)), + named("global consent refused with no local decision", ConsentConfiguration(false, null, false)), + named("local given consent takes priority over the global refused one", ConsentConfiguration(false, true, true)), + named("both local and global consents are refused", ConsentConfiguration(false, false, false)), + named("global consent refused with no local decision", ConsentConfiguration(true, null, true)), + named("both local and global consents are given", ConsentConfiguration(true, true, true)), + named("local refused consent takes priority over the global given one", ConsentConfiguration(true, false, false)), + ) + } } \ No newline at end of file