From 835bc88434fc0950387b50a6e0abae250b8be4b8 Mon Sep 17 00:00:00 2001 From: Alfredo Gutierrez Date: Tue, 6 Aug 2024 15:35:54 -0600 Subject: [PATCH] Adding Unit Tests Signed-off-by: Alfredo Gutierrez --- .../config/BlockNodeContextFactoryTest.java | 17 +++++++++ .../server/config/BlockNodeContextTest.java | 21 ++++++++++ .../server/metrics/MetricsServiceTest.java | 38 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 server/src/test/java/com/hedera/block/server/config/BlockNodeContextFactoryTest.java create mode 100644 server/src/test/java/com/hedera/block/server/config/BlockNodeContextTest.java create mode 100644 server/src/test/java/com/hedera/block/server/metrics/MetricsServiceTest.java diff --git a/server/src/test/java/com/hedera/block/server/config/BlockNodeContextFactoryTest.java b/server/src/test/java/com/hedera/block/server/config/BlockNodeContextFactoryTest.java new file mode 100644 index 000000000..d7e947884 --- /dev/null +++ b/server/src/test/java/com/hedera/block/server/config/BlockNodeContextFactoryTest.java @@ -0,0 +1,17 @@ +package com.hedera.block.server.config; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class BlockNodeContextFactoryTest { + + @Test + void create_returnsBlockNodeContext() { + BlockNodeContext context = BlockNodeContextFactory.create(); + + assertNotNull(context.metrics()); + assertNotNull(context.configuration()); + } + +} diff --git a/server/src/test/java/com/hedera/block/server/config/BlockNodeContextTest.java b/server/src/test/java/com/hedera/block/server/config/BlockNodeContextTest.java new file mode 100644 index 000000000..d12a5719b --- /dev/null +++ b/server/src/test/java/com/hedera/block/server/config/BlockNodeContextTest.java @@ -0,0 +1,21 @@ +package com.hedera.block.server.config; + +import com.swirlds.config.api.Configuration; +import com.swirlds.metrics.api.Metrics; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class BlockNodeContextTest { + + @Test + void BlockNodeContext_initializesWithMetricsAndConfiguration() { + Metrics metrics = mock(Metrics.class); + Configuration configuration = mock(Configuration.class); + + BlockNodeContext context = new BlockNodeContext(metrics, configuration); + + assertEquals(metrics, context.metrics()); + assertEquals(configuration, context.configuration()); + } +} diff --git a/server/src/test/java/com/hedera/block/server/metrics/MetricsServiceTest.java b/server/src/test/java/com/hedera/block/server/metrics/MetricsServiceTest.java new file mode 100644 index 000000000..21fd9e9aa --- /dev/null +++ b/server/src/test/java/com/hedera/block/server/metrics/MetricsServiceTest.java @@ -0,0 +1,38 @@ +package com.hedera.block.server.metrics; + +import com.swirlds.metrics.api.Counter; +import com.swirlds.metrics.api.LongGauge; +import com.swirlds.metrics.api.Metrics; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class MetricsServiceTest { + + @Test + void MetricsService_initializesExampleGauge() { + Metrics metrics = mock(Metrics.class); + LongGauge exampleGauge = mock(LongGauge.class); + when(metrics.getOrCreate(any(LongGauge.Config.class))).thenReturn(exampleGauge); + + MetricsService service = new MetricsService(metrics); + + assertEquals(exampleGauge, service.exampleGauge); + } + + @Test + void MetricsService_initializesExampleCounter() { + Metrics metrics = mock(Metrics.class); + Counter exampleCounter = mock(Counter.class); + when(metrics.getOrCreate(any(Counter.Config.class))).thenReturn(exampleCounter); + + MetricsService service = new MetricsService(metrics); + + assertEquals(exampleCounter, service.exampleCounter); + } + + @Test + void MetricsService_handlesNullMetrics() { + assertThrows(NullPointerException.class, () -> new MetricsService(null)); + } +}