From 26abf57ffb61cecd9b3d924f33c8fbee347992c6 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Thu, 16 Sep 2021 19:18:54 -0400 Subject: [PATCH] Add TypeAdapter for JDK 17 protected packages --- ...ventBasedConfigMapChangeDetectorTests.java | 32 +++++++++++++++++-- ...tEventBasedSecretsChangeDetectorTests.java | 31 ++++++++++++++++-- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedConfigMapChangeDetectorTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedConfigMapChangeDetectorTests.java index dae163f4..88b0a44b 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedConfigMapChangeDetectorTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedConfigMapChangeDetectorTests.java @@ -16,7 +16,10 @@ package org.springframework.cloud.kubernetes.client.config.reload; +import java.io.IOException; +import java.lang.reflect.Modifier; import java.time.Duration; +import java.time.OffsetDateTime; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -25,6 +28,10 @@ import java.util.concurrent.TimeUnit; import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.WireMock; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; import io.kubernetes.client.informer.EventType; import io.kubernetes.client.openapi.ApiClient; import io.kubernetes.client.openapi.JSON; @@ -89,6 +96,11 @@ class KubernetesClientEventBasedConfigMapChangeDetectorTests { @Test void watch() { + GsonBuilder builder = new GsonBuilder(); + builder.excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT, Modifier.VOLATILE) + .registerTypeAdapter(OffsetDateTime.class, new GsonOffsetDateTimeAdapter()); + Gson gson = builder.create(); + Map data = new HashMap<>(); data.put("application.properties", "spring.cloud.kubernetes.configuration.watcher.refreshDelay=0\n" + "logging.level.org.springframework.cloud.kubernetes=TRACE"); @@ -101,8 +113,7 @@ class KubernetesClientEventBasedConfigMapChangeDetectorTests { .items(Arrays.asList(applicationConfig)); stubFor(get(urlMatching("^/api/v1/namespaces/default/configmaps.*")).inScenario("watch") .whenScenarioStateIs(STARTED).withQueryParam("watch", equalTo("false")) - .willReturn(aResponse().withStatus(200).withBody(new Gson().toJson(configMapList))) - .willSetStateTo("update")); + .willReturn(aResponse().withStatus(200).withBody(gson.toJson(configMapList))).willSetStateTo("update")); Watch.Response watchResponse = new Watch.Response<>(EventType.MODIFIED.name(), new V1ConfigMap() .kind("ConfigMap").metadata(new V1ObjectMeta().namespace("default").name("bar1")).data(updateData)); @@ -156,4 +167,21 @@ class KubernetesClientEventBasedConfigMapChangeDetectorTests { verify(strategy, atLeast(3)).reload(); } + // This is needed when using JDK17 because GSON uses reflection to construct an + // OffsetDateTime but that constructor + // is protected. + public class GsonOffsetDateTimeAdapter extends TypeAdapter { + + @Override + public void write(JsonWriter jsonWriter, OffsetDateTime localDateTime) throws IOException { + jsonWriter.value(OffsetDateTime.now().toString()); + } + + @Override + public OffsetDateTime read(JsonReader jsonReader) throws IOException { + return OffsetDateTime.now(); + } + + } + } diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedSecretsChangeDetectorTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedSecretsChangeDetectorTests.java index 4827b391..4c438e89 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedSecretsChangeDetectorTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedSecretsChangeDetectorTests.java @@ -16,7 +16,10 @@ package org.springframework.cloud.kubernetes.client.config.reload; +import java.io.IOException; +import java.lang.reflect.Modifier; import java.time.Duration; +import java.time.OffsetDateTime; import java.util.Arrays; import java.util.Base64; import java.util.concurrent.TimeUnit; @@ -24,6 +27,10 @@ import java.util.concurrent.TimeUnit; import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.WireMock; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; import io.kubernetes.client.informer.EventType; import io.kubernetes.client.openapi.ApiClient; import io.kubernetes.client.openapi.JSON; @@ -88,6 +95,10 @@ class KubernetesClientEventBasedSecretsChangeDetectorTests { @Test void watch() { + GsonBuilder builder = new GsonBuilder(); + builder.excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT, Modifier.VOLATILE) + .registerTypeAdapter(OffsetDateTime.class, new GsonOffsetDateTimeAdapter()); + Gson gson = builder.create(); V1Secret dbPassword = new V1Secret().kind("Secret").metadata(new V1ObjectMeta().name("db-password")) .putStringDataItem("password", Base64.getEncoder().encodeToString("p455w0rd".getBytes())) @@ -100,8 +111,7 @@ class KubernetesClientEventBasedSecretsChangeDetectorTests { stubFor(get(urlMatching("^/api/v1/namespaces/default/secrets.*")).inScenario("watch") .whenScenarioStateIs(STARTED).withQueryParam("watch", equalTo("false")) - .willReturn(aResponse().withStatus(200).withBody(new Gson().toJson(secretList))) - .willSetStateTo("update")); + .willReturn(aResponse().withStatus(200).withBody(gson.toJson(secretList))).willSetStateTo("update")); Watch.Response watchResponse = new Watch.Response<>(EventType.MODIFIED.name(), dbPasswordUpdated); stubFor(get(urlMatching("/api/v1/namespaces/default/secrets.*")).inScenario("watch") @@ -155,4 +165,21 @@ class KubernetesClientEventBasedSecretsChangeDetectorTests { verify(strategy, atLeast(3)).reload(); } + // This is needed when using JDK17 because GSON uses reflection to construct an + // OffsetDateTime but that constructor + // is protected. + public class GsonOffsetDateTimeAdapter extends TypeAdapter { + + @Override + public void write(JsonWriter jsonWriter, OffsetDateTime localDateTime) throws IOException { + jsonWriter.value(OffsetDateTime.now().toString()); + } + + @Override + public OffsetDateTime read(JsonReader jsonReader) throws IOException { + return OffsetDateTime.now(); + } + + } + }