Skip to content

Commit

Permalink
feat: add test for event mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch committed Nov 8, 2023
1 parent bc48fcc commit 20bc30c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 33 deletions.
65 changes: 32 additions & 33 deletions src/main/kotlin/io/snyk/plugin/analytics/AnalyticsScanListener.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {

Check warning

Code scanning / detekt

The more parameters a function has the more complex it is. Long parameter lists are often used to control complex algorithms and violate the Single Responsibility Principle. Prefer functions with short parameter lists. Warning

The function getScanDoneEvent(duration: Long, product: String, critical: Int, high: Int, medium: Int, low: Int) has too many parameters. The current threshold is set to 6.
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()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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()

Check warning

Code scanning / detekt

Property is unused and should be removed. Warning test

Private property lsMock is unused.
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)
}
}

0 comments on commit 20bc30c

Please sign in to comment.