diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/DefaultKubernetesServiceInstance.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/DefaultKubernetesServiceInstance.java index 1e4af72222..8c4854a792 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/DefaultKubernetesServiceInstance.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/DefaultKubernetesServiceInstance.java @@ -16,9 +16,12 @@ package org.springframework.cloud.kubernetes.commons.discovery; +import java.io.Serializable; import java.net.URI; import java.util.Map; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTP; import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTPS; import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.NAMESPACE_METADATA_KEY; @@ -34,9 +37,10 @@ * @param namespace the namespace of the service. * @param cluster the cluster the service resides in. */ +@JsonIgnoreProperties(ignoreUnknown = true) public record DefaultKubernetesServiceInstance(String instanceId, String serviceId, String host, int port, Map metadata, boolean secure, String namespace, String cluster, - Map> podMetadata) implements KubernetesServiceInstance { + Map> podMetadata) implements KubernetesServiceInstance, Serializable { @Override public String getInstanceId() { diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/KubernetesExternalNameServiceInstance.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/KubernetesExternalNameServiceInstance.java index 5cf9740dc5..7697844426 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/KubernetesExternalNameServiceInstance.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/KubernetesExternalNameServiceInstance.java @@ -16,6 +16,7 @@ package org.springframework.cloud.kubernetes.commons.discovery; +import java.io.Serializable; import java.net.URI; import java.util.Map; @@ -28,7 +29,7 @@ * @author wind57 */ public record KubernetesExternalNameServiceInstance(String serviceId, String host, String instanceId, - Map metadata) implements ServiceInstance { + Map metadata) implements ServiceInstance, Serializable { @Override public String getServiceId() { diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/configdata/KubernetesConfigDataLocationResolverTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/configdata/KubernetesConfigDataLocationResolverTests.java index e5da36412c..ddde540893 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/configdata/KubernetesConfigDataLocationResolverTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/configdata/KubernetesConfigDataLocationResolverTests.java @@ -291,8 +291,7 @@ void testResolveProfileSpecificSix() { Profiles profiles = Mockito.mock(Profiles.class); ConfigDataLocation configDataLocation = ConfigDataLocation.of("kubernetes:abc"); - storePropertiesResolver.resolveProfileSpecific(RESOLVER_CONTEXT, - configDataLocation, profiles); + storePropertiesResolver.resolveProfileSpecific(RESOLVER_CONTEXT, configDataLocation, profiles); // on the other hand, @Default will be picked here Assertions.assertThat(storePropertiesResolver.configMapConfigProperties.enabled()).isTrue(); diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/discovery/ServiceInstanceSerializationTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/discovery/ServiceInstanceSerializationTests.java new file mode 100644 index 0000000000..bad7065280 --- /dev/null +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/discovery/ServiceInstanceSerializationTests.java @@ -0,0 +1,139 @@ +/* + * Copyright 2019-present the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.cloud.kubernetes.commons.discovery; + +import java.util.Map; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +import org.springframework.boot.test.json.BasicJsonTester; +import org.springframework.cloud.client.ServiceInstance; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author wind57 + */ +class ServiceInstanceSerializationTests { + + private static final BasicJsonTester BASIC_JSON_TESTER = new BasicJsonTester( + ServiceInstanceSerializationTests.class); + + /** + *
+	 *     {
+	 *   	"instanceId": "instanceId",
+	 *   	"serviceId": "serviceId",
+	 *   	"host": "host",
+	 *   	"port": 8080,
+	 *   	"metadata": {
+	 *     	"k8s_namespace": "spring-k8s"
+	 *   	},
+	 *   	"secure": true,
+	 *   	"namespace": "namespace",
+	 *   	"cluster": "cluster",
+	 *   	"podMetadata": {
+	 *     	"pod_root": {
+	 *       	"pod_key": "pod_value"
+	 *     	}
+	 *   	},
+	 *   	"scheme": "https",
+	 *   	"uri": "https://host:8080"
+	 * 		}
+	 * 
+ */ + @Test + void defaultKubernetesServiceInstanceSerializationTest() throws JsonProcessingException { + + DefaultKubernetesServiceInstance instance = new DefaultKubernetesServiceInstance("instanceId", "serviceId", + "host", 8080, Map.of("k8s_namespace", "spring-k8s"), true, "namespace", "cluster", + Map.of("pod_root", Map.of("pod_key", "pod_value"))); + + String serialized = new ObjectMapper().writeValueAsString(instance); + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.instanceId") + .isEqualTo("instanceId"); + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.serviceId") + .isEqualTo("serviceId"); + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.host").isEqualTo("host"); + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathNumberValue("$.port").isEqualTo(8080); + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathMapValue("$.metadata") + .containsExactlyInAnyOrderEntriesOf(Map.of("k8s_namespace", "spring-k8s")); + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathBooleanValue("$.secure").isTrue(); + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.namespace") + .isEqualTo("namespace"); + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.cluster").isEqualTo("cluster"); + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathMapValue("$.podMetadata") + .containsExactlyInAnyOrderEntriesOf(Map.of("pod_root", Map.of("pod_key", "pod_value"))); + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.scheme").isEqualTo("https"); + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.uri") + .isEqualTo("https://host:8080"); + } + + @Test + void defaultKubernetesServiceInstanceDeserializationTest() throws JsonProcessingException { + + String serialized = """ + { + "instanceId": "instanceId", + "serviceId": "serviceId", + "host": "host", + "port": 8080, + "metadata": { + "k8s_namespace": "spring-k8s" + }, + "secure": true, + "namespace": "namespace", + "cluster": "cluster", + "podMetadata": { + "pod_root": { + "pod_key": "pod_value" + } + }, + "scheme": "https", + "uri": "https://host:8080" + } + """; + + ServiceInstance deserialized = new ObjectMapper().readValue(serialized, DefaultKubernetesServiceInstance.class); + assertThat(deserialized.getInstanceId()).isEqualTo("instanceId"); + assertThat(deserialized.getServiceId()).isEqualTo("serviceId"); + assertThat(deserialized.getScheme()).isEqualTo("https"); + assertThat(deserialized.getHost()).isEqualTo("host"); + assertThat(deserialized.getPort()).isEqualTo(8080); + assertThat(deserialized.getUri().toASCIIString()).isEqualTo("https://host:8080"); + assertThat(deserialized.getMetadata()) + .containsExactlyInAnyOrderEntriesOf(Map.of("k8s_namespace", "spring-k8s")); + } + + @Test + void externalNameServiceInstanceSerializationTest() throws JsonProcessingException { + KubernetesExternalNameServiceInstance instance = new KubernetesExternalNameServiceInstance("serviceId", "host", + "instanceId", Map.of("a", "b")); + String serialized = new ObjectMapper().writeValueAsString(instance); + + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.serviceId") + .isEqualTo("serviceId"); + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.host").isEqualTo("host"); + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.instanceId") + .isEqualTo("instanceId"); + assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathMapValue("$.metadata") + .containsExactlyInAnyOrderEntriesOf(Map.of("a", "b")); + } + +} diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-discovery/src/test/java/org/springframework/cloud/kubernetes/k8s/client/discovery/TestAssertions.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-discovery/src/test/java/org/springframework/cloud/kubernetes/k8s/client/discovery/TestAssertions.java index b4c0172564..4243168612 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-discovery/src/test/java/org/springframework/cloud/kubernetes/k8s/client/discovery/TestAssertions.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-discovery/src/test/java/org/springframework/cloud/kubernetes/k8s/client/discovery/TestAssertions.java @@ -84,8 +84,6 @@ static void assertBlockingConfiguration(CapturedOutput output, int port) { assertThat(BASIC_JSON_TESTER.from(healthResult)).extractingJsonPathStringValue(BLOCKING_STATUS).isEqualTo("UP"); - assertThat(BASIC_JSON_TESTER.from(healthResult)).extractingJsonPathStringValue(BLOCKING_STATUS).isEqualTo("UP"); - assertThat(BASIC_JSON_TESTER.from(healthResult)).extractingJsonPathArrayValue(BLOCKING_SERVICES) .containsExactlyInAnyOrder("kubernetes", "service-wiremock");