From 2e024ea93c23bbaa275473e9f0529c9be57fa5c6 Mon Sep 17 00:00:00 2001 From: Mathieu DEHARBE Date: Tue, 18 Jun 2024 12:45:37 +0200 Subject: [PATCH] notificationService implementation instead of Mock Signed-off-by: Mathieu DEHARBE --- .../commons/computation/ComputationTest.java | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/powsybl/ws/commons/computation/ComputationTest.java b/src/test/java/com/powsybl/ws/commons/computation/ComputationTest.java index f551937..825046c 100644 --- a/src/test/java/com/powsybl/ws/commons/computation/ComputationTest.java +++ b/src/test/java/com/powsybl/ws/commons/computation/ComputationTest.java @@ -30,6 +30,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.cloud.stream.function.StreamBridge; import org.springframework.messaging.Message; import org.springframework.messaging.support.MessageBuilder; @@ -56,14 +57,15 @@ class ComputationTest implements WithAssertions { private static final String COMPUTATION_TYPE = "mockComputation"; @Mock + private VariantManager variantManager; + @Mock private NetworkStoreService networkStoreService; @Mock private ReportService reportService; + private final ExecutionService executionService = new ExecutionService(); + private final UuidGeneratorService uuidGeneratorService = new UuidGeneratorService(); @Mock - private ExecutionService executionService; - @Mock - private UuidGeneratorService uuidGeneratorService = new UuidGeneratorService(); - @Mock + private StreamBridge publisher; private NotificationService notificationService; @Mock private ObjectMapper objectMapper; @@ -182,7 +184,7 @@ protected CompletableFuture getCompletableFuture(MockComp final CompletableFuture completableFuture = new CompletableFuture<>(); switch (runContext.getComputationResWanted()) { case FAIL: - completableFuture.completeExceptionally(new RuntimeException("Computation failed")); + completableFuture.completeExceptionally(new RuntimeException("Computation failed but with an artificially longer messaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaage")); break; case SUCCESS: return CompletableFuture.supplyAsync(MockComputationResult::new); @@ -191,11 +193,7 @@ protected CompletableFuture getCompletableFuture(MockComp } } - @Mock - private VariantManager variantManager; - @Mock private MockComputationWorkerService workerService; - @Mock private MockComputationService computationService; private MockComputationResultContext resultContext; final UUID networkUuid = UUID.fromString("11111111-1111-1111-1111-111111111111"); @@ -211,6 +209,7 @@ protected CompletableFuture getCompletableFuture(MockComp @BeforeEach void init() { MockComputationResultService resultService = new MockComputationResultService(); + notificationService = new NotificationService(publisher); workerService = new MockComputationWorkerService( networkStoreService, notificationService, @@ -251,8 +250,7 @@ void testComputationSuccess() { workerService.consumeRun().accept(message); // test the course - verify(notificationService, times(1)) - .sendResultMessage(resultUuid, receiver, userId, null); + verify(notificationService.getPublisher(), times(1)).send(eq("publishResult-out-0"), isA(Message.class)); } @Test @@ -265,8 +263,7 @@ void testComputationFailed() { workerService.consumeRun().accept(message); // test the course - verify(notificationService, times(1)) - .publishFail(resultUuid, receiver, "java.lang.RuntimeException: Computation failed", userId, COMPUTATION_TYPE, null); + verify(notificationService.getPublisher(), times(1)).send(eq("publishFailed-out-0"), isA(Message.class)); } @Test @@ -278,10 +275,8 @@ void testComputationCancelled() { computationService.stop(resultUuid, receiver); // test the course - verify(notificationService, times(1)) - .sendCancelMessage(isA(Message.class)); + verify(notificationService.getPublisher(), times(1)).send(eq("publishCancel-out-0"), isA(Message.class)); - // TODO : how to get the message from the previous call instead of creating this one ? Message cancelMessage = MessageBuilder.withPayload("") .setHeader(HEADER_RESULT_UUID, resultUuid.toString()) .setHeader(HEADER_RECEIVER, receiver)