Skip to content

Commit

Permalink
Redo "Migrate tests to JUnit5 (#626)" with fix (#631)
Browse files Browse the repository at this point in the history
* Revert "Revert "Migrate tests to JUnit5 (#626)" (#630)"
  This reverts commit 55ca191.
* revert the missing dependency in runtime scope
* revert also modifications of main code
  • Loading branch information
Tristan-WorkGH authored Oct 24, 2024
1 parent 55ca191 commit 574f6fe
Show file tree
Hide file tree
Showing 44 changed files with 1,740 additions and 2,645 deletions.
63 changes: 21 additions & 42 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@

<properties>
<gridsuite-dependencies.version>34</gridsuite-dependencies.version>
<log4j2-mock-version>0.0.2</log4j2-mock-version>
<testcontainers.version>1.18.3</testcontainers.version>
<liquibase-hibernate-package>org.gridsuite.study.server</liquibase-hibernate-package>
<mockwebserver3.version>5.0.0-alpha.14</mockwebserver3.version>
<apache.httpclient.version>5.3</apache.httpclient.version>
<!-- FIXME: powsybl-ws-commons version is overridden. To be removed at next gridsuite-dependencies.version upgrade -->
<powsybl-ws-commons.version>1.15.0</powsybl-ws-commons.version>
Expand Down Expand Up @@ -86,6 +85,13 @@
<dependencyManagement>
<dependencies>
<!-- overrides of imports -->
<dependency><!-- To remove when integrate in next release of gridsuite-dependencies or powsybl-ws-dependencies -->
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp-bom</artifactId>
<version>${mockwebserver3.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- FIXME: powsybl-ws-commons version is overridden. To be removed at next gridsuite-dependencies.version upgrade -->
<dependency>
<groupId>com.powsybl</groupId>
Expand Down Expand Up @@ -122,15 +128,9 @@

<!-- project specific dependencies -->
<dependency>
<groupId>de.dentrassi.elasticsearch</groupId>
<artifactId>log4j2-mock</artifactId>
<version>${log4j2-mock-version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>elasticsearch</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${apache.httpclient.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down Expand Up @@ -210,16 +210,16 @@

<!-- Runtime dependencies -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<!-- Used by RestTemplate -->
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${apache.httpclient.version}</version>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<scope>runtime</scope>
</dependency>
<!-- PostgreSQL -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand Down Expand Up @@ -268,27 +268,7 @@
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<scope>test</scope>
</dependency>
<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.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockwebserver3-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -313,12 +293,12 @@
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>elasticsearch</artifactId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.dentrassi.elasticsearch</groupId>
<artifactId>log4j2-mock</artifactId>
<groupId>org.testcontainers</groupId>
<artifactId>elasticsearch</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -327,5 +307,4 @@
<scope>test</scope>
</dependency>
</dependencies>

</project>
74 changes: 25 additions & 49 deletions src/test/java/org/gridsuite/study/server/CaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,35 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.gridsuite.study.server;

/**
* @author Kevin Le Saulnier <kevin.lesaulnier at rte-france.com>
*/

import com.powsybl.commons.exceptions.UncheckedInterruptedException;
import lombok.SneakyThrows;
import mockwebserver3.Dispatcher;
import mockwebserver3.MockResponse;
import mockwebserver3.MockWebServer;
import mockwebserver3.RecordedRequest;
import mockwebserver3.junit5.internal.MockWebServerExtension;
import okhttp3.HttpUrl;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.gridsuite.study.server.repository.StudyEntity;
import org.gridsuite.study.server.repository.StudyRepository;
import org.gridsuite.study.server.service.CaseService;
import org.gridsuite.study.server.service.NetworkModificationTreeService;
import org.gridsuite.study.server.utils.elasticsearch.DisableElasticsearch;
import org.gridsuite.study.server.utils.TestUtils;
import org.gridsuite.study.server.utils.elasticsearch.DisableElasticsearch;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.stream.binder.test.OutputDestination;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
Expand All @@ -47,26 +41,25 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
/**
* @author Kevin Le Saulnier <kevin.lesaulnier at rte-france.com>
*/
@ExtendWith(MockWebServerExtension.class)
@AutoConfigureMockMvc
@SpringBootTest
@DisableElasticsearch
@ContextConfigurationWithTestChannel
public class CaseTest {

class CaseTest {
private static final Logger LOGGER = LoggerFactory.getLogger(CaseTest.class);

private static final String NETWORK_UUID_STRING = "38400000-8cf0-11bd-b23e-10b96e4ef00d";
private static final String CASE_UUID_STRING = "00000000-8cf0-11bd-b23e-10b96e4ef00d";
private static final UUID CASE_UUID = UUID.fromString(CASE_UUID_STRING);
public static final String POST = "POST";
private static final String CASE_NAME = "DefaultCaseName";

@Autowired
private MockMvc mockMvc;

private MockWebServer server;

@Autowired
private OutputDestination output;

Expand All @@ -80,16 +73,10 @@ public class CaseTest {
private StudyRepository studyRepository;

//output destinations
private String studyUpdateDestination = "study.update";

@Before
public void setup() throws IOException {

server = new MockWebServer();

// Start the server.
server.start();
private static final String STUDY_UPDATE_DESTINATION = "study.update";

@BeforeEach
void setup(final MockWebServer server) {
// Ask the server for its URL. You'll need this to make HTTP requests.
HttpUrl baseHttpUrl = server.url("");
String baseUrl = baseHttpUrl.toString().substring(0, baseHttpUrl.toString().length() - 1);
Expand All @@ -101,22 +88,18 @@ public void setup() throws IOException {
@NotNull
public MockResponse dispatch(RecordedRequest request) {
String path = Objects.requireNonNull(request.getPath());
request.getBody();

switch (path) {
default:
LOGGER.error("Unhandled method+path: " + request.getMethod() + " " + request.getPath());
return new MockResponse().setResponseCode(418).setBody("Unhandled method+path: " + request.getMethod() + " " + request.getPath());
LOGGER.error("Unhandled method+path: {} {}", request.getMethod(), request.getPath());
return new MockResponse.Builder().code(418).body("Unhandled method+path: " + request.getMethod() + " " + request.getPath()).build();
}
}

};

server.setDispatcher(dispatcher);
}

@Test
public void getCaseName() throws Exception {
void getCaseName() throws Exception {
StudyEntity studyEntity = insertDummyStudy(UUID.fromString(NETWORK_UUID_STRING), CASE_UUID, CASE_NAME);
UUID study1Uuid = studyEntity.getId();

Expand All @@ -125,7 +108,6 @@ public void getCaseName() throws Exception {
content().string(CASE_NAME));
mockMvc.perform(get("/v1/studies/{studyUuid}/case/name", UUID.randomUUID()))
.andExpect(status().isNotFound());

}

private StudyEntity insertDummyStudy(UUID networkUuid, UUID caseUuid, String caseName) {
Expand All @@ -135,25 +117,19 @@ private StudyEntity insertDummyStudy(UUID networkUuid, UUID caseUuid, String cas
return study;
}

private void cleanDB() {
@AfterEach
void tearDown(final MockWebServer server) {
List<String> destinations = List.of(STUDY_UPDATE_DESTINATION);

studyRepository.findAll().forEach(s -> networkModificationTreeService.doDeleteTree(s.getId()));
studyRepository.deleteAll();
}

@After
public void tearDown() {
List<String> destinations = List.of(studyUpdateDestination);

cleanDB();

TestUtils.assertQueuesEmptyThenClear(destinations, output);

try {
TestUtils.assertServerRequestsEmptyThenShutdown(server);
} catch (UncheckedInterruptedException e) {
LOGGER.error("Error while attempting to get the request done : ", e);
} catch (IOException e) {
// Ignoring
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.gridsuite.study.server;

import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration;
Expand Down
Loading

0 comments on commit 574f6fe

Please sign in to comment.