Skip to content

Commit

Permalink
Updating parent pom version to 5.3. Removing deprecated classes usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Waschndolos committed Nov 30, 2024
1 parent 9957621 commit 3648953
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 29 deletions.
1 change: 0 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ buildPlugin(
[platform: 'linux', jdk: 21],
[platform: 'windows', jdk: 21],
[platform: 'linux', jdk: 17],
[platform: 'linux', jdk: 11],
])
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.88</version>
<version>5.3</version>
<relativePath />
</parent>

Expand All @@ -20,7 +20,7 @@
<changelist>999999-SNAPSHOT</changelist>
<gitHubRepo>jenkinsci/prometheus-plugin</gitHubRepo>
<!-- when updated the io.jenkins.tools.bom:bom-X.Y.Z must be updated too -->
<jenkins.version>2.387.3</jenkins.version>
<jenkins.version>2.479</jenkins.version>

<prometheus.simpleclient.version>0.16.0</prometheus.simpleclient.version>
<slf4j.version>2.0.16</slf4j.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
Expand All @@ -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());
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -34,7 +35,7 @@ private static List<String> 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);
Expand All @@ -55,9 +56,10 @@ private static List<String> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -91,15 +89,15 @@ 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";
when(prometheusMetrics.getMetrics()).thenReturn(responseBody);
try (MockedStatic<DefaultPrometheusMetrics> 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);

Expand All @@ -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);

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 3648953

Please sign in to comment.