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

Code Coverage additions for Extensions #17596

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -48,4 +48,82 @@ public void testEmitterConfigWithBadExtraLabels()
Assert.assertTrue(actualMessage.contains(expectedMessage));
}

@Test
public void testDefaultConstructor()
{
PrometheusEmitterConfig config = new PrometheusEmitterConfig(null, null, null, null, null, false, false, null, null, null, null);
Assert.assertEquals(PrometheusEmitterConfig.Strategy.exporter, config.getStrategy());
Assert.assertEquals("druid", config.getNamespace());
Assert.assertNull(config.getDimensionMapPath());
}

@Test
public void testExporterStrategy()
{
PrometheusEmitterConfig config = new PrometheusEmitterConfig(PrometheusEmitterConfig.Strategy.exporter, "druid", null, 8080, null, true, true, null, null, null, null);
Assert.assertEquals(PrometheusEmitterConfig.Strategy.exporter, config.getStrategy());
Assert.assertEquals("druid", config.getNamespace());
Assert.assertEquals(8080, config.getPort());
Assert.assertTrue(config.isAddHostAsLabel());
Assert.assertTrue(config.isAddHostAsLabel());
}

@Test
public void testExporterStrategyWithoutPort()
{
Assert.assertThrows(IllegalArgumentException.class, () -> {
new PrometheusEmitterConfig(PrometheusEmitterConfig.Strategy.exporter, null, null, null, null, false, false, null, null, null, null);
});
}

@Test
public void testPushgatewayStrategy()
{
PrometheusEmitterConfig config = new PrometheusEmitterConfig(PrometheusEmitterConfig.Strategy.pushgateway, "druid", null, null, "localhost:9091", false, false, 30, null, true, 5000L);
Assert.assertEquals(PrometheusEmitterConfig.Strategy.pushgateway, config.getStrategy());
Assert.assertEquals("druid", config.getNamespace());
Assert.assertEquals("localhost:9091", config.getPushGatewayAddress());
Assert.assertFalse(config.isAddHostAsLabel());
}

@Test
public void testPushGatewayStrategyWithoutAddress()
{
Assert.assertThrows(IllegalArgumentException.class, () -> {
new PrometheusEmitterConfig(PrometheusEmitterConfig.Strategy.pushgateway, null, null, null, null, false, false, null, null, null, null);
});
}

@Test
public void testDefaultFlushPeriodForPushgateway()
{
PrometheusEmitterConfig config = new PrometheusEmitterConfig(PrometheusEmitterConfig.Strategy.pushgateway, null, null, null, "localhost:9091", false, false, null, null, null, null);
Assert.assertEquals(Integer.valueOf(15), config.getFlushPeriod());
}

@Test
public void testInvalidFlushPeriod()
{
Assert.assertThrows(IllegalArgumentException.class, () -> {
new PrometheusEmitterConfig(PrometheusEmitterConfig.Strategy.pushgateway, null, null, null, "localhost:9091", false, false, 0, null, null, null);
});
}

@Test
public void testInvalidExtraLabelName()
{
Assert.assertThrows(DruidException.class, () -> {
Map<String, String> extraLabels = new HashMap<>();
extraLabels.put("invalid label", "value");
new PrometheusEmitterConfig(null, null, null, null, null, false, false, null, extraLabels, null, null);
});
}

