From af3326ac4896119db1f01b4b9a94b4edf6f7b68e Mon Sep 17 00:00:00 2001 From: Lakshmi Priya S Date: Wed, 22 May 2024 08:25:19 +0530 Subject: [PATCH 1/3] INSTA-3874: Informix DC unit tests added --- rdb/build.gradle | 2 + .../dc/rdb/impl/informix/InformixDcTest.java | 131 ++++++++++++++++++ .../rdb/impl/informix/InformixUtilTest.java | 16 +++ .../informix/OnstatCommandExecutorTest.java | 36 +++++ .../strategy/CommandExecutorStrategyTest.java | 40 ++++++ .../strategy/MetricsCollectorTest.java | 57 ++++++++ .../strategy/SqlExecutorStrategyTest.java | 55 ++++++++ 7 files changed, 337 insertions(+) create mode 100644 rdb/src/test/java/com/instana/dc/rdb/impl/informix/InformixDcTest.java create mode 100644 rdb/src/test/java/com/instana/dc/rdb/impl/informix/InformixUtilTest.java create mode 100644 rdb/src/test/java/com/instana/dc/rdb/impl/informix/OnstatCommandExecutorTest.java create mode 100644 rdb/src/test/java/com/instana/dc/rdb/impl/informix/metric/collection/strategy/CommandExecutorStrategyTest.java create mode 100644 rdb/src/test/java/com/instana/dc/rdb/impl/informix/metric/collection/strategy/MetricsCollectorTest.java create mode 100644 rdb/src/test/java/com/instana/dc/rdb/impl/informix/metric/collection/strategy/SqlExecutorStrategyTest.java diff --git a/rdb/build.gradle b/rdb/build.gradle index ef73245..f05f703 100644 --- a/rdb/build.gradle +++ b/rdb/build.gradle @@ -26,6 +26,8 @@ dependencies { testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1") + testImplementation("org.mockito:mockito-core:3.12.4") + testImplementation("org.mockito:mockito-inline:3.12.4") implementation("com.ibm.informix:jdbc:4.50.10") implementation("mysql:mysql-connector-java:5.1.46") diff --git a/rdb/src/test/java/com/instana/dc/rdb/impl/informix/InformixDcTest.java b/rdb/src/test/java/com/instana/dc/rdb/impl/informix/InformixDcTest.java new file mode 100644 index 0000000..21a8f5d --- /dev/null +++ b/rdb/src/test/java/com/instana/dc/rdb/impl/informix/InformixDcTest.java @@ -0,0 +1,131 @@ +package com.instana.dc.rdb.impl.informix; + +import com.instana.dc.rdb.impl.informix.metric.collection.strategy.MetricsCollector; +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.metrics.DoubleGaugeBuilder; +import io.opentelemetry.api.metrics.DoubleUpDownCounterBuilder; +import io.opentelemetry.api.metrics.LongGaugeBuilder; +import io.opentelemetry.api.metrics.LongUpDownCounterBuilder; +import io.opentelemetry.api.metrics.Meter; +import io.opentelemetry.api.metrics.MeterBuilder; +import org.apache.commons.dbcp2.BasicDataSource; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; + +import java.lang.reflect.Field; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.HashMap; +import java.util.Map; + +import static com.instana.dc.CalculationMode.RATE; +import static com.instana.dc.rdb.DbDcUtil.DB_PASSWORD; +import static com.instana.dc.rdb.DbDcUtil.DB_TRANSACTION_RATE_NAME; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +public class InformixDcTest { + + private static InformixDc informixDc; + private static final MetricsCollector metricsCollector = mock(MetricsCollector.class); + + @BeforeAll + public static void init() throws SQLException { + Map properties = new HashMap<>(); + properties.put(DB_PASSWORD, "password"); + Map customInput = new HashMap<>(); + customInput.put("db.names", "instana, test"); + properties.put("custom.input", customInput); + properties.put("custom.poll.interval", Map.of("HIGH", 10)); + try (MockedConstruction mockedDataSource = Mockito.mockConstruction(BasicDataSource.class, + (mock, context) -> { + Connection connection = mock(Connection.class); + Statement statement = mock(Statement.class); + given(statement.executeQuery(anyString())).willReturn(mock(ResultSet.class)); + given(connection.createStatement()).willReturn(statement); + given(mock.getConnection()).willReturn(connection); + })) { + informixDc = new InformixDc(properties, "informix", "com.informix.jdbc.IfxDriver"); + Field metricCollector = informixDc.getClass().getDeclaredField("metricCollector"); + metricCollector.setAccessible(true); + metricCollector.set(informixDc, metricsCollector); + System.out.println(); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + @Test + public void shouldRegisterMetricsMetadata() { + OpenTelemetry openTelemetry = mock(OpenTelemetry.class); + MeterBuilder meterBuilder = buildMeterBuilder(); + given(openTelemetry.meterBuilder(any())).willReturn(meterBuilder); + informixDc.initMeters(openTelemetry); + informixDc.registerMetrics(); + assertEquals(RATE, informixDc.getRawMetric(DB_TRANSACTION_RATE_NAME).getCalculationMode()); + } + + @Test + public void shouldCollectData() { + informixDc.collectData(); + verify(metricsCollector, times(23)).collectMetrics(anyString()); + } + + private static MeterBuilder buildMeterBuilder() { + MeterBuilder meterBuilder = mock(MeterBuilder.class); + given(meterBuilder.setInstrumentationVersion(any())).willReturn(meterBuilder); + Meter meter = buildMeter(); + given(meterBuilder.build()).willReturn(meter); + return meterBuilder; + } + + private static Meter buildMeter() { + Meter meter = mock(Meter.class); + LongUpDownCounterBuilder longUpDownCounterBuilder = buildLongUpDownCounterBuilder(); + given(meter.upDownCounterBuilder(any())).willReturn(longUpDownCounterBuilder); + DoubleGaugeBuilder doubleGaugeBuilder = buildDoubleGaugeBuilder(); + given(meter.gaugeBuilder(any())).willReturn(doubleGaugeBuilder); + return meter; + } + + private static DoubleGaugeBuilder buildDoubleGaugeBuilder() { + DoubleGaugeBuilder doubleGaugeBuilder = mock(DoubleGaugeBuilder.class); + given(doubleGaugeBuilder.setUnit(any())).willReturn(doubleGaugeBuilder); + given(doubleGaugeBuilder.setDescription(any())).willReturn(doubleGaugeBuilder); + LongGaugeBuilder longGaugeBuilder = buildLongGaugeBuilder(); + given(doubleGaugeBuilder.ofLongs()).willReturn(longGaugeBuilder); + return doubleGaugeBuilder; + } + + private static LongGaugeBuilder buildLongGaugeBuilder() { + LongGaugeBuilder longGaugeBuilder = mock(LongGaugeBuilder.class); + given(longGaugeBuilder.setUnit(any())).willReturn(longGaugeBuilder); + given(longGaugeBuilder.setDescription(any())).willReturn(longGaugeBuilder); + return longGaugeBuilder; + } + + private static LongUpDownCounterBuilder buildLongUpDownCounterBuilder() { + LongUpDownCounterBuilder longUpDownCounterBuilder = mock(LongUpDownCounterBuilder.class); + given(longUpDownCounterBuilder.setUnit(any())).willReturn(longUpDownCounterBuilder); + given(longUpDownCounterBuilder.setDescription(any())).willReturn(longUpDownCounterBuilder); + DoubleUpDownCounterBuilder doubleUpDownCounterBuilder = buildDoubleUpDownCounterBuilder(); + given(longUpDownCounterBuilder.ofDoubles()).willReturn(doubleUpDownCounterBuilder); + return longUpDownCounterBuilder; + } + + private static DoubleUpDownCounterBuilder buildDoubleUpDownCounterBuilder() { + DoubleUpDownCounterBuilder doubleUpDownCounterBuilder = mock(DoubleUpDownCounterBuilder.class); + given(doubleUpDownCounterBuilder.setUnit(any())).willReturn(doubleUpDownCounterBuilder); + given(doubleUpDownCounterBuilder.setDescription(any())).willReturn(doubleUpDownCounterBuilder); + return doubleUpDownCounterBuilder; + } +} diff --git a/rdb/src/test/java/com/instana/dc/rdb/impl/informix/InformixUtilTest.java b/rdb/src/test/java/com/instana/dc/rdb/impl/informix/InformixUtilTest.java new file mode 100644 index 0000000..87a79c7 --- /dev/null +++ b/rdb/src/test/java/com/instana/dc/rdb/impl/informix/InformixUtilTest.java @@ -0,0 +1,16 @@ +package com.instana.dc.rdb.impl.informix; + +import org.junit.jupiter.api.Test; + +import java.util.Base64; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class InformixUtilTest { + + @Test + public void shouldDecodePassword() { + assertEquals("encoded_password", InformixUtil.decodePassword( + Base64.getEncoder().encodeToString("encoded_password".getBytes()))); + } +} diff --git a/rdb/src/test/java/com/instana/dc/rdb/impl/informix/OnstatCommandExecutorTest.java b/rdb/src/test/java/com/instana/dc/rdb/impl/informix/OnstatCommandExecutorTest.java new file mode 100644 index 0000000..15825cf --- /dev/null +++ b/rdb/src/test/java/com/instana/dc/rdb/impl/informix/OnstatCommandExecutorTest.java @@ -0,0 +1,36 @@ +package com.instana.dc.rdb.impl.informix; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.BDDMockito.given; + +public class OnstatCommandExecutorTest { + + private static OnstatCommandExecutor onstatCommandExecutor; + + @BeforeAll + public static void init() { + onstatCommandExecutor = new OnstatCommandExecutor("dbPath", "server"); + } + + @Test + public void shouldExecuteCommand() { + assertFalse(onstatCommandExecutor.executeCommand("script.sh").isEmpty()); + } + + @Test + public void shouldExecuteCommandWithException() { + try (MockedConstruction processBuilder = Mockito.mockConstruction(ProcessBuilder.class, + (mock, context) -> given(mock.start()).willThrow(new IOException()))) { + assertTrue(onstatCommandExecutor.executeCommand("script.sh").isEmpty()); + } + + } +} diff --git a/rdb/src/test/java/com/instana/dc/rdb/impl/informix/metric/collection/strategy/CommandExecutorStrategyTest.java b/rdb/src/test/java/com/instana/dc/rdb/impl/informix/metric/collection/strategy/CommandExecutorStrategyTest.java new file mode 100644 index 0000000..5d385e0 --- /dev/null +++ b/rdb/src/test/java/com/instana/dc/rdb/impl/informix/metric/collection/strategy/CommandExecutorStrategyTest.java @@ -0,0 +1,40 @@ +package com.instana.dc.rdb.impl.informix.metric.collection.strategy; + +import com.instana.dc.rdb.impl.informix.OnstatCommandExecutor; +import com.instana.dc.rdb.impl.informix.metric.collection.MetricCollectionMode; +import com.instana.dc.rdb.impl.informix.metric.collection.MetricDataConfig; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; + +public class CommandExecutorStrategyTest { + + private static CommandExecutorStrategy commandExecutorStrategy; + private static OnstatCommandExecutor onstatCommandExecutor; + + @BeforeAll + public static void init() { + onstatCommandExecutor = mock(OnstatCommandExecutor.class); + commandExecutorStrategy = new CommandExecutorStrategy(onstatCommandExecutor); + } + + @Test + public void shouldCollectNumberMetrics() { + MetricDataConfig metricDataConfig = new MetricDataConfig("query", MetricCollectionMode.CMD, Number.class); + given(onstatCommandExecutor.executeCommand(any())).willReturn(Optional.of(new String[]{"1"})); + assertEquals((Number) 1, commandExecutorStrategy.collectMetrics(metricDataConfig)); + } + + @Test + public void shouldCollectDoubleMetrics() { + MetricDataConfig metricDataConfig = new MetricDataConfig("query", MetricCollectionMode.CMD, Double.class); + given(onstatCommandExecutor.executeCommand(any())).willReturn(Optional.of(new String[]{"1"})); + assertEquals((Number) 1.0, commandExecutorStrategy.collectMetrics(metricDataConfig)); + } +} diff --git a/rdb/src/test/java/com/instana/dc/rdb/impl/informix/metric/collection/strategy/MetricsCollectorTest.java b/rdb/src/test/java/com/instana/dc/rdb/impl/informix/metric/collection/strategy/MetricsCollectorTest.java new file mode 100644 index 0000000..8494596 --- /dev/null +++ b/rdb/src/test/java/com/instana/dc/rdb/impl/informix/metric/collection/strategy/MetricsCollectorTest.java @@ -0,0 +1,57 @@ +package com.instana.dc.rdb.impl.informix.metric.collection.strategy; + +import com.instana.dc.rdb.DbDcUtil; +import com.instana.dc.rdb.impl.informix.OnstatCommandExecutor; +import com.instana.dc.rdb.impl.informix.metric.collection.MetricCollectionMode; +import com.instana.dc.rdb.impl.informix.metric.collection.MetricDataConfig; +import com.instana.dc.rdb.impl.informix.metric.collection.MetricsDataConfigRegister; +import org.apache.commons.dbcp2.BasicDataSource; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; + +public class MetricsCollectorTest { + + private static BasicDataSource dataSource; + private static OnstatCommandExecutor onstatCommandExecutor; + private static MetricsCollector metricsCollector; + + @BeforeAll + public static void init() { + dataSource = mock(BasicDataSource.class); + onstatCommandExecutor = mock(OnstatCommandExecutor.class); + metricsCollector = new MetricsCollector(dataSource, onstatCommandExecutor); + } + + @Test + public void shouldCollectMetricsWithSQL() throws SQLException { + MetricsDataConfigRegister.subscribeMetricDataConfig("metric", + new MetricDataConfig("query", MetricCollectionMode.SQL, Number.class)); + Connection connection = mock(Connection.class); + given(dataSource.getConnection()).willReturn(connection); + try (MockedStatic utilities = mockStatic(DbDcUtil.class)) { + utilities.when(() -> DbDcUtil.getSimpleMetricWithSql(connection, "query")) + .thenReturn(1); + assertEquals((Number) 1, metricsCollector.collectMetrics("metric")); + } + + } + + @Test + public void shouldCollectMetricsWithCMD() { + MetricsDataConfigRegister.subscribeMetricDataConfig("metric", + new MetricDataConfig("query", MetricCollectionMode.CMD, Number.class)); + given(onstatCommandExecutor.executeCommand(any())).willReturn(Optional.of(new String[]{"1"})); + assertEquals((Number) 1, metricsCollector.collectMetrics("metric")); + } +} diff --git a/rdb/src/test/java/com/instana/dc/rdb/impl/informix/metric/collection/strategy/SqlExecutorStrategyTest.java b/rdb/src/test/java/com/instana/dc/rdb/impl/informix/metric/collection/strategy/SqlExecutorStrategyTest.java new file mode 100644 index 0000000..6fb3832 --- /dev/null +++ b/rdb/src/test/java/com/instana/dc/rdb/impl/informix/metric/collection/strategy/SqlExecutorStrategyTest.java @@ -0,0 +1,55 @@ +package com.instana.dc.rdb.impl.informix.metric.collection.strategy; + +import com.instana.dc.rdb.DbDcUtil; +import com.instana.dc.rdb.impl.informix.metric.collection.MetricCollectionMode; +import com.instana.dc.rdb.impl.informix.metric.collection.MetricDataConfig; +import org.apache.commons.dbcp2.BasicDataSource; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; + +public class SqlExecutorStrategyTest { + + private static final String QUERY = "query"; + private static BasicDataSource dataSource; + private static SqlExecutorStrategy sqlExecutorStrategy; + + @BeforeAll + public static void init() { + dataSource = mock(BasicDataSource.class); + sqlExecutorStrategy = new SqlExecutorStrategy(dataSource); + } + + @Test + public void shouldCollectSimpleMetrics() throws SQLException { + Connection connection = mock(Connection.class); + given(dataSource.getConnection()).willReturn(connection); + MetricDataConfig metricDataConfig = new MetricDataConfig(QUERY, MetricCollectionMode.SQL, Number.class); + try (MockedStatic utilities = mockStatic(DbDcUtil.class)) { + utilities.when(() -> DbDcUtil.getSimpleMetricWithSql(connection, QUERY)) + .thenReturn(1); + assertEquals((Number) 1, sqlExecutorStrategy.collectMetrics(metricDataConfig)); + } + } + + @Test + public void shouldCollectListMetrics() throws SQLException { + Connection connection = mock(Connection.class); + given(dataSource.getConnection()).willReturn(connection); + MetricDataConfig metricDataConfig = new MetricDataConfig(QUERY, MetricCollectionMode.SQL, List.class); + try (MockedStatic utilities = mockStatic(DbDcUtil.class)) { + utilities.when(() -> DbDcUtil.getMetricWithSql(connection, QUERY)) + .thenReturn(List.of(1)); + assertEquals(List.of(1), sqlExecutorStrategy.collectMetrics(metricDataConfig)); + } + } +} From b71bdd1f1f7bcd1ecc23ae2353523d8dcd5a917b Mon Sep 17 00:00:00 2001 From: Lakshmi Priya S Date: Wed, 22 May 2024 08:28:15 +0530 Subject: [PATCH 2/3] INSTA-3874: Informix DC unit tests added --- .../java/com/instana/dc/rdb/impl/informix/InformixDcTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/rdb/src/test/java/com/instana/dc/rdb/impl/informix/InformixDcTest.java b/rdb/src/test/java/com/instana/dc/rdb/impl/informix/InformixDcTest.java index 21a8f5d..0608bea 100644 --- a/rdb/src/test/java/com/instana/dc/rdb/impl/informix/InformixDcTest.java +++ b/rdb/src/test/java/com/instana/dc/rdb/impl/informix/InformixDcTest.java @@ -58,7 +58,6 @@ public static void init() throws SQLException { Field metricCollector = informixDc.getClass().getDeclaredField("metricCollector"); metricCollector.setAccessible(true); metricCollector.set(informixDc, metricsCollector); - System.out.println(); } catch (NoSuchFieldException | IllegalAccessException e) { throw new RuntimeException(e); } From 146ca0fd3cf374df245d9e421a3c915583a0ba29 Mon Sep 17 00:00:00 2001 From: Lakshmi Priya S Date: Wed, 22 May 2024 10:57:22 +0530 Subject: [PATCH 3/3] INSTA-3874: Informix DC unit tests added --- rdb/build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/rdb/build.gradle b/rdb/build.gradle index f05f703..63011de 100644 --- a/rdb/build.gradle +++ b/rdb/build.gradle @@ -26,7 +26,6 @@ dependencies { testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1") - testImplementation("org.mockito:mockito-core:3.12.4") testImplementation("org.mockito:mockito-inline:3.12.4") implementation("com.ibm.informix:jdbc:4.50.10")