From 2114e52704d5657e25ac078da0831cfd2ccf2c69 Mon Sep 17 00:00:00 2001 From: Waschndolos Date: Sat, 30 Nov 2024 09:49:57 +0100 Subject: [PATCH] Fixing writer and the corresponding tests --- .../plugins/prometheus/config/PrometheusConfiguration.java | 4 ++-- .../jenkinsci/plugins/prometheus/rest/PrometheusAction.java | 5 +---- .../prometheus/config/PrometheusConfigurationTest.java | 5 +++-- .../plugins/prometheus/rest/PrometheusActionTest.java | 4 ++-- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/config/PrometheusConfiguration.java b/src/main/java/org/jenkinsci/plugins/prometheus/config/PrometheusConfiguration.java index f00f46131..f9efe8027 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/config/PrometheusConfiguration.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/config/PrometheusConfiguration.java @@ -10,7 +10,7 @@ import org.jenkinsci.plugins.prometheus.config.disabledmetrics.DisabledMetricConfig; import org.kohsuke.stapler.DataBoundSetter; import org.kohsuke.stapler.QueryParameter; -import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -80,7 +80,7 @@ public static PrometheusConfiguration get() { } @Override - public boolean configure(StaplerRequest req, JSONObject json) { + public boolean configure(StaplerRequest2 req, JSONObject json) { disabledMetricConfig = null; req.bindJSON(this, json); save(); diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/rest/PrometheusAction.java b/src/main/java/org/jenkinsci/plugins/prometheus/rest/PrometheusAction.java index 157e3df99..68d5f22dd 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/rest/PrometheusAction.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/rest/PrometheusAction.java @@ -63,10 +63,7 @@ public void generateResponse(StaplerRequest2 request, StaplerResponse2 response, response.setStatus(StaplerResponse2.SC_OK); response.setContentType(TextFormat.CONTENT_TYPE_004); response.addHeader("Cache-Control", "must-revalidate,no-cache,no-store"); - - try (Writer writer = new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8)) { - writer.write(prometheusMetrics.getMetrics()); - } + response.getWriter().write(prometheusMetrics.getMetrics()); } }; } diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/config/PrometheusConfigurationTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/config/PrometheusConfigurationTest.java index cbd25a9ee..bb44cbad2 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/config/PrometheusConfigurationTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/config/PrometheusConfigurationTest.java @@ -6,12 +6,12 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.StaplerRequest2; import org.mockito.Mockito; import java.util.Arrays; import java.util.List; +import java.util.Locale; import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable; import static org.junit.jupiter.api.Assertions.*; @@ -57,8 +57,9 @@ private static List correctMetricCollectorPeriodsProvider() { public void shouldReturnOk(String metricCollectorPeriod) throws Descriptor.FormException { //given Mockito.when(configuration.configure(any(StaplerRequest2.class), any())).thenCallRealMethod(); + Mockito.when(configuration.doCheckCollectingMetricsPeriodInSeconds(any())).thenCallRealMethod(); JSONObject config = getDefaultConfig(); - StaplerRequest request = Mockito.mock(StaplerRequest.class); + StaplerRequest2 request = Mockito.mock(StaplerRequest2.class); Mockito.doNothing().when(request).bindJSON(any(Object.class), any(JSONObject.class)); config.accumulate("collectingMetricsPeriodInSeconds", metricCollectorPeriod); diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/rest/PrometheusActionTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/rest/PrometheusActionTest.java index 2eea3fbe4..5e058f349 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/rest/PrometheusActionTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/rest/PrometheusActionTest.java @@ -89,7 +89,7 @@ public void shouldThrowExceptionWhenAuthenticationEnabledAndInsufficientPermissi } @Test - public void shouldReturnMetrics() throws IOException, ServletException, jakarta.servlet.ServletException { + public void shouldReturnMetrics() throws IOException, jakarta.servlet.ServletException { // given DefaultPrometheusMetrics prometheusMetrics = mock(DefaultPrometheusMetrics.class); String responseBody = "testMetric"; @@ -153,7 +153,7 @@ private AssertStaplerResponse assertBody(String payload) { return this; } - private AssertStaplerResponse call() throws IOException, ServletException, jakarta.servlet.ServletException { + private AssertStaplerResponse call() throws IOException, jakarta.servlet.ServletException { httpResponse.generateResponse(null, response, null); return this; }