Skip to content

Commit

Permalink
Mockito 5 (#4712)
Browse files Browse the repository at this point in the history
Mockito 5

* Synchronize the MetricsTestUtil methods to avoid test failures.
* Create a copy of the collections to remove in MetricsTestUtil.
* Updated two tests to JUnit 5 and to use mocks instead of actual metrics. Updates to MetricsTestUtil to provide clarity on NPEs.

Signed-off-by: David Venable <[email protected]>
  • Loading branch information
dlvenable authored Jul 11, 2024
1 parent 5420162 commit 8a1a416
Show file tree
Hide file tree
Showing 50 changed files with 89 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,37 @@
package org.opensearch.dataprepper.metrics;

import io.micrometer.core.instrument.Measurement;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Statistic;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class MetricsTestUtil {

public static void initMetrics() {
Metrics.globalRegistry.getRegistries().forEach(meterRegistry -> Metrics.globalRegistry.remove(meterRegistry));
Metrics.globalRegistry.getMeters().forEach(meter -> Metrics.globalRegistry.remove(meter));
public static synchronized void initMetrics() {
final Set<MeterRegistry> registries = new HashSet<>(Metrics.globalRegistry.getRegistries());
registries.forEach(Metrics.globalRegistry::remove);

final List<Meter> meters = new ArrayList<>(Metrics.globalRegistry.getMeters());
meters.forEach(Metrics.globalRegistry::remove);

Metrics.addRegistry(new SimpleMeterRegistry());
}

public static List<Measurement> getMeasurementList(final String meterName) {
return StreamSupport.stream(getRegistry().find(meterName).meter().measure().spliterator(), false)
public static synchronized List<Measurement> getMeasurementList(final String meterName) {
final Meter meter = getRegistry().find(meterName).meter();
if(meter == null)
throw new RuntimeException("No metrics meter is available for " + meterName);

return StreamSupport.stream(meter.measure().spliterator(), false)
.collect(Collectors.toList());
}

Expand Down
1 change: 0 additions & 1 deletion data-prepper-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ dependencies {
implementation 'software.amazon.awssdk:servicediscovery'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
testImplementation testLibs.junit.vintage
testImplementation testLibs.mockito.inline
testImplementation libs.commons.lang3
testImplementation project(':data-prepper-test-event')
testImplementation project(':data-prepper-test-common')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,33 @@

import com.linecorp.armeria.client.Endpoint;
import com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroup;
import io.micrometer.core.instrument.Measurement;
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.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opensearch.dataprepper.metrics.MetricNames;
import org.opensearch.dataprepper.metrics.MetricsTestUtil;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.metrics.PluginMetrics;
import org.opensearch.dataprepper.peerforwarder.HashRing;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;
import java.util.concurrent.CompletableFuture;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.function.ToDoubleFunction;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.opensearch.dataprepper.peerforwarder.discovery.PeerListProvider.PEER_ENDPOINTS;

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

private static final String ENDPOINT_1 = "10.1.1.1";
Expand All @@ -39,43 +42,40 @@ public class DnsPeerListProviderTest {
Endpoint.of(ENDPOINT_1),
Endpoint.of(ENDPOINT_2)
);
private static final String COMPONENT_SCOPE = "testComponentScope";
private static final String COMPONENT_ID = "testComponentId";

@Mock
private DnsAddressEndpointGroup dnsAddressEndpointGroup;

@Mock
private HashRing hashRing;

@Mock
private PluginMetrics pluginMetrics;

private CompletableFuture completableFuture;

private DnsPeerListProvider dnsPeerListProvider;

@Before
@BeforeEach
public void setup() {
MetricsTestUtil.initMetrics();
completableFuture = CompletableFuture.completedFuture(null);
when(dnsAddressEndpointGroup.whenReady()).thenReturn(completableFuture);

pluginMetrics = PluginMetrics.fromNames(COMPONENT_ID, COMPONENT_SCOPE);
dnsPeerListProvider = new DnsPeerListProvider(dnsAddressEndpointGroup, pluginMetrics);
}

@Test(expected = NullPointerException.class)
@Test
public void testDefaultListProviderWithNullHostname() {
new DnsPeerListProvider(null, pluginMetrics);
assertThrows(NullPointerException.class, () -> new DnsPeerListProvider(null, pluginMetrics));
}

@Test(expected = RuntimeException.class)
@Test
public void testConstructWithInterruptedException() throws Exception {
CompletableFuture mockFuture = mock(CompletableFuture.class);
when(mockFuture.get()).thenThrow(new InterruptedException());
when(dnsAddressEndpointGroup.whenReady()).thenReturn(mockFuture);

new DnsPeerListProvider(dnsAddressEndpointGroup, pluginMetrics);
assertThrows(RuntimeException.class, () -> new DnsPeerListProvider(dnsAddressEndpointGroup, pluginMetrics));
}

@Test
Expand All @@ -90,17 +90,27 @@ public void testGetPeerList() {
}

@Test
public void testActivePeerCounter() {
public void testActivePeerCounter_with_list() {
when(dnsAddressEndpointGroup.endpoints()).thenReturn(ENDPOINT_LIST);

final List<Measurement> endpointsMeasures = MetricsTestUtil.getMeasurementList(new StringJoiner(MetricNames.DELIMITER).add(COMPONENT_SCOPE).add(COMPONENT_ID)
.add(PeerListProvider.PEER_ENDPOINTS).toString());
assertEquals(1, endpointsMeasures.size());
final Measurement endpointsMeasure = endpointsMeasures.get(0);
assertEquals(2.0, endpointsMeasure.getValue(), 0);
final ArgumentCaptor<ToDoubleFunction<DnsAddressEndpointGroup>> gaugeFunctionCaptor = ArgumentCaptor.forClass(ToDoubleFunction.class);
verify(pluginMetrics).gauge(eq(PEER_ENDPOINTS), eq(dnsAddressEndpointGroup), gaugeFunctionCaptor.capture());

final ToDoubleFunction<DnsAddressEndpointGroup> gaugeFunction = gaugeFunctionCaptor.getValue();

assertThat(gaugeFunction.applyAsDouble(dnsAddressEndpointGroup), equalTo(2.0));
}

@Test
public void testActivePeerCounter_with_single() {
when(dnsAddressEndpointGroup.endpoints()).thenReturn(Collections.singletonList(Endpoint.of(ENDPOINT_1)));
assertEquals(1.0, endpointsMeasure.getValue(), 0);

final ArgumentCaptor<ToDoubleFunction<DnsAddressEndpointGroup>> gaugeFunctionCaptor = ArgumentCaptor.forClass(ToDoubleFunction.class);
verify(pluginMetrics).gauge(eq(PEER_ENDPOINTS), eq(dnsAddressEndpointGroup), gaugeFunctionCaptor.capture());

final ToDoubleFunction<DnsAddressEndpointGroup> gaugeFunction = gaugeFunctionCaptor.getValue();

assertThat(gaugeFunction.applyAsDouble(dnsAddressEndpointGroup), equalTo(1.0));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,58 @@

package org.opensearch.dataprepper.peerforwarder.discovery;

import io.micrometer.core.instrument.Measurement;
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.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opensearch.dataprepper.metrics.MetricNames;
import org.opensearch.dataprepper.metrics.MetricsTestUtil;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.metrics.PluginMetrics;
import org.opensearch.dataprepper.peerforwarder.HashRing;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;

import static org.junit.Assert.assertEquals;
import java.util.function.ToDoubleFunction;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.opensearch.dataprepper.peerforwarder.discovery.PeerListProvider.PEER_ENDPOINTS;

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

private static final String ENDPOINT_1 = "10.10.0.1";
private static final String ENDPOINT_2 = "10.10.0.2";
private static final List<String> ENDPOINT_LIST = Arrays.asList(ENDPOINT_1, ENDPOINT_2);
private static final String COMPONENT_SCOPE = "testComponentScope";
private static final String COMPONENT_ID = "testComponentId";

@Mock
private HashRing hashRing;

@Mock
private PluginMetrics pluginMetrics;

private StaticPeerListProvider staticPeerListProvider;

@Before
@BeforeEach
public void setup() {
MetricsTestUtil.initMetrics();
pluginMetrics = PluginMetrics.fromNames(COMPONENT_ID, COMPONENT_SCOPE);
staticPeerListProvider = new StaticPeerListProvider(ENDPOINT_LIST, pluginMetrics);
}

@Test(expected = RuntimeException.class)
@Test
public void testListProviderWithEmptyList() {
new StaticPeerListProvider(Collections.emptyList(), pluginMetrics);
assertThrows(RuntimeException.class, () -> new StaticPeerListProvider(Collections.emptyList(), pluginMetrics));
}

@Test(expected = RuntimeException.class)
@Test
public void testListProviderWithNullList() {
new StaticPeerListProvider(null, pluginMetrics);
assertThrows(RuntimeException.class, () -> new StaticPeerListProvider(null, pluginMetrics));
}

@Test
Expand All @@ -65,11 +67,12 @@ public void testListProviderWithNonEmptyList() {

@Test
public void testActivePeerCounter() {
final List<Measurement> endpointsMeasures = MetricsTestUtil.getMeasurementList(
new StringJoiner(MetricNames.DELIMITER).add(COMPONENT_SCOPE).add(COMPONENT_ID).add(PeerListProvider.PEER_ENDPOINTS).toString());
assertEquals(1, endpointsMeasures.size());
final Measurement endpointsMeasure = endpointsMeasures.get(0);
assertEquals(2.0, endpointsMeasure.getValue(), 0);
final ArgumentCaptor<ToDoubleFunction<List<String>>> gaugeFunctionCaptor = ArgumentCaptor.forClass(ToDoubleFunction.class);
verify(pluginMetrics).gauge(eq(PEER_ENDPOINTS), any(List.class), gaugeFunctionCaptor.capture());

final ToDoubleFunction<List<String>> gaugeFunction = gaugeFunctionCaptor.getValue();

assertThat(gaugeFunction.applyAsDouble(ENDPOINT_LIST), equalTo(2.0));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

import org.opensearch.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Arrays;
import java.util.concurrent.ExecutionException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;
import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient;

import static org.hamcrest.CoreMatchers.notNullValue;
Expand Down

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion data-prepper-logstash-configuration/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation libs.commons.lang3
testImplementation testLibs.slf4j.simple
testImplementation testLibs.mockito.inline
}

generateGrammarSource {
Expand Down
5 changes: 0 additions & 5 deletions data-prepper-pipeline-parser/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ dependencies {
testImplementation testLibs.bundles.junit
testImplementation testLibs.bundles.mockito
testImplementation testLibs.hamcrest
testImplementation 'org.powermock:powermock-module-junit4:2.0.9'
testImplementation 'org.powermock:powermock-api-mockito2:2.0.9'
testImplementation 'org.assertj:assertj-core:3.20.2'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.powermock:powermock-module-junit4:2.0.9'
testImplementation 'org.powermock:powermock-api-mockito2:2.0.9'
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class EventKeyDeserializerTest {
Expand Down
1 change: 0 additions & 1 deletion data-prepper-plugin-framework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ dependencies {
}
implementation libs.reflections.core
implementation 'com.fasterxml.jackson.core:jackson-databind'
testImplementation testLibs.mockito.inline
}
1 change: 0 additions & 1 deletion data-prepper-plugins/aggregate-processor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ dependencies {
implementation libs.opentelemetry.proto
implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation 'io.micrometer:micrometer-core'
testImplementation testLibs.mockito.inline
}

jacocoTestCoverageVerification {
Expand Down

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion data-prepper-plugins/cloudwatch-logs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ dependencies {
implementation 'org.projectlombok:lombok:1.18.26'
implementation 'org.hibernate.validator:hibernate-validator:8.0.0.Final'
testImplementation project(path: ':data-prepper-test-common')
testImplementation testLibs.mockito.inline
compileOnly 'org.projectlombok:lombok:1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'
}
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion data-prepper-plugins/common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ dependencies {
testImplementation project(':data-prepper-plugins:blocking-buffer')
testImplementation project(':data-prepper-test-event')
testImplementation libs.commons.io
testImplementation testLibs.mockito.inline
}

jacocoTestCoverageVerification {
Expand Down
1 change: 0 additions & 1 deletion data-prepper-plugins/decompress-processor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ dependencies {
implementation project(':data-prepper-plugins:common')
implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation 'io.micrometer:micrometer-core'
testImplementation testLibs.mockito.inline
}
Loading

0 comments on commit 8a1a416

Please sign in to comment.