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

Migrate from JUnit4 to JUnit5 #102

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,6 @@
</dependency>

<!-- Test dependencies -->
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
54 changes: 23 additions & 31 deletions src/test/java/org/gridsuite/gateway/ElementAccessControlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,39 @@
import com.nimbusds.jose.jwk.gen.RSAKeyGenerator;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import org.assertj.core.api.WithAssertions;
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;
import java.util.List;
import java.util.UUID;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

/**
* @author Slimane Amar <slimane.amar at rte-france.com>
*/
@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 implements WithAssertions {

@Value("${wiremock.server.port}")
int port;
Expand All @@ -69,7 +64,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
Expand Down Expand Up @@ -112,7 +107,7 @@ public void prepareToken() throws JOSEException {
}

@Test
public void testWithNoControl() {
void testWithNoControl() {
initStubForJwk();

// No control for directory server (made inside the endpoint)
Expand Down Expand Up @@ -177,7 +172,7 @@ public void testWithNoControl() {
}

@Test
public void testGetElements() {
void testGetElements() {
initStubForJwk();

UUID uuid = UUID.randomUUID();
Expand Down Expand Up @@ -271,7 +266,7 @@ public void testGetElements() {
}

@Test
public void testCreateElements() {
void testCreateElements() {
initStubForJwk();

UUID uuid = UUID.randomUUID();
Expand Down Expand Up @@ -368,7 +363,7 @@ public void testCreateElements() {
}

@Test
public void testCreateSubElements() {
void testCreateSubElements() {
initStubForJwk();

UUID uuid = UUID.randomUUID();
Expand All @@ -394,7 +389,7 @@ public void testCreateSubElements() {
}

@Test
public void testUpdateElements() {
void testUpdateElements() {
initStubForJwk();

UUID uuid = UUID.randomUUID();
Expand Down Expand Up @@ -453,7 +448,7 @@ public void testUpdateElements() {
}

@Test
public void testDeleteElements() {
void testDeleteElements() {
initStubForJwk();

UUID uuid = UUID.randomUUID();
Expand Down Expand Up @@ -527,11 +522,8 @@ public void testDeleteElements() {
}

@Test
public void testAccessControlInfos() {
List<UUID> emptyList = List.of();

IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> AccessControlInfos.create(emptyList));
assertEquals("List of elements is empty", exception.getMessage());
void testAccessControlInfos() {
assertThatIllegalArgumentException().isThrownBy(() -> AccessControlInfos.create(List.of())).withMessage("List of elements is empty");
}

private void initStubForJwk() {
Expand Down
87 changes: 42 additions & 45 deletions src/test/java/org/gridsuite/gateway/TokenValidationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.assertj.core.api.WithAssertions;
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;
Expand All @@ -50,31 +51,29 @@
/**
* @author Chamseddine Benhamed <chamseddine.benhamed at rte-france.com>
*/
@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 implements WithAssertions {

@Value("${wiremock.server.port}")
int port;
Expand All @@ -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
Expand Down Expand Up @@ -187,18 +186,18 @@ private void testWebsocket(String name) throws InterruptedException {
}
}
if (!done) {
Assert.fail("Wiremock didn't receive the websocket connection");
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");
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();
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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)));

Expand Down Expand Up @@ -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)));

Expand Down Expand Up @@ -552,7 +550,7 @@ public void invalidToken() {
.exchange()
.expectStatus().isEqualTo(401);

//test with a incorrect Authorization value
//test with an incorrect Authorization value
webClient
.get().uri("case/v1/cases")
.header("Authorization", token)
Expand All @@ -561,13 +559,12 @@ public void invalidToken() {

// test without a token
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"));
client.execute(URI.create("ws://localhost:" + this.localServerPort + "/study-notification/notify"), ws -> ws.receive().then())
.doOnSuccess(s -> 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)));
Expand All @@ -582,7 +579,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
Expand Down
Loading