From af102fce5e78b2ea76476983fd3fecb938410b2a Mon Sep 17 00:00:00 2001 From: Mathieu DEHARBE Date: Thu, 20 Jun 2024 15:00:35 +0200 Subject: [PATCH] post review Signed-off-by: Mathieu DEHARBE --- .../service/AbstractComputationService.java | 6 +- .../service/AbstractResultContext.java | 8 +-- .../commons/computation/ComputationTest.java | 63 +++++++++---------- 3 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/powsybl/ws/commons/computation/service/AbstractComputationService.java b/src/main/java/com/powsybl/ws/commons/computation/service/AbstractComputationService.java index 95575c0..e5a2b90 100644 --- a/src/main/java/com/powsybl/ws/commons/computation/service/AbstractComputationService.java +++ b/src/main/java/com/powsybl/ws/commons/computation/service/AbstractComputationService.java @@ -16,11 +16,11 @@ /** * @author Mathieu Deharbe - * @param run context specific to a computation, including parameters + * @param run context specific to a computation, including parameters * @param run service specific to a computation * @param enum status specific to a computation */ -public abstract class AbstractComputationService, T extends AbstractComputationResultService, S> { +public abstract class AbstractComputationService, T extends AbstractComputationResultService, S> { protected ObjectMapper objectMapper; protected NotificationService notificationService; @@ -47,7 +47,7 @@ public void stop(UUID resultUuid, String receiver) { public abstract List getProviders(); - public abstract UUID runAndSaveResult(R runContext); + public abstract UUID runAndSaveResult(C runContext); public void deleteResult(UUID resultUuid) { resultService.delete(resultUuid); diff --git a/src/main/java/com/powsybl/ws/commons/computation/service/AbstractResultContext.java b/src/main/java/com/powsybl/ws/commons/computation/service/AbstractResultContext.java index 80280dc..1fb8207 100644 --- a/src/main/java/com/powsybl/ws/commons/computation/service/AbstractResultContext.java +++ b/src/main/java/com/powsybl/ws/commons/computation/service/AbstractResultContext.java @@ -23,10 +23,10 @@ /** * @author Mathieu Deharbe - * @param run context specific to a computation, including parameters + * @param run context specific to a computation, including parameters */ @Data -public abstract class AbstractResultContext> { +public abstract class AbstractResultContext> { protected static final String RESULT_UUID_HEADER = "resultUuid"; @@ -43,9 +43,9 @@ public abstract class AbstractResultContext { + private static class MockComputationResultService extends AbstractComputationResultService { Map mockDBStatus = new HashMap<>(); @Override @@ -107,7 +102,7 @@ public MockComputationStatus findStatus(UUID resultUuid) { } } - public static class MockComputationObserver extends AbstractComputationObserver { + private static class MockComputationObserver extends AbstractComputationObserver { protected MockComputationObserver(@NonNull ObservationRegistry observationRegistry, @NonNull MeterRegistry meterRegistry) { super(observationRegistry, meterRegistry); } @@ -118,29 +113,29 @@ protected String getComputationType() { } @Override - protected String getResultStatus(MockComputationResult res) { + protected String getResultStatus(Object res) { return res != null ? "OK" : "NOK"; } } - public static class MockComputationRunContext extends AbstractComputationRunContext { + private static class MockComputationRunContext extends AbstractComputationRunContext { // makes the mock computation to behave in a specific way @Getter @Setter ComputationResultWanted computationResWanted = ComputationResultWanted.SUCCESS; protected MockComputationRunContext(UUID networkUuid, String variantId, String receiver, ReportInfos reportInfos, - String userId, String provider, MockComputationParameters parameters) { + String userId, String provider, Object parameters) { super(networkUuid, variantId, receiver, reportInfos, userId, provider, parameters); } } - public static class MockComputationResultContext extends AbstractResultContext { + private static class MockComputationResultContext extends AbstractResultContext { protected MockComputationResultContext(UUID resultUuid, MockComputationRunContext runContext) { super(resultUuid, runContext); } } - public class MockComputationService extends AbstractComputationService { + private static class MockComputationService extends AbstractComputationService { protected MockComputationService(NotificationService notificationService, MockComputationResultService resultService, ObjectMapper objectMapper, UuidGeneratorService uuidGeneratorService, String defaultProvider) { super(notificationService, resultService, objectMapper, uuidGeneratorService, defaultProvider); } @@ -152,17 +147,17 @@ public List getProviders() { @Override public UUID runAndSaveResult(MockComputationRunContext runContext) { - return resultUuid; + return RESULT_UUID; } } - enum ComputationResultWanted { + private enum ComputationResultWanted { SUCCESS, FAIL } - public class MockComputationWorkerService extends AbstractWorkerService { - protected MockComputationWorkerService(NetworkStoreService networkStoreService, NotificationService notificationService, ReportService reportService, MockComputationResultService resultService, ExecutionService executionService, AbstractComputationObserver observer, ObjectMapper objectMapper) { + private static class MockComputationWorkerService extends AbstractWorkerService { + protected MockComputationWorkerService(NetworkStoreService networkStoreService, NotificationService notificationService, ReportService reportService, MockComputationResultService resultService, ExecutionService executionService, AbstractComputationObserver observer, ObjectMapper objectMapper) { super(networkStoreService, notificationService, reportService, resultService, executionService, observer, objectMapper); } @@ -172,7 +167,7 @@ protected AbstractResultContext fromMessage(Message resultContext, MockComputationResult result) { } + protected void saveResult(Network network, AbstractResultContext resultContext, Object result) { } @Override protected String getComputationType() { @@ -180,14 +175,14 @@ protected String getComputationType() { } @Override - protected CompletableFuture getCompletableFuture(MockComputationRunContext runContext, String provider, UUID resultUuid) { - final CompletableFuture completableFuture = new CompletableFuture<>(); + protected CompletableFuture getCompletableFuture(MockComputationRunContext runContext, String provider, UUID resultUuid) { + final CompletableFuture completableFuture = new CompletableFuture<>(); switch (runContext.getComputationResWanted()) { case FAIL: - completableFuture.completeExceptionally(new RuntimeException("Computation failed but with an artificially longer messaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaage")); + completableFuture.completeExceptionally(new RuntimeException("Computation failed")); break; case SUCCESS: - return CompletableFuture.supplyAsync(MockComputationResult::new); + return CompletableFuture.supplyAsync(Object::new); } return completableFuture; } @@ -195,10 +190,10 @@ protected CompletableFuture getCompletableFuture(MockComp private MockComputationWorkerService workerService; private MockComputationService computationService; - private MockComputationResultContext resultContext; + private static MockComputationResultContext resultContext; final UUID networkUuid = UUID.fromString("11111111-1111-1111-1111-111111111111"); final UUID reportUuid = UUID.fromString("22222222-2222-2222-2222-222222222222"); - final UUID resultUuid = UUID.fromString("33333333-3333-3333-3333-333333333333"); + static final UUID RESULT_UUID = UUID.fromString("33333333-3333-3333-3333-333333333333"); final String reporterId = "44444444-4444-4444-4444-444444444444"; final String userId = "MockComputation_UserId"; final String receiver = "MockComputation_Receiver"; @@ -223,18 +218,18 @@ void init() { MessageBuilder builder = MessageBuilder .withPayload("") - .setHeader(HEADER_RESULT_UUID, resultUuid.toString()) + .setHeader(HEADER_RESULT_UUID, RESULT_UUID.toString()) .setHeader(HEADER_RECEIVER, receiver) .setHeader(HEADER_USER_ID, userId); message = builder.build(); runContext = new MockComputationRunContext(networkUuid, null, receiver, - new ReportInfos(reportUuid, reporterId, COMPUTATION_TYPE), userId, provider, new MockComputationParameters()); + new ReportInfos(reportUuid, reporterId, COMPUTATION_TYPE), userId, provider, new Object()); runContext.setNetwork(network); - resultContext = new MockComputationResultContext(resultUuid, runContext); + resultContext = new MockComputationResultContext(RESULT_UUID, runContext); } - void initComputationExecution() { + private void initComputationExecution() { when(networkStoreService.getNetwork(eq(networkUuid), any(PreloadingStrategy.class))) .thenReturn(network); when(network.getVariantManager()).thenReturn(variantManager); @@ -268,21 +263,21 @@ void testComputationFailed() { @Test void testComputationCancelled() { - MockComputationStatus baseStatus = NOT_DONE; - computationService.setStatus(List.of(resultUuid), baseStatus); - assertEquals(baseStatus, computationService.getStatus(resultUuid)); + MockComputationStatus baseStatus = MockComputationStatus.NOT_DONE; + computationService.setStatus(List.of(RESULT_UUID), baseStatus); + assertEquals(baseStatus, computationService.getStatus(RESULT_UUID)); - computationService.stop(resultUuid, receiver); + computationService.stop(RESULT_UUID, receiver); // test the course verify(notificationService.getPublisher(), times(1)).send(eq("publishCancel-out-0"), isA(Message.class)); Message cancelMessage = MessageBuilder.withPayload("") - .setHeader(HEADER_RESULT_UUID, resultUuid.toString()) + .setHeader(HEADER_RESULT_UUID, RESULT_UUID.toString()) .setHeader(HEADER_RECEIVER, receiver) .build(); CancelContext cancelContext = CancelContext.fromMessage(cancelMessage); - assertEquals(resultUuid, cancelContext.resultUuid()); + assertEquals(RESULT_UUID, cancelContext.resultUuid()); assertEquals(receiver, cancelContext.receiver()); } }