diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorFactory.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorFactory.kt index 90b69c176..9434f377a 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorFactory.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorFactory.kt @@ -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 { @@ -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)) @@ -160,4 +155,9 @@ open class ResourceEditorFactory protected constructor( ApplicationManager.getApplication().invokeLater(runnable) } } + + /* for testing purposes */ + protected open fun getTelemetryMessageBuilder(): TelemetryMessageBuilder { + return TelemetryService.instance; + } } diff --git a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorFactoryTest.kt b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorFactoryTest.kt index 36d41b62b..be951b63c 100644 --- a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorFactoryTest.kt +++ b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditorFactoryTest.kt @@ -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 @@ -64,7 +65,7 @@ class ResourceEditorFactoryTest { valueFrom: secretKeyRef: name: postgres-secret - key: secret.password + key: secret.password """.trimIndent() private val resource = createResource(deployment) @@ -125,7 +126,13 @@ 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, @@ -133,7 +140,6 @@ class ResourceEditorFactoryTest { isTemporary, hasKubernetesResource, createResourceEditor, - reportTelemetry, getProjectManager ) @@ -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, @@ -301,7 +317,6 @@ class ResourceEditorFactoryTest { isTemporary, hasKubernetesResource, createResourceEditor, - reportTelemetry, getProjectManager ) { @@ -314,5 +329,9 @@ class ResourceEditorFactoryTest { // dont execute in UI thread runnable.invoke() } + + override fun getTelemetryMessageBuilder(): TelemetryMessageBuilder { + return telemetryMessageBuilder + } } -} \ No newline at end of file +}