Skip to content

Commit

Permalink
Updates our OTel plugin projects to use JUnit 5 from JUnit 4. Use pac…
Browse files Browse the repository at this point in the history
…kage protected access for the tests and methods. (opensearch-project#3970)

Signed-off-by: David Venable <[email protected]>
  • Loading branch information
dlvenable authored Jan 23, 2024
1 parent 11218ad commit 4071361
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml'
implementation libs.guava.core
testImplementation testLibs.junit.vintage
testImplementation 'org.assertj:assertj-core:3.24.2'
testImplementation testLibs.mockito.inline
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
import io.opentelemetry.proto.resource.v1.Resource;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.model.configuration.PluginSetting;
import org.opensearch.dataprepper.model.metric.Bucket;
import org.opensearch.dataprepper.model.metric.DefaultBucket;
Expand All @@ -38,8 +38,8 @@
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class MetricsPluginExponentialHistogramTest {
@ExtendWith(MockitoExtension.class)
class MetricsPluginExponentialHistogramTest {

private static final Double MAX_ERROR = 0.00001;

Expand All @@ -63,15 +63,15 @@ public class MetricsPluginExponentialHistogramTest {
.setFlags(1)
.build();

@Before
public void init() {
@BeforeEach
void init() {
PluginSetting testsettings = new PluginSetting("testsettings", Collections.emptyMap());
testsettings.setPipelineName("testpipeline");
rawProcessor = new OTelMetricsRawProcessor(testsettings, config);
}

@Test
public void testWithMaxScaleExceedingConfiguredNegativeScale() {
void testWithMaxScaleExceedingConfiguredNegativeScale() {
when(config.getExponentialHistogramMaxAllowedScale()).thenReturn(-2);
lenient().when(config.getCalculateExponentialHistogramBuckets()).thenReturn(true);
ExponentialHistogram histogram = ExponentialHistogram.newBuilder()
Expand All @@ -82,7 +82,7 @@ public void testWithMaxScaleExceedingConfiguredNegativeScale() {
}

@Test
public void testWithMaxScaleExceedingConfiguredPositiveScale() {
void testWithMaxScaleExceedingConfiguredPositiveScale() {
when(config.getExponentialHistogramMaxAllowedScale()).thenReturn(2);
lenient().when(config.getCalculateExponentialHistogramBuckets()).thenReturn(true);
ExponentialHistogram histogram = ExponentialHistogram.newBuilder()
Expand All @@ -93,7 +93,7 @@ public void testWithMaxScaleExceedingConfiguredPositiveScale() {
}

@Test
public void test() throws JsonProcessingException {
void test() throws JsonProcessingException {
when(config.getExponentialHistogramMaxAllowedScale()).thenReturn(10);
lenient().when(config.getCalculateExponentialHistogramBuckets()).thenReturn(true);
ExponentialHistogram histogram = ExponentialHistogram.newBuilder()
Expand All @@ -117,7 +117,7 @@ public void test() throws JsonProcessingException {
}

@Test
public void testWithHistogramCalculationFlagDisabled() throws JsonProcessingException {
void testWithHistogramCalculationFlagDisabled() throws JsonProcessingException {
when(config.getCalculateExponentialHistogramBuckets()).thenReturn(false);
lenient().when(config.getExponentialHistogramMaxAllowedScale()).thenReturn(10);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import io.opentelemetry.proto.metrics.v1.ScopeMetrics;
import io.opentelemetry.proto.resource.v1.Resource;
import org.apache.commons.codec.binary.Hex;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.model.configuration.PluginSetting;
import org.opensearch.dataprepper.model.metric.Metric;
import org.opensearch.dataprepper.model.record.Record;
Expand All @@ -40,12 +40,11 @@
import java.util.Random;
import java.util.concurrent.TimeUnit;

import static org.opensearch.dataprepper.plugins.processor.otelmetrics.OTelMetricsProtoHelperTest.getRandomBytes;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

@RunWith(MockitoJUnitRunner.class)
public class MetricsPluginGaugeTest {
@ExtendWith(MockitoExtension.class)
class MetricsPluginGaugeTest {

private static final Long START_TIME = TimeUnit.MILLISECONDS.toNanos(ZonedDateTime.of(
LocalDateTime.of(2020, 5, 24, 14, 0, 0),
Expand All @@ -65,15 +64,15 @@ private byte[] getRandomBytes(int len) {
return bytes;
}

@Before
public void init() {
@BeforeEach
void init() {
PluginSetting testsettings = new PluginSetting("testsettings", Collections.emptyMap());
testsettings.setPipelineName("testpipeline");
rawProcessor = new OTelMetricsRawProcessor(testsettings, new OtelMetricsRawProcessorConfig());
}

@Test
public void testInstrumentationLibrary() throws JsonProcessingException {
void testInstrumentationLibrary() throws JsonProcessingException {
NumberDataPoint.Builder p1 = NumberDataPoint.newBuilder().setAsInt(4);
Gauge gauge = Gauge.newBuilder().addDataPoints(p1).build();

Expand Down Expand Up @@ -127,7 +126,7 @@ public void testInstrumentationLibrary() throws JsonProcessingException {
}

@Test
public void testScopeMetricsLibrary() throws JsonProcessingException {
void testScopeMetricsLibrary() throws JsonProcessingException {
NumberDataPoint.Builder p1 = NumberDataPoint.newBuilder().setAsInt(4);
Gauge gauge = Gauge.newBuilder().addDataPoints(p1).build();

Expand Down Expand Up @@ -181,7 +180,7 @@ public void testScopeMetricsLibrary() throws JsonProcessingException {
}

@Test
public void testWithExemplar() throws JsonProcessingException {
void testWithExemplar() throws JsonProcessingException {

byte[] spanId = getRandomBytes(8);
byte[] traceId = getRandomBytes(8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import io.opentelemetry.proto.metrics.v1.InstrumentationLibraryMetrics;
import io.opentelemetry.proto.metrics.v1.ResourceMetrics;
import io.opentelemetry.proto.resource.v1.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.model.configuration.PluginSetting;
import org.opensearch.dataprepper.model.metric.Bucket;
import org.opensearch.dataprepper.model.metric.DefaultBucket;
Expand All @@ -35,8 +35,8 @@
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class MetricsPluginHistogramTest {
@ExtendWith(MockitoExtension.class)
class MetricsPluginHistogramTest {

private OTelMetricsRawProcessor rawProcessor;

Expand All @@ -56,16 +56,16 @@ public class MetricsPluginHistogramTest {
.setFlags(1)
.build();

@Before
public void init() {
@BeforeEach
void init() {
PluginSetting testsettings = new PluginSetting("testsettings", Collections.emptyMap());
testsettings.setPipelineName("testpipeline");
when(config.getFlattenAttributesFlag()).thenReturn(true);
rawProcessor = new OTelMetricsRawProcessor(testsettings, config);
}

@Test
public void test() throws JsonProcessingException {
void test() throws JsonProcessingException {
when(config.getCalculateHistogramBuckets()).thenReturn(true);
Histogram histogram = Histogram.newBuilder().addDataPoints(HISTOGRAM_DATA_POINT).build();

Expand All @@ -82,7 +82,7 @@ public void test() throws JsonProcessingException {
}

@Test
public void testWithConfigFlagDisabled() throws JsonProcessingException {
void testWithConfigFlagDisabled() throws JsonProcessingException {
when(config.getCalculateHistogramBuckets()).thenReturn(false);

Histogram histogram = Histogram.newBuilder().addDataPoints(HISTOGRAM_DATA_POINT).build();
Expand All @@ -98,7 +98,7 @@ public void testWithConfigFlagDisabled() throws JsonProcessingException {
}

@Test
public void testWithConfigFlagDisabledAndNoFlattenedAttributes() throws JsonProcessingException {
void testWithConfigFlagDisabledAndNoFlattenedAttributes() throws JsonProcessingException {
PluginSetting testsettings = new PluginSetting("testsettings", Collections.emptyMap());
testsettings.setPipelineName("testpipeline");
when(config.getFlattenAttributesFlag()).thenReturn(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,38 @@
import io.opentelemetry.proto.metrics.v1.ResourceMetrics;
import io.opentelemetry.proto.metrics.v1.Sum;
import io.opentelemetry.proto.resource.v1.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.model.configuration.PluginSetting;
import org.opensearch.dataprepper.model.record.Record;
import org.opensearch.dataprepper.model.metric.JacksonMetric;
import org.opensearch.dataprepper.model.record.Record;

import java.util.Arrays;
import java.util.Collections;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.hamcrest.Matchers.equalTo;

@RunWith(MockitoJUnitRunner.class)
public class MetricsPluginSumTest {
@ExtendWith(MockitoExtension.class)
class MetricsPluginSumTest {

OTelMetricsRawProcessor rawProcessor;

@Before
public void init() {
@BeforeEach
void init() {
PluginSetting testsettings = new PluginSetting("testsettings", Collections.emptyMap());
testsettings.setPipelineName("testpipeline");
rawProcessor = new OTelMetricsRawProcessor(testsettings, new OtelMetricsRawProcessorConfig());
}

@Test
public void test() throws JsonProcessingException {
void test() throws JsonProcessingException {

final KeyValue childAttr1 = KeyValue.newBuilder().setKey("statement").setValue(AnyValue.newBuilder()
.setIntValue(1_000).build()).build();
Expand Down Expand Up @@ -113,7 +113,7 @@ public void test() throws JsonProcessingException {
}

@Test
public void missingNameInvalidMetricTest() throws JsonProcessingException {
void missingNameInvalidMetricTest() throws JsonProcessingException {

final KeyValue childAttr1 = KeyValue.newBuilder().setKey("statement").setValue(AnyValue.newBuilder()
.setIntValue(1_000).build()).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,37 @@
import io.opentelemetry.proto.metrics.v1.Summary;
import io.opentelemetry.proto.metrics.v1.SummaryDataPoint;
import io.opentelemetry.proto.resource.v1.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.model.configuration.PluginSetting;
import org.opensearch.dataprepper.model.record.Record;
import org.opensearch.dataprepper.model.metric.JacksonMetric;
import org.opensearch.dataprepper.model.record.Record;

import java.util.Arrays;
import java.util.Collections;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

@RunWith(MockitoJUnitRunner.class)
public class MetricsPluginSummaryTest {
@ExtendWith(MockitoExtension.class)
class MetricsPluginSummaryTest {

private OTelMetricsRawProcessor rawProcessor;

@Before
public void init() {
@BeforeEach
void init() {
PluginSetting testsettings = new PluginSetting("testsettings", Collections.emptyMap());
testsettings.setPipelineName("testpipeline");
rawProcessor = new OTelMetricsRawProcessor(testsettings, new OtelMetricsRawProcessorConfig());
}

@Test
public void testSummaryProcessing() throws JsonProcessingException {
void testSummaryProcessing() throws JsonProcessingException {
SummaryDataPoint dataPoint = SummaryDataPoint.newBuilder()
.addQuantileValues(SummaryDataPoint.ValueAtQuantile.newBuilder()
.setQuantile(0.5)
Expand Down
1 change: 0 additions & 1 deletion data-prepper-plugins/otel-metrics-source/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ dependencies {
implementation libs.commons.lang3
implementation libs.bouncycastle.bcprov
implementation libs.bouncycastle.bcpkix
testImplementation testLibs.junit.vintage
testImplementation 'org.assertj:assertj-core:3.24.2'
testImplementation testLibs.mockito.inline
testImplementation libs.commons.io
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
import io.grpc.health.v1.HealthGrpc;
import io.grpc.inprocess.InProcessChannelBuilder;
import io.grpc.inprocess.InProcessServerBuilder;
import org.junit.Assert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;


public class HealthGrpcServiceTest {

Expand Down Expand Up @@ -60,6 +61,6 @@ void testHealthCheckResponse() {
HealthCheckResponse response =
blockingStub.check(HealthCheckRequest.newBuilder().build());

Assert.assertEquals(HealthCheckResponse.ServingStatus.SERVING, response.getStatus());
assertEquals(HealthCheckResponse.ServingStatus.SERVING, response.getStatus());
}
}
Loading

0 comments on commit 4071361

Please sign in to comment.