Skip to content

Commit

Permalink
[FLINK-26229][datadog] Migrate tests to JUnit5
Browse files Browse the repository at this point in the history
  • Loading branch information
zentol committed Mar 7, 2022
1 parent 9725933 commit 0546cb0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,46 @@

import org.apache.flink.metrics.Counter;
import org.apache.flink.metrics.SimpleCounter;
import org.apache.flink.util.TestLogger;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

/** Tests for the {@link DCounter}. */
public class DCounterTest extends TestLogger {
class DCounterTest {

@Test
public void testGetMetricValue() {
void testGetMetricValue() {
final Counter backingCounter = new SimpleCounter();
final DCounter counter =
new DCounter(
backingCounter, "counter", "localhost", Collections.emptyList(), () -> 0);

// sane initial state
assertEquals(0L, counter.getMetricValue());
assertThat(counter.getMetricValue()).isEqualTo(0L);
counter.ackReport();
assertEquals(0L, counter.getMetricValue());
assertThat(counter.getMetricValue()).isEqualTo(0L);

// value is compared against initial state 0
backingCounter.inc(10);
assertEquals(10L, counter.getMetricValue());
assertThat(counter.getMetricValue()).isEqualTo(10L);

// last value was not acked, should still be compared against initial state 0
backingCounter.inc(10);
assertEquals(20L, counter.getMetricValue());
assertThat(counter.getMetricValue()).isEqualTo(20L);

// last value (20) acked, now target of comparison
counter.ackReport();
assertEquals(0L, counter.getMetricValue());
assertThat(counter.getMetricValue()).isEqualTo(0L);

// we now compare against the acked value
backingCounter.inc(10);
assertEquals(10L, counter.getMetricValue());
assertThat(counter.getMetricValue()).isEqualTo(10L);

// properly handle decrements
backingCounter.dec(10);
assertEquals(0L, counter.getMetricValue());
assertThat(counter.getMetricValue()).isEqualTo(0L);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,19 @@
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.MapperFeature;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/** Tests for the DatadogHttpClient. */
public class DatadogHttpClientTest {
class DatadogHttpClientTest {

private static final List<String> tags = Arrays.asList("tag1", "tag2");
private static final String TAGS_AS_JSON =
Expand All @@ -59,38 +56,40 @@ public class DatadogHttpClientTest {

private static final long MOCKED_SYSTEM_MILLIS = 123L;

@Test(expected = IllegalArgumentException.class)
public void testClientWithEmptyKey() {
new DatadogHttpClient("", null, 123, DataCenter.US, false);
@Test
void testClientWithEmptyKey() {
assertThatThrownBy(() -> new DatadogHttpClient("", null, 123, DataCenter.US, false))
.isInstanceOf(IllegalArgumentException.class);
}

@Test(expected = IllegalArgumentException.class)
public void testClientWithNullKey() {
new DatadogHttpClient(null, null, 123, DataCenter.US, false);
@Test
void testClientWithNullKey() {
assertThatThrownBy(() -> new DatadogHttpClient(null, null, 123, DataCenter.US, false))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
public void testGetProxyWithNullProxyHost() {
void testGetProxyWithNullProxyHost() {
DatadogHttpClient client =
new DatadogHttpClient("anApiKey", null, 123, DataCenter.US, false);
assert (client.getProxy() == Proxy.NO_PROXY);
}

@Test
public void testGetProxy() {
void testGetProxy() {
DatadogHttpClient client =
new DatadogHttpClient("anApiKey", "localhost", 123, DataCenter.US, false);

assertTrue(client.getProxy().address() instanceof InetSocketAddress);
assertThat(client.getProxy().address()).isInstanceOf(InetSocketAddress.class);

InetSocketAddress proxyAddress = (InetSocketAddress) client.getProxy().address();

assertEquals(123, proxyAddress.getPort());
assertEquals("localhost", proxyAddress.getHostString());
assertThat(proxyAddress.getPort()).isEqualTo(123);
assertThat(proxyAddress.getHostString()).isEqualTo("localhost");
}

@Test
public void serializeGauge() throws JsonProcessingException {
void serializeGauge() throws JsonProcessingException {
DSeries series = new DSeries();
series.add(new DGauge(() -> 1, METRIC, HOST, tags, () -> MOCKED_SYSTEM_MILLIS));

Expand All @@ -100,7 +99,7 @@ public void serializeGauge() throws JsonProcessingException {
}

@Test
public void serializeGaugeWithoutHost() throws JsonProcessingException {
void serializeGaugeWithoutHost() throws JsonProcessingException {
DSeries series = new DSeries();
series.add(new DGauge(() -> 1, METRIC, null, tags, () -> MOCKED_SYSTEM_MILLIS));

Expand All @@ -110,7 +109,7 @@ public void serializeGaugeWithoutHost() throws JsonProcessingException {
}

@Test
public void serializeCounter() throws JsonProcessingException {
void serializeCounter() throws JsonProcessingException {
DSeries series = new DSeries();
series.add(
new DCounter(new TestCounter(1), METRIC, HOST, tags, () -> MOCKED_SYSTEM_MILLIS));
Expand All @@ -121,7 +120,7 @@ public void serializeCounter() throws JsonProcessingException {
}

@Test
public void serializeCounterWithoutHost() throws JsonProcessingException {
void serializeCounterWithoutHost() throws JsonProcessingException {
DSeries series = new DSeries();
series.add(
new DCounter(new TestCounter(1), METRIC, null, tags, () -> MOCKED_SYSTEM_MILLIS));
Expand All @@ -132,7 +131,7 @@ public void serializeCounterWithoutHost() throws JsonProcessingException {
}

@Test
public void serializeMeter() throws JsonProcessingException {
void serializeMeter() throws JsonProcessingException {
DSeries series = new DSeries();
series.add(new DMeter(new TestMeter(0, 1), METRIC, HOST, tags, () -> MOCKED_SYSTEM_MILLIS));

Expand All @@ -142,7 +141,7 @@ public void serializeMeter() throws JsonProcessingException {
}

@Test
public void serializeMeterWithoutHost() throws JsonProcessingException {
void serializeMeterWithoutHost() throws JsonProcessingException {
DSeries series = new DSeries();
series.add(new DMeter(new TestMeter(0, 1), METRIC, null, tags, () -> MOCKED_SYSTEM_MILLIS));

Expand All @@ -152,7 +151,7 @@ public void serializeMeterWithoutHost() throws JsonProcessingException {
}

@Test
public void serializeHistogram() throws JsonProcessingException {
void serializeHistogram() throws JsonProcessingException {
DHistogram h =
new DHistogram(new TestHistogram(), METRIC, HOST, tags, () -> MOCKED_SYSTEM_MILLIS);

Expand All @@ -179,20 +178,17 @@ private static void assertSerialization(String json, MetricAssertion... metricAs
final MetricAssertion metricAssertion = metricAssertions[i];

if (metricAssertion.expectHost) {
assertThat(parsedJson.get(DMetric.FIELD_NAME_HOST).asText(), is(HOST));
assertThat(parsedJson.get(DMetric.FIELD_NAME_HOST).asText()).isEqualTo(HOST);
} else {
assertThat(parsedJson.get(DMetric.FIELD_NAME_HOST), nullValue());
assertThat(parsedJson.get(DMetric.FIELD_NAME_HOST)).isNull();
}
assertThat(
parsedJson.get(DMetric.FIELD_NAME_METRIC).asText(),
is(METRIC + metricAssertion.metricNameSuffix));
assertThat(
parsedJson.get(DMetric.FIELD_NAME_TYPE).asText(),
is(metricAssertion.expectedType.name()));
assertThat(
parsedJson.get(DMetric.FIELD_NAME_POINTS).toString(),
is(String.format("[[123,%s]]", metricAssertion.expectedValue)));
assertThat(parsedJson.get(DMetric.FIELD_NAME_TAGS).toString(), is(TAGS_AS_JSON));
assertThat(parsedJson.get(DMetric.FIELD_NAME_METRIC).asText())
.isEqualTo(METRIC + metricAssertion.metricNameSuffix);
assertThat(parsedJson.get(DMetric.FIELD_NAME_TYPE).asText())
.isEqualTo(metricAssertion.expectedType.name());
assertThat(parsedJson.get(DMetric.FIELD_NAME_POINTS).toString())
.isEqualTo(String.format("[[123,%s]]", metricAssertion.expectedValue));
assertThat(parsedJson.get(DMetric.FIELD_NAME_TAGS).toString()).isEqualTo(TAGS_AS_JSON);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
package org.apache.flink.metrics.datadog;

import org.apache.flink.metrics.util.MetricReporterTestUtils;
import org.apache.flink.util.TestLogger;

import org.junit.Test;
import org.junit.jupiter.api.Test;

/** Tests for the {@link DatadogHttpReporterFactory}. */
public class DatadogHttpReporterFactoryTest extends TestLogger {
class DatadogHttpReporterFactoryTest {

@Test
public void testMetricReporterSetupViaSPI() {
void testMetricReporterSetupViaSPI() {
MetricReporterTestUtils.testMetricReporterSetupViaSPI(DatadogHttpReporterFactory.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http:https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

org.apache.flink.util.TestLoggerExtension

0 comments on commit 0546cb0

Please sign in to comment.