Skip to content

Commit 85355f1

Browse files
Merge pull request #66 from strangelookingnerd/migrate_to_junit5
Migrate tests to JUnit5
2 parents c88207a + a4387fd commit 85355f1

File tree

2 files changed

+52
-91
lines changed

2 files changed

+52
-91
lines changed

src/test/java/com/github/farmgeek4life/jenkins/negotiatesso/NegSecFilterTests.java

+18-26
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
11
package com.github.farmgeek4life.jenkins.negotiatesso;
22

33
import jakarta.servlet.http.HttpServletRequest;
4-
import static org.junit.Assert.assertFalse;
5-
import static org.junit.Assert.assertTrue;
6-
import org.junit.Rule;
74

8-
import org.junit.Test;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
9+
import org.junit.jupiter.api.Test;
910
import org.jvnet.hudson.test.JenkinsRule;
11+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1012
import org.mockito.Mockito;
1113

12-
public class NegSecFilterTests {
13-
/**
14-
* Time limit in seconds for timed tests.
15-
*/
16-
public static final int TIME_LIMIT = 10;
17-
18-
/**
19-
* Jenkins rule instance.
20-
*/
21-
// CS IGNORE VisibilityModifier FOR NEXT 3 LINES. REASON: Mocks tests.
22-
@Rule
23-
public JenkinsRule rule = new JenkinsRule();
14+
@WithJenkins
15+
class NegSecFilterTests {
2416

2517
@Test
26-
public void test_cleanRequest() {
27-
assertTrue(NegSecFilter.cleanRequest("http://host:8080/whoAmI").equals("/whoAmI"));
28-
assertTrue(NegSecFilter.cleanRequest("http://host/whoAmI").equals("/whoAmI"));
29-
assertTrue(NegSecFilter.cleanRequest("http://host/securityRealm").equals("/securityRealm"));
30-
assertTrue(NegSecFilter.cleanRequest("https://host:8080/job/jobName").equals("/job/jobName"));
31-
assertTrue(NegSecFilter.cleanRequest("http://host:8080/git/notifyCommit?url=http://gitserver/gitrepo.git").equals("/git/notifyCommit"));
32-
assertTrue(NegSecFilter.cleanRequest("/whoAmI").equals("/whoAmI"));
33-
assertTrue(NegSecFilter.cleanRequest("/git/notifyCommit?url=http://gitserver/gitrepo.git").equals("/git/notifyCommit"));
18+
void test_cleanRequest(JenkinsRule rule) {
19+
assertEquals("/whoAmI", NegSecFilter.cleanRequest("http://host:8080/whoAmI"));
20+
assertEquals("/whoAmI", NegSecFilter.cleanRequest("http://host/whoAmI"));
21+
assertEquals("/securityRealm", NegSecFilter.cleanRequest("http://host/securityRealm"));
22+
assertEquals("/job/jobName", NegSecFilter.cleanRequest("https://host:8080/job/jobName"));
23+
assertEquals("/git/notifyCommit", NegSecFilter.cleanRequest("http://host:8080/git/notifyCommit?url=http://gitserver/gitrepo.git"));
24+
assertEquals("/whoAmI", NegSecFilter.cleanRequest("/whoAmI"));
25+
assertEquals("/git/notifyCommit", NegSecFilter.cleanRequest("/git/notifyCommit?url=http://gitserver/gitrepo.git"));
3426
}
3527

3628
@Test
37-
public void test_shouldAttemptAuthentication() {
29+
void test_shouldAttemptAuthentication(JenkinsRule rule) {
3830
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
3931

4032
assertTrue(NegSecFilter.shouldAttemptAuthentication(rule.jenkins, request, "/job/SomeJob"));
@@ -50,7 +42,7 @@ public void test_shouldAttemptAuthentication() {
5042
}
5143

5244
@Test
53-
public void test_shouldNotAttemptAuthentication() {
45+
void test_shouldNotAttemptAuthentication(JenkinsRule rule) {
5446
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
5547
Mockito.when(request.getParameter("encrypt")).thenReturn("true");
5648

src/test/java/com/github/farmgeek4life/jenkins/negotiatesso/NegotiateConfigTests.java

+34-65
Original file line numberDiff line numberDiff line change
@@ -27,101 +27,70 @@
2727
*/
2828
package com.github.farmgeek4life.jenkins.negotiatesso;
2929

30-
import org.htmlunit.FailingHttpStatusCodeException;
3130
import org.htmlunit.html.HtmlElement;
3231
import org.htmlunit.html.HtmlForm;
3332
import org.htmlunit.html.HtmlPage;
34-
import org.junit.Rule;
35-
import org.junit.Test;
36-
import org.junit.runners.model.Statement;
37-
import org.jvnet.hudson.test.RestartableJenkinsRule;
38-
import org.jvnet.hudson.test.RestartableJenkinsRule.Step;
33+
import org.junit.jupiter.api.Test;
34+
import org.jvnet.hudson.test.JenkinsRule;
35+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
3936

40-
import static org.junit.Assert.assertFalse;
41-
import static org.junit.Assert.assertNotNull;
42-
import static org.junit.Assert.assertTrue;
37+
import static hudson.Functions.isWindows;
38+
import static org.junit.jupiter.api.Assertions.assertFalse;
39+
import static org.junit.jupiter.api.Assertions.assertNotNull;
40+
import static org.junit.jupiter.api.Assertions.assertTrue;
4341

4442
/**
4543
* UI tests for the global configuration page of the plugin.
4644
* @author Bryson Gibbons
4745
*/
48-
public class NegotiateConfigTests {
49-
/**
50-
* Time limit in seconds for timed tests.
51-
*/
52-
public static final int TIME_LIMIT = 10;
53-
54-
/**
55-
* Jenkins rule instance.
56-
*/
57-
// CS IGNORE VisibilityModifier FOR NEXT 3 LINES. REASON: Mocks tests.
58-
@Rule
59-
public RestartableJenkinsRule rule = new RestartableJenkinsRule();
60-
61-
/**
62-
*
63-
*/
64-
private boolean IsWindowsOS() {
65-
return System.getProperty("os.name").toLowerCase().contains("win");
66-
}
46+
@WithJenkins
47+
class NegotiateConfigTests {
6748

6849
/**
6950
* Tests if Negotiate SSO plugin has a section on the global config page.
7051
*/
7152
@Test
72-
public void testNegotiateHasConfigPage() {
73-
rule.then(r -> {
74-
HtmlPage currentPage = rule.j.createWebClient().goTo("configureSecurity");
75-
HtmlElement enabled = currentPage.getElementByName("_.enabled");
76-
assertNotNull("Negotiate configuration page missing.", enabled);
77-
});
78-
53+
void testNegotiateHasConfigPage(JenkinsRule rule) throws Exception {
54+
HtmlPage currentPage = rule.createWebClient().goTo("configureSecurity");
55+
HtmlElement enabled = currentPage.getElementByName("_.enabled");
56+
assertNotNull(enabled, "Negotiate configuration page missing.");
7957
}
8058

8159
/**
8260
* Tests if Negotiate SSO plugin block can be expanded.
8361
*/
8462
@Test
85-
public void testEnableNegotiate() {
86-
rule.then(r -> {
87-
HtmlPage currentPage = rule.j.createWebClient().goTo("configureSecurity");
88-
HtmlElement enabled = currentPage.getElementByName("_.enabled");
89-
enabled.fireEvent("click");
90-
assertNotNull("Optional block wasn't expanded.", currentPage.getElementByName("_.redirectEnabled"));
91-
});
63+
void testEnableNegotiate(JenkinsRule rule) throws Exception {
64+
HtmlPage currentPage = rule.createWebClient().goTo("configureSecurity");
65+
HtmlElement enabled = currentPage.getElementByName("_.enabled");
66+
enabled.fireEvent("click");
67+
assertNotNull(currentPage.getElementByName("_.redirectEnabled"), "Optional block wasn't expanded.");
9268
}
9369

9470
/**
9571
* Tests if the NegotiateSSO class changes attributes if a new config is submitted.
9672
* @throws Exception if something goes wrong
9773
*/
9874
@Test
99-
public void testIfConfigCanBeUpdated() throws Exception {
100-
rule.then(r -> {
101-
assertFalse("Plugin already enabled", NegotiateSSO.getInstance().getEnabled());
75+
void testIfConfigCanBeUpdated(JenkinsRule rule) throws Exception {
76+
assertFalse(NegotiateSSO.getInstance().getEnabled(), "Plugin already enabled");
10277

103-
HtmlPage currentPage = rule.j.createWebClient().goTo("configureSecurity");
104-
HtmlForm form = currentPage.getFormByName("config");
105-
assertNotNull(form);
78+
HtmlPage currentPage = rule.createWebClient().goTo("configureSecurity");
79+
HtmlForm form = currentPage.getFormByName("config");
80+
assertNotNull(form);
10681

107-
form.getInputByName("_.enabled").click();
108-
form.getSelectByName("_.principalFormat").setSelectedAttribute("both", true);
109-
form.getSelectByName("_.roleFormat").setSelectedAttribute("sid", true);
82+
form.getInputByName("_.enabled").click();
83+
form.getSelectByName("_.principalFormat").setSelectedAttribute("both", true);
84+
form.getSelectByName("_.roleFormat").setSelectedAttribute("sid", true);
11085

111-
try {
112-
rule.j.submit(form);
113-
// CS IGNORE EmptyBlock FOR NEXT 3 LINES. REASON: Mocks Tests.
114-
} catch (FailingHttpStatusCodeException e) {
115-
// Expected since filter cannot be added to Jenkins rule.
116-
}
86+
rule.submit(form);
11787

118-
boolean wasEnabled = NegotiateSSO.getInstance().getEnabled();
119-
if (IsWindowsOS()) {
120-
assertTrue("Plugin wasn't enabled after saving the new config", wasEnabled);
121-
}
122-
else {
123-
assertFalse("Plugin was enabled on a non-Windows OS", wasEnabled);
124-
}
125-
});
88+
boolean wasEnabled = NegotiateSSO.getInstance().getEnabled();
89+
if (isWindows()) {
90+
assertTrue(wasEnabled, "Plugin wasn't enabled after saving the new config");
91+
}
92+
else {
93+
assertFalse(wasEnabled, "Plugin was enabled on a non-Windows OS");
94+
}
12695
}
12796
}

0 commit comments

Comments
 (0)