Skip to content

Commit

Permalink
fix: simply and fix AnalyticsScanListenerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch committed Jan 31, 2024
1 parent f563bf1 commit af6f866
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package io.snyk.plugin.analytics

import com.intellij.openapi.project.Project
import io.mockk.every
import io.mockk.justRun
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.verify
Expand All @@ -13,32 +15,36 @@ import io.snyk.plugin.services.SnykApplicationSettingsStateService
import io.snyk.plugin.services.SnykTaskQueueService
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertNotNull
import org.eclipse.lsp4j.ExecuteCommandParams
import org.eclipse.lsp4j.services.LanguageServer
import org.junit.After
import org.junit.Before
import org.junit.Test
import snyk.common.lsp.LanguageServerWrapper
import snyk.pluginInfo
import java.util.concurrent.CompletableFuture

class AnalyticsScanListenerTest {
private lateinit var cut: AnalyticsScanListener
private val projectMock: Project = mockk()
private val lsMock: LanguageServer = mockk()
private val settings = SnykApplicationSettingsStateService()
private val languageServerWrapper: LanguageServerWrapper = mockk()

@Before
fun setUp() {
unmockkAll()

mockkStatic("io.snyk.plugin.UtilsKt")
every { pluginSettings() } returns settings

mockkObject(SnykTaskQueueService.Companion)
every { SnykTaskQueueService.ls } returns languageServerWrapper
justRun { languageServerWrapper.sendReportAnalyticsCommand(any()) }

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"

cut = AnalyticsScanListener(projectMock)
}

Expand Down Expand Up @@ -81,57 +87,29 @@ class AnalyticsScanListenerTest {

@Test
fun `testScanListener scanningIacFinished should call language server to report analytics`() {
val languageServerWrapper = LanguageServerWrapper("dummy")
languageServerWrapper.languageServer = lsMock
every { SnykTaskQueueService?.ls } returns languageServerWrapper
every {
lsMock.workspaceService.executeCommand(any<ExecuteCommandParams>())
} returns CompletableFuture.completedFuture(null)

cut.snykScanListener.scanningIacFinished(mockk(relaxed = true))

verify { lsMock.workspaceService.executeCommand(any<ExecuteCommandParams>()) }
verify { languageServerWrapper.sendReportAnalyticsCommand(any()) }
}

@Test
fun `testScanListener scanningOssFinished should call language server to report analytics`() {
val languageServerWrapper = LanguageServerWrapper("dummy")
languageServerWrapper.languageServer = lsMock
every { SnykTaskQueueService?.ls } returns languageServerWrapper
every {
lsMock.workspaceService.executeCommand(any<ExecuteCommandParams>())
} returns CompletableFuture.completedFuture(null)

cut.snykScanListener.scanningOssFinished(mockk(relaxed = true))

verify { lsMock.workspaceService.executeCommand(any<ExecuteCommandParams>()) }
verify { languageServerWrapper.sendReportAnalyticsCommand(any()) }
}

@Test
fun `testScanListener scanningCodeFinished should call language server to report analytics`() {
val languageServerWrapper = LanguageServerWrapper("dummy")
languageServerWrapper.languageServer = lsMock
every { SnykTaskQueueService?.ls } returns languageServerWrapper
every {
lsMock.workspaceService.executeCommand(any<ExecuteCommandParams>())
} returns CompletableFuture.completedFuture(null)

cut.snykScanListener.scanningSnykCodeFinished(mockk(relaxed = true))

verify { lsMock.workspaceService.executeCommand(any<ExecuteCommandParams>()) }
verify { languageServerWrapper.sendReportAnalyticsCommand(any()) }
}

@Test
fun `testScanListener scanningContainerFinished should call language server to report analytics`() {
val languageServerWrapper = LanguageServerWrapper("dummy")
languageServerWrapper.languageServer = lsMock
every { SnykTaskQueueService?.ls } returns languageServerWrapper
every {
lsMock.workspaceService.executeCommand(any<ExecuteCommandParams>())
} returns CompletableFuture.completedFuture(null)

cut.snykScanListener.scanningContainerFinished(mockk(relaxed = true))

verify { lsMock.workspaceService.executeCommand(any<ExecuteCommandParams>()) }
verify { languageServerWrapper.sendReportAnalyticsCommand(any()) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import snyk.oss.OssService
import snyk.trust.confirmScanningAndSetWorkspaceTrustedStateIfNeeded
import java.util.concurrent.TimeUnit

@Suppress("FunctionName")
class SnykTaskQueueServiceTest : LightPlatformTestCase() {

private lateinit var ossServiceMock: OssService
Expand Down

0 comments on commit af6f866

Please sign in to comment.