From 20bc30c30fe6728eb8e3567b882a886207a62dfb Mon Sep 17 00:00:00 2001 From: Bastian Doetsch Date: Wed, 8 Nov 2023 10:55:48 +0100 Subject: [PATCH] feat: add test for event mapping --- .../plugin/analytics/AnalyticsScanListener.kt | 65 ++++++++-------- .../analytics/AnalyticsScanListenerTest.kt | 74 +++++++++++++++++++ 2 files changed, 106 insertions(+), 33 deletions(-) create mode 100644 src/test/kotlin/io/snyk/plugin/analytics/AnalyticsScanListenerTest.kt diff --git a/src/main/kotlin/io/snyk/plugin/analytics/AnalyticsScanListener.kt b/src/main/kotlin/io/snyk/plugin/analytics/AnalyticsScanListener.kt index 916b11464..6729865a8 100644 --- a/src/main/kotlin/io/snyk/plugin/analytics/AnalyticsScanListener.kt +++ b/src/main/kotlin/io/snyk/plugin/analytics/AnalyticsScanListener.kt @@ -1,6 +1,5 @@ package io.snyk.plugin.analytics -import com.intellij.openapi.application.ApplicationInfo import com.intellij.openapi.components.Service import com.intellij.openapi.project.Project import io.snyk.plugin.events.SnykScanListener @@ -15,44 +14,44 @@ import snyk.iac.IacResult import snyk.oss.OssResult import snyk.pluginInfo -@Service +@Service(Service.Level.PROJECT) class AnalyticsScanListener(val project: Project) { + fun getScanDoneEvent( + duration: Long, product: String, critical: Int, high: Int, medium: Int, low: Int + ): ScanDoneEvent { + return ScanDoneEvent( + ScanDoneEvent.Data( + type = "analytics", + attributes = ScanDoneEvent.Attributes( + deviceId = pluginSettings().userAnonymousId, + application = pluginInfo.integrationName, + applicationVersion = pluginInfo.integrationVersion, + os = SystemUtils.OS_NAME, + arch = SystemUtils.OS_ARCH, + integrationName = pluginInfo.integrationName, + integrationVersion = pluginInfo.integrationVersion, + integrationEnvironment = pluginInfo.integrationEnvironment, + integrationEnvironmentVersion = pluginInfo.integrationEnvironmentVersion, + eventType = "Scan done", + status = "Succeeded", + scanType = product, + uniqueIssueCount = ScanDoneEvent.UniqueIssueCount( + critical = critical, + high = high, + medium = medium, + low = low + ), + durationMs = "$duration", + ) + ) + ) + } + fun initScanListener() = project.messageBus.connect().subscribe( SnykScanListener.SNYK_SCAN_TOPIC, object : SnykScanListener { var start: Long = 0 - private fun getScanDoneEvent( - duration: Long, product: String, critical: Int, high: Int, medium: Int, low: Int - ): ScanDoneEvent { - return ScanDoneEvent( - ScanDoneEvent.Data( - type = "analytics", - attributes = ScanDoneEvent.Attributes( - deviceId = pluginSettings().userAnonymousId, - application = ApplicationInfo.getInstance().fullApplicationName, - applicationVersion = ApplicationInfo.getInstance().fullVersion, - os = SystemUtils.OS_NAME, - arch = SystemUtils.OS_ARCH, - integrationName = pluginInfo.integrationName, - integrationVersion = pluginInfo.integrationVersion, - integrationEnvironment = pluginInfo.integrationEnvironment, - integrationEnvironmentVersion = pluginInfo.integrationEnvironmentVersion, - eventType = "Scan done", - status = "Succeeded", - scanType = product, - uniqueIssueCount = ScanDoneEvent.UniqueIssueCount( - critical = critical, - high = high, - medium = medium, - low = low - ), - durationMs = "$duration", - ) - ) - ) - } - override fun scanningStarted() { start = System.currentTimeMillis() } diff --git a/src/test/kotlin/io/snyk/plugin/analytics/AnalyticsScanListenerTest.kt b/src/test/kotlin/io/snyk/plugin/analytics/AnalyticsScanListenerTest.kt new file mode 100644 index 000000000..809deacb6 --- /dev/null +++ b/src/test/kotlin/io/snyk/plugin/analytics/AnalyticsScanListenerTest.kt @@ -0,0 +1,74 @@ +package io.snyk.plugin.analytics + +import com.intellij.openapi.project.Project +import io.mockk.every +import io.mockk.mockk +import io.mockk.mockkStatic +import io.mockk.unmockkAll +import io.snyk.plugin.pluginSettings +import io.snyk.plugin.services.SnykApplicationSettingsStateService +import junit.framework.TestCase.assertEquals +import junit.framework.TestCase.assertNotNull +import org.apache.commons.lang.SystemUtils +import org.eclipse.lsp4j.services.LanguageServer +import org.junit.After +import org.junit.Before +import org.junit.Test +import snyk.pluginInfo + +class AnalyticsScanListenerTest { + private val projectMock: Project = mockk() + private val lsMock: LanguageServer = mockk() + private val settings = SnykApplicationSettingsStateService() + + @Before + fun setUp() { + unmockkAll() + mockkStatic("io.snyk.plugin.UtilsKt") + every { pluginSettings() } returns settings + mockkStatic("snyk.PluginInformationKt") + every { pluginInfo } returns mockk(relaxed = true) + every { pluginInfo.integrationName } returns "Snyk Intellij Plugin" + every { pluginInfo.integrationVersion } returns "2.4.61" + every { pluginInfo.integrationEnvironment } returns "IntelliJ IDEA" + every { pluginInfo.integrationEnvironmentVersion } returns "2020.3.2" + } + + @After + fun tearDown() { + unmockkAll() + } + + @Test + fun testGetScanDone() { + val cut = AnalyticsScanListener(projectMock) + val scanDoneEvent = cut.getScanDoneEvent( + 1, + "product", + 1, + 1, + 1, + 1 + ) + assertEquals("1", scanDoneEvent.data.attributes.durationMs) + assertEquals("product", scanDoneEvent.data.attributes.scanType) + assertEquals(1, scanDoneEvent.data.attributes.uniqueIssueCount.critical) + assertEquals(1, scanDoneEvent.data.attributes.uniqueIssueCount.high) + assertEquals(1, scanDoneEvent.data.attributes.uniqueIssueCount.medium) + assertEquals(1, scanDoneEvent.data.attributes.uniqueIssueCount.low) + + assertEquals("Snyk Intellij Plugin", scanDoneEvent.data.attributes.application) + assertEquals("2.4.61", scanDoneEvent.data.attributes.applicationVersion) + assertEquals("IntelliJ IDEA", scanDoneEvent.data.attributes.integrationEnvironment) + assertEquals("2020.3.2", scanDoneEvent.data.attributes.integrationEnvironmentVersion) + assertEquals(SystemUtils.OS_NAME, scanDoneEvent.data.attributes.os) + assertEquals(SystemUtils.OS_ARCH, scanDoneEvent.data.attributes.arch) + + assertEquals("analytics", scanDoneEvent.data.type) + assertEquals("Scan done", scanDoneEvent.data.attributes.eventType) + assertEquals("Succeeded", scanDoneEvent.data.attributes.status) + assertEquals(settings.userAnonymousId, scanDoneEvent.data.attributes.deviceId) + + assertNotNull(scanDoneEvent.data.attributes.timestampFinished) + } +}