diff --git a/config/src/test/java/io/confluent/common/config/AbstractConfigTest.java b/config/src/test/java/io/confluent/common/config/AbstractConfigTest.java new file mode 100644 index 00000000000..e8c686fa7c4 --- /dev/null +++ b/config/src/test/java/io/confluent/common/config/AbstractConfigTest.java @@ -0,0 +1,114 @@ +/** + * Copyright 2017 Confluent Inc. + * + * Licensed 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 io.confluent.common.config; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import static org.junit.Assert.*; + +/** + * Unit tests for class {@link AbstractConfig}. + * + * @see AbstractConfig + */ +public class AbstractConfigTest { + + @Test + public void testGetPropsFromFileReturningEmptyProperties() { + Properties properties = AbstractConfig.getPropsFromFile(null); + + assertTrue(properties.isEmpty()); + } + + @Test + public void testWithPrefixWithNonEmptyStringReturnsEmptyMap() { + ConfigDef configDef = new ConfigDef(); + Map hashMap = new HashMap<>(); + AbstractConfig abstractConfig = new AbstractConfig(configDef, hashMap); + Map hashMapTwo = new HashMap<>(); + hashMapTwo.put(",..", ",.."); + Map map = abstractConfig.withPrefix(",..", hashMapTwo); + + assertEquals(0, map.size()); + } + + @Test + public void testWithPrefixWithNonEmptyStrings() { + ConfigDef configDef = new ConfigDef(); + Map hashMap = new HashMap<>(); + hashMap.put("- asd1", "-asd"); + hashMap.put(" asd1", "-asd"); + AbstractConfig abstractConfig = new AbstractConfig(configDef, hashMap); + Map map = abstractConfig.withPrefix("-", hashMap); + + assertEquals(1, map.size()); + assertEquals(" asd1", map.keySet().iterator().next()); + assertEquals(" asd1=-asd", map.entrySet().iterator().next().toString()); + } + + @Test + public void testFailsToCreateAbstractConfigThrowsConfigException() { + ConfigDef configDef = new ConfigDef(); + Map hashMap = new HashMap<>(); + hashMap.put(configDef, new Long((-1995L))); + + try { + new AbstractConfig(configDef, hashMap); + fail("Expecting exception: ConfigException"); + } catch (ConfigException e) { + assertEquals(AbstractConfig.class.getName(), e.getStackTrace()[0].getClassName()); + } + } + + @Test + public void testGetStringThrowsConfigException() { + ConfigDef configDef = new ConfigDef(); + Map hashMap = new HashMap<>(); + AbstractConfig abstractConfig = new AbstractConfig(configDef, hashMap); + + try { + abstractConfig.getString(" could not be found."); + fail("Expecting exception: ConfigException"); + } catch (ConfigException e) { + assertEquals(AbstractConfig.class.getName(), e.getStackTrace()[0].getClassName()); + } + } + + @Test + public void testValuesWithPrefix() { + ConfigDef configDef = new ConfigDef(); + Map hashMap = new HashMap<>(); + AbstractConfig abstractConfig = new AbstractConfig(configDef, hashMap); + Map map = abstractConfig.valuesWithPrefix(""); + + assertEquals(0, map.size()); + } + + @Test + public void testOriginalsWithPrefix() { + ConfigDef configDef = new ConfigDef(); + Map hashMap = new HashMap<>(); + AbstractConfig abstractConfig = new AbstractConfig(configDef, hashMap); + Map map = abstractConfig.originalsWithPrefix("~cOo+qt{k]k}#%y"); + + assertEquals(0, map.size()); + } + +} \ No newline at end of file diff --git a/metrics/src/test/java/io/confluent/common/metrics/stats/PercentilesTest.java b/metrics/src/test/java/io/confluent/common/metrics/stats/PercentilesTest.java new file mode 100644 index 00000000000..5fa89c06c47 --- /dev/null +++ b/metrics/src/test/java/io/confluent/common/metrics/stats/PercentilesTest.java @@ -0,0 +1,94 @@ +/** + * Copyright 2017 Confluent Inc. + * + * Licensed 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 io.confluent.common.metrics.stats; + +import io.confluent.common.metrics.MetricConfig; +import io.confluent.common.metrics.MetricName; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * Unit tests for class {@link Percentiles}. + * + * @see Percentiles + */ +public class PercentilesTest { + + @Test + public void testValue() { + Percentiles.BucketSizing percentiles_BucketSizing = Percentiles.BucketSizing.LINEAR; + Percentiles percentiles = new Percentiles(1846, 1846, percentiles_BucketSizing, new Percentile[0]); + MetricConfig metricConfig = new MetricConfig(); + percentiles.record(metricConfig, 28L, 1792L); + + assertEquals(Double.POSITIVE_INFINITY, percentiles.value(metricConfig, 1846, 27.78662642648307), 0.01); + } + + @Test + public void testFailsToCreatePercentilesTaking5ArgumentsThrowsIllegalArgumentException() { + try { + new Percentiles(44, (-121.169), 44, Percentiles.BucketSizing.LINEAR, null); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals(Percentiles.class.getName(), e.getStackTrace()[0].getClassName()); + } + } + + @Test + public void testFailsToCreatePercentilesTaking4ArgumentsThrowsIllegalArgumentException() { + try { + new Percentiles(58, 58, null, new Percentile[7]); + fail("Expecting exception: IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals(Percentiles.class.getName(), e.getStackTrace()[0].getClassName()); + } + } + + @Test + public void testCombineWithEmptyList() { + Percentiles.BucketSizing percentiles_BucketSizing = Percentiles.BucketSizing.LINEAR; + Percentile[] percentileArray = new Percentile[0]; + Percentiles percentiles = new Percentiles(146, 1846, percentiles_BucketSizing, percentileArray); + MetricConfig metricConfig = new MetricConfig(); + List list = percentiles.samples; + + assertEquals(Double.NaN, percentiles.combine(list, metricConfig, 1846), 0.01); + } + + @Test + public void testStatsThrowsNullPointerException() { + Percentiles.BucketSizing percentiles_BucketSizing = Percentiles.BucketSizing.LINEAR; + Percentile[] percentileArray = new Percentile[2]; + Map hashMap = new HashMap<>(); + MetricName metricName = new MetricName("|~a`", "|~a`", hashMap); + Percentile percentile = new Percentile(metricName, (-961)); + percentileArray[0] = percentile; + Percentiles percentiles = new Percentiles((-961), 0.0, percentiles_BucketSizing, percentileArray); + + try { + percentiles.stats(); + fail("Expecting exception: NullPointerException"); + } catch (NullPointerException e) { + assertEquals(Percentiles.class.getName(), e.getStackTrace()[0].getClassName()); + } + } +} \ No newline at end of file