Skip to content

Commit

Permalink
Removes double brace initialization and includes this as a checkstyle…
Browse files Browse the repository at this point in the history
… rule to prevent future use. (opensearch-project#2035)

Signed-off-by: David Venable <[email protected]>
  • Loading branch information
dlvenable authored Nov 22, 2022
1 parent 83e64d8 commit 2342d9f
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 172 deletions.
1 change: 1 addition & 0 deletions config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<module name="TreeWalker">
<module name="AvoidStarImport" />
<module name="AvoidDoubleBraceInitializationCheck" />

<!-- Unused imports are forbidden -->
<module name="UnusedImports" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,45 @@

package org.opensearch.dataprepper.metrics;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opensearch.dataprepper.model.configuration.PluginSetting;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Timer;
import org.junit.Assert;
import org.junit.Test;

import java.util.Collections;
import java.util.StringJoiner;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class PluginMetricsTest {
private static final String PLUGIN_NAME = "testPlugin";
private static final String PIPELINE_NAME = "pipelineName";
private static final String TAG_KEY = "tagKey";
private static final String TAG_VALUE = "tagValue";
private static final PluginSetting PLUGIN_SETTING = new PluginSetting(PLUGIN_NAME, Collections.emptyMap()) {{
setPipelineName(PIPELINE_NAME);
}};
private static final PluginMetrics PLUGIN_METRICS = PluginMetrics.fromPluginSetting(PLUGIN_SETTING);
private PluginMetrics objectUnderTest;
private PluginSetting pluginSetting;

@BeforeEach
void setUp() {
pluginSetting = mock(PluginSetting.class);
when(pluginSetting.getName()).thenReturn(PLUGIN_NAME);
when(pluginSetting.getPipelineName()).thenReturn(PIPELINE_NAME);

objectUnderTest = PluginMetrics.fromPluginSetting(pluginSetting);
}

@Test
public void testCounter() {
final Counter counter = PLUGIN_METRICS.counter("counter");
Assert.assertEquals(
final Counter counter = objectUnderTest.counter("counter");
assertEquals(
new StringJoiner(MetricNames.DELIMITER)
.add(PIPELINE_NAME).add(PLUGIN_NAME)
.add("counter").toString(),
Expand All @@ -39,29 +52,29 @@ public void testCounter() {

@Test
public void testCounterWithTags() {
final Counter counter = PLUGIN_METRICS.counterWithTags("counter", TAG_KEY, TAG_VALUE);
Assert.assertEquals(
final Counter counter = objectUnderTest.counterWithTags("counter", TAG_KEY, TAG_VALUE);
assertEquals(
new StringJoiner(MetricNames.DELIMITER)
.add(PIPELINE_NAME).add(PLUGIN_NAME)
.add("counter").toString(),
counter.getId().getName());

Assert.assertEquals(TAG_VALUE, counter.getId().getTag(TAG_KEY));
assertEquals(TAG_VALUE, counter.getId().getTag(TAG_KEY));
}

@Test
public void testCustomMetricsPrefixCounter() {
final Counter counter = PLUGIN_METRICS.counter("counter", PIPELINE_NAME);
Assert.assertEquals(
final Counter counter = objectUnderTest.counter("counter", PIPELINE_NAME);
assertEquals(
new StringJoiner(MetricNames.DELIMITER)
.add(PIPELINE_NAME).add("counter").toString(),
counter.getId().getName());
}

@Test
public void testTimer() {
final Timer timer = PLUGIN_METRICS.timer("timer");
Assert.assertEquals(
final Timer timer = objectUnderTest.timer("timer");
assertEquals(
new StringJoiner(MetricNames.DELIMITER)
.add(PIPELINE_NAME).add(PLUGIN_NAME)
.add("timer").toString(),
Expand All @@ -70,20 +83,20 @@ public void testTimer() {

@Test
public void testTimerWithTags() {
final Timer timer = PLUGIN_METRICS.timerWithTags("timer", TAG_KEY, TAG_VALUE);
Assert.assertEquals(
final Timer timer = objectUnderTest.timerWithTags("timer", TAG_KEY, TAG_VALUE);
assertEquals(
new StringJoiner(MetricNames.DELIMITER)
.add(PIPELINE_NAME).add(PLUGIN_NAME)
.add("timer").toString(),
timer.getId().getName());

Assert.assertEquals(TAG_VALUE, timer.getId().getTag(TAG_KEY));
assertEquals(TAG_VALUE, timer.getId().getTag(TAG_KEY));
}

@Test
public void testSummary() {
final DistributionSummary summary = PLUGIN_METRICS.summary("summary");
Assert.assertEquals(
final DistributionSummary summary = objectUnderTest.summary("summary");
assertEquals(
new StringJoiner(MetricNames.DELIMITER)
.add(PIPELINE_NAME).add(PLUGIN_NAME)
.add("summary").toString(),
Expand All @@ -93,28 +106,28 @@ public void testSummary() {
@Test
public void testNumberGauge() {
final AtomicInteger atomicInteger = new AtomicInteger(0);
final AtomicInteger gauge = PLUGIN_METRICS.gauge("gauge", atomicInteger);
Assert.assertNotNull(
final AtomicInteger gauge = objectUnderTest.gauge("gauge", atomicInteger);
assertNotNull(
Metrics.globalRegistry.get(new StringJoiner(MetricNames.DELIMITER)
.add(PIPELINE_NAME).add(PLUGIN_NAME)
.add("gauge").toString()).meter());
Assert.assertEquals(atomicInteger.get(), gauge.get());
assertEquals(atomicInteger.get(), gauge.get());
}

@Test
public void testReferenceGauge() {
final String testString = "abc";
final String gauge = PLUGIN_METRICS.gauge("gauge", testString, String::length);
Assert.assertNotNull(
final String gauge = objectUnderTest.gauge("gauge", testString, String::length);
assertNotNull(
Metrics.globalRegistry.get(new StringJoiner(MetricNames.DELIMITER)
.add(PIPELINE_NAME).add(PLUGIN_NAME)
.add("gauge").toString()).meter());
Assert.assertEquals(3, gauge.length());
assertEquals(3, gauge.length());
}

@Test
public void testEmptyPipelineName() {
Assert.assertThrows(
assertThrows(
IllegalArgumentException.class,
() -> PluginMetrics.fromPluginSetting(new PluginSetting("badSetting", Collections.emptyMap())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@

import javax.inject.Named;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

@Named
class LiteralTypeConversionsConfiguration {
@Bean
public Map<Class<? extends Serializable>, Function<Object, Object>> literalTypeConversions() {
return new HashMap<Class<? extends Serializable>, Function<Object, Object>>() {{
put(String.class, Function.identity());
put(Boolean.class, Function.identity());
put(Integer.class, Function.identity());
put(Float.class, Function.identity());
put(Double.class, o -> ((Double) o).floatValue());
}};
return Map.of(
String.class, Function.identity(),
Boolean.class, Function.identity(),
Integer.class, Function.identity(),
Float.class, Function.identity(),
Double.class, o -> ((Double) o).floatValue()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public NumericCompareOperator greaterThanOperator() {
final Map<Class<? extends Number>, Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>>>
operandsToOperationMap = new HashMap<>();
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> intOperations =
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{
put(Integer.class, (lhs, rhs) -> (Integer) lhs > (Integer) rhs);
put(Float.class, (lhs, rhs) -> (Integer) lhs > (Float) rhs);
}};
Map.of(
Integer.class, (lhs, rhs) -> (Integer) lhs > (Integer) rhs,
Float.class, (lhs, rhs) -> (Integer) lhs > (Float) rhs
);
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> floatOperations =
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{
put(Integer.class, (lhs, rhs) -> (Float) lhs > (Integer) rhs);
put(Float.class, (lhs, rhs) -> (Float) lhs > (Float) rhs);
}};
Map.of(
Integer.class, (lhs, rhs) -> (Float) lhs > (Integer) rhs,
Float.class, (lhs, rhs) -> (Float) lhs > (Float) rhs
);

operandsToOperationMap.put(Integer.class, intOperations);
operandsToOperationMap.put(Float.class, floatOperations);
Expand All @@ -48,15 +48,15 @@ public NumericCompareOperator greaterThanOrEqualOperator() {
final Map<Class<? extends Number>, Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>>>
operandsToOperationMap = new HashMap<>();
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> intOperations =
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{
put(Integer.class, (lhs, rhs) -> (Integer) lhs >= (Integer) rhs);
put(Float.class, (lhs, rhs) -> (Integer) lhs >= (Float) rhs);
}};
Map.of(
Integer.class, (lhs, rhs) -> (Integer) lhs >= (Integer) rhs,
Float.class, (lhs, rhs) -> (Integer) lhs >= (Float) rhs
);
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> floatOperations =
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{
put(Integer.class, (lhs, rhs) -> (Float) lhs >= (Integer) rhs);
put(Float.class, (lhs, rhs) -> (Float) lhs >= (Float) rhs);
}};
Map.of(
Integer.class, (lhs, rhs) -> (Float) lhs >= (Integer) rhs,
Float.class, (lhs, rhs) -> (Float) lhs >= (Float) rhs
);

operandsToOperationMap.put(Integer.class, intOperations);
operandsToOperationMap.put(Float.class, floatOperations);
Expand All @@ -69,15 +69,15 @@ public NumericCompareOperator lessThanOperator() {
final Map<Class<? extends Number>, Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>>>
operandsToOperationMap = new HashMap<>();
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> intOperations =
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{
put(Integer.class, (lhs, rhs) -> (Integer) lhs < (Integer) rhs);
put(Float.class, (lhs, rhs) -> (Integer) lhs < (Float) rhs);
}};
Map.of(
Integer.class, (lhs, rhs) -> (Integer) lhs < (Integer) rhs,
Float.class, (lhs, rhs) -> (Integer) lhs < (Float) rhs
);
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> floatOperations =
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{
put(Integer.class, (lhs, rhs) -> (Float) lhs < (Integer) rhs);
put(Float.class, (lhs, rhs) -> (Float) lhs < (Float) rhs);
}};
Map.of(
Integer.class, (lhs, rhs) -> (Float) lhs < (Integer) rhs,
Float.class, (lhs, rhs) -> (Float) lhs < (Float) rhs
);

operandsToOperationMap.put(Integer.class, intOperations);
operandsToOperationMap.put(Float.class, floatOperations);
Expand All @@ -90,15 +90,15 @@ public NumericCompareOperator lessThanOrEqualOperator() {
final Map<Class<? extends Number>, Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>>>
operandsToOperationMap = new HashMap<>();
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> intOperations =
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{
put(Integer.class, (lhs, rhs) -> (Integer) lhs <= (Integer) rhs);
put(Float.class, (lhs, rhs) -> (Integer) lhs <= (Float) rhs);
}};
Map.of(
Integer.class, (lhs, rhs) -> (Integer) lhs <= (Integer) rhs,
Float.class, (lhs, rhs) -> (Integer) lhs <= (Float) rhs
);
final Map<Class<? extends Number>, BiFunction<Object, Object, Boolean>> floatOperations =
new HashMap<Class<? extends Number>, BiFunction<Object, Object, Boolean>>() {{
put(Integer.class, (lhs, rhs) -> (Float) lhs <= (Integer) rhs);
put(Float.class, (lhs, rhs) -> (Float) lhs <= (Float) rhs);
}};
Map.of(
Integer.class, (lhs, rhs) -> (Float) lhs <= (Integer) rhs,
Float.class, (lhs, rhs) -> (Float) lhs <= (Float) rhs
);

operandsToOperationMap.put(Integer.class, intOperations);
operandsToOperationMap.put(Float.class, floatOperations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ private BlockingBuffer<Record<Log>> getBuffer() {
final HashMap<String, Object> integerHashMap = new HashMap<>();
integerHashMap.put("buffer_size", 1);
integerHashMap.put("batch_size", 1);
final PluginSetting pluginSetting = new PluginSetting("blocking_buffer", integerHashMap) {{
setPipelineName(TEST_PIPELINE_NAME);
}};
final PluginSetting pluginSetting = new PluginSetting("blocking_buffer", integerHashMap);
pluginSetting.setPipelineName(TEST_PIPELINE_NAME);
return new BlockingBuffer<>(pluginSetting);
}

Expand Down Expand Up @@ -276,11 +275,10 @@ public void testHealthCheckUnauthenticatedDisabled() {
// Prepare
when(sourceConfig.isUnauthenticatedHealthCheck()).thenReturn(false);
when(sourceConfig.getAuthentication()).thenReturn(new PluginModel("http_basic",
new HashMap()
{{
put("username", "test");
put("password", "test");
}}));
Map.of(
"username", "test",
"password", "test"
)));
pluginMetrics = PluginMetrics.fromNames(PLUGIN_NAME, TEST_PIPELINE_NAME);
HTTPSourceUnderTest = new HTTPSource(sourceConfig, pluginMetrics, pluginFactory, pipelineDescription);

Expand Down
Loading

0 comments on commit 2342d9f

Please sign in to comment.