Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package com.redhat.devtools.gateway.kubeconfig
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
import com.redhat.devtools.gateway.openshift.mapOfNotNull
import io.kubernetes.client.persister.ConfigPersister
import org.yaml.snakeyaml.DumperOptions
import java.io.File
Expand All @@ -22,18 +23,17 @@ class BlockStyleFilePersister(private val file: File) : ConfigPersister {

@Throws(java.io.IOException::class)
override fun save(
contexts: ArrayList<Any?>,
clusters: ArrayList<Any?>,
users: ArrayList<Any?>,
contexts: ArrayList<Any?>?,
clusters: ArrayList<Any?>?,
users: ArrayList<Any?>?,
preferences: Any?,
currentContext: String?
) {
val config = mapOf(
val config = mapOfNotNull(
"apiVersion" to "v1",
"kind" to "Config",
"current-context" to currentContext,
"preferences" to preferences,

"clusters" to clusters,
"contexts" to contexts,
"users" to users,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ abstract class KubeConfigUpdate private constructor(
abstract fun apply()

protected fun save(
contexts: ArrayList<Any?>,
clusters: ArrayList<Any?>,
users: ArrayList<Any?>,
contexts: ArrayList<Any?>?,
clusters: ArrayList<Any?>?,
users: ArrayList<Any?>?,
preferences: Any,
currentContext: String?,
path: Path?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ object Utils {

}

fun <K, V> mapOfNotNull(vararg pairs: Pair<K, V?>): Map<K, V> {
return pairs.mapNotNull { (key, value) ->
value?.let { key to it }
}.toMap()
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package com.redhat.devtools.gateway.view.steps

import com.intellij.openapi.application.invokeLater
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.wm.impl.welcomeScreen.WelcomeScreenUIManager
Expand Down Expand Up @@ -231,7 +232,8 @@ class DevSpacesServerStepView(
token.trim())
.apply()
} catch (e: Exception) {
Dialogs.error( e.message ?: "Could not update kube config file", "Kubeconfig Update Failed")
thisLogger().warn(e.message ?: "Could not save configuration file", e)
Dialogs.error( e.message ?: "Could not save configuration file", "Save Config Failed")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,96 @@ class UtilsTest {
// then
assertThat(value).isNull()
}

@Test
fun `#mapOfNotNull returns map with all non-null values`() {
// when
val result = mapOfNotNull(
"key1" to "value1",
"key2" to "value2",
"key3" to "value3"
)

// then
assertThat(result).hasSize(3)
assertThat(result).containsEntry("key1", "value1")
assertThat(result).containsEntry("key2", "value2")
assertThat(result).containsEntry("key3", "value3")
}

@Test
fun `#mapOfNotNull filters out null values`() {
// when
val result = mapOfNotNull(
"key1" to "value1",
"key2" to null,
"key3" to "value3",
"key4" to null
)

// then
assertThat(result).hasSize(2)
assertThat(result).containsEntry("key1", "value1")
assertThat(result).containsEntry("key3", "value3")
assertThat(result).doesNotContainKey("key2")
assertThat(result).doesNotContainKey("key4")
}

@Test
fun `#mapOfNotNull returns empty map when all values are null`() {
// when
val result = mapOfNotNull(
"key1" to null,
"key2" to null,
"key3" to null
)

// then
assertThat(result).isEmpty()
}

@Test
fun `#mapOfNotNull returns empty map when no pairs provided`() {
// when
val result = mapOfNotNull<String, String>()

// then
assertThat(result).isEmpty()
}

@Test
fun `#mapOfNotNull works with different value types`() {
// when
val stringResult = mapOfNotNull("key1" to "value", "key2" to null)
val intResult = mapOfNotNull("key1" to 42, "key2" to null, "key3" to 100)
val booleanResult = mapOfNotNull("key1" to true, "key2" to null, "key3" to false)

// then
assertThat(stringResult).hasSize(1)
assertThat(stringResult).containsEntry("key1", "value")

assertThat(intResult).hasSize(2)
assertThat(intResult).containsEntry("key1", 42)
assertThat(intResult).containsEntry("key3", 100)

assertThat(booleanResult).hasSize(2)
assertThat(booleanResult).containsEntry("key1", true)
assertThat(booleanResult).containsEntry("key3", false)
}

@Test
fun `#mapOfNotNull preserves order of non-null entries`() {
// when
val result = mapOfNotNull(
"key1" to "value1",
"key2" to null,
"key3" to "value3"
)

// then
val entries = result.entries.toList()
assertThat(entries).hasSize(2)
assertThat(entries[0].key).isEqualTo("key1")
assertThat(entries[1].key).isEqualTo("key3")
}
}
Loading