This is an automated email from the ASF dual-hosted git repository.

liuhongyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 3a3cc0430c feat: add shenyu-registry-k8s module unit test (#6206)
3a3cc0430c is described below

commit 3a3cc0430c7c6795aa195b8a7631925786c7db42
Author: shown <[email protected]>
AuthorDate: Tue Oct 21 19:29:38 2025 +0800

    feat: add shenyu-registry-k8s module unit test (#6206)
    
    Signed-off-by: yuluo-yx <[email protected]>
    Co-authored-by: zhengpeng <[email protected]>
    Co-authored-by: aias00 <[email protected]>
---
 shenyu-registry/shenyu-registry-kubernetes/pom.xml |  18 ++
 .../registry/kubernetes/KubernetesClientTest.java  | 179 ++++++++++++++++
 .../registry/kubernetes/KubernetesConfigTest.java  | 102 +++++++++
 .../KubernetesInstanceRegisterRepositoryTest.java  | 228 +++++++++++++++++++++
 .../kubernetes/KubernetesInstanceTest.java         | 213 +++++++++++++++++++
 5 files changed, 740 insertions(+)

diff --git a/shenyu-registry/shenyu-registry-kubernetes/pom.xml 
b/shenyu-registry/shenyu-registry-kubernetes/pom.xml
index 9b6c2d8293..c396b5b0c2 100644
--- a/shenyu-registry/shenyu-registry-kubernetes/pom.xml
+++ b/shenyu-registry/shenyu-registry-kubernetes/pom.xml
@@ -38,5 +38,23 @@
             <artifactId>spring-web</artifactId>
             <scope>provided</scope>
         </dependency>
+
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-junit-jupiter</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git 
a/shenyu-registry/shenyu-registry-kubernetes/src/test/java/org/apache/shenyu/registry/kubernetes/KubernetesClientTest.java
 
b/shenyu-registry/shenyu-registry-kubernetes/src/test/java/org/apache/shenyu/registry/kubernetes/KubernetesClientTest.java
new file mode 100644
index 0000000000..33e4cb540b
--- /dev/null
+++ 
b/shenyu-registry/shenyu-registry-kubernetes/src/test/java/org/apache/shenyu/registry/kubernetes/KubernetesClientTest.java
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.registry.kubernetes;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.RestTemplate;
+
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test for KubernetesClient.
+ */
+public final class KubernetesClientTest {
+
+    private KubernetesClient kubernetesClient;
+
+    private RestTemplate restTemplate;
+
+    private KubernetesConfig kubernetesConfig;
+
+    @BeforeEach
+    public void setUp() throws Exception {
+        kubernetesConfig = new KubernetesConfig();
+        kubernetesConfig.setDiscoveryServerUrl("http://localhost:8761";);
+        kubernetesConfig.setEnabled(true);
+        kubernetesConfig.setNamespaces(Arrays.asList("default", "shenyu"));
+
+        kubernetesClient = new KubernetesClient(kubernetesConfig);
+
+        // Mock the RestTemplate
+        restTemplate = mock(RestTemplate.class);
+        Class<? extends KubernetesClient> clazz = kubernetesClient.getClass();
+        Field restField = clazz.getDeclaredField("rest");
+        restField.setAccessible(true);
+        restField.set(kubernetesClient, restTemplate);
+    }
+
+    @Test
+    public void testSelectInstances() {
+        String serviceId = "test-service";
+        KubernetesInstance instance1 = createKubernetesInstance("instance-1", 
serviceId, "192.168.1.1", 8080, "default");
+        KubernetesInstance instance2 = createKubernetesInstance("instance-2", 
serviceId, "192.168.1.2", 8081, "shenyu");
+        KubernetesInstance[] instances = new KubernetesInstance[]{instance1, 
instance2};
+
+        ResponseEntity<KubernetesInstance[]> responseEntity = new 
ResponseEntity<>(instances, HttpStatus.OK);
+        when(restTemplate.getForEntity(anyString(), 
eq(KubernetesInstance[].class), any(Object[].class)))
+                .thenReturn(responseEntity);
+
+        List<KubernetesInstance> result = 
kubernetesClient.selectInstances(serviceId);
+
+        assertNotNull(result);
+        assertEquals(2, result.size());
+        assertEquals("192.168.1.1", result.get(0).getHost());
+        assertEquals(8080, result.get(0).getPort());
+    }
+
+    @Test
+    public void testSelectInstancesWithNamespaceFilter() {
+        String serviceId = "test-service";
+        KubernetesInstance instance1 = createKubernetesInstance("instance-1", 
serviceId, "192.168.1.1", 8080, "default");
+        KubernetesInstance instance2 = createKubernetesInstance("instance-2", 
serviceId, "192.168.1.2", 8081, "other-namespace");
+        KubernetesInstance instance3 = createKubernetesInstance("instance-3", 
serviceId, "192.168.1.3", 8082, "shenyu");
+        KubernetesInstance[] instances = new KubernetesInstance[]{instance1, 
instance2, instance3};
+
+        ResponseEntity<KubernetesInstance[]> responseEntity = new 
ResponseEntity<>(instances, HttpStatus.OK);
+        when(restTemplate.getForEntity(anyString(), 
eq(KubernetesInstance[].class), any(Object[].class)))
+                .thenReturn(responseEntity);
+
+        List<KubernetesInstance> result = 
kubernetesClient.selectInstances(serviceId);
+
+        // Only instances in "default" and "shenyu" namespaces should be 
returned
+        assertEquals(2, result.size());
+        assertTrue(result.stream().anyMatch(i -> 
"default".equals(i.getNamespace())));
+        assertTrue(result.stream().anyMatch(i -> 
"shenyu".equals(i.getNamespace())));
+        assertTrue(result.stream().noneMatch(i -> 
"other-namespace".equals(i.getNamespace())));
+    }
+
+    @Test
+    public void testSelectInstancesWithEmptyNamespaceFilter() {
+        KubernetesConfig config = new KubernetesConfig();
+        config.setDiscoveryServerUrl("http://localhost:8761";);
+        config.setEnabled(true);
+        config.setNamespaces(Arrays.asList());
+
+        KubernetesClient client = new KubernetesClient(config);
+
+        String serviceId = "test-service";
+        KubernetesInstance instance1 = createKubernetesInstance("instance-1", 
serviceId, "192.168.1.1", 8080, "any-namespace");
+        KubernetesInstance instance2 = createKubernetesInstance("instance-2", 
serviceId, "192.168.1.2", 8081, "other-namespace");
+        KubernetesInstance[] instances = new KubernetesInstance[]{instance1, 
instance2};
+
+        RestTemplate mockRest = mock(RestTemplate.class);
+        ResponseEntity<KubernetesInstance[]> responseEntity = new 
ResponseEntity<>(instances, HttpStatus.OK);
+        when(mockRest.getForEntity(anyString(), 
eq(KubernetesInstance[].class), any(Object[].class)))
+                .thenReturn(responseEntity);
+
+        try {
+            Field restField = client.getClass().getDeclaredField("rest");
+            restField.setAccessible(true);
+            restField.set(client, mockRest);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+        List<KubernetesInstance> result = client.selectInstances(serviceId);
+
+        // All instances should be returned when namespace filter is empty
+        assertEquals(2, result.size());
+    }
+
+    @Test
+    public void testSelectInstancesWithNullResponse() {
+        String serviceId = "test-service";
+
+        ResponseEntity<KubernetesInstance[]> responseEntity = new 
ResponseEntity<>(null, HttpStatus.OK);
+        when(restTemplate.getForEntity(anyString(), 
eq(KubernetesInstance[].class), any(Object[].class)))
+                .thenReturn(responseEntity);
+
+        List<KubernetesInstance> result = 
kubernetesClient.selectInstances(serviceId);
+
+        assertNotNull(result);
+        assertTrue(result.isEmpty());
+    }
+
+    @Test
+    public void testSelectInstancesWithEmptyResponse() {
+        String serviceId = "test-service";
+        KubernetesInstance[] instances = new KubernetesInstance[]{};
+
+        ResponseEntity<KubernetesInstance[]> responseEntity = new 
ResponseEntity<>(instances, HttpStatus.OK);
+        when(restTemplate.getForEntity(anyString(), 
eq(KubernetesInstance[].class), any(Object[].class)))
+                .thenReturn(responseEntity);
+
+        List<KubernetesInstance> result = 
kubernetesClient.selectInstances(serviceId);
+
+        assertNotNull(result);
+        assertTrue(result.isEmpty());
+    }
+
+    private KubernetesInstance createKubernetesInstance(final String 
instanceId, final String serviceId,
+                                                         final String host, 
final int port, final String namespace) {
+        KubernetesInstance instance = new KubernetesInstance();
+        instance.setInstanceId(instanceId);
+        instance.setServiceId(serviceId);
+        instance.setHost(host);
+        instance.setPort(port);
+        instance.setNamespace(namespace);
+        return instance;
+    }
+}
diff --git 
a/shenyu-registry/shenyu-registry-kubernetes/src/test/java/org/apache/shenyu/registry/kubernetes/KubernetesConfigTest.java
 
b/shenyu-registry/shenyu-registry-kubernetes/src/test/java/org/apache/shenyu/registry/kubernetes/KubernetesConfigTest.java
new file mode 100644
index 0000000000..fbd9051e31
--- /dev/null
+++ 
b/shenyu-registry/shenyu-registry-kubernetes/src/test/java/org/apache/shenyu/registry/kubernetes/KubernetesConfigTest.java
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.registry.kubernetes;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Test for KubernetesConfig.
+ */
+public final class KubernetesConfigTest {
+
+    @Test
+    public void testDefaultConstructor() {
+        KubernetesConfig config = new KubernetesConfig();
+
+        assertNotNull(config);
+        assertTrue(config.isEnabled());
+        assertNotNull(config.getNamespaces());
+        assertTrue(config.getNamespaces().isEmpty());
+    }
+
+    @Test
+    public void testSetAndGetDiscoveryServerUrl() {
+        KubernetesConfig config = new KubernetesConfig();
+        String url = "http://localhost:8761";;
+
+        config.setDiscoveryServerUrl(url);
+
+        assertEquals(url, config.getDiscoveryServerUrl());
+    }
+
+    @Test
+    public void testSetAndGetEnabled() {
+        KubernetesConfig config = new KubernetesConfig();
+
+        config.setEnabled(false);
+        assertFalse(config.isEnabled());
+
+        config.setEnabled(true);
+        assertTrue(config.isEnabled());
+    }
+
+    @Test
+    public void testSetAndGetNamespaces() {
+        KubernetesConfig config = new KubernetesConfig();
+        List<String> namespaces = Arrays.asList("default", "shenyu", "test");
+
+        config.setNamespaces(namespaces);
+
+        assertEquals(3, config.getNamespaces().size());
+        assertTrue(config.getNamespaces().contains("default"));
+        assertTrue(config.getNamespaces().contains("shenyu"));
+        assertTrue(config.getNamespaces().contains("test"));
+    }
+
+    @Test
+    public void testSetEmptyNamespaces() {
+        KubernetesConfig config = new KubernetesConfig();
+        List<String> namespaces = Arrays.asList();
+
+        config.setNamespaces(namespaces);
+
+        assertNotNull(config.getNamespaces());
+        assertTrue(config.getNamespaces().isEmpty());
+    }
+
+    @Test
+    public void testMultipleSetAndGet() {
+        KubernetesConfig config = new KubernetesConfig();
+
+        config.setDiscoveryServerUrl("http://test:8080";);
+        config.setEnabled(false);
+        config.setNamespaces(Arrays.asList("namespace1", "namespace2"));
+
+        assertEquals("http://test:8080";, config.getDiscoveryServerUrl());
+        assertFalse(config.isEnabled());
+        assertEquals(2, config.getNamespaces().size());
+    }
+}
diff --git 
a/shenyu-registry/shenyu-registry-kubernetes/src/test/java/org/apache/shenyu/registry/kubernetes/KubernetesInstanceRegisterRepositoryTest.java
 
b/shenyu-registry/shenyu-registry-kubernetes/src/test/java/org/apache/shenyu/registry/kubernetes/KubernetesInstanceRegisterRepositoryTest.java
new file mode 100644
index 0000000000..13b070c721
--- /dev/null
+++ 
b/shenyu-registry/shenyu-registry-kubernetes/src/test/java/org/apache/shenyu/registry/kubernetes/KubernetesInstanceRegisterRepositoryTest.java
@@ -0,0 +1,228 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.registry.kubernetes;
+
+import org.apache.shenyu.registry.api.config.RegisterConfig;
+import org.apache.shenyu.registry.api.entity.InstanceEntity;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.RestTemplate;
+
+import java.lang.reflect.Field;
+import java.util.List;
+import java.util.Properties;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test for KubernetesInstanceRegisterRepository.
+ */
+public final class KubernetesInstanceRegisterRepositoryTest {
+
+    private KubernetesInstanceRegisterRepository repository;
+
+    private RestTemplate restTemplate;
+
+    private final String serverUrl = "http://localhost:8761";;
+
+    private final String namespace = "default,shenyu";
+
+    @BeforeEach
+    public void setUp() throws Exception {
+        repository = new KubernetesInstanceRegisterRepository();
+
+        RegisterConfig config = new RegisterConfig();
+        config.setServerLists(serverUrl);
+        config.setEnabled(true);
+        Properties properties = new Properties();
+        properties.setProperty("namespaces", namespace);
+        config.setProps(properties);
+
+        repository.init(config);
+
+        // Mock the RestTemplate
+        restTemplate = mock(RestTemplate.class);
+        Class<? extends KubernetesInstanceRegisterRepository> clazz = 
repository.getClass();
+        Field kubernetesClientField = 
clazz.getDeclaredField("kubernetesClient");
+        kubernetesClientField.setAccessible(true);
+        KubernetesClient kubernetesClient = (KubernetesClient) 
kubernetesClientField.get(repository);
+
+        Class<? extends KubernetesClient> clientClazz = 
kubernetesClient.getClass();
+        Field restField = clientClazz.getDeclaredField("rest");
+        restField.setAccessible(true);
+        restField.set(kubernetesClient, restTemplate);
+    }
+
+    @Test
+    public void testInit() {
+        RegisterConfig config = new RegisterConfig();
+        config.setServerLists("http://test:8080";);
+        config.setEnabled(true);
+        Properties properties = new Properties();
+        properties.setProperty("namespaces", "test-namespace");
+        config.setProps(properties);
+
+        KubernetesInstanceRegisterRepository newRepository = new 
KubernetesInstanceRegisterRepository();
+        newRepository.init(config);
+
+        assertNotNull(newRepository);
+    }
+
+    @Test
+    public void testPersistInstance() {
+        InstanceEntity instance = InstanceEntity.builder()
+                .appName("test-service")
+                .host("192.168.1.1")
+                .port(8080)
+                .build();
+
+        // persistInstance is currently empty implementation
+        repository.persistInstance(instance);
+        // No exception means success
+        assertTrue(true);
+    }
+
+    @Test
+    public void testSelectInstances() {
+        String serviceId = "test-service";
+        KubernetesInstance instance1 = createKubernetesInstance("instance-1", 
serviceId, "192.168.1.1", 8080, false, "default");
+        KubernetesInstance instance2 = createKubernetesInstance("instance-2", 
serviceId, "192.168.1.2", 8081, false, "shenyu");
+        KubernetesInstance[] instances = new KubernetesInstance[]{instance1, 
instance2};
+
+        ResponseEntity<KubernetesInstance[]> responseEntity = new 
ResponseEntity<>(instances, HttpStatus.OK);
+        when(restTemplate.getForEntity(anyString(), 
eq(KubernetesInstance[].class), any(Object[].class)))
+                .thenReturn(responseEntity);
+
+        List<InstanceEntity> result = repository.selectInstances(serviceId);
+
+        assertEquals(2, result.size());
+        assertEquals(serviceId, result.get(0).getAppName());
+        assertEquals("192.168.1.1", result.get(0).getHost());
+        assertEquals(8080, result.get(0).getPort());
+        assertEquals("http://192.168.1.1:8080";, 
result.get(0).getUri().toString());
+    }
+
+    @Test
+    public void testSelectInstancesWithSecure() {
+        String serviceId = "test-service";
+        KubernetesInstance instance = createKubernetesInstance("instance-1", 
serviceId, "192.168.1.1", 8443, true, "default");
+        KubernetesInstance[] instances = new KubernetesInstance[]{instance};
+
+        ResponseEntity<KubernetesInstance[]> responseEntity = new 
ResponseEntity<>(instances, HttpStatus.OK);
+        when(restTemplate.getForEntity(anyString(), 
eq(KubernetesInstance[].class), any(Object[].class)))
+                .thenReturn(responseEntity);
+
+        List<InstanceEntity> result = repository.selectInstances(serviceId);
+
+        assertEquals(1, result.size());
+        assertEquals("https://192.168.1.1:8443";, 
result.get(0).getUri().toString());
+    }
+
+    @Test
+    public void testSelectInstancesWithDefaultPort() {
+        String serviceId = "test-service";
+        KubernetesInstance instance1 = createKubernetesInstance("instance-1", 
serviceId, "192.168.1.1", 0, false, "default");
+        KubernetesInstance instance2 = createKubernetesInstance("instance-2", 
serviceId, "192.168.1.2", -1, true, "default");
+        KubernetesInstance[] instances = new KubernetesInstance[]{instance1, 
instance2};
+
+        ResponseEntity<KubernetesInstance[]> responseEntity = new 
ResponseEntity<>(instances, HttpStatus.OK);
+        when(restTemplate.getForEntity(anyString(), 
eq(KubernetesInstance[].class), any(Object[].class)))
+                .thenReturn(responseEntity);
+
+        List<InstanceEntity> result = repository.selectInstances(serviceId);
+
+        assertEquals(2, result.size());
+        assertEquals("http://192.168.1.1:80";, 
result.get(0).getUri().toString());
+        assertEquals("https://192.168.1.2:443";, 
result.get(1).getUri().toString());
+    }
+
+    @Test
+    public void testSelectInstancesEmpty() {
+        String serviceId = "test-service";
+        KubernetesInstance[] instances = new KubernetesInstance[]{};
+
+        ResponseEntity<KubernetesInstance[]> responseEntity = new 
ResponseEntity<>(instances, HttpStatus.OK);
+        when(restTemplate.getForEntity(anyString(), 
eq(KubernetesInstance[].class), any(Object[].class)))
+                .thenReturn(responseEntity);
+
+        List<InstanceEntity> result = repository.selectInstances(serviceId);
+
+        assertTrue(result.isEmpty());
+    }
+
+    @Test
+    public void testSelectInstancesWithNullResponse() {
+        String serviceId = "test-service";
+
+        ResponseEntity<KubernetesInstance[]> responseEntity = new 
ResponseEntity<>(null, HttpStatus.OK);
+        when(restTemplate.getForEntity(anyString(), 
eq(KubernetesInstance[].class), any(Object[].class)))
+                .thenReturn(responseEntity);
+
+        List<InstanceEntity> result = repository.selectInstances(serviceId);
+
+        assertTrue(result.isEmpty());
+    }
+
+    @Test
+    public void testSelectInstancesFilteredByNamespace() {
+        String serviceId = "test-service";
+        KubernetesInstance instance1 = createKubernetesInstance("instance-1", 
serviceId, "192.168.1.1", 8080, false, "default");
+        KubernetesInstance instance2 = createKubernetesInstance("instance-2", 
serviceId, "192.168.1.2", 8081, false, "other-namespace");
+        KubernetesInstance[] instances = new KubernetesInstance[]{instance1, 
instance2};
+
+        ResponseEntity<KubernetesInstance[]> responseEntity = new 
ResponseEntity<>(instances, HttpStatus.OK);
+        when(restTemplate.getForEntity(anyString(), 
eq(KubernetesInstance[].class), any(Object[].class)))
+                .thenReturn(responseEntity);
+
+        List<InstanceEntity> result = repository.selectInstances(serviceId);
+
+        // Only instance1 should be returned as it's in "default" namespace
+        assertEquals(1, result.size());
+        assertEquals("192.168.1.1", result.get(0).getHost());
+    }
+
+    @Test
+    public void testClose() {
+        // close() calls super.close() which is a default implementation
+        repository.close();
+        // No exception means success
+        assertTrue(true);
+    }
+
+    private KubernetesInstance createKubernetesInstance(final String 
instanceId, final String serviceId,
+                                                         final String host, 
final int port, final boolean secure,
+                                                         final String 
namespace) {
+        KubernetesInstance instance = new KubernetesInstance();
+        instance.setInstanceId(instanceId);
+        instance.setServiceId(serviceId);
+        instance.setHost(host);
+        instance.setPort(port);
+        instance.setSecure(secure);
+        instance.setNamespace(namespace);
+        return instance;
+    }
+}
diff --git 
a/shenyu-registry/shenyu-registry-kubernetes/src/test/java/org/apache/shenyu/registry/kubernetes/KubernetesInstanceTest.java
 
b/shenyu-registry/shenyu-registry-kubernetes/src/test/java/org/apache/shenyu/registry/kubernetes/KubernetesInstanceTest.java
new file mode 100644
index 0000000000..944ca7a024
--- /dev/null
+++ 
b/shenyu-registry/shenyu-registry-kubernetes/src/test/java/org/apache/shenyu/registry/kubernetes/KubernetesInstanceTest.java
@@ -0,0 +1,213 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.registry.kubernetes;
+
+import org.junit.jupiter.api.Test;
+
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Test for KubernetesInstance.
+ */
+public final class KubernetesInstanceTest {
+
+    @Test
+    public void testDefaultConstructor() {
+        KubernetesInstance instance = new KubernetesInstance();
+
+        assertNotNull(instance);
+        assertNull(instance.getInstanceId());
+        assertNull(instance.getServiceId());
+        assertNull(instance.getHost());
+        assertEquals(0, instance.getPort());
+        assertFalse(instance.isSecure());
+    }
+
+    @Test
+    public void testParameterizedConstructor() {
+        Map<String, String> metadata = new HashMap<>();
+        metadata.put("key1", "value1");
+        metadata.put("key2", "value2");
+
+        URI uri = URI.create("http://localhost:8080";);
+
+        KubernetesInstance instance = new KubernetesInstance(
+                "instance-1",
+                "test-service",
+                "192.168.1.1",
+                8080,
+                false,
+                uri,
+                metadata,
+                "http",
+                "default"
+        );
+
+        assertEquals("instance-1", instance.getInstanceId());
+        assertEquals("test-service", instance.getServiceId());
+        assertEquals("192.168.1.1", instance.getHost());
+        assertEquals(8080, instance.getPort());
+        assertFalse(instance.isSecure());
+        assertEquals(uri, instance.getUri());
+        assertEquals(metadata, instance.getMetadata());
+        assertEquals("http", instance.getScheme());
+        assertEquals("default", instance.getNamespace());
+    }
+
+    @Test
+    public void testSetAndGetInstanceId() {
+        KubernetesInstance instance = new KubernetesInstance();
+        String instanceId = "instance-123";
+
+        instance.setInstanceId(instanceId);
+
+        assertEquals(instanceId, instance.getInstanceId());
+    }
+
+    @Test
+    public void testSetAndGetServiceId() {
+        KubernetesInstance instance = new KubernetesInstance();
+        String serviceId = "test-service";
+
+        instance.setServiceId(serviceId);
+
+        assertEquals(serviceId, instance.getServiceId());
+    }
+
+    @Test
+    public void testSetAndGetHost() {
+        KubernetesInstance instance = new KubernetesInstance();
+        String host = "192.168.1.100";
+
+        instance.setHost(host);
+
+        assertEquals(host, instance.getHost());
+    }
+
+    @Test
+    public void testSetAndGetPort() {
+        KubernetesInstance instance = new KubernetesInstance();
+        int port = 9090;
+
+        instance.setPort(port);
+
+        assertEquals(port, instance.getPort());
+    }
+
+    @Test
+    public void testSetAndGetSecure() {
+        KubernetesInstance instance = new KubernetesInstance();
+
+        instance.setSecure(true);
+        assertTrue(instance.isSecure());
+
+        instance.setSecure(false);
+        assertFalse(instance.isSecure());
+    }
+
+    @Test
+    public void testSetAndGetUri() {
+        KubernetesInstance instance = new KubernetesInstance();
+        URI uri = URI.create("https://example.com:8443";);
+
+        instance.setUri(uri);
+
+        assertEquals(uri, instance.getUri());
+    }
+
+    @Test
+    public void testSetAndGetMetadata() {
+        KubernetesInstance instance = new KubernetesInstance();
+        Map<String, String> metadata = new HashMap<>();
+        metadata.put("env", "production");
+        metadata.put("version", "1.0.0");
+
+        instance.setMetadata(metadata);
+
+        assertEquals(2, instance.getMetadata().size());
+        assertEquals("production", instance.getMetadata().get("env"));
+        assertEquals("1.0.0", instance.getMetadata().get("version"));
+    }
+
+    @Test
+    public void testSetAndGetScheme() {
+        KubernetesInstance instance = new KubernetesInstance();
+        String scheme = "https";
+
+        instance.setScheme(scheme);
+
+        assertEquals(scheme, instance.getScheme());
+    }
+
+    @Test
+    public void testSetAndGetNamespace() {
+        KubernetesInstance instance = new KubernetesInstance();
+        String namespace = "shenyu-namespace";
+
+        instance.setNamespace(namespace);
+
+        assertEquals(namespace, instance.getNamespace());
+    }
+
+    @Test
+    public void testAllSettersAndGetters() {
+        KubernetesInstance instance = new KubernetesInstance();
+        Map<String, String> metadata = new HashMap<>();
+        metadata.put("test", "value");
+        URI uri = URI.create("http://test:8080";);
+
+        instance.setInstanceId("id-1");
+        instance.setServiceId("service-1");
+        instance.setHost("10.0.0.1");
+        instance.setPort(8888);
+        instance.setSecure(true);
+        instance.setUri(uri);
+        instance.setMetadata(metadata);
+        instance.setScheme("https");
+        instance.setNamespace("test-namespace");
+
+        assertEquals("id-1", instance.getInstanceId());
+        assertEquals("service-1", instance.getServiceId());
+        assertEquals("10.0.0.1", instance.getHost());
+        assertEquals(8888, instance.getPort());
+        assertTrue(instance.isSecure());
+        assertEquals(uri, instance.getUri());
+        assertEquals(metadata, instance.getMetadata());
+        assertEquals("https", instance.getScheme());
+        assertEquals("test-namespace", instance.getNamespace());
+    }
+
+    @Test
+    public void testEmptyMetadata() {
+        KubernetesInstance instance = new KubernetesInstance();
+        Map<String, String> emptyMetadata = new HashMap<>();
+
+        instance.setMetadata(emptyMetadata);
+
+        assertNotNull(instance.getMetadata());
+        assertTrue(instance.getMetadata().isEmpty());
+    }
+}

Reply via email to