Skip to content

Commit cee92a5

Browse files
fix(modification-creation): do not create modification when computation result is NOT_OK (#53)
* fix(modification-creation): do not create modification when computation result is NOT_OK * fix: sonar issue
1 parent 2692654 commit cee92a5

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/main/java/org/gridsuite/voltageinit/server/service/VoltageInitWorkerService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public Consumer<Message<String>> consumeRun() {
212212
LOGGER.info("Just run in {}s", TimeUnit.NANOSECONDS.toSeconds(nanoTime - startTime.getAndSet(nanoTime)));
213213

214214
if (openReacResult != null) { // result available
215-
UUID modificationsGroupUuid = networkModificationService.createVoltageInitModificationGroup(network, openReacResult);
215+
UUID modificationsGroupUuid = createModificationGroup(openReacResult, network);
216216
Map<String, Bus> networkBuses = network.getBusView().getBusStream().collect(Collectors.toMap(Bus::getId, Function.identity()));
217217
voltageInitObserver.observe("results.save", () ->
218218
resultRepository.insert(resultContext.getResultUuid(), openReacResult, networkBuses, modificationsGroupUuid, openReacResult.getStatus().name()));
@@ -247,6 +247,12 @@ public Consumer<Message<String>> consumeRun() {
247247
};
248248
}
249249

250+
private UUID createModificationGroup(OpenReacResult openReacResult, Network network) {
251+
return openReacResult.getStatus() == OpenReacStatus.OK ?
252+
networkModificationService.createVoltageInitModificationGroup(network, openReacResult) :
253+
null;
254+
}
255+
250256
@Bean
251257
public Consumer<Message<String>> consumeCancel() {
252258
return message -> cancelVoltageInitAsync(VoltageInitCancelContext.fromMessage(message));

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@
7777
import static com.powsybl.network.store.model.NetworkStoreApi.VERSION;
7878
import static org.gridsuite.voltageinit.server.service.NotificationService.CANCEL_MESSAGE;
7979
import static org.gridsuite.voltageinit.server.service.NotificationService.HEADER_USER_ID;
80-
import static org.junit.Assert.assertEquals;
81-
import static org.junit.Assert.assertNotNull;
80+
import static org.junit.Assert.*;
8281
import static org.mockito.ArgumentMatchers.any;
8382
import static org.mockito.ArgumentMatchers.eq;
8483
import static org.mockito.BDDMockito.given;
@@ -173,6 +172,12 @@ private OpenReacResult buildOpenReacResult() {
173172
return openReacResult;
174173
}
175174

175+
private OpenReacResult buildNokOpenReacResult() {
176+
OpenReacAmplIOFiles openReacAmplIOFiles = new OpenReacAmplIOFiles(openReacParameters, network, false);
177+
openReacResult = new OpenReacResult(OpenReacStatus.NOT_OK, openReacAmplIOFiles, INDICATORS);
178+
return openReacResult;
179+
}
180+
176181
private VoltageInitParametersEntity buildVoltageInitParametersEntity() {
177182
return VoltageInitParametersInfos.builder()
178183
.voltageLimitsModification(List.of(VoltageLimitInfos.builder()
@@ -359,6 +364,37 @@ public void runTest() throws Exception {
359364

360365
}
361366

367+
@Test
368+
public void testReturnsResultAndDoesNotGenerateModificationIfResultNotOk() throws Exception {
369+
try (MockedStatic<OpenReacRunner> openReacRunnerMockedStatic = Mockito.mockStatic(OpenReacRunner.class)) {
370+
openReacRunnerMockedStatic.when(() -> OpenReacRunner.runAsync(eq(network), eq(VARIANT_2_ID), any(OpenReacParameters.class), any(OpenReacConfig.class), any(ComputationManager.class)))
371+
.thenReturn(CompletableFutureTask.runAsync(this::buildNokOpenReacResult, ForkJoinPool.commonPool()));
372+
373+
MvcResult result = mockMvc.perform(post(
374+
"/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
375+
.header(HEADER_USER_ID, "userId"))
376+
.andExpect(status().isOk())
377+
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
378+
.andReturn();
379+
assertEquals(RESULT_UUID, mapper.readValue(result.getResponse().getContentAsString(), UUID.class));
380+
381+
Message<byte[]> resultMessage = output.receive(TIMEOUT, "voltageinit.result");
382+
assertEquals(RESULT_UUID.toString(), resultMessage.getHeaders().get("resultUuid"));
383+
assertEquals("me", resultMessage.getHeaders().get("receiver"));
384+
385+
result = mockMvc.perform(get(
386+
"/" + VERSION + "/results/{resultUuid}", RESULT_UUID))
387+
.andExpect(status().isOk())
388+
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
389+
.andReturn();
390+
391+
VoltageInitResult resultDto = mapper.readValue(result.getResponse().getContentAsString(), VoltageInitResult.class);
392+
assertEquals(RESULT_UUID, resultDto.getResultUuid());
393+
assertEquals(INDICATORS, resultDto.getIndicators());
394+
assertNull(resultDto.getModificationsGroupUuid());
395+
}
396+
}
397+
362398
@Test
363399
public void runWrongNetworkTest() throws Exception {
364400
MvcResult result = mockMvc.perform(post(

0 commit comments

Comments
 (0)