Skip to content

Commit

Permalink
Merge pull request #528 from strangelookingnerd/migrate_to_junit5
Browse files Browse the repository at this point in the history
Migrate tests to JUnit5
  • Loading branch information
michael-doubez authored Feb 18, 2025
2 parents dfcd90e + a1dd3c1 commit 687d73d
Show file tree
Hide file tree
Showing 21 changed files with 628 additions and 687 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>4051.v78dce3ce8b_d6</version>
<version>4136.vca_c3202a_7fd1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,60 @@
package org.jenkinsci.plugins.oic;

import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import hudson.security.SecurityRealm;
import hudson.util.Secret;
import io.jenkins.plugins.casc.ConfigurationContext;
import io.jenkins.plugins.casc.ConfiguratorRegistry;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
import io.jenkins.plugins.casc.model.CNode;
import java.util.ArrayList;
import java.util.List;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.oic.OicSecurityRealm.TokenAuthMethod;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static io.jenkins.plugins.casc.misc.Util.getJenkinsRoot;
import static io.jenkins.plugins.casc.misc.Util.toStringFromYamlFile;
import static io.jenkins.plugins.casc.misc.Util.toYamlString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ConfigurationAsCodeTest {
@WithJenkinsConfiguredWithCode
class ConfigurationAsCodeTest {

@Rule(order = 1)
public final JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
@RegisterExtension
static WellKnownMockExtension wellKnownMockExtension = new WellKnownMockExtension(
"MOCK_PORT",
"{\"issuer\": \"http://localhost:%1$d/\","
+ "\"authorization_endpoint\": \"http://localhost:%1$d/authorize\","
+ "\"token_endpoint\":\"http://localhost:%1$d/token\","
+ "\"userinfo_endpoint\":\"http://localhost:%1$d/user\","
+ "\"jwks_uri\":\"http://localhost:%1$d/jwks\","
+ "\"scopes_supported\": [\"openid\",\"email\"],"
+ "\"subject_types_supported\": [\"public\"],"
+ "\"end_session_endpoint\":\"http://localhost:%1$d/logout\"}");

@Test
@ConfiguredWithCode("ConfigurationAsCode.yml")
public void testConfig() {
void testConfig(JenkinsConfiguredWithCodeRule j) {
SecurityRealm realm = Jenkins.get().getSecurityRealm();

assertTrue(realm instanceof OicSecurityRealm);
assertInstanceOf(OicSecurityRealm.class, realm);
OicSecurityRealm oicSecurityRealm = (OicSecurityRealm) realm;

OicServerManualConfiguration serverConf =
Expand Down Expand Up @@ -72,7 +86,7 @@ public void testConfig() {

@Test
@ConfiguredWithCode("ConfigurationAsCode.yml")
public void testExport() throws Exception {
void testExport(JenkinsConfiguredWithCodeRule j) throws Exception {
ConfigurationContext context = new ConfigurationContext(ConfiguratorRegistry.get());

CNode yourAttribute =
Expand All @@ -97,10 +111,10 @@ public void testExport() throws Exception {

@Test
@ConfiguredWithCode("ConfigurationAsCodeMinimal.yml")
public void testMinimal() throws Exception {
void testMinimal(JenkinsConfiguredWithCodeRule j) {
SecurityRealm realm = Jenkins.get().getSecurityRealm();

assertTrue(realm instanceof OicSecurityRealm);
assertInstanceOf(OicSecurityRealm.class, realm);
OicSecurityRealm oicSecurityRealm = (OicSecurityRealm) realm;
OicServerManualConfiguration serverConf =
(OicServerManualConfiguration) oicSecurityRealm.getServerConfiguration();
Expand All @@ -120,25 +134,13 @@ public void testMinimal() throws Exception {
assertEquals("sub", oicSecurityRealm.getUserNameField());
assertTrue(oicSecurityRealm.isLogoutFromOpenidProvider());
assertFalse(oicSecurityRealm.isRootURLFromRequest());
assertEquals(null, serverConf.getJwksServerUrl());
assertNull(serverConf.getJwksServerUrl());
assertFalse(oicSecurityRealm.isDisableTokenVerification());
}

@Rule(order = 0)
public final WellKnownMockRule wellKnownMockRule = new WellKnownMockRule(
"MOCK_PORT",
"{\"issuer\": \"http://localhost:%1$d/\","
+ "\"authorization_endpoint\": \"http://localhost:%1$d/authorize\","
+ "\"token_endpoint\":\"http://localhost:%1$d/token\","
+ "\"userinfo_endpoint\":\"http://localhost:%1$d/user\","
+ "\"jwks_uri\":\"http://localhost:%1$d/jwks\","
+ "\"scopes_supported\": [\"openid\",\"email\"],"
+ "\"subject_types_supported\": [\"public\"],"
+ "\"end_session_endpoint\":\"http://localhost:%1$d/logout\"}");

@Test
@ConfiguredWithCode("ConfigurationAsCodeMinimalWellKnown.yml")
public void testMinimalWellKnown() throws Exception {
void testMinimalWellKnown(JenkinsConfiguredWithCodeRule j) {
SecurityRealm realm = Jenkins.get().getSecurityRealm();
assertThat(realm, instanceOf(OicSecurityRealm.class));
OicSecurityRealm oicSecurityRealm = (OicSecurityRealm) realm;
Expand All @@ -147,7 +149,7 @@ public void testMinimalWellKnown() throws Exception {
OicServerWellKnownConfiguration serverConf =
(OicServerWellKnownConfiguration) oicSecurityRealm.getServerConfiguration();

String urlBase = String.format("http://localhost:%d", wellKnownMockRule.port());
String urlBase = String.format("http://localhost:%d", wellKnownMockExtension.getPort());

assertFalse(oicSecurityRealm.isDisableSslVerification());
assertNull(oicSecurityRealm.getEmailFieldName());
Expand All @@ -165,33 +167,33 @@ public void testMinimalWellKnown() throws Exception {
assertEquals(urlBase + "/well.known", serverConf.getWellKnownOpenIDConfigurationUrl());
}

/** Class to setup WireMockRule for well known with stub and setting port in env variable
/** Class to setup WellKnownMockExtension for well known with stub and setting port in env variable
*/
public class WellKnownMockRule extends WireMockRule {
public static class WellKnownMockExtension extends WireMockExtension {
private final String mockPortEnvName;
private final String wellKnownAnswer;
private String previousEnvValue;

public WellKnownMockRule(String mockPortEnvName, String wellKnownAnswer) {
super(new WireMockConfiguration().dynamicPort(), true);
public WellKnownMockExtension(String mockPortEnvName, String wellKnownAnswer) {
super(WireMockExtension.newInstance()
.failOnUnmatchedRequests(true)
.options(wireMockConfig().dynamicPort()));
this.mockPortEnvName = mockPortEnvName;
this.wellKnownAnswer = wellKnownAnswer;
}

@Override
protected void before() {
protected void onBeforeAll(ExtensionContext context, WireMockRuntimeInfo info) {
this.previousEnvValue = System.getProperty(this.mockPortEnvName);
System.setProperty(this.mockPortEnvName, String.valueOf(port()));
System.setProperty(this.mockPortEnvName, String.valueOf(getPort()));
stubFor(get(urlPathEqualTo("/well.known"))
.willReturn(aResponse()
.withHeader("Content-Type", "text/html; charset=utf-8")
.withBody(String.format(this.wellKnownAnswer, port()))));
super.before();
.withBody(String.format(this.wellKnownAnswer, getPort()))));
}

@Override
protected void after() {
super.after();
protected void onAfterAll(ExtensionContext context, WireMockRuntimeInfo info) {
if (this.previousEnvValue != null) {
System.setProperty(this.mockPortEnvName, this.previousEnvValue);
} else {
Expand Down
39 changes: 18 additions & 21 deletions src/test/java/org/jenkinsci/plugins/oic/DescriptorImplTest.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
package org.jenkinsci.plugins.oic;

import hudson.util.FormValidation;
import java.io.IOException;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.oic.OicSecurityRealm.DescriptorImpl;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class DescriptorImplTest {

@Rule
public JenkinsRule jenkinsRule = new JenkinsRule();
@WithJenkins
class DescriptorImplTest {

private Jenkins jenkins;

@Before
public void setUp() {
@BeforeEach
void setUp(JenkinsRule jenkinsRule) {
jenkins = jenkinsRule.getInstance();
}

@Test
public void testOicSecurityRealmDescriptorImplManual() throws Exception {
void testOicSecurityRealmDescriptorImplManual() throws Exception {
OicSecurityRealm.DescriptorImpl descriptor = (DescriptorImpl) jenkins.getDescriptor(OicSecurityRealm.class);

assertNotNull(descriptor);
Expand Down Expand Up @@ -56,7 +53,7 @@ public void testOicSecurityRealmDescriptorImplManual() throws Exception {
}

@Test
public void testOicSecurityRealmDescriptorImplAuto() throws Exception {
void testOicSecurityRealmDescriptorImplAuto() throws Exception {
OicSecurityRealm.DescriptorImpl descriptor =
(DescriptorImpl) jenkins.getDescriptorOrDie(OicSecurityRealm.class);

Expand Down Expand Up @@ -84,7 +81,7 @@ public void testOicSecurityRealmDescriptorImplAuto() throws Exception {
}

@Test
public void doCheckUserNameField() throws IOException {
void doCheckUserNameField() {
OicSecurityRealm.DescriptorImpl descriptor =
(DescriptorImpl) jenkins.getDescriptorOrDie(OicSecurityRealm.class);

Expand All @@ -98,7 +95,7 @@ public void doCheckUserNameField() throws IOException {
}

@Test
public void doCheckFullNameFieldName() throws IOException {
void doCheckFullNameFieldName() {
OicSecurityRealm.DescriptorImpl descriptor =
(DescriptorImpl) jenkins.getDescriptorOrDie(OicSecurityRealm.class);

Expand All @@ -108,7 +105,7 @@ public void doCheckFullNameFieldName() throws IOException {
}

@Test
public void doCheckEmailFieldName() throws IOException {
void doCheckEmailFieldName() {
OicSecurityRealm.DescriptorImpl descriptor =
(DescriptorImpl) jenkins.getDescriptorOrDie(OicSecurityRealm.class);

Expand All @@ -118,7 +115,7 @@ public void doCheckEmailFieldName() throws IOException {
}

@Test
public void doCheckGroupsFieldName() throws IOException {
void doCheckGroupsFieldName() {
OicSecurityRealm.DescriptorImpl descriptor =
(DescriptorImpl) jenkins.getDescriptorOrDie(OicSecurityRealm.class);

Expand All @@ -128,7 +125,7 @@ public void doCheckGroupsFieldName() throws IOException {
}

@Test
public void doCheckTokenFieldToCheckKey() throws IOException {
void doCheckTokenFieldToCheckKey() {
OicSecurityRealm.DescriptorImpl descriptor =
(DescriptorImpl) jenkins.getDescriptorOrDie(OicSecurityRealm.class);

Expand All @@ -138,7 +135,7 @@ public void doCheckTokenFieldToCheckKey() throws IOException {
}

@Test
public void doCheckPostLogoutRedirectUrl() throws IOException {
void doCheckPostLogoutRedirectUrl() {
OicSecurityRealm.DescriptorImpl descriptor =
(DescriptorImpl) jenkins.getDescriptorOrDie(OicSecurityRealm.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class EscapeHatchCrumbExclusionTest {
class EscapeHatchCrumbExclusionTest {
private EscapeHatchCrumbExclusion crumb = new EscapeHatchCrumbExclusion();

private HttpServletResponse response = null;
Expand All @@ -28,24 +26,21 @@ public String getPathInfo() {
}

@Test
public void process_WithNullPath() throws IOException, ServletException {
void process_WithNullPath() throws IOException, ServletException {
MockHttpServletRequest request = newRequestWithPath("");
assertFalse(crumb.process(request, response, chain));
}

@Test
public void process_WithWrongPath() throws IOException, ServletException {
void process_WithWrongPath() throws IOException, ServletException {
MockHttpServletRequest request = newRequestWithPath("fictionalPath");
assertFalse(crumb.process(request, response, chain));
}

@Test
public void process_WithGoodPath() throws IOException, ServletException {
chain = new FilterChain() {
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1) throws IOException, ServletException {
// do nothing
}
void process_WithGoodPath() throws IOException, ServletException {
chain = (arg0, arg1) -> {
// do nothing
};

MockHttpServletRequest request = newRequestWithPath("/securityRealm/escapeHatch");
Expand Down
Loading

0 comments on commit 687d73d

Please sign in to comment.