Skip to content

Commit aff60c2

Browse files
migrate tests to junit5
1 parent 71180a9 commit aff60c2

File tree

5 files changed

+80
-134
lines changed

5 files changed

+80
-134
lines changed

pom.xml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -189,31 +189,16 @@
189189
<artifactId>powsybl-iidm-test</artifactId>
190190
<scope>test</scope>
191191
</dependency>
192-
<dependency>
193-
<groupId>com.squareup.okhttp3</groupId>
194-
<artifactId>okhttp</artifactId>
195-
<scope>test</scope>
196-
</dependency>
197192
<dependency>
198193
<groupId>com.squareup.okhttp3</groupId>
199194
<artifactId>mockwebserver</artifactId>
200195
<scope>test</scope>
201196
</dependency>
202-
<dependency>
203-
<groupId>junit</groupId>
204-
<artifactId>junit</artifactId>
205-
<scope>test</scope>
206-
</dependency>
207197
<dependency>
208198
<groupId>org.assertj</groupId>
209199
<artifactId>assertj-guava</artifactId>
210200
<scope>test</scope>
211201
</dependency>
212-
<dependency>
213-
<groupId>org.junit.vintage</groupId>
214-
<artifactId>junit-vintage-engine</artifactId>
215-
<scope>test</scope>
216-
</dependency>
217202
<dependency>
218203
<groupId>org.mockito</groupId>
219204
<artifactId>mockito-core</artifactId>

src/test/java/org/gridsuite/voltageinit/server/SupervisionControllerTest.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,31 @@
66
*/
77
package org.gridsuite.voltageinit.server;
88

9-
import org.junit.Test;
10-
import org.junit.runner.RunWith;
9+
import org.junit.jupiter.api.Test;
1110
import org.springframework.beans.factory.annotation.Autowired;
1211
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1312
import org.springframework.boot.test.context.SpringBootTest;
1413
import org.springframework.http.MediaType;
15-
import org.springframework.test.context.junit4.SpringRunner;
1614
import org.springframework.test.web.servlet.MockMvc;
1715
import org.springframework.test.web.servlet.MvcResult;
1816

19-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2019
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
2120
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2221

