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 4599e99
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 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,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))
Expand Down Expand Up @@ -160,4 +154,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 @@ -64,7 +64,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 @@ -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(
Expand All @@ -133,7 +136,6 @@ class ResourceEditorFactoryTest {
isTemporary,
hasKubernetesResource,
createResourceEditor,
reportTelemetry,
getProjectManager
)

Expand Down Expand Up @@ -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,
Expand All @@ -301,7 +302,6 @@ class ResourceEditorFactoryTest {
isTemporary,
hasKubernetesResource,
createResourceEditor,
reportTelemetry,
getProjectManager
) {

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

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

0 comments on commit 4599e99

Please sign in to comment.