Skip to content

Commit

Permalink
Migrate tests to JUnit5
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH authored Oct 17, 2024
2 parents 9219e96 + 713c190 commit 5cccfb8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 56 deletions.
33 changes: 8 additions & 25 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@
</dependencyManagement>

<dependencies>
<!-- Compilation dependencies -->
<!-- Annotation processor dependencies -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<!-- Compilation dependencies -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand All @@ -120,6 +123,10 @@
<groupId>com.powsybl</groupId>
<artifactId>powsybl-commons</artifactId>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-ws-commons</artifactId>
</dependency>

<!-- Runtime dependencies -->
<dependency>
Expand All @@ -132,37 +139,13 @@
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-ws-commons</artifactId>
</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.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-jetty12</artifactId>
Expand Down
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.caseimport.server;

import jakarta.annotation.PostConstruct;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package org.gridsuite.caseimport.server;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.http.client.MultipartBodyBuilder;
Expand All @@ -33,7 +32,6 @@ public class CaseService {
private final RestTemplate restTemplate;
private String caseServerBaseUri;

@Autowired
public CaseService(@Value("${powsybl.services.case-server.base-uri:http://case-server/}") String caseServerBaseUri,
RestTemplate restTemplate) {
this.caseServerBaseUri = caseServerBaseUri;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package org.gridsuite.caseimport.server;

import org.gridsuite.caseimport.server.dto.ElementAttributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -36,7 +35,6 @@ public class DirectoryService {
private static final String HEADER_USER_ID = "userId";
public static final String ELEMENT = "ELEMENT";

@Autowired
public DirectoryService(
@Value("${gridsuite.services.directory-server.base-uri:http://directory-server/}") String directoryServerBaseUri, RestTemplate restTemplate) {
this.directoryServerBaseUri = directoryServerBaseUri;
Expand Down
58 changes: 32 additions & 26 deletions src/test/java/org/gridsuite/caseimport/server/CaseImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,32 @@
*/
package org.gridsuite.caseimport.server;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.WireMockServer;
import org.gridsuite.caseimport.server.utils.WireMockUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.*;
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.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.ResourceUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
* @author Abdelsalem Hedhili <abdelsalem.hedhili at rte-france.com>
*/
@RunWith(SpringRunner.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@AutoConfigureMockMvc
@SpringBootTest
public class CaseImportTest {
class CaseImportTest {
private static final String TEST_FILE = "testCase.xiidm";
private static final String TEST_CASE_NAME = "testCase";
private static final String TEST_FILE_WITH_ERRORS = "testCase_with_errors.xiidm";
Expand All @@ -52,33 +47,45 @@ public class CaseImportTest {

private WireMockUtils wireMockUtils;

@Autowired
ObjectMapper objectMapper;
@Autowired
private MockMvc mockMvc;
@Autowired
private DirectoryService directoryService;
@Autowired
private CaseService caseService;

@Before
public void setup() throws IOException {
@BeforeAll
void initialize() {
wireMockServer = new WireMockServer(wireMockConfig().dynamicPort());
wireMockUtils = new WireMockUtils(wireMockServer);

// Start the server.
wireMockServer.start();
}

@AfterAll
void tearDown() {
wireMockServer.shutdown();
}

@BeforeEach
void setup() {
wireMockServer.resetAll();
directoryService.setDirectoryServerBaseUri(wireMockServer.baseUrl());
caseService.setBaseUri(wireMockServer.baseUrl());
}

@AfterAll
void cleanup() {
wireMockServer.checkForUnmatchedRequests();
}

@Test
public void testImportCase() throws Exception {
void testImportCase() throws Exception {
wireMockUtils.stubImportCase(TEST_FILE);
wireMockUtils.stubAddDirectoryElement(DEFAULT_IMPORT_DIRECTORY);
try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) {
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, "text/xml", is);
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, MediaType.TEXT_XML_VALUE, is);

mockMvc.perform(multipart("/v1/cases").file(mockFile)
.header("userId", USER1)
Expand All @@ -89,11 +96,11 @@ public void testImportCase() throws Exception {
}

@Test
public void testImportCaseWithBadRequestError() throws Exception {
void testImportCaseWithBadRequestError() throws Exception {
wireMockUtils.stubImportCaseWithErrorInvalid(TEST_FILE_WITH_ERRORS);
wireMockUtils.stubAddDirectoryElement(DEFAULT_IMPORT_DIRECTORY);
try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) {
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE_WITH_ERRORS, "text/xml", is);
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE_WITH_ERRORS, MediaType.TEXT_XML_VALUE, is);

mockMvc.perform(multipart("/v1/cases").file(mockFile)
.header("userId", USER1)
Expand All @@ -104,11 +111,11 @@ public void testImportCaseWithBadRequestError() throws Exception {
}

@Test
public void testImportCaseWithUnprocessableEntityError() throws Exception {
void testImportCaseWithUnprocessableEntityError() throws Exception {
wireMockUtils.stubImportCaseWithErrorBadExtension(TEST_INCORRECT_FILE);
wireMockUtils.stubAddDirectoryElement(DEFAULT_IMPORT_DIRECTORY);
try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) {
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_INCORRECT_FILE, "text/xml", is);
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_INCORRECT_FILE, MediaType.TEXT_XML_VALUE, is);

mockMvc.perform(multipart("/v1/cases").file(mockFile)
.header("userId", USER1)
Expand All @@ -119,11 +126,11 @@ public void testImportCaseWithUnprocessableEntityError() throws Exception {
}

@Test
public void testImportCaseWithInvalidOrigin() throws Exception {
void testImportCaseWithInvalidOrigin() throws Exception {
wireMockUtils.stubImportCaseWithErrorInvalid(TEST_FILE);
wireMockUtils.stubAddDirectoryElement(DEFAULT_IMPORT_DIRECTORY);
try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) {
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, "text/xml", is);
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, MediaType.TEXT_XML_VALUE, is);

mockMvc.perform(multipart("/v1/cases").file(mockFile)
.header("userId", USER1)
Expand All @@ -135,12 +142,12 @@ public void testImportCaseWithInvalidOrigin() throws Exception {
}

@Test
public void testImportCaseWithValidOrigin() throws Exception {
void testImportCaseWithValidOrigin() throws Exception {
final String caseName = "testCase";
wireMockUtils.stubImportCase(TEST_FILE);
wireMockUtils.stubAddDirectoryElement(CASE_ORIGIN_1_DIRECTORY);
try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) {
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, "text/xml", is);
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, MediaType.TEXT_XML_VALUE, is);

mockMvc.perform(multipart("/v1/cases").file(mockFile)
.header("userId", USER1)
Expand All @@ -155,11 +162,11 @@ public void testImportCaseWithValidOrigin() throws Exception {
}

@Test
public void testGivenEmptyCaseNameUseFilename() throws Exception {
void testGivenEmptyCaseNameUseFilename() throws Exception {
wireMockUtils.stubImportCase(TEST_FILE);
wireMockUtils.stubAddDirectoryElement(CASE_ORIGIN_1_DIRECTORY);
try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) {
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, "text/xml", is);
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, MediaType.TEXT_XML_VALUE, is);

mockMvc.perform(multipart("/v1/cases").file(mockFile)
.header("userId", USER1)
Expand All @@ -171,6 +178,5 @@ public void testGivenEmptyCaseNameUseFilename() throws Exception {
jsonPath("caseName").value(TEST_CASE_NAME),
jsonPath("parentDirectory").value(CASE_ORIGIN_1_DIRECTORY));
}

}
}

0 comments on commit 5cccfb8

Please sign in to comment.