27
27
import com .powsybl .openreac .parameters .input .OpenReacParameters ;
28
28
import com .powsybl .openreac .parameters .output .OpenReacResult ;
29
29
import com .powsybl .openreac .parameters .output .OpenReacStatus ;
30
- import lombok .SneakyThrows ;
31
30
import okhttp3 .HttpUrl ;
32
31
import okhttp3 .mockwebserver .Dispatcher ;
33
32
import okhttp3 .mockwebserver .MockResponse ;
44
43
import org .gridsuite .voltageinit .server .service .UuidGeneratorService ;
45
44
import org .gridsuite .voltageinit .server .service .parameters .FilterService ;
46
45
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 ;
51
50
import org .mockito .MockedStatic ;
52
51
import org .mockito .Mockito ;
53
- import org .mockito .MockitoAnnotations ;
52
+ import org .mockito .junit . jupiter . MockitoExtension ;
54
53
import org .springframework .beans .factory .annotation .Autowired ;
55
54
import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
56
55
import org .springframework .boot .test .context .SpringBootTest ;
61
60
import org .springframework .messaging .Message ;
62
61
import org .springframework .test .context .ContextConfiguration ;
63
62
import org .springframework .test .context .ContextHierarchy ;
64
- import org .springframework .test .context .junit4 .SpringRunner ;
65
63
import org .springframework .test .web .servlet .MockMvc ;
66
64
import org .springframework .test .web .servlet .MvcResult ;
67
65
import org .springframework .transaction .support .TransactionSynchronizationManager ;
75
73
import static com .powsybl .network .store .model .NetworkStoreApi .VERSION ;
76
74
import static org .gridsuite .voltageinit .server .service .NotificationService .CANCEL_MESSAGE ;
77
75
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 ;
80
78
import static org .mockito .ArgumentMatchers .any ;
81
79
import static org .mockito .ArgumentMatchers .eq ;
82
80
import static org .mockito .BDDMockito .given ;
87
85
/**
88
86
* @author Etienne Homer <etienne.homer at rte-france.com>
89
87
*/
90
- @ RunWith ( SpringRunner .class )
88
+ @ ExtendWith ({ MockitoExtension .class } )
91
89
@ AutoConfigureMockMvc
92
90
@ SpringBootTest
93
91
@ ContextHierarchy ({@ ContextConfiguration (classes = {VoltageInitApplication .class , TestChannelBinderConfiguration .class })})
94
- public class VoltageInitControllerTest {
95
-
92
+ class VoltageInitControllerTest {
96
93
private static final UUID NETWORK_UUID = UUID .fromString ("7928181c-7977-4592-ba19-88027e4254e4" );
97
94
private static final UUID OTHER_NETWORK_UUID = UUID .fromString ("06824085-db85-4883-9458-8c5c9f1585d6" );
98
95
private static final UUID RESULT_UUID = UUID .fromString ("0c8de370-3e6c-4d72-b292-d355a97e0d5d" );
@@ -130,21 +127,17 @@ public class VoltageInitControllerTest {
130
127
@ MockBean
131
128
private UuidGeneratorService uuidGeneratorService ;
132
129
133
- private final RestTemplateConfig restTemplateConfig = new RestTemplateConfig ();
134
- private final ObjectMapper mapper = restTemplateConfig . objectMapper () ;
130
+ @ Autowired
131
+ private ObjectMapper mapper ;
135
132
136
133
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 ;
143
136
144
- private MockWebServer server ;
137
+ public MockWebServer server ;
145
138
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 );
148
141
149
142
GeneratorModification .Modifs m1 = new GeneratorModification .Modifs ();
150
143
m1 .setTargetV (228. );
@@ -161,10 +154,9 @@ private OpenReacResult buildOpenReacResult() {
161
154
openReacAmplIOFiles .getNetworkModifications ().getShuntModifications ().add (new ShuntCompensatorModification ("SHUNT_1" , true , 1 ));
162
155
163
156
openReacResult = new OpenReacResult (OpenReacStatus .OK , openReacAmplIOFiles , INDICATORS );
164
- return openReacResult ;
165
157
}
166
158
167
- private VoltageInitParametersEntity buildVoltageInitParametersEntity () {
159
+ private static VoltageInitParametersEntity buildVoltageInitParametersEntity () {
168
160
return VoltageInitParametersInfos .builder ()
169
161
.voltageLimitsModification (List .of (VoltageLimitInfos .builder ()
170
162
.priority (0 )
@@ -199,10 +191,8 @@ private VoltageInitParametersEntity buildVoltageInitParametersEntity() {
199
191
.build ().toEntity ();
200
192
}
201
193
202
- @ Before
194
+ @ BeforeEach
203
195
public void setUp () throws Exception {
204
- MockitoAnnotations .initMocks (this );
205
-
206
196
server = new MockWebServer ();
207
197
server .start ();
208
198
@@ -221,28 +211,21 @@ public void setUp() throws Exception {
221
211
given (networkStoreService .getNetwork (NETWORK_UUID , PreloadingStrategy .COLLECTION )).willReturn (network );
222
212
given (networkStoreService .getNetwork (OTHER_NETWORK_UUID , PreloadingStrategy .ALL_COLLECTIONS_NEEDED_FOR_BUS_VIEW )).willThrow (new PowsyblException ("Not found" ));
223
213
224
- network1 = EurostagTutorialExample1Factory .createWithMoreGenerators (new NetworkFactoryImpl ());
225
- network1 .getVariantManager ().cloneVariant (VariantManagerConstants .INITIAL_VARIANT_ID , VARIANT_2_ID );
226
-
227
214
// OpenReac run mocking
228
- openReacParameters = new OpenReacParameters ();
229
- openReacResult = buildOpenReacResult ();
230
-
215
+ buildOpenReacResult ();
231
216
completableFutureResultsTask = CompletableFutureTask .runAsync (() -> openReacResult , ForkJoinPool .commonPool ());
232
217
233
218
// UUID service mocking to always generate the same result UUID
234
219
given (uuidGeneratorService .generate ()).willReturn (RESULT_UUID );
235
220
236
221
final Dispatcher dispatcher = new Dispatcher () {
237
- @ SneakyThrows
238
222
@ Override
239
223
public MockResponse dispatch (RecordedRequest request ) {
240
224
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 ())) {
243
226
return new MockResponse ().setResponseCode (200 )
244
227
.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 ())) {
246
229
return new MockResponse ().setResponseCode (200 ).setBody ("\" " + MODIFICATIONS_GROUP_UUID + "\" " )
247
230
.addHeader ("Content-Type" , "application/json; charset=utf-8" );
248
231
} else if (path .matches ("/v1/filters/export\\ ?networkUuid=" + NETWORK_UUID + "&variantId=" + VARIANT_2_ID + "&ids=.*" )) {
@@ -268,15 +251,15 @@ public MockResponse dispatch(RecordedRequest request) {
268
251
}
269
252
}
270
253
271
- @ SneakyThrows
272
- @ After
273
- public void tearDown () {
254
+ @ AfterEach
255
+ public void tearDown () throws Exception {
274
256
mockMvc .perform (delete ("/" + VERSION + "/results" ))
275
257
.andExpect (status ().isOk ());
258
+ server .shutdown ();
276
259
}
277
260
278
261
@ Test
279
- public void runTest () throws Exception {
262
+ void runTest () throws Exception {
280
263
try (MockedStatic <OpenReacRunner > openReacRunnerMockedStatic = Mockito .mockStatic (OpenReacRunner .class )) {
281
264
openReacRunnerMockedStatic .when (() -> OpenReacRunner .runAsync (eq (network ), eq (VARIANT_2_ID ), any (OpenReacParameters .class ), any (OpenReacConfig .class ), any (ComputationManager .class )))
282
265
.thenReturn (completableFutureResultsTask );
@@ -339,7 +322,7 @@ public void runTest() throws Exception {
339
322
}
340
323
341
324
@ Test
342
- public void runWrongNetworkTest () throws Exception {
325
+ void runWrongNetworkTest () throws Exception {
343
326
MvcResult result = mockMvc .perform (post (
344
327
"/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId=" + VARIANT_2_ID , OTHER_NETWORK_UUID )
345
328
.header (HEADER_USER_ID , "userId" ))
@@ -360,7 +343,7 @@ public void runWrongNetworkTest() throws Exception {
360
343
}
361
344
362
345
@ Test
363
- public void runWithReportTest () throws Exception {
346
+ void runWithReportTest () throws Exception {
364
347
MvcResult result = mockMvc .perform (post (
365
348
"/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId={variantId}&reportType=VoltageInit&reportUuid=" + REPORT_UUID + "&reporterId=" + UUID .randomUUID (), NETWORK_UUID , VARIANT_2_ID )
366
349
.header (HEADER_USER_ID , "userId" ))
@@ -370,7 +353,7 @@ public void runWithReportTest() throws Exception {
370
353
}
371
354
372
355
@ Test
373
- public void stopTest () throws Exception {
356
+ void stopTest () throws Exception {
374
357
try (MockedStatic <OpenReacRunner > openReacRunnerMockedStatic = Mockito .mockStatic (OpenReacRunner .class )) {
375
358
openReacRunnerMockedStatic .when (() -> OpenReacRunner .runAsync (eq (network ), eq (VARIANT_2_ID ), any (OpenReacParameters .class ), any (OpenReacConfig .class ), any (ComputationManager .class )))
376
359
.thenReturn (completableFutureResultsTask );
@@ -396,9 +379,8 @@ public void stopTest() throws Exception {
396
379
}
397
380
}
398
381
399
- @ SneakyThrows
400
382
@ Test
401
- public void getStatusTest () {
383
+ void getStatusTest () throws Exception {
402
384
MvcResult result = mockMvc .perform (get (
403
385
"/" + VERSION + "/results/{resultUuid}/status" , RESULT_UUID ))
404
386
.andExpect (status ().isOk ())
@@ -416,9 +398,8 @@ public void getStatusTest() {
416
398
assertEquals (VoltageInitStatus .NOT_DONE .name (), result .getResponse ().getContentAsString ());
417
399
}
418
400
419
- @ SneakyThrows
420
401
@ Test
421
- public void postCompletionAdapterTest () {
402
+ void postCompletionAdapterTest () {
422
403
CompletableFutureTask <OpenReacResult > task = CompletableFutureTask .runAsync (() -> openReacResult , ForkJoinPool .commonPool ());
423
404
PostCompletionAdapter adapter = new PostCompletionAdapter ();
424
405
adapter .execute (task );
0 commit comments