Skip to content

Commit

Permalink
feat: add tests for language server wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch committed Nov 8, 2023
1 parent cfaac31 commit b71064c
Showing 1 changed file with 60 additions and 3 deletions.
63 changes: 60 additions & 3 deletions src/test/java/snyk/common/lsp/LanguageServerWrapperTest.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,70 @@
package snyk.common.lsp

import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.verify
import io.snyk.plugin.pluginSettings
import io.snyk.plugin.services.SnykApplicationSettingsStateService
import org.eclipse.lsp4j.ExecuteCommandParams
import org.eclipse.lsp4j.InitializeParams
import org.eclipse.lsp4j.services.LanguageServer
import org.junit.After
import org.junit.Before
import org.junit.Test
import snyk.pluginInfo
import java.util.concurrent.CompletableFuture

class LanguageServerWrapperTest {

private val projectMock: Project = mockk()
private val lsMock: LanguageServer = mockk()
private lateinit var cut: LanguageServerWrapper

@Before
fun setUp() {
unmockkAll()
mockkStatic("io.snyk.plugin.UtilsKt")
every { pluginSettings() } returns SnykApplicationSettingsStateService()
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 = LanguageServerWrapper("dummy")
cut.languageServer = lsMock
}

@After
fun tearDown() {
unmockkAll()
}

@Test
fun initialize() {
val languageServerWrapper = LanguageServerWrapper("/Users/bdoetsch/workspace/cli/binary-releases/snyk-macos")
languageServerWrapper.initialize()
fun `sendInitializeMessage should send an initialize message to the language server`() {
val rootManagerMock = mockk<ProjectRootManager>(relaxed = true)
every { projectMock.getService(ProjectRootManager::class.java) } returns rootManagerMock
every { rootManagerMock.contentRoots } returns emptyArray()
every { lsMock.initialize(any<InitializeParams>()) } returns CompletableFuture.completedFuture(null)

cut.sendInitializeMessage(projectMock)

verify { lsMock.initialize(any<InitializeParams>()) }
}

@Test
fun `sendReportAnalyticsCommand should send a reportAnalytics command to the language server`() {
every {
lsMock.workspaceService.executeCommand(any<ExecuteCommandParams>())
} returns CompletableFuture.completedFuture(null)

cut.sendReportAnalyticsCommand(mockk(relaxed = true))

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

0 comments on commit b71064c

Please sign in to comment.