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

refactor: JUnit Jupiter migration from JUnit 4.x #1669

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
19 changes: 6 additions & 13 deletions env/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,6 @@
<artifactId>spotbugs-annotations</artifactId>
<optional>true</optional>
</dependency>

<!-- test utilities -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand All @@ -80,6 +68,11 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand All @@ -88,7 +81,7 @@
<dependency>
<groupId>io.github.hakky54</groupId>
<artifactId>logcaptor</artifactId>
<version>2.9.3</version>
<version>${logcaptor.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/
package com.sap.cloud.security.config;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.util.Collections;
Expand All @@ -19,7 +19,7 @@ public class OAuth2ServiceConfigurationBuilderTest {

private OAuth2ServiceConfigurationBuilder cut;

@Before
@BeforeEach
public void setUp() {
cut = OAuth2ServiceConfigurationBuilder.forService(Service.XSUAA);
}
Expand Down Expand Up @@ -221,4 +221,4 @@ public void x509IntegrationTest() {
assertThat(configuration.getCertUrl()).isEqualTo(URI.create(url));
assertThat(configuration.getCredentialType()).isEqualTo(CredentialType.X509);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/
package com.sap.cloud.security.json;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.time.LocalDate;
Expand All @@ -31,7 +31,7 @@ public class DefaultJsonObjectTest {

private DefaultJsonObject cut;

@Before
@BeforeEach
public void setUp() {
cut = createJsonParser(KEY_1, STRING_VALUE);
}
Expand Down Expand Up @@ -227,7 +227,7 @@ public void notVulnerableToStackOverflowException() {
try {
new DefaultJsonObject(deeplyNestedJsonObject);
} catch (StackOverflowError e) {
Assert.fail("Encountered StackoverflowError.");
Assertions.fail("Encountered StackoverflowError.");
} catch (Exception ignored) {
}
}
Expand All @@ -251,4 +251,4 @@ private DefaultJsonObject createJsonParser(String key, Object value) {
private String createJsonObjectString(String key, Object value) {
return String.format("{%s : %s}", key, value);
}
}
}
17 changes: 5 additions & 12 deletions java-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,6 @@
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>

<!-- test utilities -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand All @@ -65,6 +53,11 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/
package com.sap.cloud.security.config;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

public class ClientCredentialsTest {
ClientIdentity cut = new ClientCredentials("clientId", "clientSecret");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
*/
package com.sap.cloud.security.config;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class CredentialTypeTest {

@Test
public void from() {
Assert.assertEquals(CredentialType.X509, CredentialType.from("x509"));
Assert.assertEquals(CredentialType.X509_GENERATED, CredentialType.from("X509_GENERATED"));
Assert.assertEquals(CredentialType.X509_PROVIDED, CredentialType.from("X509_PROVIDED"));
Assert.assertEquals(CredentialType.X509_ATTESTED, CredentialType.from("X509_ATTESTED"));
Assert.assertEquals(CredentialType.X509_ATTESTED, CredentialType.from("x509_attested"));
Assert.assertEquals(CredentialType.INSTANCE_SECRET, CredentialType.from("instance-secret"));
Assert.assertEquals(CredentialType.BINDING_SECRET, CredentialType.from("binding-secret"));
Assertions.assertEquals(CredentialType.X509, CredentialType.from("x509"));
Assertions.assertEquals(CredentialType.X509_GENERATED, CredentialType.from("X509_GENERATED"));
Assertions.assertEquals(CredentialType.X509_PROVIDED, CredentialType.from("X509_PROVIDED"));
Assertions.assertEquals(CredentialType.X509_ATTESTED, CredentialType.from("X509_ATTESTED"));
Assertions.assertEquals(CredentialType.X509_ATTESTED, CredentialType.from("x509_attested"));
Assertions.assertEquals(CredentialType.INSTANCE_SECRET, CredentialType.from("instance-secret"));
Assertions.assertEquals(CredentialType.BINDING_SECRET, CredentialType.from("binding-secret"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
package com.sap.cloud.security.config;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -21,4 +21,4 @@ public void getCFNameOfXsuaa_shouldReturnCorrectName() {
assertThat(Service.XSUAA.getCFName()).isEqualTo("xsuaa");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/
package com.sap.cloud.security.token;

import org.junit.Test;
import org.junit.jupiter.api.Test;

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

public class TokenTest {

Expand Down
2 changes: 1 addition & 1 deletion java-security-it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<!-- Run with mvn failsafe:integration-test -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M4</version>
<version>${maven-failsafe-plugin.version}}</version>
<configuration>
<includes>
<include>**/*.java</include>
Expand Down
1 change: 1 addition & 0 deletions java-security-test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ It can be used to generate tokens with custom properties and claims, that, toget
### JUnit 4 Test
Set up a [SecurityTestRule](./src/main/java/com/sap/cloud/security/test/SecurityTestRule.java) with the different configuration methods it provides. It acts as an `ExternalResource` that starts the `WireMock` server and optionally a [Jetty servlet container](#jetty-application-server) before the tests.
> :exclamation: Make sure to call `tearDown` after the tests to stop the servers and free resources.
> :exclamation: Mplease consider migrating to JUnit5 and use an Extension instead of a rule.

The following code is an example how to mount a Servlet on the embedded Jetty servet container and test access to its endpoint with a valid token generated by [JwtGenerator](#jwt-generation).

Expand Down
15 changes: 11 additions & 4 deletions java-security-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
<groupId>com.sap.cloud.security</groupId>
<artifactId>java-security</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down Expand Up @@ -59,6 +55,12 @@
<artifactId>spotbugs-annotations</artifactId>
<optional>true</optional>
</dependency>
<!--scope provided as we want to get rid of JUnit4 by still allowing to use the existing SecurityTestRule-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>provided</scope>
</dependency>

<!-- test dependencies -->
<dependency>
Expand All @@ -76,6 +78,11 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

import javax.annotation.Nullable;

@Deprecated(since = "3.6.0", forRemoval = true)
/**
* @deprecated since 3.6.0. Use {@link XsuaaExtension.class} or {@link IasExtension.class} in a JUnit5 test instead.
*/
public class SecurityTestRule extends ExternalResource
implements SecurityTestContext, ServiceMockConfiguration, ApplicationServerConfiguration {

Expand Down Expand Up @@ -137,4 +141,4 @@ protected void after() {
base.tearDown();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,11 @@
import com.sap.cloud.security.token.Token;
import com.sap.cloud.security.token.TokenClaims;
import com.sap.cloud.security.token.TokenHeader;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
Expand Down Expand Up @@ -50,17 +44,12 @@ public class JwtGeneratorTest {
private static RSAKeys keys;
private JwtGenerator cut;

private static final Path RESOURCES_PATH = Paths.get(JwtGeneratorTest.class.getResource("/").getPath());

@ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder(RESOURCES_PATH.toFile());

@BeforeClass
@BeforeAll
public static void setUpClass() throws Exception {
keys = RSAKeys.fromKeyFiles("/publicKey.txt", "/privateKey.txt");
}

@Before
@BeforeEach
public void setUp() {
cut = JwtGenerator.getInstance(XSUAA, DEFAULT_CLIENT_ID)
.withPrivateKey(keys.getPrivate());
Expand Down Expand Up @@ -292,11 +281,8 @@ public void withClaimValue_asJsonObjectContainingList() {
}

@Test
public void loadClaimsFromFile_doesNotContainValidJson_throwsException() throws IOException {
File emptyFile = temporaryFolder.newFile("empty");
String temporaryFolderName = emptyFile.getParentFile().getName();
String resourcePath = "/" + temporaryFolderName + "/empty";

public void loadClaimsFromFile_doesNotContainValidJson_throwsException() {
String resourcePath = "/emptyFile.json";
assertThatThrownBy(() -> cut.withClaimsFromFile(resourcePath).createToken())
.isInstanceOf(JsonParsingException.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
package com.sap.cloud.security.test;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
Expand All @@ -31,4 +31,4 @@ public void fromKeyFiles() throws IOException, InvalidKeySpecException,
assertThat(keys.getPrivate()).isNotNull();
assertThat(keys.getPublic()).isNotNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,4 @@ private static String readContent(CloseableHttpResponse response) throws IOExcep
.collect(Collectors.joining());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package com.sap.cloud.security.test;

import com.sap.cloud.security.config.Service;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -21,4 +21,4 @@ public void wireMockServerIsNotRunningAfterTearDown() throws Exception {
assertThat(cut.wireMockServer.isRunning()).isFalse();
}

}
}
Empty file.
Loading
Loading