Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #63

Merged
merged 8 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.thoughtworks.xstream.XStream;
import com.tikal.hudson.plugins.notification.model.JobState;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public enum Format {
XML {
Expand All @@ -27,17 +28,17 @@
@Override
protected byte[] serialize(JobState jobState) throws IOException {
xstream.processAnnotations(JobState.class);
return xstream.toXML(jobState).getBytes("UTF-8");
return xstream.toXML(jobState).getBytes(StandardCharsets.UTF_8);
}
},
JSON {
private final transient Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();

@Override
protected byte[] serialize(JobState jobState) throws IOException {
return gson.toJson(jobState).getBytes("UTF-8");
return gson.toJson(jobState).getBytes(StandardCharsets.UTF_8);

Check warning on line 41 in src/main/java/com/tikal/hudson/plugins/notification/Format.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 31-41 are not covered by tests
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

@DataBoundConstructor
public HudsonNotificationProperty(List<Endpoint> endpoints) {
this.endpoints = new ArrayList<Endpoint>(endpoints);
this.endpoints = new ArrayList<>(endpoints);

Check warning on line 28 in src/main/java/com/tikal/hudson/plugins/notification/HudsonNotificationProperty.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 28 is not covered by tests
}

public List<Endpoint> getEndpoints() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ private Result findLastBuildThatFinished(Run run) {
return null;
}

@SuppressWarnings("CastToConcreteClass")
public void handle(Run run, TaskListener listener, long timestamp) {
handle(run, listener, timestamp, false, null, 0, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,17 @@
// Verifying if the HTTP_PROXY is available
final String httpProxyUrl = System.getenv().get("http_proxy");
URL proxyUrl = null;
if (httpProxyUrl != null && httpProxyUrl.length() > 0) {
if (httpProxyUrl != null && !httpProxyUrl.isEmpty()) {

Check warning on line 70 in src/main/java/com/tikal/hudson/plugins/notification/Protocol.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 70 is only partially covered, 3 branches are missing
proxyUrl = new URL(httpProxyUrl);
if (!proxyUrl.getProtocol().startsWith("http")) {
throw new IllegalArgumentException("Not an http(s) url: " + httpProxyUrl);
}
}

Proxy proxy = Proxy.NO_PROXY;
if (Jenkins.getInstance() != null && Jenkins.getInstance().proxy != null) {
proxy = Jenkins.getInstance().proxy.createProxy(targetUrl.getHost());
Jenkins jenkins = Jenkins.getInstanceOrNull();
if (jenkins != null && jenkins.proxy != null) {

Check warning on line 79 in src/main/java/com/tikal/hudson/plugins/notification/Protocol.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 79 is only partially covered, 3 branches are missing
proxy = jenkins.proxy.createProxy(targetUrl.getHost());

Check warning on line 80 in src/main/java/com/tikal/hudson/plugins/notification/Protocol.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 80 is not covered by tests
} else if (proxyUrl != null) {
// Proxy connection to the address provided
final int proxyPort = proxyUrl.getPort() > 0 ? proxyUrl.getPort() : 80;
Expand All @@ -101,12 +102,9 @@
connection.setReadTimeout(timeout);
connection.connect();
try {
OutputStream output = connection.getOutputStream();
try {
try (OutputStream output = connection.getOutputStream()) {
output.write(data);
output.flush();
} finally {
output.close();
}
} finally {
// Follow an HTTP Temporary Redirect if we get one,
Expand Down Expand Up @@ -156,6 +154,6 @@
}

private static boolean isEmpty(String s) {
return ((s == null) || (s.trim().length() < 1));
return ((s == null) || (s.trim().isEmpty()));

Check warning on line 157 in src/main/java/com/tikal/hudson/plugins/notification/Protocol.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 157 is not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}

for (String s : strings) {
if ((s == null) || (s.trim().length() < 1)) {
if ((s == null) || (s.trim().isEmpty())) {

Check warning on line 30 in src/main/java/com/tikal/hudson/plugins/notification/Utils.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 30 is only partially covered, one branch is missing
return true;
}
}
Expand All @@ -40,7 +40,6 @@
* @param strings Strings to check for empty (whitespace is trimmed) or null.
* @throws java.lang.IllegalArgumentException Throws this exception if any string is empty.
*/
@SuppressWarnings("ReturnOfNull")
public static void verifyNotEmpty(String... strings) {
if (isEmpty(strings)) {
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
* notification.jar:
* archive: http://localhost:8080/job/notification-plugin/78/artifact/target/notification.jar
*/
private final Map<String, Map<String, String>> artifacts = new HashMap<String, Map<String, String>>();
private final Map<String, Map<String, String>> artifacts = new HashMap<>();

public int getNumber() {
return number;
Expand Down Expand Up @@ -140,75 +140,75 @@
}

public void setParameters(Map<String, String> params) {
this.parameters = new HashMap<String, String>(params);
this.parameters = new HashMap<>(params);
}

public Map<String, Map<String, String>> getArtifacts() {
return artifacts;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public String getDisplayName() {
return displayName;
}

public ScmState getScm() {
return scm;
}

public void setScm(ScmState scmState) {
this.scm = scmState;
}

public StringBuilder getLog() {
return this.log;
}

public void setLog(StringBuilder log) {
this.log = log;
}

public String getNotes() {
return notes;
}

public void setNotes(String buildNotes) {
this.notes = buildNotes;
}

public TestState getTestSummary() {
return testSummary;
}

public void setTestSummary(TestState testSummary) {
this.testSummary = testSummary;
}

/**
* Updates artifacts Map with S3 links, if corresponding publisher is available.
* @param job Job to update
* @param run Run to update
*/
public void updateArtifacts(Job job, Run run) {
updateArchivedArtifacts(run);
updateS3Artifacts(job, run);
}

private void updateArchivedArtifacts(Run run) {
@SuppressWarnings("unchecked")
List<Run.Artifact> buildArtifacts = run.getArtifacts();

for (Run.Artifact a : buildArtifacts) {
String artifactUrl = Jenkins.getInstance().getRootUrl() + run.getUrl() + "artifact/" + a.getHref();
String artifactUrl = Jenkins.get().getRootUrl() + run.getUrl() + "artifact/" + a.getHref();

Check warning on line 205 in src/main/java/com/tikal/hudson/plugins/notification/model/BuildState.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 143-205 are not covered by tests
updateArtifact(a.relativePath, "archive", artifactUrl);
}
}

private void updateS3Artifacts(Job job, Run run) {
if (Jenkins.getInstance().getPlugin("s3") == null) {
if (Jenkins.get().getPlugin("s3") == null) {

Check warning on line 211 in src/main/java/com/tikal/hudson/plugins/notification/model/BuildState.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 211 is only partially covered, one branch is missing
return;
}
if (!(run instanceof AbstractBuild)) {
Expand Down Expand Up @@ -263,7 +263,7 @@
verifyNotEmpty(fileName, locationName, locationUrl);

if (!artifacts.containsKey(fileName)) {
artifacts.put(fileName, new HashMap<String, String>());
artifacts.put(fileName, new HashMap<>());

Check warning on line 266 in src/main/java/com/tikal/hudson/plugins/notification/model/BuildState.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 266 is not covered by tests
}

if (artifacts.get(fileName).containsKey(locationName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static com.tikal.hudson.plugins.notification.UrlType.PUBLIC;
import static com.tikal.hudson.plugins.notification.UrlType.SECRET;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyBoolean;
Expand All @@ -26,6 +25,7 @@
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.List;
import jenkins.model.Jenkins;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -184,7 +184,7 @@ public void testRunNoProperty() {
public void testRunNoPreviousRunUrlNull() {
when(run.getParent()).thenReturn(job);
when(job.getProperty(HudsonNotificationProperty.class)).thenReturn(property);
when(property.getEndpoints()).thenReturn(asList(endpoint));
when(property.getEndpoints()).thenReturn(List.of(endpoint));
when(endpoint.getUrlInfo()).thenReturn(urlInfo);

Phase.STARTED.handle(run, listener, 0L);
Expand All @@ -197,7 +197,7 @@ public void testRunNoPreviousRunUrlNull() {
public void testRunNoPreviousRunUrlTypePublicUnresolvedUrl() throws IOException, InterruptedException {
when(run.getParent()).thenReturn(job);
when(job.getProperty(HudsonNotificationProperty.class)).thenReturn(property);
when(property.getEndpoints()).thenReturn(asList(endpoint));
when(property.getEndpoints()).thenReturn(List.of(endpoint));
when(endpoint.getUrlInfo()).thenReturn(urlInfo);
when(run.getEnvironment(listener)).thenReturn(environment);
when(urlInfo.getUrlOrId()).thenReturn("$someUrl");
Expand All @@ -216,7 +216,7 @@ public void testRunPreviousRunUrlTypePublic() throws IOException, InterruptedExc
byte[] data = "data".getBytes();
try (MockedStatic<Jenkins> jenkinsMockedStatic = mockStatic(Jenkins.class)) {
jenkinsMockedStatic.when(Jenkins::getInstanceOrNull).thenReturn(jenkins);
jenkinsMockedStatic.when(Jenkins::getInstance).thenReturn(jenkins);
jenkinsMockedStatic.when(Jenkins::get).thenReturn(jenkins);

Protocol httpProtocolSpy = spy(Protocol.HTTP);
when(endpoint.getProtocol()).thenReturn(httpProtocolSpy);
Expand All @@ -230,7 +230,7 @@ public void testRunPreviousRunUrlTypePublic() throws IOException, InterruptedExc

when(run.getParent()).thenReturn(job);
when(job.getProperty(HudsonNotificationProperty.class)).thenReturn(property);
when(property.getEndpoints()).thenReturn(asList(endpoint));
when(property.getEndpoints()).thenReturn(List.of(endpoint));
when(endpoint.getUrlInfo()).thenReturn(urlInfo);
when(endpoint.getBranch()).thenReturn("branchName");
when(run.getEnvironment(listener)).thenReturn(environment);
Expand All @@ -256,7 +256,7 @@ public void testRunPreviousRunUrlTypeSecret() throws IOException, InterruptedExc
try (MockedStatic<Jenkins> jenkinsMockedStatic = mockStatic(Jenkins.class);
MockedStatic<Utils> utilsMockedStatic = mockStatic(Utils.class)) {
jenkinsMockedStatic.when(Jenkins::getInstanceOrNull).thenReturn(jenkins);
jenkinsMockedStatic.when(Jenkins::getInstance).thenReturn(jenkins);
jenkinsMockedStatic.when(Jenkins::get).thenReturn(jenkins);
utilsMockedStatic
.when(() -> Utils.getSecretUrl("credentialsId", jenkins))
.thenReturn("$secretUrl");
Expand All @@ -273,7 +273,7 @@ public void testRunPreviousRunUrlTypeSecret() throws IOException, InterruptedExc

when(run.getParent()).thenReturn(job);
when(job.getProperty(HudsonNotificationProperty.class)).thenReturn(property);
when(property.getEndpoints()).thenReturn(asList(endpoint));
when(property.getEndpoints()).thenReturn(List.of(endpoint));
when(endpoint.getUrlInfo()).thenReturn(urlInfo);
when(endpoint.getBranch()).thenReturn(".*");
when(run.getEnvironment(listener)).thenReturn(environment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ protected void doPost(HttpServletRequest httpRequest, HttpServletResponse httpRe
doPost(request, httpResponse);
}

protected void doPost(Request request, HttpServletResponse httpResponse) throws IOException {
protected void doPost(Request request, HttpServletResponse httpResponse) {
// noop
}
}
Expand All @@ -147,7 +147,7 @@ static class RedirectHandler extends RecordingServlet {
}

@Override
protected void doPost(Request request, HttpServletResponse httpResponse) throws IOException {
protected void doPost(Request request, HttpServletResponse httpResponse) {
httpResponse.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
httpResponse.setHeader("Location", redirectURI);
}
Expand Down Expand Up @@ -194,7 +194,7 @@ public String getUrl(String path) {
}

@Override
public void setUp() throws Exception {
public void setUp() {
servers = new LinkedList<>();
}

Expand All @@ -206,7 +206,7 @@ public void tearDown() throws Exception {
}

public void testHttpPost() throws Exception {
BlockingQueue<Request> requests = new LinkedBlockingQueue<Request>();
BlockingQueue<Request> requests = new LinkedBlockingQueue<>();

UrlFactory urlFactory = startServer(new RecordingServlet(requests), "/realpath");

Expand All @@ -220,7 +220,7 @@ public void testHttpPost() throws Exception {
}

public void testHttpPostWithBasicAuth() throws Exception {
BlockingQueue<Request> requests = new LinkedBlockingQueue<Request>();
BlockingQueue<Request> requests = new LinkedBlockingQueue<>();

UrlFactory urlFactory = startSecureServer(new RecordingServlet(requests), "/realpath", "fred:foo");

Expand All @@ -235,7 +235,7 @@ public void testHttpPostWithBasicAuth() throws Exception {
}

public void testHttpPostWithRedirects() throws Exception {
BlockingQueue<Request> requests = new LinkedBlockingQueue<Request>();
BlockingQueue<Request> requests = new LinkedBlockingQueue<>();

UrlFactory urlFactory = startServer(new RecordingServlet(requests), "/realpath");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.tikal.hudson.plugins.notification.test;

import com.tikal.hudson.plugins.notification.HostnamePort;
import junit.framework.Assert;
import org.junit.Assert;
import org.junit.Test;

public class HostnamePortTest {
Expand Down