From 824bbb9c31881ca821a4f75bbc77d05fe7ed4c04 Mon Sep 17 00:00:00 2001 From: almeidast Date: Wed, 25 Sep 2024 18:41:03 +0100 Subject: [PATCH] fix package of DequeueStatisticCollectorTest add more testes for failure cases simplify code as suggested in PR --- .../DequeueStatisticCollectorTest.java | 63 ++++++++++++++++--- 1 file changed, 54 insertions(+), 9 deletions(-) rename src/test/java/org/swisspush/redisques/{ => util}/DequeueStatisticCollectorTest.java (50%) diff --git a/src/test/java/org/swisspush/redisques/DequeueStatisticCollectorTest.java b/src/test/java/org/swisspush/redisques/util/DequeueStatisticCollectorTest.java similarity index 50% rename from src/test/java/org/swisspush/redisques/DequeueStatisticCollectorTest.java rename to src/test/java/org/swisspush/redisques/util/DequeueStatisticCollectorTest.java index 219d49dd..a9ea3332 100644 --- a/src/test/java/org/swisspush/redisques/DequeueStatisticCollectorTest.java +++ b/src/test/java/org/swisspush/redisques/util/DequeueStatisticCollectorTest.java @@ -1,4 +1,4 @@ -package org.swisspush.redisques; +package org.swisspush.redisques.util; import io.vertx.core.Future; import io.vertx.core.Vertx; @@ -10,8 +10,6 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.swisspush.redisques.util.DequeueStatistic; -import org.swisspush.redisques.util.DequeueStatisticCollector; import java.util.HashMap; import java.util.Map; @@ -33,6 +31,7 @@ public void setUp() { sharedData = mock(SharedData.class); asyncMap = mock(AsyncMap.class); + // Mock sharedData.getAsyncMap to return asyncMap doAnswer(invocation -> { io.vertx.core.Handler>> handler = invocation.getArgument(1); handler.handle(Future.succeededFuture(asyncMap)); @@ -41,19 +40,19 @@ public void setUp() { when(vertx.sharedData()).thenReturn(sharedData); - // Set up the enabled and disabled DequeueStatisticCollector + // Initialize DequeueStatisticCollector with enabled/disabled stats collection dequeueStatisticCollectorEnabled = new DequeueStatisticCollector(vertx, true); dequeueStatisticCollectorDisabled = new DequeueStatisticCollector(vertx, false); } @Test public void testGetAllDequeueStatisticsEnabled(TestContext context) { - // Mocking asyncMap.entries() to return a non-empty map + // Mock asyncMap.entries() to return a non-empty map Map dequeueStats = new HashMap<>(); dequeueStats.put("queue1", new DequeueStatistic()); when(asyncMap.entries()).thenReturn(Future.succeededFuture(dequeueStats)); - // Test for when dequeue statistics are enabled + // Test when dequeue statistics are enabled Async async = context.async(); dequeueStatisticCollectorEnabled.getAllDequeueStatistics().onComplete(result -> { context.assertTrue(result.succeeded()); @@ -68,7 +67,7 @@ public void testGetAllDequeueStatisticsEnabled(TestContext context) { @Test public void testGetAllDequeueStatisticsDisabled(TestContext context) { - // Test for when dequeue statistics are disabled + // Test when dequeue statistics are disabled Async async = context.async(); dequeueStatisticCollectorDisabled.getAllDequeueStatistics().onComplete(result -> { context.assertTrue(result.succeeded()); @@ -77,7 +76,53 @@ public void testGetAllDequeueStatisticsDisabled(TestContext context) { }); // Verify that sharedData and asyncMap were NOT used - verify(sharedData, never()).getAsyncMap(anyString(), any()); - verify(asyncMap, never()).entries(); + verifyNoInteractions(sharedData); + verifyNoInteractions(asyncMap); + } + + @Test + public void testGetAllDequeueStatisticsAsyncMapFailure(TestContext context) { + // Simulate failure in sharedData.getAsyncMap + doAnswer(invocation -> { + io.vertx.core.Handler>> handler = invocation.getArgument(1); + handler.handle(Future.failedFuture(new RuntimeException("Failed to retrieve async map"))); + return null; + }).when(sharedData).getAsyncMap(anyString(), any()); + + // Test when asyncMap retrieval fails + Async async = context.async(); + dequeueStatisticCollectorEnabled.getAllDequeueStatistics().onComplete(result -> { + context.assertTrue(result.failed()); + context.assertEquals("Failed to retrieve async map", result.cause().getMessage()); + async.complete(); + }); + + // Verify that sharedData.getAsyncMap was used, but asyncMap.entries() was not + verify(sharedData, times(1)).getAsyncMap(anyString(), any()); + verifyNoInteractions(asyncMap); + } + + @Test + public void testGetAllDequeueStatisticsEntriesFailure(TestContext context) { + // Simulate success in sharedData.getAsyncMap, but failure in asyncMap.entries + doAnswer(invocation -> { + io.vertx.core.Handler>> handler = invocation.getArgument(1); + handler.handle(Future.succeededFuture(asyncMap)); + return null; + }).when(sharedData).getAsyncMap(anyString(), any()); + + when(asyncMap.entries()).thenReturn(Future.failedFuture(new RuntimeException("Failed to retrieve entries"))); + + // Test when asyncMap.entries fails + Async async = context.async(); + dequeueStatisticCollectorEnabled.getAllDequeueStatistics().onComplete(result -> { + context.assertTrue(result.failed()); + context.assertEquals("Failed to retrieve entries", result.cause().getMessage()); + async.complete(); + }); + + // Verify that sharedData.getAsyncMap and asyncMap.entries were used correctly + verify(sharedData, times(1)).getAsyncMap(anyString(), any()); + verify(asyncMap, times(1)).entries(); } }