From 0546cb0326e14fd0bce8e45a1c6bd0d0abd56606 Mon Sep 17 00:00:00 2001 From: Chesnay Schepler Date: Mon, 7 Mar 2022 09:47:27 +0100 Subject: [PATCH] [FLINK-26229][datadog] Migrate tests to JUnit5 --- .../flink/metrics/datadog/DCounterTest.java | 23 +++--- .../datadog/DatadogHttpClientTest.java | 70 +++++++++---------- .../DatadogHttpReporterFactoryTest.java | 7 +- .../org.junit.jupiter.api.extension.Extension | 16 +++++ 4 files changed, 63 insertions(+), 53 deletions(-) create mode 100644 flink-metrics/flink-metrics-datadog/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension diff --git a/flink-metrics/flink-metrics-datadog/src/test/java/org/apache/flink/metrics/datadog/DCounterTest.java b/flink-metrics/flink-metrics-datadog/src/test/java/org/apache/flink/metrics/datadog/DCounterTest.java index 1ce1f2fba71d7..c5a8ab858a507 100644 --- a/flink-metrics/flink-metrics-datadog/src/test/java/org/apache/flink/metrics/datadog/DCounterTest.java +++ b/flink-metrics/flink-metrics-datadog/src/test/java/org/apache/flink/metrics/datadog/DCounterTest.java @@ -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); } } diff --git a/flink-metrics/flink-metrics-datadog/src/test/java/org/apache/flink/metrics/datadog/DatadogHttpClientTest.java b/flink-metrics/flink-metrics-datadog/src/test/java/org/apache/flink/metrics/datadog/DatadogHttpClientTest.java index 0f256d084c8c2..ee8eb4935ef3c 100644 --- a/flink-metrics/flink-metrics-datadog/src/test/java/org/apache/flink/metrics/datadog/DatadogHttpClientTest.java +++ b/flink-metrics/flink-metrics-datadog/src/test/java/org/apache/flink/metrics/datadog/DatadogHttpClientTest.java @@ -27,7 +27,7 @@ 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; @@ -35,14 +35,11 @@ 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 tags = Arrays.asList("tag1", "tag2"); private static final String TAGS_AS_JSON = @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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); @@ -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); } } diff --git a/flink-metrics/flink-metrics-datadog/src/test/java/org/apache/flink/metrics/datadog/DatadogHttpReporterFactoryTest.java b/flink-metrics/flink-metrics-datadog/src/test/java/org/apache/flink/metrics/datadog/DatadogHttpReporterFactoryTest.java index 37545daab98c4..91a0aa70b95d4 100644 --- a/flink-metrics/flink-metrics-datadog/src/test/java/org/apache/flink/metrics/datadog/DatadogHttpReporterFactoryTest.java +++ b/flink-metrics/flink-metrics-datadog/src/test/java/org/apache/flink/metrics/datadog/DatadogHttpReporterFactoryTest.java @@ -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); } } diff --git a/flink-metrics/flink-metrics-datadog/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension b/flink-metrics/flink-metrics-datadog/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension new file mode 100644 index 0000000000000..28999133c2b0f --- /dev/null +++ b/flink-metrics/flink-metrics-datadog/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension @@ -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://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 \ No newline at end of file