<commit_before>package net.nemerosa.ontrack.extension.sonarqube

import com.nhaarman.mockitokotlin2.*
import net.nemerosa.ontrack.common.RunProfile
import net.nemerosa.ontrack.common.getOrNull
import net.nemerosa.ontrack.extension.general.BuildLinkDisplayProperty
import net.nemerosa.ontrack.extension.general.BuildLinkDisplayPropertyType
import net.nemerosa.ontrack.extension.general.ReleaseProperty
import net.nemerosa.ontrack.extension.general.ReleasePropertyType
import net.nemerosa.ontrack.extension.sonarqube.client.SonarQubeClient
import net.nemerosa.ontrack.extension.sonarqube.client.SonarQubeClientFactory
import net.nemerosa.ontrack.extension.sonarqube.configuration.SonarQubeConfiguration
import net.nemerosa.ontrack.extension.sonarqube.configuration.SonarQubeConfigurationService
import net.nemerosa.ontrack.extension.sonarqube.measures.SonarQubeMeasures
import net.nemerosa.ontrack.extension.sonarqube.measures.SonarQubeMeasuresCollectionService
import net.nemerosa.ontrack.extension.sonarqube.measures.SonarQubeMeasuresInformationExtension
import net.nemerosa.ontrack.extension.sonarqube.measures.SonarQubeMeasuresSettings
import net.nemerosa.ontrack.extension.sonarqube.property.SonarQubeProperty
import net.nemerosa.ontrack.extension.sonarqube.property.SonarQubePropertyType
import net.nemerosa.ontrack.it.AbstractDSLTestSupport
import net.nemerosa.ontrack.model.metrics.MetricsExportService
import net.nemerosa.ontrack.model.security.GlobalSettings
import net.nemerosa.ontrack.model.structure.Build
import net.nemerosa.ontrack.model.structure.Project
import net.nemerosa.ontrack.test.TestUtils.uid
import net.nemerosa.ontrack.test.assertIs
import org.junit.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.context.annotation.Profile
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class SonarQubeIT : AbstractDSLTestSupport() {

    @Autowired
    private lateinit var sonarQubeConfigurationService: SonarQubeConfigurationService

    @Autowired
    private lateinit var sonarQubeMeasuresCollectionService: SonarQubeMeasuresCollectionService

    @Autowired
    private lateinit var metricsExportService: MetricsExportService

    @Autowired
    private lateinit var informationExtension: SonarQubeMeasuresInformationExtension

    @Test
    fun `Launching the collection on validation run`() {
        val returnedMeasures = mapOf(
                "measure-1" to 12.3,
                "measure-2" to 45.0
        )
        testCollectionWithListener(
                actualMeasures = mapOf(
                        "measure-1" to 12.3,
                        "measure-2" to 45.0
                ),
                returnedMeasures = returnedMeasures
        ) { build ->
            // Checks that metrics are exported
            returnedMeasures.forEach { (name, value) ->
                verify(metricsExportService).exportMetrics(
                        metric = eq("ontrack_sonarqube_measure"),
                        tags = eq(mapOf(
                                "project" to build.project.name,
                                "branch" to build.branch.name,
                                "build" to build.name,
                                "version" to "1.0.0",
                                "metric" to name
                        )),
                        fields = eq(mapOf(
                                "value" to value
                        )),
                        timestamp = any()
                )
            }
            // Checks the entity information
            val information = informationExtension.getInformation(build).getOrNull()
            assertNotNull(information) {
                assertIs<SonarQubeMeasures>(it.data) { q ->
                    assertEquals(
                            returnedMeasures,
                            q.measures
                    )
                }
            }
        }
    }

    @Test<commit_msg>#689 Testing the configuration of a different validation stamp<commit_after>package net.nemerosa.ontrack.extension.sonarqube

import com.nhaarman.mockitokotlin2.*
import net.nemerosa.ontrack.common.RunProfile
import net.nemerosa.ontrack.common.getOrNull
import net.nemerosa.ontrack.extension.general.BuildLinkDisplayProperty
import net.nemerosa.ontrack.extension.general.BuildLinkDisplayPropertyType
import net.nemerosa.ontrack.extension.general.ReleaseProperty
import net.nemerosa.ontrack.extension.general.ReleasePropertyType
import net.nemerosa.ontrack.extension.sonarqube.client.SonarQubeClient
import net.nemerosa.ontrack.extension.sonarqube.client.SonarQubeClientFactory
import net.nemerosa.ontrack.extension.sonarqube.configuration.SonarQubeConfiguration
import net.nemerosa.ontrack.extension.sonarqube.configuration.SonarQubeConfigurationService
import net.nemerosa.ontrack.extension.sonarqube.measures.SonarQubeMeasures
import net.nemerosa.ontrack.extension.sonarqube.measures.SonarQubeMeasuresCollectionService
import net.nemerosa.ontrack.extension.sonarqube.measures.SonarQubeMeasuresInformationExtension
import net.nemerosa.ontrack.extension.sonarqube.measures.SonarQubeMeasuresSettings
import net.nemerosa.ontrack.extension.sonarqube.property.SonarQubeProperty
import net.nemerosa.ontrack.extension.sonarqube.property.SonarQubePropertyType
import net.nemerosa.ontrack.it.AbstractDSLTestSupport
import net.nemerosa.ontrack.model.metrics.MetricsExportService
import net.nemerosa.ontrack.model.security.GlobalSettings
import net.nemerosa.ontrack.model.structure.Build
import net.nemerosa.ontrack.model.structure.Project
import net.nemerosa.ontrack.test.TestUtils.uid
import net.nemerosa.ontrack.test.assertIs
import org.junit.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.context.annotation.Profile
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class SonarQubeIT : AbstractDSLTestSupport() {

    @Autowired
    private lateinit var sonarQubeConfigurationService: SonarQubeConfigurationService

    @Autowired
    private lateinit var sonarQubeMeasuresCollectionService: SonarQubeMeasuresCollectionService

    @Autowired
    private lateinit var metricsExportService: MetricsExportService

    @Autowired
    private lateinit var informationExtension: SonarQubeMeasuresInformationExtension

    @Test
    fun `Launching the collection on validation run`() {
        val returnedMeasures = mapOf(
                "measure-1" to 12.3,
                "measure-2" to 45.0
        )
        testCollectionWithListener(
                actualMeasures = mapOf(
                        "measure-1" to 12.3,
                        "measure-2" to 45.0
                ),
                returnedMeasures = returnedMeasures
        ) { build ->
            // Checks that metrics are exported
            returnedMeasures.forEach { (name, value) ->
                verify(metricsExportService).exportMetrics(
                        metric = eq("ontrack_sonarqube_measure"),
                        tags = eq(mapOf(
                                "project" to build.project.name,
                                "branch" to build.branch.name,
                                "build" to build.name,
                                "version" to "1.0.0",
                                "metric" to name
                        )),
                        fields = eq(mapOf(
                                "value" to value
                        )),
                        timestamp = any()
                )
            }
            // Checks the entity information
            val information = informationExtension.getInformation(build).getOrNull()
            assertNotNull(information) {
                assertIs<SonarQubeMeasures>(it.data) { q ->
                    assertEquals(
                            returnedMeasures,
                            q.measures
                    )
                }
            }
        }
    }

    @Test
    fun `Launching the collection on validation run using a non default validation stamp`() {
        testCollectionWithListener(
                validationStamp = "sonar-qube",
                actualMeasures = mapOf(
                        "measure-1" to 12.3,
                        "measure-2" to 45.0
                ),
                returnedMeasures = mapOf(
                        "measure-1" to 12.3,
                        "measure-2" to 45.0
                )
        )
    }

    @Test
{'fraction_non_alphanumeric': 0.07198290406028568, 'fraction_numerical': 0.0066359239680575865, 'mean_word_length': 2.3554716981132073, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '835835', 'n_tokens_mistral': 2417, 'n_tokens_neox': 2304, 'n_words': 386}