From 4599e9943bfb63c1a77b4e703ff7a7039e33588f Mon Sep 17 00:00:00 2001 From: Andre Dietisheim Date: Thu, 28 Dec 2023 10:53:28 +0100 Subject: [PATCH] allow to mock telemetry message builder (#693) Signed-off-by: Andre Dietisheim --- .../kubernetes/editor/ResourceEditorFactory.kt | 17 ++++++++--------- .../editor/ResourceEditorFactoryTest.kt | 16 ++++++++++------ 2 files changed, 18 insertions(+), 15 deletions(-) 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..35bb1fb69 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,9 @@ open class ResourceEditorFactory protected constructor( ) { return null } - val telemetry = TelemetryService.instance.action(TelemetryService.NAME_PREFIX_EDITOR + "open") + val telemetry = getTelemetryMessageBuilder().action(TelemetryService.NAME_PREFIX_EDITOR + "open") return try { - runAsync { reportTelemetry.invoke(editor, project, telemetry) } + runAsync { TelemetryService.sendTelemetry(getKubernetesResourceInfo(editor.file, project), telemetry) } val resourceEditor = createResourceEditor.invoke(editor, project) resourceEditor.createToolbar() getProjectManager.invoke().addProjectManagerListener(project, onProjectClosed(resourceEditor)) @@ -160,4 +154,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..b896e7f23 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 @@ -64,7 +64,7 @@ class ResourceEditorFactoryTest { valueFrom: secretKeyRef: name: postgres-secret - key: secret.password + key: secret.password """.trimIndent() private val resource = createResource(deployment) @@ -124,6 +124,9 @@ class ResourceEditorFactoryTest { private val resourceEditor: ResourceEditor = mock { on { editor } doReturn fileEditor } + private val telemetryMessageBuilder: TelemetryMessageBuilder = mock { + on { action(any()) } doReturn mock() + } private val editorFactory = TestableResourceEditorFactory( @@ -133,7 +136,6 @@ class ResourceEditorFactoryTest { isTemporary, hasKubernetesResource, createResourceEditor, - reportTelemetry, getProjectManager ) @@ -285,14 +287,13 @@ class ResourceEditorFactoryTest { verify(virtualFile).putUserData(KEY_RESOURCE_EDITOR, editor) } - private open class TestableResourceEditorFactory( + 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 +302,6 @@ class ResourceEditorFactoryTest { isTemporary, hasKubernetesResource, createResourceEditor, - reportTelemetry, getProjectManager ) { @@ -314,5 +314,9 @@ class ResourceEditorFactoryTest { // dont execute in UI thread runnable.invoke() } + + override fun getTelemetryMessageBuilder(): TelemetryMessageBuilder { + return telemetryMessageBuilder + } } -} \ No newline at end of file +}