Skip to content

Commit

Permalink
allow to mock telemetry message builder (#693)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Dec 28, 2023
1 parent 65b8558 commit 1bbad04
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ open class ResourceEditorFactory protected constructor(
private val createResourceEditor: (FileEditor, Project) -> ResourceEditor =
{ editor, project -> ResourceEditor(editor, IResourceModel.getInstance(), project) },
/* for mocking purposes */
private val reportTelemetry: (FileEditor, Project, TelemetryMessageBuilder.ActionMessage) -> Unit = { editor, project, telemetry ->
val resourceInfo = getKubernetesResourceInfo(editor.file, project)
TelemetryService.sendTelemetry(resourceInfo, telemetry)
},
/* for mocking purposes */
private val getProjectManager: () -> ProjectManager = { ProjectManager.getInstance() }

private val getProjectManager: () -> ProjectManager = { ProjectManager.getInstance() },
) {

companion object {
Expand Down Expand Up @@ -123,9 +117,10 @@ open class ResourceEditorFactory protected constructor(
) {
return null
}
val telemetry = TelemetryService.instance.action(TelemetryService.NAME_PREFIX_EDITOR + "open")
return try {
runAsync { reportTelemetry.invoke(editor, project, telemetry) }
val telemetry = getTelemetryMessageBuilder().action(TelemetryService.NAME_PREFIX_EDITOR + "open")
// val telemetry = TelemetryService.instance.action(TelemetryService.NAME_PREFIX_EDITOR + "open")
return try {
runAsync { TelemetryService.sendTelemetry(getKubernetesResourceInfo(editor.file, project), telemetry) }
val resourceEditor = createResourceEditor.invoke(editor, project)
resourceEditor.createToolbar()
getProjectManager.invoke().addProjectManagerListener(project, onProjectClosed(resourceEditor))
Expand Down Expand Up @@ -160,4 +155,9 @@ open class ResourceEditorFactory protected constructor(
ApplicationManager.getApplication().invokeLater(runnable)
}
}

/* for testing purposes */
protected open fun getTelemetryMessageBuilder(): TelemetryMessageBuilder {
return TelemetryService.instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.nhaarman.mockitokotlin2.whenever
import com.redhat.devtools.intellij.kubernetes.editor.ResourceEditor.Companion.KEY_RESOURCE_EDITOR
import com.redhat.devtools.intellij.kubernetes.model.util.createResource
import com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder
import com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder.ActionMessage
import io.fabric8.kubernetes.api.model.HasMetadata
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
Expand Down Expand Up @@ -64,7 +65,7 @@ class ResourceEditorFactoryTest {
valueFrom:
secretKeyRef:
name: postgres-secret
key: secret.password
key: secret.password
""".trimIndent()

private val resource = createResource<HasMetadata>(deployment)
Expand Down Expand Up @@ -125,15 +126,20 @@ class ResourceEditorFactoryTest {
on { editor } doReturn fileEditor
}

private val editorFactory =
private val actionMessage: ActionMessage = mock {
on { property(any(), any()) } doReturn mock
}

private val telemetryMessageBuilder: TelemetryMessageBuilder = mockTelemetryMessageBuilder()

private val editorFactory =
TestableResourceEditorFactory(
getFileEditorManager,
createResourceFile,
isValidType,
isTemporary,
hasKubernetesResource,
createResourceEditor,
reportTelemetry,
getProjectManager
)

Expand Down Expand Up @@ -285,14 +291,24 @@ class ResourceEditorFactoryTest {
verify(virtualFile).putUserData(KEY_RESOURCE_EDITOR, editor)
}

private open class TestableResourceEditorFactory(
private fun mockTelemetryMessageBuilder(): TelemetryMessageBuilder {
val actionMessage: ActionMessage = mock {
on { property(any(), any()) } doReturn mock
}

return mock {
on { action(any()) } doReturn actionMessage
}

}

private open inner class TestableResourceEditorFactory(
getFileEditorManager: (project: Project) -> FileEditorManager,
createResourceFile: (resource: HasMetadata) -> ResourceFile?,
isValidType: (file: VirtualFile?) -> Boolean,
isTemporary: (file: VirtualFile?) -> Boolean,
hasKubernetesResource: (FileEditor, Project) -> Boolean,
createResourceEditor: (FileEditor, Project) -> ResourceEditor,
reportTelemetry: (FileEditor, Project, TelemetryMessageBuilder.ActionMessage) -> Unit,
getProjectManager: () -> ProjectManager
) : ResourceEditorFactory(
getFileEditorManager,
Expand All @@ -301,7 +317,6 @@ class ResourceEditorFactoryTest {
isTemporary,
hasKubernetesResource,
createResourceEditor,
reportTelemetry,
getProjectManager
) {

Expand All @@ -314,5 +329,9 @@ class ResourceEditorFactoryTest {
// dont execute in UI thread
runnable.invoke()
}

override fun getTelemetryMessageBuilder(): TelemetryMessageBuilder {
return telemetryMessageBuilder
}
}
}
}

0 comments on commit 1bbad04

Please sign in to comment.