Skip to content

Commit b9a7a75

Browse files
committed
make generator testable & add more error-case tests
1 parent 181f5ab commit b9a7a75

File tree

3 files changed

+75
-19
lines changed

3 files changed

+75
-19
lines changed

src/main/kotlin/org/contextmapper/intellij/actions/PlantUMLAction.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent
55
import com.intellij.openapi.actionSystem.CommonDataKeys
66
import com.intellij.openapi.diagnostic.Logger
77
import com.intellij.openapi.vfs.LocalFileSystem
8+
import com.redhat.devtools.lsp4ij.LanguageServerManager
89
import com.redhat.devtools.lsp4ij.commands.CommandExecutor
910
import org.contextmapper.intellij.actions.generators.ContextMapperGenerator
1011
import org.contextmapper.intellij.actions.generators.HandledGeneratorException
@@ -16,7 +17,7 @@ import kotlin.io.path.Path
1617
private val logger = Logger.getInstance(PlantUMLAction::class.java)
1718

1819
class PlantUMLAction : AnAction() {
19-
private val generator = ContextMapperGenerator { context -> CommandExecutor.executeCommand(context) }
20+
private val commandExecutor: LspCommandExecutor = { context -> CommandExecutor.executeCommand(context) }
2021

2122
override fun actionPerformed(event: AnActionEvent) {
2223
val project = event.project
@@ -32,6 +33,7 @@ class PlantUMLAction : AnAction() {
3233
val commandArgs = listOf(file.path, outDir)
3334
val command = Command("Generate PlantUML Diagrams", PLANT_UML_GENERATOR_COMMAND, commandArgs)
3435

36+
val generator = ContextMapperGenerator(commandExecutor, LanguageServerManager.getInstance(project))
3537
generator.generate(project, command)
3638
.whenComplete { result, ex ->
3739
if (ex != null || result.isFailure) {

src/main/kotlin/org/contextmapper/intellij/actions/generators/ContextMapperGenerator.kt

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.contextmapper.intellij.actions.generators
22

33
import com.intellij.openapi.project.Project
4+
import com.redhat.devtools.lsp4ij.LanguageServerItem
45
import com.redhat.devtools.lsp4ij.LanguageServerManager
56
import com.redhat.devtools.lsp4ij.commands.CommandExecutor
67
import com.redhat.devtools.lsp4ij.commands.LSPCommandContext
@@ -14,7 +15,8 @@ import org.eclipse.lsp4j.jsonrpc.ResponseErrorException
1415
import java.util.concurrent.CompletableFuture
1516

1617
class ContextMapperGenerator(
17-
private val commandExecutor: LspCommandExecutor
18+
private val commandExecutor: LspCommandExecutor,
19+
private val languageServerManager: LanguageServerManager
1820
) {
1921
fun generate(
2022
project: Project,
@@ -24,23 +26,13 @@ class ContextMapperGenerator(
2426

2527
val context = LSPCommandContext(command, project)
2628

27-
val languageServer =
28-
try {
29-
LanguageServerManager.getInstance(project).getLanguageServer(CONTEXT_MAPPER_SERVER_ID)
30-
.join()
31-
} catch (ex: Exception) {
32-
future.complete(
33-
Result.failure(
34-
ContextMapperGeneratorException(
35-
"Could not find language server instance.",
36-
ex,
37-
),
38-
),
39-
)
40-
return future
41-
}
29+
val languageServerResult = getLanguageServer()
30+
if (languageServerResult.isFailure) {
31+
future.complete(Result.failure(languageServerResult.exceptionOrNull()!!))
32+
return future
33+
}
4234
context.preferredLanguageServerId = CONTEXT_MAPPER_SERVER_ID
43-
context.preferredLanguageServer = languageServer
35+
context.preferredLanguageServer = languageServerResult.getOrNull()
4436

4537
val response = commandExecutor(context)
4638
CoroutineScope(Dispatchers.IO).launch {
@@ -85,6 +77,22 @@ class ContextMapperGenerator(
8577
}
8678
}
8779

80+
private fun getLanguageServer(): Result<LanguageServerItem?> {
81+
return try {
82+
val languageServer =
83+
languageServerManager.getLanguageServer(CONTEXT_MAPPER_SERVER_ID)
84+
.join()
85+
Result.success(languageServer)
86+
} catch (ex: Exception) {
87+
Result.failure(
88+
ContextMapperGeneratorException(
89+
"Could not find language server instance.",
90+
ex,
91+
),
92+
)
93+
}
94+
}
95+
8896
private fun extractGeneratedFiles(returnedValue: Any): Result<List<String>> {
8997
when (returnedValue) {
9098
is List<*> -> {

src/test/kotlin/org/contextmapper/intellij/actions/generators/ContextMapperGeneratorTest.kt

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package org.contextmapper.intellij.actions.generators
22

33
import com.intellij.openapi.project.Project
4+
import com.redhat.devtools.lsp4ij.LanguageServerManager
45
import com.redhat.devtools.lsp4ij.commands.CommandExecutor
56
import com.redhat.devtools.lsp4ij.settings.UserDefinedLanguageServerSettings
67
import io.mockk.CapturingSlot
78
import io.mockk.every
89
import io.mockk.mockk
910
import io.mockk.slot
1011
import org.contextmapper.intellij.actions.LspCommandExecutor
12+
import org.contextmapper.intellij.utils.CONTEXT_MAPPER_SERVER_ID
1113
import org.eclipse.lsp4j.Command
14+
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException
1215
import org.junit.jupiter.api.Assertions.assertEquals
1316
import org.junit.jupiter.api.Assertions.assertNotNull
1417
import org.junit.jupiter.api.Assertions.assertTrue
@@ -20,13 +23,21 @@ class ContextMapperGeneratorTest() {
2023

2124
private lateinit var successMessage: CapturingSlot<String>
2225
private lateinit var lspCommandExecutor: LspCommandExecutor
26+
private lateinit var languageServerManager: LanguageServerManager
2327
private lateinit var project: Project
2428
private lateinit var generator: ContextMapperGenerator
2529

2630
@BeforeEach
2731
fun setup() {
2832
successMessage = slot()
2933
lspCommandExecutor = mockk(relaxed = true)
34+
languageServerManager =
35+
mockk(relaxed = true) {
36+
every { getLanguageServer(any()) } returns
37+
mockk {
38+
every { join() } returns mockk(relaxed = true)
39+
}
40+
}
3041
project =
3142
mockk {
3243
every { basePath } returns "/tmp"
@@ -35,7 +46,7 @@ class ContextMapperGeneratorTest() {
3546
relaxed = true,
3647
)
3748
}
38-
generator = ContextMapperGenerator(lspCommandExecutor)
49+
generator = ContextMapperGenerator(lspCommandExecutor, languageServerManager)
3950
}
4051

4152
@Test
@@ -110,4 +121,39 @@ class ContextMapperGeneratorTest() {
110121
assertTrue(result.isFailure)
111122
assertTrue { result.exceptionOrNull() is ContextMapperGeneratorException }
112123
}
124+
125+
@Test
126+
fun testFailedGenerationWithHandledException() {
127+
every { lspCommandExecutor.invoke(any()) } returns
128+
mockk<CommandExecutor.LSPCommandResponse> {
129+
every { response } returns
130+
mockk {
131+
every { join() } returns mockk<ResponseErrorException>()
132+
}
133+
}
134+
135+
val result =
136+
generator.generate(project, command)
137+
.join()
138+
139+
assertNotNull(result)
140+
assertTrue { result.isFailure }
141+
assertTrue { result.exceptionOrNull() is HandledGeneratorException }
142+
}
143+
144+
@Test
145+
fun testGeneratorWithMissingLanguageServer() {
146+
every { languageServerManager.getLanguageServer(eq(CONTEXT_MAPPER_SERVER_ID)) } returns
147+
mockk {
148+
every { join() } throws RuntimeException()
149+
}
150+
151+
val result =
152+
generator.generate(project, command)
153+
.join()
154+
155+
assertNotNull(result)
156+
assertTrue { result.isFailure }
157+
assertTrue { result.exceptionOrNull() is ContextMapperGeneratorException }
158+
}
113159
}

0 commit comments

Comments
 (0)