@Test
public void testNegativeWaitForShutdownDelay()
{
Assert.assertThrows(DruidException.class, () -> {
new PrometheusEmitterConfig(null, null, null, null, null, false, false, null, null, null, -1L);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.collect.ImmutableMap;
import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.HTTPServer;
import io.prometheus.client.exporter.PushGateway;
import org.apache.druid.java.util.emitter.core.Emitter;
import org.apache.druid.java.util.emitter.service.ServiceMetricEvent;
Expand Down Expand Up @@ -229,11 +230,15 @@ public void testEmitterStart()
PrometheusEmitter exportEmitter = new PrometheusEmitter(exportEmitterConfig);
exportEmitter.start();
Assert.assertNotNull(exportEmitter.getServer());
Assert.assertTrue(exportEmitter.getServer() instanceof HTTPServer);
exportEmitter.close();

PrometheusEmitterConfig pushEmitterConfig = new PrometheusEmitterConfig(PrometheusEmitterConfig.Strategy.pushgateway, "namespace2", null, 0, "pushgateway", true, true, 60, null, false, null);
PrometheusEmitter pushEmitter = new PrometheusEmitter(pushEmitterConfig);
pushEmitter.start();
Assert.assertNotNull(pushEmitter.getPushGateway());
Assert.assertTrue(pushEmitter.getPushGateway() instanceof PushGateway);
pushEmitter.close();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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.
*/

package org.apache.druid.msq.indexing;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class MSQTaskListTest
{
@Test
public void testGetTaskIds()
{
List<String> taskIds = Arrays.asList("task1", "task2", "task3");
MSQTaskList msqTaskList = new MSQTaskList(taskIds);
assertEquals(taskIds, msqTaskList.getTaskIds());
}

@Test
public void testConstructorWithNullTaskIds()
{
Executable executable = () -> new MSQTaskList(null);
assertThrows(NullPointerException.class, executable);
}

@Test
public void testEquals()
{
List<String> taskIds1 = Arrays.asList("task1", "task2");
List<String> taskIds2 = Arrays.asList("task1", "task2");
MSQTaskList msqTaskList1 = new MSQTaskList(taskIds1);
MSQTaskList msqTaskList2 = new MSQTaskList(taskIds2);
assertEquals(msqTaskList1, msqTaskList2);
assertTrue(msqTaskList1.equals(msqTaskList1));
assertFalse(msqTaskList1.equals(null));
}

@Test
public void testNotEquals()
{
List<String> taskIds1 = Arrays.asList("task1", "task2");
List<String> taskIds2 = Arrays.asList("task3", "task4");
MSQTaskList msqTaskList1 = new MSQTaskList(taskIds1);
MSQTaskList msqTaskList2 = new MSQTaskList(taskIds2);
assertNotEquals(msqTaskList1, msqTaskList2);
assertFalse(msqTaskList1.equals(msqTaskList2));
assertFalse(msqTaskList1.equals(new Object()));
}

@Test
public void testHashCode()
{
List<String> taskIds1 = Arrays.asList("task1", "task2");
MSQTaskList msqTaskList1 = new MSQTaskList(taskIds1);
MSQTaskList msqTaskList2 = new MSQTaskList(taskIds1);
assertEquals(msqTaskList1.hashCode(), msqTaskList2.hashCode());
}

@Test
public void testToString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not needed.

{
List<String> taskIds = Arrays.asList("task1", "task2");
MSQTaskList msqTaskList = new MSQTaskList(taskIds);
assertEquals("MSQTaskList{taskIds=[task1, task2]}", msqTaskList.toString());
}

@Test
public void testJsonSerialization() throws Exception
{
List<String> taskIds = Arrays.asList("task1", "task2");
MSQTaskList msqTaskList = new MSQTaskList(taskIds);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(msqTaskList);
MSQTaskList deserialized = mapper.readValue(json, MSQTaskList.class);
assertEquals(msqTaskList, deserialized);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.apache.druid.indexer.partitions.PartitionsSpec;
import org.apache.druid.segment.IndexSpec;
import org.apache.druid.segment.TestHelper;
import org.apache.druid.segment.column.StringEncodingStrategy;
Expand Down Expand Up @@ -76,4 +77,39 @@ public void testEquals()
.usingGetClass()
.verify();
}

@Test
public void testDefaultValuesForElements()
{
MSQTuningConfig msqTuningConfig = new MSQTuningConfig(null, null, null, null, null);
Assert.assertEquals(1, msqTuningConfig.getMaxNumWorkers());
Assert.assertEquals(100000, msqTuningConfig.getMaxRowsInMemory());
Assert.assertEquals(PartitionsSpec.DEFAULT_MAX_ROWS_PER_SEGMENT, msqTuningConfig.getRowsPerSegment());
Assert.assertEquals(null, msqTuningConfig.getMaxNumSegments());
Assert.assertEquals(IndexSpec.DEFAULT, msqTuningConfig.getIndexSpec());
}

@Test
public void testCustomValuesForElements()
{
MSQTuningConfig msqTuningConfig = new MSQTuningConfig(5, 200000, 5000, 10, IndexSpec.builder().build());
Assert.assertEquals(5, msqTuningConfig.getMaxNumWorkers());
Assert.assertEquals(200000, msqTuningConfig.getMaxRowsInMemory());
Assert.assertEquals(5000, msqTuningConfig.getRowsPerSegment());
Assert.assertEquals(Integer.valueOf(10), msqTuningConfig.getMaxNumSegments());
Assert.assertEquals(IndexSpec.builder().build(), msqTuningConfig.getIndexSpec());
}

@Test
public void testToString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to test the toString method? AFAICT, the toString of this class is used only for debug logging.

{
MSQTuningConfig msqTuningConfig = new MSQTuningConfig(1, 100000, 5000, 10, IndexSpec.builder().build());
String toStringResult = msqTuningConfig.toString();
Assert.assertNotNull(toStringResult);
Assert.assertTrue(toStringResult.contains("maxNumWorkers=1"));
Assert.assertTrue(toStringResult.contains("maxRowsInMemory=100000"));
Assert.assertTrue(toStringResult.contains("rowsPerSegment=5000"));
Assert.assertTrue(toStringResult.contains("maxNumSegments=10"));
Assert.assertTrue(toStringResult.contains("indexSpec=IndexSpec{bitmapSerdeFactory=RoaringBitmapSerdeFactory{}, dimensionCompression=lz4, stringDictionaryEncoding=Utf8{}, metricCompression=lz4, longEncoding=longs, complexMetricCompression=null, jsonCompression=null, segmentLoader=null}"));
}
}
25 changes: 25 additions & 0 deletions extensions-core/simple-client-sslcontext/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,30 @@
<artifactId>jackson-databind</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.
*/

package org.apache.druid.https;

import org.apache.druid.metadata.PasswordProvider;
import org.junit.Test;

import java.lang.reflect.Field;

import static org.junit.Assert.assertEquals;

public class SSLClientConfigTest
{
@Test
public void testGetters() throws NoSuchFieldException, IllegalAccessException
{
SSLClientConfig sslClientConfig = new SSLClientConfig();

String expectedProtocol = "TLS";
String expectedTrustStoreType = "JKS";
String expectedTrustStorePath = "/path/to/truststore";
String expectedTrustStoreAlgorithm = "SunX509";
PasswordProvider expectedTrustStorePasswordProvider = () -> "trustStorePassword";
String expectedKeyStorePath = "/path/to/keystore";
String expectedKeyStoreType = "JKS";
String expectedCertAlias = "alias";
PasswordProvider expectedKeyStorePasswordProvider = () -> "keyStorePassword";
PasswordProvider expectedKeyManagerPasswordProvider = () -> "keyManagerPassword";
String expectedKeyManagerFactoryAlgorithm = "SunX509";
Boolean expectedValidateHostnames = true;

// Use reflection to set the private fields
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use reflection for this.
Either

  • add a constructor
  • OR, override the getters to return the expected values
  • OR, deserialize the object from a json string

setField(sslClientConfig, "protocol", expectedProtocol);
setField(sslClientConfig, "trustStoreType", expectedTrustStoreType);
setField(sslClientConfig, "trustStorePath", expectedTrustStorePath);
setField(sslClientConfig, "trustStoreAlgorithm", expectedTrustStoreAlgorithm);
setField(sslClientConfig, "trustStorePasswordProvider", expectedTrustStorePasswordProvider);
setField(sslClientConfig, "keyStorePath", expectedKeyStorePath);
setField(sslClientConfig, "keyStoreType", expectedKeyStoreType);
setField(sslClientConfig, "certAlias", expectedCertAlias);
setField(sslClientConfig, "keyStorePasswordProvider", expectedKeyStorePasswordProvider);
setField(sslClientConfig, "keyManagerPasswordProvider", expectedKeyManagerPasswordProvider);
setField(sslClientConfig, "keyManagerFactoryAlgorithm", expectedKeyManagerFactoryAlgorithm);
setField(sslClientConfig, "validateHostnames", expectedValidateHostnames);

assertEquals(expectedProtocol, sslClientConfig.getProtocol());
assertEquals(expectedTrustStoreType, sslClientConfig.getTrustStoreType());
assertEquals(expectedProtocol, sslClientConfig.getProtocol());
assertEquals(expectedTrustStoreType, sslClientConfig.getTrustStoreType());
assertEquals(expectedTrustStorePath, sslClientConfig.getTrustStorePath());
assertEquals(expectedTrustStoreAlgorithm, sslClientConfig.getTrustStoreAlgorithm());
assertEquals(expectedTrustStorePasswordProvider, sslClientConfig.getTrustStorePasswordProvider());
assertEquals(expectedKeyStorePath, sslClientConfig.getKeyStorePath());
assertEquals(expectedKeyStoreType, sslClientConfig.getKeyStoreType());
assertEquals(expectedCertAlias, sslClientConfig.getCertAlias());
assertEquals(expectedKeyStorePasswordProvider, sslClientConfig.getKeyStorePasswordProvider());
assertEquals(expectedKeyManagerPasswordProvider, sslClientConfig.getKeyManagerPasswordProvider());
assertEquals(expectedKeyManagerFactoryAlgorithm, sslClientConfig.getKeyManagerFactoryAlgorithm());
assertEquals(expectedValidateHostnames, sslClientConfig.getValidateHostnames());
assertEquals("SSLClientConfig{protocol='TLS', trustStoreType='JKS', trustStorePath='/path/to/truststore', trustStoreAlgorithm='SunX509', keyStorePath='/path/to/keystore', keyStoreType='JKS', certAlias='alias', keyManagerFactoryAlgorithm='SunX509', validateHostnames='true'}", sslClientConfig.toString());
}

private void setField(Object object, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException
{
Field field = object.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(object, value);
}
}
Loading
Loading