diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/ActiveContext.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/ActiveContext.kt index a9e28a2a3..1a296ffb3 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/ActiveContext.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/ActiveContext.kt @@ -112,10 +112,12 @@ abstract class ActiveContext( override fun getCurrentNamespace(): String? { val current = client.namespace - return if (current.isNullOrEmpty()) { - null - } else { + return if (!current.isNullOrEmpty()) { current + } else { + val allNamespaces = getAllResources(namespaceKind, NO_NAMESPACE) + val namespace = allNamespaces.find { namespace:HasMetadata -> "default" == namespace.metadata.name } ?: allNamespaces.firstOrNull() + return namespace?.metadata?.name } } diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/KubernetesContext.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/KubernetesContext.kt index 20219b62e..e9b105e14 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/KubernetesContext.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/KubernetesContext.kt @@ -44,6 +44,7 @@ open class KubernetesContext( ) { override val namespaceKind : ResourceKind = NamespacesOperator.KIND + private val replicasOperator = KubernetesReplicas( NonCachingSingleResourceOperator(client), object: ResourcesRetrieval { diff --git a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/KubernetesContextTest.kt b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/KubernetesContextTest.kt index 2c5eb0760..552112ca0 100644 --- a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/KubernetesContextTest.kt +++ b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/KubernetesContextTest.kt @@ -29,6 +29,7 @@ import com.redhat.devtools.intellij.kubernetes.model.client.ClientAdapter import com.redhat.devtools.intellij.kubernetes.model.client.KubeClientAdapter import com.redhat.devtools.intellij.kubernetes.model.context.IActiveContext.ResourcesIn import com.redhat.devtools.intellij.kubernetes.model.dashboard.IDashboard +import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.NAMESPACE1 import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.NAMESPACE2 import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.NAMESPACE3 @@ -39,6 +40,7 @@ import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.client import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.customResource import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.customResourceDefinition import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.namedContext +import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.namespaceListOperation import com.redhat.devtools.intellij.kubernetes.model.mocks.ClientMocks.resource import com.redhat.devtools.intellij.kubernetes.model.mocks.Mocks.namespacedResourceOperator import com.redhat.devtools.intellij.kubernetes.model.mocks.Mocks.nonNamespacedResourceOperator @@ -71,8 +73,10 @@ import org.junit.Test class KubernetesContextTest { + private val DEFAULT_NAMESPACE = resource("default", null, "defaultNsUid1", "v1", "1") + private val modelChange: ResourceModelObservable = mock() - private val allNamespaces = arrayOf(NAMESPACE1, NAMESPACE2, NAMESPACE3) + private val allNamespaces = arrayOf(NAMESPACE1, NAMESPACE2, DEFAULT_NAMESPACE, NAMESPACE3) private val currentNamespace = NAMESPACE2 private val client = KubeClientAdapter(client(currentNamespace.metadata.name, allNamespaces)) private val namespaceWatchOperation: (watcher: Watcher) -> Watch? = { null } @@ -205,13 +209,40 @@ class KubernetesContextTest { } @Test - fun `#getCurrentNamespace should use null if no namespace set in client`() { + fun `#getCurrentNamespace should return 'default' namespace if no current namespace set in client`() { // given whenever(client.get().namespace) .thenReturn(null) // when val namespace = context.getCurrentNamespace() // then + assertThat(namespace).isEqualTo(DEFAULT_NAMESPACE.metadata.name) + } + + @Test + fun `#getCurrentNamespace should return 1st namespace if no current namespace set in client and there's no 'default' namespace`() { + // given + val allNamespaces = arrayOf(NAMESPACE1, NAMESPACE2, NAMESPACE3) + whenever(namespacesOperator.allResources) + .thenReturn(allNamespaces.asList()) + whenever(client.get().namespace) + .thenReturn(null) + // when + val namespace = context.getCurrentNamespace() + // then + assertThat(namespace).isEqualTo(NAMESPACE1.metadata.name) + } + + @Test + fun `#getCurrentNamespace should return null if no current namespace set in client and there are no namespaces`() { + // given + whenever(namespacesOperator.allResources) + .thenReturn(emptyList()) + whenever(client.get().namespace) + .thenReturn(null) + // when + val namespace = context.getCurrentNamespace() + // then assertThat(namespace).isNull() } diff --git a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/model/mocks/ClientMocks.kt b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/model/mocks/ClientMocks.kt index 88cb339f4..ea85d7a09 100644 --- a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/model/mocks/ClientMocks.kt +++ b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/model/mocks/ClientMocks.kt @@ -97,7 +97,7 @@ object ClientMocks { } } - private fun namespaceListOperation(namespaces: Array): NamespaceListOperation { + fun namespaceListOperation(namespaces: Array): NamespaceListOperation { val namespaceList: NamespaceList = mock() doReturn(namespaces.asList()) .whenever(namespaceList).items