Skip to content

Commit

Permalink
Merge pull request #51 from BorisYaoA/Adapt_notification_plugin_for_j…
Browse files Browse the repository at this point in the history
…etty12_ee8

[JENKINS-73148] Adapt notification plugin for jetty12 EE8
  • Loading branch information
BorisYaoA authored Oct 3, 2024
2 parents a312806 + a0d3d46 commit 576cbbc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
5 changes: 4 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/usr/bin/env groovy

/* `buildPlugin` step provided by: https://github.com/jenkins-infra/pipeline-library */
buildPlugin()
buildPlugin(useContainerAgent: true, configurations: [
[platform: 'linux', jdk: 21],
[platform: 'windows', jdk: 17]
])
23 changes: 9 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.40</version>
<version>4.86</version>
<relativePath />
</parent>

Expand All @@ -21,8 +21,11 @@
<properties>
<revision>1.18</revision>
<changelist>-SNAPSHOT</changelist>
<jenkins.version>2.289.3</jenkins.version>
<jenkins.version>2.472</jenkins.version>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<!-- TODO JENKINS-73339 until in parent POM -->
<jenkins-test-harness.version>2250.v03a_1295b_0a_30</jenkins-test-harness.version>
<maven.compiler.release>17</maven.compiler.release>
</properties>

<repositories>
Expand Down Expand Up @@ -55,8 +58,8 @@
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.289.x</artifactId>
<version>1362.v59f2f3db_80ee</version>
<artifactId>bom-2.462.x</artifactId>
<version>3258.vcdcf15936a_fd</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand All @@ -66,9 +69,8 @@
<dependencies>
<!-- http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.google.code.gson%22%20AND%20a%3A%22gson%22 -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
<groupId>io.jenkins.plugins</groupId>
<artifactId>gson-api</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down Expand Up @@ -126,13 +128,6 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import hudson.Util;
import hudson.model.Run;
import hudson.model.TaskListener;
import org.eclipse.collections.impl.factory.Sets;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
Expand Down Expand Up @@ -123,8 +122,7 @@ public String getDisplayName() {

@Override
public Set<? extends Class<?>> getRequiredContext() {
return Sets.immutable.of(FilePath.class, FlowNode.class, Run.class, TaskListener.class)
.castToSet();
return Set.of(FilePath.class, FlowNode.class, Run.class, TaskListener.class);

Check warning on line 125 in src/main/java/com/tikal/hudson/plugins/notification/NotifyStep.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 125 is not covered by tests
}
}
}
34 changes: 22 additions & 12 deletions src/test/java/com/tikal/hudson/plugins/notification/PhaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ public class PhaseTest {
@Mock
private EnvVars environment;
@Mock
private Protocol protocol;
@Mock
private Format format;
@Mock
private PrintStream logger;
@Mock
private Jenkins jenkins;
Expand Down Expand Up @@ -163,6 +159,16 @@ public void testRunPreviousRunUrlTypePublic() throws IOException, InterruptedExc
jenkinsMockedStatic.when(Jenkins::getInstanceOrNull).thenReturn(jenkins);
jenkinsMockedStatic.when(Jenkins::getInstance).thenReturn(jenkins);

Protocol httpProtocolSpy = spy(Protocol.HTTP);
when(endpoint.getProtocol()).thenReturn(httpProtocolSpy);
doNothing().when(httpProtocolSpy).send(anyString(), any(byte[].class), anyInt(), anyBoolean());

Format jsonFormatSpy = spy(Format.JSON);
JobState jobState = new JobState();
when(endpoint.getFormat()).thenReturn(jsonFormatSpy);
doReturn(data).when(jsonFormatSpy).serialize(isA(JobState.class));
assertEquals(data, jsonFormatSpy.serialize(jobState));

when(run.getParent()).thenReturn(job);
when(job.getProperty(HudsonNotificationProperty.class)).thenReturn(property);
when(property.getEndpoints()).thenReturn(asList(endpoint));
Expand All @@ -175,15 +181,12 @@ public void testRunPreviousRunUrlTypePublic() throws IOException, InterruptedExc
when(environment.containsKey("BRANCH_NAME")).thenReturn(true);
when(environment.get("BRANCH_NAME")).thenReturn("branchName");
when(listener.getLogger()).thenReturn(logger);
when(endpoint.getProtocol()).thenReturn(protocol);
when(endpoint.getTimeout()).thenReturn(42);
when(endpoint.getFormat()).thenReturn(format);
when(format.serialize(isA(JobState.class))).thenReturn(data);

Phase.STARTED.handle(run, listener, 1L);

verify(logger).printf("Notifying endpoint with %s%n", "url 'expandedUrl'");
verify(protocol).send("expandedUrl", data, 42, false);
verify(httpProtocolSpy).send("expandedUrl", data, 42, false);
verify(run).getPreviousCompletedBuild();
}
}
Expand All @@ -197,6 +200,16 @@ public void testRunPreviousRunUrlTypeSecret() throws IOException, InterruptedExc
jenkinsMockedStatic.when(Jenkins::getInstance).thenReturn(jenkins);
utilsMockedStatic.when(() -> Utils.getSecretUrl("credentialsId", jenkins)).thenReturn("$secretUrl");

