Skip to content

Commit

Permalink
Fixing writer and the corresponding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Waschndolos committed Nov 30, 2024
1 parent 4740658 commit 2114e52
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
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 @@ -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());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -57,8 +57,9 @@ private static List<String> 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);

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

0 comments on commit 2114e52

Please sign in to comment.