23-
import static org.junit.Assert.assertEquals;
24-
2522
/**
2623
* @author Hugo Marcellin <hugo.marcelin at rte-france.com>
2724
*/
28-
@RunWith(SpringRunner.class)
2925
@AutoConfigureMockMvc
3026
@SpringBootTest
31-
public class SupervisionControllerTest {
27+
class SupervisionControllerTest {
3228

3329
@Autowired
3430
private MockMvc mockMvc;
3531

3632
@Test
37-
public void testResultCount() throws Exception {
33+
void testResultCount() throws Exception {
3834
//get the result timeline uuid of the calculation
3935
MvcResult mvcResult = mockMvc.perform(get("/v1/supervision/results-count"))
4036
.andExpect(status().isOk())
@@ -43,7 +39,6 @@ public void testResultCount() throws Exception {
4339

4440
String resultCount = mvcResult.getResponse().getContentAsString();
4541
assertEquals("0", resultCount);
46-
4742
}
4843
}
4944

src/test/java/org/gridsuite/voltageinit/server/VoltageInitControllerTest.java

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.powsybl.openreac.parameters.input.OpenReacParameters;
2828
import com.powsybl.openreac.parameters.output.OpenReacResult;
2929
import com.powsybl.openreac.parameters.output.OpenReacStatus;
30-
import lombok.SneakyThrows;
3130
import okhttp3.HttpUrl;
3231
import okhttp3.mockwebserver.Dispatcher;
3332
import okhttp3.mockwebserver.MockResponse;
@@ -44,13 +43,13 @@
4443
import org.gridsuite.voltageinit.server.service.UuidGeneratorService;
4544
import org.gridsuite.voltageinit.server.service.parameters.FilterService;
4645
import org.gridsuite.voltageinit.server.util.annotations.PostCompletionAdapter;
47-
import org.junit.After;
48-
import org.junit.Before;
49-
import org.junit.Test;
50-
import org.junit.runner.RunWith;
46+
import org.junit.jupiter.api.AfterEach;
47+
import org.junit.jupiter.api.BeforeEach;
48+
import org.junit.jupiter.api.Test;
49+
import org.junit.jupiter.api.extension.ExtendWith;
5150
import org.mockito.MockedStatic;
5251
import org.mockito.Mockito;
53-
import org.mockito.MockitoAnnotations;
52+
import org.mockito.junit.jupiter.MockitoExtension;
5453
import org.springframework.beans.factory.annotation.Autowired;
5554
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
5655
import org.springframework.boot.test.context.SpringBootTest;
@@ -61,7 +60,6 @@
6160
import org.springframework.messaging.Message;
6261
import org.springframework.test.context.ContextConfiguration;
6362
import org.springframework.test.context.ContextHierarchy;
64-
import org.springframework.test.context.junit4.SpringRunner;
6563
import org.springframework.test.web.servlet.MockMvc;
6664
import org.springframework.test.web.servlet.MvcResult;
6765
import org.springframework.transaction.support.TransactionSynchronizationManager;
@@ -75,8 +73,8 @@
7573
import static com.powsybl.network.store.model.NetworkStoreApi.VERSION;
7674
import static org.gridsuite.voltageinit.server.service.NotificationService.CANCEL_MESSAGE;
7775
import static org.gridsuite.voltageinit.server.service.NotificationService.HEADER_USER_ID;
78-
import static org.junit.Assert.assertEquals;
79-
import static org.junit.Assert.assertNotNull;
76+
import static org.junit.jupiter.api.Assertions.assertEquals;
77+
import static org.junit.jupiter.api.Assertions.assertNotNull;
8078
import static org.mockito.ArgumentMatchers.any;
8179
import static org.mockito.ArgumentMatchers.eq;
8280
import static org.mockito.BDDMockito.given;
@@ -87,12 +85,11 @@
8785
/**
8886
* @author Etienne Homer <etienne.homer at rte-france.com>
8987
*/
90-
@RunWith(SpringRunner.class)
88+
@ExtendWith({ MockitoExtension.class })
9189
@AutoConfigureMockMvc
9290
@SpringBootTest
9391
@ContextHierarchy({@ContextConfiguration(classes = {VoltageInitApplication.class, TestChannelBinderConfiguration.class})})
94-
public class VoltageInitControllerTest {
95-
92+
class VoltageInitControllerTest {
9693
private static final UUID NETWORK_UUID = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4");
9794
private static final UUID OTHER_NETWORK_UUID = UUID.fromString("06824085-db85-4883-9458-8c5c9f1585d6");
9895
private static final UUID RESULT_UUID = UUID.fromString("0c8de370-3e6c-4d72-b292-d355a97e0d5d");
@@ -130,21 +127,17 @@ public class VoltageInitControllerTest {
130127
@MockBean
131128
private UuidGeneratorService uuidGeneratorService;
132129

133-
private final RestTemplateConfig restTemplateConfig = new RestTemplateConfig();
134-
private final ObjectMapper mapper = restTemplateConfig.objectMapper();
130+
@Autowired
131+
private ObjectMapper mapper;
135132

136133
private Network network;
137-
private Network network1;
138-
private Network networkForMergingView;
139-
private Network otherNetworkForMergingView;
140-
OpenReacParameters openReacParameters;
141-
OpenReacResult openReacResult;
142-
CompletableFutureTask<OpenReacResult> completableFutureResultsTask;
134+
private OpenReacResult openReacResult;
135+
private CompletableFutureTask<OpenReacResult> completableFutureResultsTask;
143136

144-
private MockWebServer server;
137+
public MockWebServer server;
145138

146-
private OpenReacResult buildOpenReacResult() {
147-
OpenReacAmplIOFiles openReacAmplIOFiles = new OpenReacAmplIOFiles(openReacParameters, network, false);
139+
private void buildOpenReacResult() {
140+
OpenReacAmplIOFiles openReacAmplIOFiles = new OpenReacAmplIOFiles(new OpenReacParameters(), network, false);
148141

149142
GeneratorModification.Modifs m1 = new GeneratorModification.Modifs();
150143
m1.setTargetV(228.);
@@ -161,10 +154,9 @@ private OpenReacResult buildOpenReacResult() {
161154
openReacAmplIOFiles.getNetworkModifications().getShuntModifications().add(new ShuntCompensatorModification("SHUNT_1", true, 1));
162155

163156
openReacResult = new OpenReacResult(OpenReacStatus.OK, openReacAmplIOFiles, INDICATORS);
164-
return openReacResult;
165157
}
166158

167-
private VoltageInitParametersEntity buildVoltageInitParametersEntity() {
159+
private static VoltageInitParametersEntity buildVoltageInitParametersEntity() {
168160
return VoltageInitParametersInfos.builder()
169161
.voltageLimitsModification(List.of(VoltageLimitInfos.builder()
170162
.priority(0)
@@ -199,10 +191,8 @@ private VoltageInitParametersEntity buildVoltageInitParametersEntity() {
199191
.build().toEntity();
200192
}
201193

202-
@Before
194+
@BeforeEach
203195
public void setUp() throws Exception {
204-
MockitoAnnotations.initMocks(this);
205-
206196
server = new MockWebServer();
207197
server.start();
208198

@@ -221,28 +211,21 @@ public void setUp() throws Exception {
221211
given(networkStoreService.getNetwork(NETWORK_UUID, PreloadingStrategy.COLLECTION)).willReturn(network);
222212
given(networkStoreService.getNetwork(OTHER_NETWORK_UUID, PreloadingStrategy.ALL_COLLECTIONS_NEEDED_FOR_BUS_VIEW)).willThrow(new PowsyblException("Not found"));
223213

224-
network1 = EurostagTutorialExample1Factory.createWithMoreGenerators(new NetworkFactoryImpl());
225-
network1.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_2_ID);
226-
227214
// OpenReac run mocking
228-
openReacParameters = new OpenReacParameters();
229-
openReacResult = buildOpenReacResult();
230-
215+
buildOpenReacResult();
231216
completableFutureResultsTask = CompletableFutureTask.runAsync(() -> openReacResult, ForkJoinPool.commonPool());
232217

233218
// UUID service mocking to always generate the same result UUID
234219
given(uuidGeneratorService.generate()).willReturn(RESULT_UUID);
235220

236221
final Dispatcher dispatcher = new Dispatcher() {
237-
@SneakyThrows
238222
@Override
239223
public MockResponse dispatch(RecordedRequest request) {
240224
String path = Objects.requireNonNull(request.getPath());
241-
242-
if (path.matches("/v1/groups/.*") && request.getMethod().equals("DELETE")) {
225+
if (path.matches("/v1/groups/.*") && "DELETE".equals(request.getMethod())) {
243226
return new MockResponse().setResponseCode(200)
244227
.addHeader("Content-Type", "application/json; charset=utf-8");
245-
} else if (path.matches("/v1/groups/modification") && request.getMethod().equals("POST")) {
228+
} else if (path.matches("/v1/groups/modification") && "POST".equals(request.getMethod())) {
246229
return new MockResponse().setResponseCode(200).setBody("\"" + MODIFICATIONS_GROUP_UUID + "\"")
247230
.addHeader("Content-Type", "application/json; charset=utf-8");
248231
} else if (path.matches("/v1/filters/export\\?networkUuid=" + NETWORK_UUID + "&variantId=" + VARIANT_2_ID + "&ids=.*")) {
@@ -268,15 +251,15 @@ public MockResponse dispatch(RecordedRequest request) {
268251
}
269252
}
270253

271-
@SneakyThrows
272-
@After
273-
public void tearDown() {
254+
@AfterEach
255+
public void tearDown() throws Exception {
274256
mockMvc.perform(delete("/" + VERSION + "/results"))
275257
.andExpect(status().isOk());
258+
server.shutdown();
276259
}
277260

278261
@Test
279-
public void runTest() throws Exception {
262+
void runTest() throws Exception {
280263
try (MockedStatic<OpenReacRunner> openReacRunnerMockedStatic = Mockito.mockStatic(OpenReacRunner.class)) {
281264
openReacRunnerMockedStatic.when(() -> OpenReacRunner.runAsync(eq(network), eq(VARIANT_2_ID), any(OpenReacParameters.class), any(OpenReacConfig.class), any(ComputationManager.class)))
282265
.thenReturn(completableFutureResultsTask);
@@ -339,7 +322,7 @@ public void runTest() throws Exception {
339322
}
340323

341324
@Test
342-
public void runWrongNetworkTest() throws Exception {
325+
void runWrongNetworkTest() throws Exception {
343326
MvcResult result = mockMvc.perform(post(
344327
"/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId=" + VARIANT_2_ID, OTHER_NETWORK_UUID)
345328
.header(HEADER_USER_ID, "userId"))
@@ -360,7 +343,7 @@ public void runWrongNetworkTest() throws Exception {
360343
}
361344

362345
@Test
363-
public void runWithReportTest() throws Exception {
346+
void runWithReportTest() throws Exception {
364347
MvcResult result = mockMvc.perform(post(
365348
"/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId={variantId}&reportType=VoltageInit&reportUuid=" + REPORT_UUID + "&reporterId=" + UUID.randomUUID(), NETWORK_UUID, VARIANT_2_ID)
366349
.header(HEADER_USER_ID, "userId"))
@@ -370,7 +353,7 @@ public void runWithReportTest() throws Exception {
370353
}
371354

372355
@Test
373-
public void stopTest() throws Exception {
356+
void stopTest() throws Exception {
374357
try (MockedStatic<OpenReacRunner> openReacRunnerMockedStatic = Mockito.mockStatic(OpenReacRunner.class)) {
375358
openReacRunnerMockedStatic.when(() -> OpenReacRunner.runAsync(eq(network), eq(VARIANT_2_ID), any(OpenReacParameters.class), any(OpenReacConfig.class), any(ComputationManager.class)))
376359
.thenReturn(completableFutureResultsTask);
@@ -396,9 +379,8 @@ public void stopTest() throws Exception {
396379
}
397380
}
398381

399-
@SneakyThrows
400382
@Test
401-
public void getStatusTest() {
383+
void getStatusTest() throws Exception {
402384
MvcResult result = mockMvc.perform(get(
403385
"/" + VERSION + "/results/{resultUuid}/status", RESULT_UUID))
404386
.andExpect(status().isOk())
@@ -416,9 +398,8 @@ public void getStatusTest() {
416398
assertEquals(VoltageInitStatus.NOT_DONE.name(), result.getResponse().getContentAsString());
417399
}
418400

419-
@SneakyThrows
420401
@Test
421-
public void postCompletionAdapterTest() {
402+
void postCompletionAdapterTest() {
422403
CompletableFutureTask<OpenReacResult> task = CompletableFutureTask.runAsync(() -> openReacResult, ForkJoinPool.commonPool());
423404
PostCompletionAdapter adapter = new PostCompletionAdapter();
424405
adapter.execute(task);

0 commit comments

Comments
 (0)