From 440da793a21a68f5880b06abde3c2df0cb12b475 Mon Sep 17 00:00:00 2001 From: Tristan Chuine Date: Tue, 23 Apr 2024 14:05:00 +0200 Subject: [PATCH] Migrate to Junit5 --- pom.xml | 10 --- .../gateway/ElementAccessControlTest.java | 50 ++++++----- .../gateway/TokenValidationTest.java | 82 +++++++++---------- 3 files changed, 63 insertions(+), 79 deletions(-) diff --git a/pom.xml b/pom.xml index 6e2fde6..3b0300e 100644 --- a/pom.xml +++ b/pom.xml @@ -119,16 +119,6 @@ - - junit - junit - test - - - org.junit.vintage - junit-vintage-engine - test - org.springframework.boot spring-boot-starter-test diff --git a/src/test/java/org/gridsuite/gateway/ElementAccessControlTest.java b/src/test/java/org/gridsuite/gateway/ElementAccessControlTest.java index 25878ae..0e63c3d 100644 --- a/src/test/java/org/gridsuite/gateway/ElementAccessControlTest.java +++ b/src/test/java/org/gridsuite/gateway/ElementAccessControlTest.java @@ -17,15 +17,14 @@ import com.nimbusds.jwt.SignedJWT; import org.gridsuite.gateway.dto.AccessControlInfos; import org.gridsuite.gateway.endpoints.ExploreServer; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock; import org.springframework.http.HttpStatus; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.reactive.server.WebTestClient; import java.util.Date; @@ -33,26 +32,23 @@ import java.util.UUID; import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * @author Slimane Amar */ -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, - properties = { - "gridsuite.services.directory-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.explore-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.study-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.actions-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.filter-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.user-admin-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.sensitivity-analysis-server.base-uri=http://localhost:${wiremock.server.port}", - } -) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { + "gridsuite.services.directory-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.explore-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.study-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.actions-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.filter-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.user-admin-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.sensitivity-analysis-server.base-uri=http://localhost:${wiremock.server.port}", +}) @AutoConfigureWireMock(port = 0) -public class ElementAccessControlTest { +class ElementAccessControlTest { @Value("${wiremock.server.port}") int port; @@ -69,7 +65,7 @@ public class ElementAccessControlTest { private RSAKey rsaKey; - @Before + @BeforeEach public void prepareToken() throws JOSEException { // RSA signatures require a public and private RSA key pair, the public key // must be made known to the JWS recipient in order to verify the signatures @@ -112,7 +108,7 @@ public void prepareToken() throws JOSEException { } @Test - public void testWithNoControl() { + void testWithNoControl() { initStubForJwk(); // No control for directory server (made inside the endpoint) @@ -177,7 +173,7 @@ public void testWithNoControl() { } @Test - public void testGetElements() { + void testGetElements() { initStubForJwk(); UUID uuid = UUID.randomUUID(); @@ -271,7 +267,7 @@ public void testGetElements() { } @Test - public void testCreateElements() { + void testCreateElements() { initStubForJwk(); UUID uuid = UUID.randomUUID(); @@ -368,7 +364,7 @@ public void testCreateElements() { } @Test - public void testCreateSubElements() { + void testCreateSubElements() { initStubForJwk(); UUID uuid = UUID.randomUUID(); @@ -394,7 +390,7 @@ public void testCreateSubElements() { } @Test - public void testUpdateElements() { + void testUpdateElements() { initStubForJwk(); UUID uuid = UUID.randomUUID(); @@ -453,7 +449,7 @@ public void testUpdateElements() { } @Test - public void testDeleteElements() { + void testDeleteElements() { initStubForJwk(); UUID uuid = UUID.randomUUID(); @@ -527,7 +523,7 @@ public void testDeleteElements() { } @Test - public void testAccessControlInfos() { + void testAccessControlInfos() { List emptyList = List.of(); IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> AccessControlInfos.create(emptyList)); diff --git a/src/test/java/org/gridsuite/gateway/TokenValidationTest.java b/src/test/java/org/gridsuite/gateway/TokenValidationTest.java index 693f422..1f32a47 100644 --- a/src/test/java/org/gridsuite/gateway/TokenValidationTest.java +++ b/src/test/java/org/gridsuite/gateway/TokenValidationTest.java @@ -9,27 +9,28 @@ import com.github.tomakehurst.wiremock.client.VerificationException; import com.github.tomakehurst.wiremock.core.WireMockConfiguration; import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer; -import com.nimbusds.jose.*; +import com.nimbusds.jose.JOSEException; +import com.nimbusds.jose.JWSAlgorithm; +import com.nimbusds.jose.JWSHeader; +import com.nimbusds.jose.JWSSigner; import com.nimbusds.jose.crypto.RSASSASigner; import com.nimbusds.jose.jwk.RSAKey; import com.nimbusds.jose.jwk.gen.RSAKeyGenerator; import com.nimbusds.jwt.JWTClaimsSet; import com.nimbusds.jwt.SignedJWT; -import lombok.SneakyThrows; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock; import org.springframework.cloud.contract.wiremock.WireMockConfigurationCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpHeaders; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.web.reactive.socket.client.StandardWebSocketClient; import org.springframework.web.reactive.socket.client.WebSocketClient; @@ -50,31 +51,29 @@ /** * @author Chamseddine Benhamed */ -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, - properties = {"powsybl.services.case-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.study-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.merge-orchestrator-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.merge-notification-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.directory-notification-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.actions-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.study-notification-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.config-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.config-notification-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.directory-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.explore-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.cgmes-boundary-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.dynamic-mapping-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.filter-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.report-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.network-modification-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.user-admin-server.base-uri=http://localhost:${wiremock.server.port}", - "gridsuite.services.sensitivity-analysis-server.base-uri=http://localhost:${wiremock.server.port}", - "allowed-issuers=http://localhost:${wiremock.server.port}" - }) - +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { + "powsybl.services.case-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.study-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.merge-orchestrator-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.merge-notification-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.directory-notification-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.actions-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.study-notification-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.config-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.config-notification-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.directory-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.explore-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.cgmes-boundary-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.dynamic-mapping-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.filter-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.report-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.network-modification-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.user-admin-server.base-uri=http://localhost:${wiremock.server.port}", + "gridsuite.services.sensitivity-analysis-server.base-uri=http://localhost:${wiremock.server.port}", + "allowed-issuers=http://localhost:${wiremock.server.port}" +}) @AutoConfigureWireMock(port = 0) -public class TokenValidationTest { +class TokenValidationTest { @Value("${wiremock.server.port}") int port; @@ -97,7 +96,7 @@ public class TokenValidationTest { @Autowired WebTestClient webClient; - @Before + @BeforeEach public void prepareToken() throws JOSEException { // RSA signatures require a public and private RSA key pair, the public key // must be made known to the JWS recipient in order to verify the signatures @@ -187,18 +186,18 @@ private void testWebsocket(String name) throws InterruptedException { } } if (!done) { - Assert.fail("Wiremock didn't receive the websocket connection"); + Assertions.fail("Wiremock didn't receive the websocket connection"); } try { wsconnection.timeout(Duration.ofMillis(100)).block(); - Assert.fail("websocket client was closed but should remain open"); + Assertions.fail("websocket client was closed but should remain open"); } catch (Exception ignored) { //should timeout } } @Test - public void gatewayTest() { + void gatewayTest() { initStubForJwk(); UUID elementUuid = UUID.randomUUID(); @@ -370,8 +369,7 @@ private void testToken(UUID elementUuid, String token) { } @Test - @SneakyThrows - public void testWebsockets() { + void testWebsockets() throws InterruptedException { initStubForJwk(); stubFor(get(urlPathEqualTo("/notify")).withHeader("userId", equalTo("chmits")) @@ -413,7 +411,7 @@ private void initStubForJwk() { } @Test - public void testJwksUpdate() { + void testJwksUpdate() { stubFor(head(urlEqualTo(String.format("/v1/users/%s", "chmits"))).withPort(port) .willReturn(aResponse().withStatus(200))); @@ -481,7 +479,7 @@ public void testJwksUpdate() { } @Test - public void invalidToken() { + void invalidToken() { stubFor(head(urlEqualTo(String.format("/v1/users/%s", "chmits"))).withPort(port) .willReturn(aResponse().withStatus(200))); @@ -563,11 +561,11 @@ public void invalidToken() { WebSocketClient client = new StandardWebSocketClient(); client.execute(URI.create("ws://localhost:" + this.localServerPort + "/study-notification/notify"), - ws -> ws.receive().then()).doOnSuccess(s -> Assert.fail("Should have thrown")); + ws -> ws.receive().then()).doOnSuccess(s -> Assertions.fail("Should have thrown")); } @Test - public void forbiddenUserTest() { + void forbiddenUserTest() { initStubForJwk(); stubFor(head(urlEqualTo(String.format("/v1/users/%s", "chmits"))).withPort(port) .willReturn(aResponse().withStatus(204))); @@ -582,7 +580,7 @@ public void forbiddenUserTest() { @TestConfiguration static class MyTestConfiguration { @Bean - WireMockConfigurationCustomizer optionsCustomizer() { + public WireMockConfigurationCustomizer optionsCustomizer() { return new WireMockConfigurationCustomizer() { private static final String SEC_WEBSOCKET_MAGIC = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; @Override