From 364895387b501fc555d27ecee3a8301c3a6b1239 Mon Sep 17 00:00:00 2001 From: Waschndolos Date: Sat, 30 Nov 2024 09:13:40 +0100 Subject: [PATCH] Updating parent pom version to 5.3. Removing deprecated classes usage --- Jenkinsfile | 1 - pom.xml | 4 +-- .../config/PrometheusConfiguration.java | 4 +-- .../prometheus/rest/PrometheusAction.java | 25 +++++++++++++------ .../config/PrometheusConfigurationTest.java | 10 +++++--- .../prometheus/rest/PrometheusActionTest.java | 22 ++++++++-------- 6 files changed, 37 insertions(+), 29 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 359793b27..ca2f7231c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -7,5 +7,4 @@ buildPlugin( [platform: 'linux', jdk: 21], [platform: 'windows', jdk: 21], [platform: 'linux', jdk: 17], - [platform: 'linux', jdk: 11], ]) diff --git a/pom.xml b/pom.xml index 0963d8d71..9f23e361d 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.jenkins-ci.plugins plugin - 4.88 + 5.3 @@ -20,7 +20,7 @@ 999999-SNAPSHOT jenkinsci/prometheus-plugin - 2.387.3 + 2.479 0.16.0 2.0.16 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 a0ea00bda..68d5f22dd 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/rest/PrometheusAction.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/rest/PrometheusAction.java @@ -10,8 +10,13 @@ import org.jenkinsci.plugins.prometheus.service.DefaultPrometheusMetrics; import org.jenkinsci.plugins.prometheus.service.PrometheusMetrics; import org.kohsuke.stapler.HttpResponse; -import org.kohsuke.stapler.StaplerRequest; -import org.kohsuke.stapler.StaplerResponse; +import org.kohsuke.stapler.StaplerRequest2; +import org.kohsuke.stapler.StaplerResponse2; + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; @Extension public class PrometheusAction implements UnprotectedRootAction { @@ -33,7 +38,7 @@ public String getUrlName() { return PrometheusConfiguration.get().getUrlName(); } - public HttpResponse doDynamic(StaplerRequest request) { + public HttpResponse doDynamic(StaplerRequest2 request) { if (request.getRestOfPath().equals(PrometheusConfiguration.get().getAdditionalPath())) { if (hasAccess()) { return prometheusResponse(); @@ -50,12 +55,16 @@ private boolean hasAccess() { return true; } + private HttpResponse prometheusResponse() { - return (request, response, node) -> { - response.setStatus(StaplerResponse.SC_OK); - response.setContentType(TextFormat.CONTENT_TYPE_004); - response.addHeader("Cache-Control", "must-revalidate,no-cache,no-store"); - response.getWriter().write(prometheusMetrics.getMetrics()); + return new HttpResponse() { + @Override + public void generateResponse(StaplerRequest2 request, StaplerResponse2 response, Object node) throws IOException { + response.setStatus(StaplerResponse2.SC_OK); + response.setContentType(TextFormat.CONTENT_TYPE_004); + response.addHeader("Cache-Control", "must-revalidate,no-cache,no-store"); + 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 aa5048050..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,11 +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.*; @@ -34,7 +35,7 @@ private static List wrongMetricCollectorPeriodsProvider() { @MethodSource("wrongMetricCollectorPeriodsProvider") public void shouldGetErrorWhenNotPositiveNumber(String metricCollectorPeriod) throws Descriptor.FormException { //given - Mockito.when(configuration.configure(any(), any())).thenCallRealMethod(); + Mockito.when(configuration.configure(any(StaplerRequest2.class), any())).thenCallRealMethod(); Mockito.when(configuration.doCheckCollectingMetricsPeriodInSeconds(any())).thenCallRealMethod(); JSONObject config = getDefaultConfig(); config.accumulate("collectingMetricsPeriodInSeconds", metricCollectorPeriod); @@ -55,9 +56,10 @@ private static List correctMetricCollectorPeriodsProvider() { @MethodSource("correctMetricCollectorPeriodsProvider") public void shouldReturnOk(String metricCollectorPeriod) throws Descriptor.FormException { //given - Mockito.when(configuration.configure(any(), any())).thenCallRealMethod(); + 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 c50ce75e9..5e058f349 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/rest/PrometheusActionTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/rest/PrometheusActionTest.java @@ -10,9 +10,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.kohsuke.stapler.HttpResponse; -import org.kohsuke.stapler.StaplerRequest; -import org.kohsuke.stapler.StaplerResponse; +import org.kohsuke.stapler.*; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.junit.jupiter.MockitoExtension; @@ -55,10 +53,10 @@ public void tearDown() { } @Test - public void shouldThrowExceptionWhenDoesNotMatchPath() throws IOException, ServletException { + public void shouldThrowExceptionWhenDoesNotMatchPath() throws IOException, ServletException, jakarta.servlet.ServletException { // given PrometheusAction action = new PrometheusAction(); - StaplerRequest request = mock(StaplerRequest.class); + StaplerRequest2 request = mock(StaplerRequest2.class); String url = ""; when(request.getRestOfPath()).thenReturn(url); @@ -72,10 +70,10 @@ public void shouldThrowExceptionWhenDoesNotMatchPath() throws IOException, Servl } @Test - public void shouldThrowExceptionWhenAuthenticationEnabledAndInsufficientPermission() throws IOException, ServletException { + public void shouldThrowExceptionWhenAuthenticationEnabledAndInsufficientPermission() throws IOException, ServletException, jakarta.servlet.ServletException { // given PrometheusAction action = new PrometheusAction(); - StaplerRequest request = mock(StaplerRequest.class); + StaplerRequest2 request = mock(StaplerRequest2.class); String url = "prometheus"; when(request.getRestOfPath()).thenReturn(url); when(configuration.isUseAuthenticatedEndpoint()).thenReturn(true); @@ -91,7 +89,7 @@ public void shouldThrowExceptionWhenAuthenticationEnabledAndInsufficientPermissi } @Test - public void shouldReturnMetrics() throws IOException, ServletException { + public void shouldReturnMetrics() throws IOException, jakarta.servlet.ServletException { // given DefaultPrometheusMetrics prometheusMetrics = mock(DefaultPrometheusMetrics.class); String responseBody = "testMetric"; @@ -99,7 +97,7 @@ public void shouldReturnMetrics() throws IOException, ServletException { try (MockedStatic defaultPrometheusMetricsMockedStatic = mockStatic(DefaultPrometheusMetrics.class)) { defaultPrometheusMetricsMockedStatic.when(DefaultPrometheusMetrics::get).thenReturn(prometheusMetrics); PrometheusAction action = new PrometheusAction(); - StaplerRequest request = mock(StaplerRequest.class); + StaplerRequest2 request = mock(StaplerRequest2.class); String url = "prometheus"; when(request.getRestOfPath()).thenReturn(url); @@ -117,14 +115,14 @@ public void shouldReturnMetrics() throws IOException, ServletException { } private static class AssertStaplerResponse { - private final StaplerResponse response; + private final StaplerResponse2 response; private final HttpResponse httpResponse; private final StringWriter stringWriter; private AssertStaplerResponse(HttpResponse httpResponse) throws IOException { this.httpResponse = httpResponse; - this.response = mock(StaplerResponse.class); + this.response = mock(StaplerResponse2.class); stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); @@ -155,7 +153,7 @@ private AssertStaplerResponse assertBody(String payload) { return this; } - private AssertStaplerResponse call() throws IOException, ServletException { + private AssertStaplerResponse call() throws IOException, jakarta.servlet.ServletException { httpResponse.generateResponse(null, response, null); return this; }