-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from instana/INSTA-3874_informix_DC_unit_tests
Informix DC unit tests
- Loading branch information
Showing
7 changed files
with
335 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
rdb/src/test/java/com/instana/dc/rdb/impl/informix/InformixDcTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
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<String, Object> properties = new HashMap<>(); | ||
properties.put(DB_PASSWORD, "password"); | ||
Map<String, Object> 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<BasicDataSource> 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); | ||
} 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; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
rdb/src/test/java/com/instana/dc/rdb/impl/informix/InformixUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()))); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
rdb/src/test/java/com/instana/dc/rdb/impl/informix/OnstatCommandExecutorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> processBuilder = Mockito.mockConstruction(ProcessBuilder.class, | ||
(mock, context) -> given(mock.start()).willThrow(new IOException()))) { | ||
assertTrue(onstatCommandExecutor.executeCommand("script.sh").isEmpty()); | ||
} | ||
|
||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../instana/dc/rdb/impl/informix/metric/collection/strategy/CommandExecutorStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...ava/com/instana/dc/rdb/impl/informix/metric/collection/strategy/MetricsCollectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<DbDcUtil> 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")); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
.../com/instana/dc/rdb/impl/informix/metric/collection/strategy/SqlExecutorStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<DbDcUtil> 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<DbDcUtil> utilities = mockStatic(DbDcUtil.class)) { | ||
utilities.when(() -> DbDcUtil.getMetricWithSql(connection, QUERY)) | ||
.thenReturn(List.of(1)); | ||
assertEquals(List.of(1), sqlExecutorStrategy.collectMetrics(metricDataConfig)); | ||
} | ||
} | ||
} |