Protocol httpProtocolSpy = spy(Protocol.HTTP);
when(endpoint.getProtocol()).thenReturn(httpProtocolSpy);
doNothing().when(httpProtocolSpy).send(anyString(), any(byte[].class), anyInt(), anyBoolean());

Format jsonFormatSpy = spy(Format.JSON);
JobState jobState = new JobState();
when(endpoint.getFormat()).thenReturn(jsonFormatSpy);
doReturn(data).when(jsonFormatSpy).serialize(isA(JobState.class));
assertEquals(data, jsonFormatSpy.serialize(jobState));

when(run.getParent()).thenReturn(job);
when(job.getProperty(HudsonNotificationProperty.class)).thenReturn(property);
when(property.getEndpoints()).thenReturn(asList(endpoint));
Expand All @@ -208,15 +221,12 @@ public void testRunPreviousRunUrlTypeSecret() throws IOException, InterruptedExc
when(urlInfo.getUrlType()).thenReturn(SECRET);
when(environment.expand("$secretUrl")).thenReturn("secretUrl");
when(listener.getLogger()).thenReturn(logger);
when(endpoint.getProtocol()).thenReturn(protocol);
when(endpoint.getTimeout()).thenReturn(42);
when(endpoint.getFormat()).thenReturn(format);
when(format.serialize(isA(JobState.class))).thenReturn(data);

Phase.STARTED.handle(run, listener, 1L);

verify(logger).printf( "Notifying endpoint with %s%n","credentials id 'credentialsId'");
verify(protocol).send("secretUrl", data, 42, false);
verify(httpProtocolSpy).send("secretUrl", data, 42, false);
verify(run).getPreviousCompletedBuild();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
*/
package com.tikal.hudson.plugins.notification;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.io.CharStreams;
import junit.framework.TestCase;
import org.eclipse.jetty.ee8.servlet.ServletContextHandler;
import org.eclipse.jetty.ee8.servlet.ServletHolder;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
Expand Down Expand Up @@ -92,7 +93,7 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return Objects.toStringHelper(this)
return MoreObjects.toStringHelper(this)
.add("url", url)
.add("method", method)
.add("body", body)
Expand Down Expand Up @@ -170,9 +171,10 @@ private UrlFactory startSecureServer(Servlet servlet, String path, String author
ServerConnector connector = new ServerConnector(server);
server.setConnectors(new Connector[] {connector});

ServletHandler servletHandler = new ServletHandler();
server.setHandler(servletHandler);
servletHandler.addServletWithMapping(new ServletHolder(servlet), path);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addServlet(new ServletHolder(servlet), path);
server.setHandler(context);

server.start();
servers.add(server);
Expand Down

0 comments on commit 576cbbc

Please sign in to comment.