Skip to content

Commit 58e240e

Browse files
committed
fix: fixed unit-tests
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 9e95681 commit 58e240e

File tree

3 files changed

+18
-59
lines changed

3 files changed

+18
-59
lines changed

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/util/ResourceEditorUtils.kt

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ fun hasKubernetesResource(file: VirtualFile?, project: Project): Boolean {
6565
* @param resourceInfo the resource info to inspect
6666
*/
6767
fun isKubernetesResource(resourceInfo: KubernetesResourceInfo?): Boolean {
68-
return resourceInfo?.typeInfo?.apiGroup?.isNotBlank() ?: false
69-
&& resourceInfo?.typeInfo?.kind?.isNotBlank() ?: false
68+
return resourceInfo?.apiGroup?.isNotBlank() ?: false
69+
&& resourceInfo?.kind?.isNotBlank() ?: false
7070
}
7171

7272
fun isKubernetesResource(kind: String, info: KubernetesTypeInfo?): Boolean {
@@ -95,49 +95,6 @@ fun getKubernetesResourceInfo(file: VirtualFile?, project: Project): KubernetesR
9595
}
9696
}
9797

98-
fun getChildren(document: YAMLDocument): List<YAMLPsiElement> {
99-
val topLevelValue: YAMLValue = document.topLevelValue ?: return emptyList()
100-
101-
return when (topLevelValue) {
102-
is YAMLMapping ->
103-
topLevelValue.keyValues.toList()
104-
is YAMLSequence ->
105-
topLevelValue.items
106-
else ->
107-
emptyList()
108-
}
109-
}
110-
111-
fun getChildren(file: JsonFile): List<JsonElement> {
112-
return file.allTopLevelValues
113-
}
114-
115-
/**
116-
* Returns the [PsiElement] named "metadata" within the children of the given [PsiElement].
117-
* Only [YAMLKeyValue] and [JsonProperty] are supported. Returns `null` otherwise.
118-
*
119-
* @param element the PsiElement whose "metadata" child should be found.
120-
* @return the PsiElement named "metadata"
121-
*/
122-
private fun getMetadata(parent: PsiElement): PsiElement? {
123-
return getElement(KEY_METADATA, parent)
124-
}
125-
126-
private fun getElement(key: String, parent: PsiElement): PsiElement? {
127-
return when (parent) {
128-
is YAMLValue ->
129-
parent.children
130-
.filterIsInstance<YAMLKeyValue>()
131-
.find { it.name == key }
132-
is JsonValue ->
133-
parent.children.toList()
134-
.filterIsInstance<JsonProperty>()
135-
.find { it.name == key }
136-
else ->
137-
null
138-
}
139-
}
140-
14198
/**
14299
* Returns the [PsiElement] named "data" within the children of the given [PsiElement].
143100
* Only [YAMLKeyValue] and [JsonProperty] are supported. Returns `null` otherwise.

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/telemetry/TelemetryService.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ object TelemetryService {
5656
}
5757

5858
fun sendTelemetry(info: KubernetesResourceInfo?, telemetry: TelemetryMessageBuilder.ActionMessage) {
59-
telemetry.property(PROP_RESOURCE_KIND, kindOrUnknown(info?.typeInfo)).send()
59+
telemetry.property(PROP_RESOURCE_KIND, kindOrUnknown(info?.kind, info?.apiGroup)).send()
6060
}
6161

6262
fun getKinds(resources: Collection<HasMetadata>): String {
@@ -69,16 +69,13 @@ object TelemetryService {
6969
}
7070

7171
private fun kindOrUnknown(kind: ResourceKind<*>?): String {
72-
return if (kind != null) {
73-
"${kind.version}/${kind.kind}"
74-
} else {
75-
"unknown"
76-
}
72+
return kindOrUnknown(kind?.kind, kind?.version);
7773
}
7874

79-
private fun kindOrUnknown(info: KubernetesTypeInfo?): String {
80-
return if (info != null) {
81-
"${info.apiGroup}/${info.kind}"
75+
private fun kindOrUnknown(kind: String?, apiGroup: String?): String {
76+
return if (kind != null
77+
&& apiGroup != null) {
78+
"${kind}/${apiGroup}"
8279
} else {
8380
"unknown"
8481
}

src/test/kotlin/com/redhat/devtools/intellij/kubernetes/model/mocks/Mocks.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,16 @@ object Mocks {
198198
}
199199

200200
fun kubernetesResourceInfo(name: String?, namespace: String?, typeInfo: KubernetesTypeInfo): KubernetesResourceInfo {
201-
return mock {
202-
on { this.name } doReturn name
203-
on { this.namespace } doReturn namespace
204-
on { this.typeInfo } doReturn typeInfo
205-
}
201+
val mock = mock<KubernetesResourceInfo>()
202+
whenever(mock.name)
203+
.thenReturn(name)
204+
whenever(mock.namespace)
205+
.thenReturn(namespace)
206+
whenever(mock.kind)
207+
.thenAnswer { typeInfo.kind } // avoid unfinished stubbing error in mockito
208+
whenever(mock.apiGroup)
209+
.thenAnswer { typeInfo.apiGroup } // avoid unfinished stubbing error in mockito
210+
return mock
206211
}
207212

208213
fun clientConfig(currentContext: NamedContext?, allContexts: List<NamedContext>): ClientConfig {

0 commit comments

Comments
 (0)