Skip to content

Commit

Permalink
Migrate to Junit5
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH committed Apr 23, 2024
1 parent de10754 commit 440da79
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 79 deletions.
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
50 changes: 23 additions & 27 deletions src/test/java/org/gridsuite/gateway/ElementAccessControlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,38 @@
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;
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;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.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 {

@Value("${wiremock.server.port}")
int port;
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -177,7 +173,7 @@ public void testWithNoControl() {
}

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

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

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

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

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

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

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

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

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

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

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

IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> AccessControlInfos.create(emptyList));
Expand Down
82 changes: 40 additions & 42 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.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;
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 {

@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");
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();
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 @@ -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)));
Expand All @@ -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
Expand Down

0 comments on commit 440da79

Please sign in to comment.