-
Notifications
You must be signed in to change notification settings - Fork 1k
fix-1641: serialization concerns (1) #2062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5ddeb9c
fix-1641: serialization concerns
wind57 e5de9db
fix-1641: by default, nothing is cacheable
wind57 2b65fd7
fix-1641: fix deserialization
wind57 484fb92
fix-1641: fix checkstyle
wind57 d9714e0
revert cacheable
wind57 f6bcde7
fix checkstyle
wind57 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
...springframework/cloud/kubernetes/commons/discovery/ServiceInstanceSerializationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
|
|
||
| /** | ||
| * <pre> | ||
| * { | ||
| * "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" | ||
| * } | ||
| * </pre> | ||
| */ | ||
| @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")); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should start with all services being non-cacheable and I will work in reverse, to add cacheable separate clients.
Removing this option is a breaking change, but adding separate clients on top, would be not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets separate removing the cachable annotations into its own PR to keep things a bit cleaner
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, done.