Skip to content

add-tests #81

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

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
@@ -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<Integer, Object> hashMap = new HashMap<>();
AbstractConfig abstractConfig = new AbstractConfig(configDef, hashMap);
Map<String, String> hashMapTwo = new HashMap<>();
hashMapTwo.put(",..", ",..");
Map<String, Object> map = abstractConfig.withPrefix(",..", hashMapTwo);

assertEquals(0, map.size());
}

@Test
public void testWithPrefixWithNonEmptyStrings() {
ConfigDef configDef = new ConfigDef();
Map<String, Object> hashMap = new HashMap<>();
hashMap.put("- asd1", "-asd");
hashMap.put(" asd1", "-asd");
AbstractConfig abstractConfig = new AbstractConfig(configDef, hashMap);
Map<String, Object> 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<Object, Long> 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<Integer, String> 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<Object, Boolean> hashMap = new HashMap<>();
AbstractConfig abstractConfig = new AbstractConfig(configDef, hashMap);
Map<String, Object> map = abstractConfig.valuesWithPrefix("");

assertEquals(0, map.size());
}

@Test
public void testOriginalsWithPrefix() {
ConfigDef configDef = new ConfigDef();
Map<Integer, String> hashMap = new HashMap<>();
AbstractConfig abstractConfig = new AbstractConfig(configDef, hashMap);
Map<String, Object> map = abstractConfig.originalsWithPrefix("~cOo+qt{k]k}#%y");

assertEquals(0, map.size());
}

}
Original file line number Diff line number Diff line change
@@ -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<SampledStat.Sample> 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<String, String> 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());
}
}
}