From f48745da35d0f61cfc138f91c4a49469192bc36e Mon Sep 17 00:00:00 2001 From: Martin Pokorny <89339813+mPokornyETM@users.noreply.github.com> Date: Sat, 27 Apr 2024 00:03:21 +0200 Subject: [PATCH] Adapt plugin for Data Table API 2.0.x (#642) --- pom.xml | 5 +- .../tableLabels/table.jelly | 4 +- .../tableQueue/table.jelly | 4 +- .../tableResources/table.jelly | 22 +------ .../lockableresources/LockStepTest.java | 8 +-- ...ckStepTest_manualUnreserveUnblocksJob.java | 6 +- .../LockStepWithRestartTest.java | 4 +- .../LockableResourceApiTest.java | 5 +- .../lockableresources/TestHelpers.java | 57 +++++++++++++------ 9 files changed, 61 insertions(+), 54 deletions(-) diff --git a/pom.xml b/pom.xml index 389024e5e..5142f8efd 100644 --- a/pom.xml +++ b/pom.xml @@ -65,8 +65,8 @@ io.jenkins.tools.bom - bom-2.387.x - 2543.vfb_1a_5fb_9496d + bom-2.440.x + 2977.vdf61ecb_fb_e2d pom import @@ -81,6 +81,7 @@ io.jenkins.plugins data-tables-api + 2.0.5-1 org.jenkins-ci.plugins diff --git a/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableLabels/table.jelly b/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableLabels/table.jelly index 63f44ff37..92107d2cd 100644 --- a/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableLabels/table.jelly +++ b/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableLabels/table.jelly @@ -32,7 +32,7 @@ THE SOFTWARE.
- + diff --git a/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableQueue/table.jelly b/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableQueue/table.jelly index 4f6231c00..468c79953 100644 --- a/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableQueue/table.jelly +++ b/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableQueue/table.jelly @@ -62,7 +62,7 @@ THE SOFTWARE.
${%labels.table.column.labels}${%labels.table.column.labels} ${%labels.table.column.assigned} ${%labels.table.column.free} ${%labels.table.column.percentage}
${%queue.table.column.action} - + diff --git a/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.jelly b/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.jelly index 3ae5ddfba..6b741f072 100644 --- a/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.jelly +++ b/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.jelly @@ -35,7 +35,7 @@ THE SOFTWARE.
${%queue.table.column.request.type} ${%queue.table.column.request.info}${%queue.table.column.reason}${%queue.table.column.reason} ${%queue.table.column.requested.by} ${%queue.table.column.requested.at} ${%queue.table.column.priority}
- - - - - - - - - - - - - - - - - + - + diff --git a/src/test/java/org/jenkins/plugins/lockableresources/LockStepTest.java b/src/test/java/org/jenkins/plugins/lockableresources/LockStepTest.java index 4972e7d3c..0bd111b95 100644 --- a/src/test/java/org/jenkins/plugins/lockableresources/LockStepTest.java +++ b/src/test/java/org/jenkins/plugins/lockableresources/LockStepTest.java @@ -745,9 +745,8 @@ public void unlockButtonWithWaitingRuns() throws Exception { WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("lock('resource1') { semaphore('wait-inside') }", true)); - JenkinsRule.WebClient wc = j.createWebClient(); - WorkflowRun prevBuild = null; + TestHelpers testHelpers = new TestHelpers(); for (int i = 0; i < 3; i++) { WorkflowRun rNext = p.scheduleBuild2(0).waitForStart(); LOGGER.info("start build " + rNext); @@ -756,11 +755,8 @@ public void unlockButtonWithWaitingRuns() throws Exception { j.waitForMessage("[resource1] is locked by build " + prevBuild.getFullDisplayName(), rNext); LOGGER.info("is paused " + rNext); isPaused(rNext, 1, 1); - // List resources = new ArrayList<>(); - // resources.add(LockableResourcesManager.get().fromName("resource1")); LOGGER.info("unlock resource1"); - // LockableResourcesManager.get().unlock(resources, null); - TestHelpers.clickButton(wc, "unlock"); + testHelpers.clickButton("unlock", "resource1"); } j.waitForMessage("Trying to acquire lock on [Resource: resource1]", rNext); diff --git a/src/test/java/org/jenkins/plugins/lockableresources/LockStepTest_manualUnreserveUnblocksJob.java b/src/test/java/org/jenkins/plugins/lockableresources/LockStepTest_manualUnreserveUnblocksJob.java index c44f06abb..d90e26e45 100644 --- a/src/test/java/org/jenkins/plugins/lockableresources/LockStepTest_manualUnreserveUnblocksJob.java +++ b/src/test/java/org/jenkins/plugins/lockableresources/LockStepTest_manualUnreserveUnblocksJob.java @@ -25,9 +25,9 @@ public class LockStepTest_manualUnreserveUnblocksJob extends LockStepTestBase { @Test public void manualUnreserveUnblocksJob() throws Exception { LockableResourcesManager.get().createResource("resource1"); - JenkinsRule.WebClient wc = j.createWebClient(); - TestHelpers.clickButton(wc, "reserve"); + TestHelpers testHelpers = new TestHelpers(); + testHelpers.clickButton("reserve", "resource1"); LockableResource resource1 = LockableResourcesManager.get().fromName("resource1"); assertNotNull(resource1); resource1.setReservedBy("someone"); @@ -45,7 +45,7 @@ public void manualUnreserveUnblocksJob() throws Exception { WorkflowRun r = p.scheduleBuild2(0).waitForStart(); j.waitForMessage("[resource1] is not free, waiting for execution ...", r); j.assertLogNotContains("I am inside", r); - TestHelpers.clickButton(wc, "unreserve"); + testHelpers.clickButton("unreserve", "resource1"); j.waitForMessage("I am inside", r); j.assertLogContains("I am inside", r); j.assertBuildStatusSuccess(j.waitForCompletion(r)); diff --git a/src/test/java/org/jenkins/plugins/lockableresources/LockStepWithRestartTest.java b/src/test/java/org/jenkins/plugins/lockableresources/LockStepWithRestartTest.java index 5aad90593..338d79521 100644 --- a/src/test/java/org/jenkins/plugins/lockableresources/LockStepWithRestartTest.java +++ b/src/test/java/org/jenkins/plugins/lockableresources/LockStepWithRestartTest.java @@ -180,7 +180,9 @@ public void checkQueueAfterRestart() throws Throwable { JenkinsRule.WebClient wc = j.createWebClient(); wc.login("user"); - TestHelpers.clickButton(wc, "unreserve"); + + TestHelpers testHelpers = new TestHelpers(); + testHelpers.clickButton("unreserve", "resource1"); lrm.unreserve(Collections.singletonList(lrm.fromName("resource1"))); diff --git a/src/test/java/org/jenkins/plugins/lockableresources/LockableResourceApiTest.java b/src/test/java/org/jenkins/plugins/lockableresources/LockableResourceApiTest.java index 3a7a12666..bb547f8f0 100644 --- a/src/test/java/org/jenkins/plugins/lockableresources/LockableResourceApiTest.java +++ b/src/test/java/org/jenkins/plugins/lockableresources/LockableResourceApiTest.java @@ -37,9 +37,10 @@ public void reserveUnreserveApi() throws Exception { JenkinsRule.WebClient wc = j.createWebClient(); wc.login("user"); - TestHelpers.clickButton(wc, "reserve"); + TestHelpers testHelpers = new TestHelpers(); + testHelpers.clickButton("reserve", "a1"); assertThat(LockableResourcesManager.get().fromName("a1").isReserved(), is(true)); - TestHelpers.clickButton(wc, "unreserve"); + testHelpers.clickButton("unreserve", "a1"); assertThat(LockableResourcesManager.get().fromName("a1").isReserved(), is(false)); } diff --git a/src/test/java/org/jenkins/plugins/lockableresources/TestHelpers.java b/src/test/java/org/jenkins/plugins/lockableresources/TestHelpers.java index e14cb8fd5..4df135a10 100644 --- a/src/test/java/org/jenkins/plugins/lockableresources/TestHelpers.java +++ b/src/test/java/org/jenkins/plugins/lockableresources/TestHelpers.java @@ -8,19 +8,21 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; +import static org.mockito.Mockito.when; import hudson.model.FreeStyleProject; import hudson.model.Queue; import java.io.IOException; -import java.util.List; import java.util.logging.Logger; import jenkins.model.Jenkins; import net.sf.json.JSONArray; import net.sf.json.JSONObject; -import org.htmlunit.html.HtmlElement; -import org.htmlunit.html.HtmlElementUtil; -import org.htmlunit.html.HtmlPage; +import org.jenkins.plugins.lockableresources.actions.LockableResourcesRootAction; import org.jvnet.hudson.test.JenkinsRule; +import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerResponse; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; public final class TestHelpers { @@ -29,8 +31,18 @@ public final class TestHelpers { private static final int SLEEP_TIME = 100; private static final int MAX_WAIT = 5000; + @Mock + private StaplerRequest req; + + @Mock + private StaplerResponse rsp; + + private AutoCloseable mocks; + // Utility class - private TestHelpers() {} + public TestHelpers() { + this.mocks = MockitoAnnotations.openMocks(this); + } public static void waitForQueue(Jenkins jenkins, FreeStyleProject job) throws InterruptedException { waitForQueue(jenkins, job, Queue.Item.class); @@ -71,20 +83,31 @@ public static JSONObject getApiData(JenkinsRule rule) throws IOException { return rule.getJSON("plugin/lockable-resources/api/json").getJSONObject(); } - // Currently assumes one resource or only clicks the button for the first resource - public static void clickButton(JenkinsRule.WebClient wc, String action) throws Exception { - // disable exceptions, otherwise it will not parse jQuery scripts (used ba DataTable plugin) - wc.getOptions().setThrowExceptionOnScriptError(false); - HtmlPage htmlPage = wc.goTo("lockable-resources"); - List allButtons = htmlPage.getDocumentElement().getElementsByTagName("button"); + /** SImulate the click on the button in the LRM page + * note: Currently does not click on the button. Just simulate the doAction (stapler request) + * on the given resource. + * We shall provide some better solution like selenium tests. But for now it is fine. + */ + public void clickButton(String action, String resourceName) throws Exception { + LOGGER.info(action + " on " + resourceName); + LockableResourcesRootAction doAction = new LockableResourcesRootAction(); + when(req.getMethod()).thenReturn("POST"); + when(req.getParameter("resource")).thenReturn(resourceName); - HtmlElement reserveButton = null; - for (HtmlElement b : allButtons) { - String onClick = b.getAttribute("onClick"); - if (onClick != null && onClick.contains(action)) { - reserveButton = b; + switch (action) { + case "reserve": { + doAction.doReserve(req, rsp); + break; + } + case "unreserve": { + doAction.doUnreserve(req, rsp); + break; + } + case "unlock": { + LOGGER.info("doUnlock"); + doAction.doUnlock(req, rsp); + break; } } - HtmlElementUtil.click(reserveButton); } }
${%resources.table.column.index}${%resources.table.column.resource}${%resources.table.column.resource} ${%resources.table.column.status} ${%resources.table.column.timestamp}${%resources.table.column.labels}${%resources.table.column.labels} ${%resources.table.column.properties} ${%resources.table.column.action}