Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mockito 5 #4712

Merged
merged 4 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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));
KarstenSchnitter marked this conversation as resolved.
Show resolved Hide resolved
}

@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));
KarstenSchnitter marked this conversation as resolved.
Show resolved Hide resolved
}

@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));
KarstenSchnitter marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
Expand Down
Loading