diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 00c76551542..715bbe261de 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -106,6 +106,7 @@ kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = kotlin-stdLibJdk8 = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin" } kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" } mockito-core = { module = "org.mockito:mockito-core", version.ref = "mockito" } +mockito-junit-jupiter = { module = "org.mockito:mockito-junit-jupiter", version.ref = "mockito" } mockito-kotlin = { module = "org.mockito.kotlin:mockito-kotlin", version.ref = "mockitoKotlin" } mockk = { module = "io.mockk:mockk", version.ref="mockk" } nimbus-jose-jwt = {module = "com.nimbusds:nimbus-jose-jwt", version.ref = "nimbus-jose-jwt"} @@ -121,7 +122,7 @@ zjsonpatch = { module = "com.flipkart.zjsonpatch:zjsonpatch", version.ref = "zjs [bundles] jackson = ["jackson-datetime", "jackson-kotlin", "jackson-yaml", "jackson-xml"] kotlin = ["kotlin-stdLibJdk8", "kotlin-reflect"] -mockito = ["mockito-core", "mockito-kotlin"] +mockito = ["mockito-core", "mockito-junit-jupiter", "mockito-kotlin"] sshd = ["sshd-core", "sshd-scp", "sshd-sftp"] [plugins] diff --git a/plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ArtifactHelper.kt b/plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ArtifactHelper.kt index e0fe212b9d4..8787259bf08 100644 --- a/plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ArtifactHelper.kt +++ b/plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ArtifactHelper.kt @@ -112,7 +112,7 @@ class ArtifactHelper(private val lspArtifactsPath: Path = DEFAULT_ARTIFACT_PATH, logger.info { "Attempt ${currentAttempt.get()} of $maxDownloadAttempts to download LSP artifacts" } try { - withBackgroundProgress( + return withBackgroundProgress( project, AwsCoreBundle.message("amazonqFeatureDev.placeholder.downloading_and_extracting_lsp_artifacts"), cancellable = true @@ -123,9 +123,12 @@ class ArtifactHelper(private val lspArtifactsPath: Path = DEFAULT_ARTIFACT_PATH, .mapNotNull { it.filename } .forEach { filename -> extractZipFile(downloadPath.resolve(filename), downloadPath) } logger.info { "Successfully downloaded and moved LSP artifacts to $downloadPath" } + + return@withBackgroundProgress downloadPath } + + return@withBackgroundProgress null } - return downloadPath } catch (e: Exception) { when (e) { is CancellationException -> { diff --git a/plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ManifestFetcher.kt b/plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ManifestFetcher.kt index 92d0f8d9e98..4ccfbeca491 100644 --- a/plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ManifestFetcher.kt +++ b/plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ManifestFetcher.kt @@ -19,7 +19,7 @@ import java.nio.file.Path class ManifestFetcher( private val lspManifestUrl: String = DEFAULT_MANIFEST_URL, private val manifestManager: ManifestManager = ManifestManager(), - private val lspManifestFilePath: Path = DEFAULT_MANIFEST_PATH, + private val manifestPath: Path = DEFAULT_MANIFEST_PATH, ) { companion object { private val logger = getLogger() @@ -34,6 +34,10 @@ class ManifestFetcher( .resolve("jetbrains-lsp-manifest.json") } + @get:VisibleForTesting + internal val lspManifestFilePath: Path + get() = manifestPath + /** * Method which will be used to fetch latest manifest. * */ diff --git a/plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ArtifactHelperTest.kt b/plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ArtifactHelperTest.kt index cf0dc137901..5fede85b27e 100644 --- a/plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ArtifactHelperTest.kt +++ b/plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ArtifactHelperTest.kt @@ -4,6 +4,7 @@ package software.aws.toolkits.jetbrains.services.amazonq.lsp.artifacts import com.intellij.openapi.project.Project +import com.intellij.testFramework.ApplicationExtension import com.intellij.util.io.createDirectories import com.intellij.util.text.SemVer import io.mockk.Runs @@ -14,16 +15,16 @@ import io.mockk.mockkStatic import io.mockk.spyk import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat -import org.jetbrains.annotations.TestOnly import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.api.io.TempDir import org.mockito.kotlin.mock import software.aws.toolkits.jetbrains.services.amazonq.lsp.artifacts.ArtifactManager.SupportedManifestVersionRange import software.aws.toolkits.jetbrains.services.amazonq.project.manifest.ManifestManager import java.nio.file.Path -@TestOnly +@ExtendWith(ApplicationExtension::class) class ArtifactHelperTest { @TempDir lateinit var tempDir: Path diff --git a/plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ManifestFetcherTest.kt b/plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ManifestFetcherTest.kt index 62e17089eef..b5a1bd32fac 100644 --- a/plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ManifestFetcherTest.kt +++ b/plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/artifacts/ManifestFetcherTest.kt @@ -3,22 +3,28 @@ package software.aws.toolkits.jetbrains.services.amazonq.lsp.artifacts +import com.intellij.testFramework.ApplicationExtension +import com.intellij.testFramework.utils.io.createFile import io.mockk.every +import io.mockk.junit5.MockKExtension import io.mockk.mockkStatic import org.assertj.core.api.Assertions.assertThat -import org.jetbrains.annotations.TestOnly import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.junit.jupiter.api.io.TempDir +import org.mockito.junit.jupiter.MockitoExtension import org.mockito.kotlin.atLeastOnce import org.mockito.kotlin.never -import org.mockito.kotlin.reset import org.mockito.kotlin.spy import org.mockito.kotlin.verify import org.mockito.kotlin.whenever import software.aws.toolkits.jetbrains.core.getTextFromUrl import software.aws.toolkits.jetbrains.services.amazonq.project.manifest.ManifestManager +import java.nio.file.Path +import java.nio.file.Paths -@TestOnly +@ExtendWith(ApplicationExtension::class, MockitoExtension::class, MockKExtension::class) class ManifestFetcherTest { private lateinit var manifestFetcher: ManifestFetcher @@ -42,7 +48,6 @@ class ManifestFetcherTest { @Test fun `should return valid result from local should not execute remote method`() { - reset(manifestFetcher) whenever(manifestFetcher.fetchManifestFromLocal()).thenReturn(manifest) assertThat(manifestFetcher.fetch()).isNotNull().isEqualTo(manifest) @@ -65,8 +70,6 @@ class ManifestFetcherTest { mockkStatic("software.aws.toolkits.jetbrains.core.HttpUtilsKt") every { getTextFromUrl(any()) } returns "ManifestContent" - whenever(manifestManager.readManifestFile("")).thenReturn(null) - assertThat(manifestFetcher.fetchManifestFromRemote()).isNull() } @@ -84,17 +87,36 @@ class ManifestFetcherTest { @Test fun `fetchManifestFromRemote should return null if manifest is deprecated`() { mockkStatic("software.aws.toolkits.jetbrains.core.HttpUtilsKt") - every { getTextFromUrl(any()) } returns "ManifestContent" - - val deprecatedManifest = ManifestManager.Manifest(isManifestDeprecated = true) - - whenever(manifestManager.readManifestFile("")).thenReturn(deprecatedManifest) + every { getTextFromUrl(any()) } returns + // language=JSON + """ + { + "manifestSchemaVersion": "1.0", + "isManifestDeprecated": true + } + """.trimIndent() assertThat(manifestFetcher.fetchManifestFromRemote()).isNull() } @Test - fun `fetchManifestFromLocal should return null`() { + fun `fetchManifestFromLocal should return null if path does not exist locally`() { + whenever(manifestFetcher.lspManifestFilePath).thenReturn(Paths.get("does", "not", "exist")) + assertThat(manifestFetcher.fetchManifestFromLocal()).isNull() + } + + @Test + fun `fetchManifestFromLocal should return local path if exists locally`(@TempDir tempDir: Path) { + val manifestFile = tempDir.createFile("manifest.json") + manifestFile.toFile().writeText( + // language=JSON + """ + { + "manifestSchemaVersion": "1.0" + } + """.trimIndent() + ) + whenever(manifestFetcher.lspManifestFilePath).thenReturn(manifestFile) assertThat(manifestFetcher.fetchManifestFromLocal()).isNull() } }