Skip to content

Commit bcacabb

Browse files
author
Slimane AMAR
committed
Fix tests
1 parent 8ff803b commit bcacabb

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/main/java/org/gridsuite/study/server/service/StudyServerExecutionService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ public CompletableFuture<Void> runAsync(Runnable runnable) {
3939
}
4040

4141
public CompletableFuture<Void> runAsyncAndComplete(Runnable runnable) {
42+
if (runnable == null) {
43+
return CompletableFuture.completedFuture(null);
44+
}
4245
return CompletableFuture.runAsync(runnable, executorService).whenCompleteAsync((r, t) -> logAsyncError(t));
4346
}
4447

4548
private void logAsyncError(Throwable e) {
46-
if (e != null) {
49+
if (LOGGER.isErrorEnabled() && e != null) {
4750
LOGGER.error(e.toString(), e);
4851
}
4952
}

src/test/java/org/gridsuite/study/server/LoadFLowUnitTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
import org.gridsuite.study.server.repository.StudyRepository;
1717
import org.gridsuite.study.server.service.*;
1818
import org.gridsuite.study.server.utils.elasticsearch.DisableElasticsearch;
19+
import org.junit.jupiter.api.BeforeEach;
1920
import org.junit.jupiter.api.Test;
2021
import org.mockito.ArgumentCaptor;
22+
import org.mockito.stubbing.Answer;
2123
import org.springframework.beans.factory.annotation.Autowired;
2224
import org.springframework.boot.test.context.SpringBootTest;
2325
import org.springframework.boot.test.mock.mockito.MockBean;
@@ -26,6 +28,7 @@
2628
import java.util.List;
2729
import java.util.Optional;
2830
import java.util.UUID;
31+
import java.util.concurrent.CompletableFuture;
2932

3033
import static org.assertj.core.api.Assertions.assertThat;
3134
import static org.gridsuite.study.server.dto.ComputationType.LOAD_FLOW;
@@ -71,6 +74,18 @@ class LoadFLowUnitTest {
7174
@MockBean
7275
StudyRepository studyRepository;
7376

77+
@SpyBean
78+
private StudyServerExecutionService studyServerExecutionService;
79+
80+
@BeforeEach
81+
void setup() {
82+
// Synchronize for tests
83+
when(studyServerExecutionService.runAsyncAndComplete(any(Runnable.class))).thenAnswer((Answer<CompletableFuture>) invocation -> {
84+
((Runnable) invocation.getArguments()[0]).run();
85+
return CompletableFuture.completedFuture(null);
86+
});
87+
}
88+
7489
@Test
7590
void testRunLoadFlow() {
7691
when(rootNetworkNodeInfoService.getComputationResultUuid(nodeUuid, rootNetworkUuid, LOAD_FLOW)).thenReturn(null);

src/test/java/org/gridsuite/study/server/NetworkModificationTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import org.gridsuite.study.server.service.*;
4747
import org.gridsuite.study.server.service.client.dynamicsecurityanalysis.DynamicSecurityAnalysisClient;
4848
import org.gridsuite.study.server.service.client.dynamicsimulation.DynamicSimulationClient;
49-
import org.gridsuite.study.server.service.LoadFlowService;
5049
import org.gridsuite.study.server.service.shortcircuit.ShortCircuitService;
5150
import org.gridsuite.study.server.utils.*;
5251
import org.gridsuite.study.server.utils.elasticsearch.DisableElasticsearch;
@@ -58,6 +57,7 @@
5857
import org.junit.jupiter.api.extension.ExtendWith;
5958
import org.junit.jupiter.params.ParameterizedTest;
6059
import org.junit.jupiter.params.provider.EnumSource;
60+
import org.mockito.stubbing.Answer;
6161
import org.slf4j.Logger;
6262
import org.slf4j.LoggerFactory;
6363
import org.springframework.beans.factory.annotation.Autowired;
@@ -76,6 +76,7 @@
7676
import org.springframework.test.web.servlet.MvcResult;
7777

7878
import java.util.*;
79+
import java.util.concurrent.CompletableFuture;
7980

8081
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
8182
import static org.gridsuite.study.server.StudyConstants.HEADER_ERROR_MESSAGE;
@@ -86,6 +87,7 @@
8687
import static org.gridsuite.study.server.utils.SendInput.POST_ACTION_SEND_INPUT;
8788
import static org.hamcrest.MatcherAssert.assertThat;
8889
import static org.junit.jupiter.api.Assertions.*;
90+
import static org.mockito.ArgumentMatchers.any;
8991
import static org.mockito.Mockito.doReturn;
9092
import static org.mockito.Mockito.when;
9193
import static org.springframework.http.MediaType.APPLICATION_JSON;
@@ -234,6 +236,9 @@ class NetworkModificationTest {
234236
@Autowired
235237
private TestUtils studyTestUtils;
236238

239+
@SpyBean
240+
private StudyServerExecutionService studyServerExecutionService;
241+
237242
@Autowired
238243
private ObjectMapper objectMapper;
239244

@@ -261,6 +266,12 @@ void setup(final MockWebServer server) {
261266

262267
when(networkStoreService.getNetwork(NETWORK_UUID)).thenReturn(network);
263268

269+
// Synchronize for tests
270+
when(studyServerExecutionService.runAsyncAndComplete(any(Runnable.class))).thenAnswer((Answer<CompletableFuture>) invocation -> {
271+
((Runnable) invocation.getArguments()[0]).run();
272+
return CompletableFuture.completedFuture(null);
273+
});
274+
264275
wireMockServer = new WireMockServer(wireMockConfig().dynamicPort().extensions(new SendInput(input)));
265276
wireMockUtils = new WireMockUtils(wireMockServer);
266277

0 commit comments

Comments
 (0)