aboutsummaryrefslogtreecommitdiff
path: root/api/src/test/java/io/opencensus/metrics
diff options
context:
space:
mode:
Diffstat (limited to 'api/src/test/java/io/opencensus/metrics')
-rw-r--r--api/src/test/java/io/opencensus/metrics/DerivedDoubleGaugeTest.java89
-rw-r--r--api/src/test/java/io/opencensus/metrics/DerivedLongGaugeTest.java89
-rw-r--r--api/src/test/java/io/opencensus/metrics/DoubleGaugeTest.java88
-rw-r--r--api/src/test/java/io/opencensus/metrics/LabelKeyTest.java86
-rw-r--r--api/src/test/java/io/opencensus/metrics/LabelValueTest.java74
-rw-r--r--api/src/test/java/io/opencensus/metrics/LongGaugeTest.java87
-rw-r--r--api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java220
-rw-r--r--api/src/test/java/io/opencensus/metrics/MetricsComponentTest.java40
-rw-r--r--api/src/test/java/io/opencensus/metrics/MetricsTest.java71
-rw-r--r--api/src/test/java/io/opencensus/metrics/export/DistributionTest.java331
-rw-r--r--api/src/test/java/io/opencensus/metrics/export/ExportComponentTest.java33
-rw-r--r--api/src/test/java/io/opencensus/metrics/export/MetricDescriptorTest.java103
-rw-r--r--api/src/test/java/io/opencensus/metrics/export/MetricProducerManagerTest.java80
-rw-r--r--api/src/test/java/io/opencensus/metrics/export/MetricTest.java180
-rw-r--r--api/src/test/java/io/opencensus/metrics/export/PointTest.java69
-rw-r--r--api/src/test/java/io/opencensus/metrics/export/SummaryTest.java189
-rw-r--r--api/src/test/java/io/opencensus/metrics/export/TimeSeriesTest.java151
-rw-r--r--api/src/test/java/io/opencensus/metrics/export/ValueTest.java168
18 files changed, 2148 insertions, 0 deletions
diff --git a/api/src/test/java/io/opencensus/metrics/DerivedDoubleGaugeTest.java b/api/src/test/java/io/opencensus/metrics/DerivedDoubleGaugeTest.java
new file mode 100644
index 00000000..dbae3c49
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/DerivedDoubleGaugeTest.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics;
+
+import io.opencensus.common.ToDoubleFunction;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link DerivedDoubleGauge}. */
+// TODO(mayurkale): Add more tests, once DerivedDoubleGauge plugs-in into the registry.
+@RunWith(JUnit4.class)
+public class DerivedDoubleGaugeTest {
+ @Rule public ExpectedException thrown = ExpectedException.none();
+
+ private static final String NAME = "name";
+ private static final String DESCRIPTION = "description";
+ private static final String UNIT = "1";
+ private static final List<LabelKey> LABEL_KEY =
+ Collections.singletonList(LabelKey.create("key", "key description"));
+ private static final List<LabelValue> LABEL_VALUES =
+ Collections.singletonList(LabelValue.create("value"));
+ private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<LabelValue>();
+
+ private final DerivedDoubleGauge derivedDoubleGauge =
+ DerivedDoubleGauge.newNoopDerivedDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ private static final ToDoubleFunction<Object> doubleFunction =
+ new ToDoubleFunction<Object>() {
+ @Override
+ public double applyAsDouble(Object value) {
+ return 5.0;
+ }
+ };
+
+ @Test
+ public void noopCreateTimeSeries_WithNullLabelValues() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelValues");
+ derivedDoubleGauge.createTimeSeries(null, null, doubleFunction);
+ }
+
+ @Test
+ public void noopCreateTimeSeries_WithNullElement() {
+ List<LabelValue> labelValues = Collections.singletonList(null);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelValue element should not be null.");
+ derivedDoubleGauge.createTimeSeries(labelValues, null, doubleFunction);
+ }
+
+ @Test
+ public void noopCreateTimeSeries_WithInvalidLabelSize() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Incorrect number of labels.");
+ derivedDoubleGauge.createTimeSeries(EMPTY_LABEL_VALUES, null, doubleFunction);
+ }
+
+ @Test
+ public void createTimeSeries_WithNullFunction() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("function");
+ derivedDoubleGauge.createTimeSeries(LABEL_VALUES, null, null);
+ }
+
+ @Test
+ public void noopRemoveTimeSeries_WithNullLabelValues() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelValues");
+ derivedDoubleGauge.removeTimeSeries(null);
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/DerivedLongGaugeTest.java b/api/src/test/java/io/opencensus/metrics/DerivedLongGaugeTest.java
new file mode 100644
index 00000000..6a462881
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/DerivedLongGaugeTest.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics;
+
+import io.opencensus.common.ToLongFunction;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link DerivedLongGauge}. */
+// TODO(mayurkale): Add more tests, once DerivedLongGauge plugs-in into the registry.
+@RunWith(JUnit4.class)
+public class DerivedLongGaugeTest {
+ @Rule public ExpectedException thrown = ExpectedException.none();
+
+ private static final String NAME = "name";
+ private static final String DESCRIPTION = "description";
+ private static final String UNIT = "1";
+ private static final List<LabelKey> LABEL_KEY =
+ Collections.singletonList(LabelKey.create("key", "key description"));
+ private static final List<LabelValue> LABEL_VALUES =
+ Collections.singletonList(LabelValue.create("value"));
+ private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<LabelValue>();
+
+ private final DerivedLongGauge derivedLongGauge =
+ DerivedLongGauge.newNoopDerivedLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ private static final ToLongFunction<Object> longFunction =
+ new ToLongFunction<Object>() {
+ @Override
+ public long applyAsLong(Object value) {
+ return 5;
+ }
+ };
+
+ @Test
+ public void noopCreateTimeSeries_WithNullLabelValues() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelValues");
+ derivedLongGauge.createTimeSeries(null, null, longFunction);
+ }
+
+ @Test
+ public void noopCreateTimeSeries_WithNullElement() {
+ List<LabelValue> labelValues = Collections.singletonList(null);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelValue element should not be null.");
+ derivedLongGauge.createTimeSeries(labelValues, null, longFunction);
+ }
+
+ @Test
+ public void noopCreateTimeSeries_WithInvalidLabelSize() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Incorrect number of labels.");
+ derivedLongGauge.createTimeSeries(EMPTY_LABEL_VALUES, null, longFunction);
+ }
+
+ @Test
+ public void createTimeSeries_WithNullFunction() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("function");
+ derivedLongGauge.createTimeSeries(LABEL_VALUES, null, null);
+ }
+
+ @Test
+ public void noopRemoveTimeSeries_WithNullLabelValues() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelValues");
+ derivedLongGauge.removeTimeSeries(null);
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/DoubleGaugeTest.java b/api/src/test/java/io/opencensus/metrics/DoubleGaugeTest.java
new file mode 100644
index 00000000..b0cdea7c
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/DoubleGaugeTest.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link DoubleGauge}. */
+@RunWith(JUnit4.class)
+public class DoubleGaugeTest {
+ @Rule public ExpectedException thrown = ExpectedException.none();
+
+ private static final String NAME = "name";
+ private static final String DESCRIPTION = "description";
+ private static final String UNIT = "1";
+ private static final List<LabelKey> LABEL_KEY =
+ Collections.singletonList(LabelKey.create("key", "key description"));
+ private static final List<LabelValue> LABEL_VALUES =
+ Collections.singletonList(LabelValue.create("value"));
+ private static final List<LabelKey> EMPTY_LABEL_KEYS = new ArrayList<LabelKey>();
+ private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<LabelValue>();
+
+ // TODO(mayurkale): Add more tests, once DoubleGauge plugs-in into the registry.
+
+ @Test
+ public void noopGetOrCreateTimeSeries_WithNullLabelValues() {
+ DoubleGauge doubleGauge =
+ DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, EMPTY_LABEL_KEYS);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelValues");
+ doubleGauge.getOrCreateTimeSeries(null);
+ }
+
+ @Test
+ public void noopGetOrCreateTimeSeries_WithNullElement() {
+ List<LabelValue> labelValues = Collections.singletonList(null);
+ DoubleGauge doubleGauge = DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelValue element should not be null.");
+ doubleGauge.getOrCreateTimeSeries(labelValues);
+ }
+
+ @Test
+ public void noopGetOrCreateTimeSeries_WithInvalidLabelSize() {
+ DoubleGauge doubleGauge = DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Incorrect number of labels.");
+ doubleGauge.getOrCreateTimeSeries(EMPTY_LABEL_VALUES);
+ }
+
+ @Test
+ public void noopRemoveTimeSeries_WithNullLabelValues() {
+ DoubleGauge doubleGauge = DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelValues");
+ doubleGauge.removeTimeSeries(null);
+ }
+
+ @Test
+ public void noopSameAs() {
+ DoubleGauge doubleGauge = DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ assertThat(doubleGauge.getDefaultTimeSeries()).isSameAs(doubleGauge.getDefaultTimeSeries());
+ assertThat(doubleGauge.getDefaultTimeSeries())
+ .isSameAs(doubleGauge.getOrCreateTimeSeries(LABEL_VALUES));
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/LabelKeyTest.java b/api/src/test/java/io/opencensus/metrics/LabelKeyTest.java
new file mode 100644
index 00000000..83f2b59a
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/LabelKeyTest.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.testing.EqualsTester;
+import java.util.Arrays;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link LabelKey}. */
+@RunWith(JUnit4.class)
+public class LabelKeyTest {
+
+ private static final LabelKey KEY = LabelKey.create("key", "description");
+
+ @Test
+ public void testGetKey() {
+ assertThat(KEY.getKey()).isEqualTo("key");
+ }
+
+ @Test
+ public void testGetDescription() {
+ assertThat(KEY.getDescription()).isEqualTo("description");
+ }
+
+ @Test
+ public void create_NoLengthConstraint() {
+ // We have a length constraint of 256-characters for TagKey. That constraint doesn't apply to
+ // LabelKey.
+ char[] chars = new char[300];
+ Arrays.fill(chars, 'k');
+ String key = new String(chars);
+ assertThat(LabelKey.create(key, "").getKey()).isEqualTo(key);
+ }
+
+ @Test
+ public void create_WithUnprintableChars() {
+ String key = "\2ab\3cd";
+ String description = "\4ef\5gh";
+ LabelKey labelKey = LabelKey.create(key, description);
+ assertThat(labelKey.getKey()).isEqualTo(key);
+ assertThat(labelKey.getDescription()).isEqualTo(description);
+ }
+
+ @Test
+ public void create_WithNonAsciiChars() {
+ String key = "é”®";
+ String description = "测试用键";
+ LabelKey nonAsciiKey = LabelKey.create(key, description);
+ assertThat(nonAsciiKey.getKey()).isEqualTo(key);
+ assertThat(nonAsciiKey.getDescription()).isEqualTo(description);
+ }
+
+ @Test
+ public void create_Empty() {
+ LabelKey emptyKey = LabelKey.create("", "");
+ assertThat(emptyKey.getKey()).isEmpty();
+ assertThat(emptyKey.getDescription()).isEmpty();
+ }
+
+ @Test
+ public void testLabelKeyEquals() {
+ new EqualsTester()
+ .addEqualityGroup(LabelKey.create("foo", ""), LabelKey.create("foo", ""))
+ .addEqualityGroup(LabelKey.create("foo", "description"))
+ .addEqualityGroup(LabelKey.create("bar", ""))
+ .testEquals();
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/LabelValueTest.java b/api/src/test/java/io/opencensus/metrics/LabelValueTest.java
new file mode 100644
index 00000000..e5526b2f
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/LabelValueTest.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.testing.EqualsTester;
+import java.util.Arrays;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link LabelValue}. */
+@RunWith(JUnit4.class)
+public class LabelValueTest {
+
+ private static final LabelValue VALUE = LabelValue.create("value");
+ private static final LabelValue UNSET = LabelValue.create(null);
+ private static final LabelValue EMPTY = LabelValue.create("");
+
+ @Test
+ public void testGetValue() {
+ assertThat(VALUE.getValue()).isEqualTo("value");
+ assertThat(UNSET.getValue()).isNull();
+ assertThat(EMPTY.getValue()).isEmpty();
+ }
+
+ @Test
+ public void create_NoLengthConstraint() {
+ // We have a length constraint of 256-characters for TagValue. That constraint doesn't apply to
+ // LabelValue.
+ char[] chars = new char[300];
+ Arrays.fill(chars, 'v');
+ String value = new String(chars);
+ assertThat(LabelValue.create(value).getValue()).isEqualTo(value);
+ }
+
+ @Test
+ public void create_WithUnprintableChars() {
+ String value = "\2ab\3cd";
+ assertThat(LabelValue.create(value).getValue()).isEqualTo(value);
+ }
+
+ @Test
+ public void create_WithNonAsciiChars() {
+ String value = "值";
+ LabelValue nonAsciiValue = LabelValue.create(value);
+ assertThat(nonAsciiValue.getValue()).isEqualTo(value);
+ }
+
+ @Test
+ public void testLabelValueEquals() {
+ new EqualsTester()
+ .addEqualityGroup(LabelValue.create("foo"), LabelValue.create("foo"))
+ .addEqualityGroup(UNSET)
+ .addEqualityGroup(EMPTY)
+ .addEqualityGroup(LabelValue.create("bar"))
+ .testEquals();
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/LongGaugeTest.java b/api/src/test/java/io/opencensus/metrics/LongGaugeTest.java
new file mode 100644
index 00000000..eedb287c
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/LongGaugeTest.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link LongGauge}. */
+@RunWith(JUnit4.class)
+public class LongGaugeTest {
+ @Rule public ExpectedException thrown = ExpectedException.none();
+
+ private static final String NAME = "name";
+ private static final String DESCRIPTION = "description";
+ private static final String UNIT = "1";
+ private static final List<LabelKey> LABEL_KEY =
+ Collections.singletonList(LabelKey.create("key", "key description"));
+ private static final List<LabelValue> LABEL_VALUES =
+ Collections.singletonList(LabelValue.create("value"));
+ private static final List<LabelKey> EMPTY_LABEL_KEYS = new ArrayList<LabelKey>();
+ private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<LabelValue>();
+
+ // TODO(mayurkale): Add more tests, once LongGauge plugs-in into the registry.
+
+ @Test
+ public void noopGetOrCreateTimeSeries_WithNullLabelValues() {
+ LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, EMPTY_LABEL_KEYS);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelValues");
+ longGauge.getOrCreateTimeSeries(null);
+ }
+
+ @Test
+ public void noopGetOrCreateTimeSeries_WithNullElement() {
+ List<LabelValue> labelValues = Collections.singletonList(null);
+ LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelValue element should not be null.");
+ longGauge.getOrCreateTimeSeries(labelValues);
+ }
+
+ @Test
+ public void noopGetOrCreateTimeSeries_WithInvalidLabelSize() {
+ LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Incorrect number of labels.");
+ longGauge.getOrCreateTimeSeries(EMPTY_LABEL_VALUES);
+ }
+
+ @Test
+ public void noopRemoveTimeSeries_WithNullLabelValues() {
+ LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelValues");
+ longGauge.removeTimeSeries(null);
+ }
+
+ @Test
+ public void noopSameAs() {
+ LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ assertThat(longGauge.getDefaultTimeSeries()).isSameAs(longGauge.getDefaultTimeSeries());
+ assertThat(longGauge.getDefaultTimeSeries())
+ .isSameAs(longGauge.getOrCreateTimeSeries(LABEL_VALUES));
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java b/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java
new file mode 100644
index 00000000..d8a26cc8
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java
@@ -0,0 +1,220 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import java.util.Collections;
+import java.util.List;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link MetricRegistry}. */
+@RunWith(JUnit4.class)
+public class MetricRegistryTest {
+ @Rule public ExpectedException thrown = ExpectedException.none();
+
+ private static final String NAME = "name";
+ private static final String NAME_2 = "name2";
+ private static final String NAME_3 = "name3";
+ private static final String NAME_4 = "name4";
+ private static final String DESCRIPTION = "description";
+ private static final String UNIT = "1";
+ private static final List<LabelKey> LABEL_KEY =
+ Collections.singletonList(LabelKey.create("key", "key description"));
+ private static final List<LabelValue> LABEL_VALUES =
+ Collections.singletonList(LabelValue.create("value"));
+ private final MetricRegistry metricRegistry =
+ MetricsComponent.newNoopMetricsComponent().getMetricRegistry();
+
+ @Test
+ public void noopAddLongGauge_NullName() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("name");
+ metricRegistry.addLongGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddLongGauge_NullDescription() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("description");
+ metricRegistry.addLongGauge(NAME, null, UNIT, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddLongGauge_NullUnit() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("unit");
+ metricRegistry.addLongGauge(NAME, DESCRIPTION, null, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddLongGauge_NullLabels() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelKeys");
+ metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, null);
+ }
+
+ @Test
+ public void noopAddLongGauge_WithNullElement() {
+ List<LabelKey> labelKeys = Collections.singletonList(null);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelKey element should not be null.");
+ metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, labelKeys);
+ }
+
+ @Test
+ public void noopAddDoubleGauge_NullName() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("name");
+ metricRegistry.addDoubleGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddDoubleGauge_NullDescription() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("description");
+ metricRegistry.addDoubleGauge(NAME_2, null, UNIT, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddDoubleGauge_NullUnit() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("unit");
+ metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, null, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddDoubleGauge_NullLabels() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelKeys");
+ metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, null);
+ }
+
+ @Test
+ public void noopAddDoubleGauge_WithNullElement() {
+ List<LabelKey> labelKeys = Collections.singletonList(null);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelKey element should not be null.");
+ metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, labelKeys);
+ }
+
+ @Test
+ public void noopAddDerivedLongGauge_NullName() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("name");
+ metricRegistry.addDerivedLongGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddDerivedLongGauge_NullDescription() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("description");
+ metricRegistry.addDerivedLongGauge(NAME_3, null, UNIT, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddDerivedLongGauge_NullUnit() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("unit");
+ metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, null, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddDerivedLongGauge_NullLabels() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelKeys");
+ metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, null);
+ }
+
+ @Test
+ public void noopAddDerivedLongGauge_WithNullElement() {
+ List<LabelKey> labelKeys = Collections.singletonList(null);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelKey element should not be null.");
+ metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, labelKeys);
+ }
+
+ @Test
+ public void noopAddDerivedDoubleGauge_NullName() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("name");
+ metricRegistry.addDerivedDoubleGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddDerivedDoubleGauge_NullDescription() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("description");
+ metricRegistry.addDerivedDoubleGauge(NAME_4, null, UNIT, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddDerivedDoubleGauge_NullUnit() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("unit");
+ metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, null, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddDerivedDoubleGauge_NullLabels() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelKeys");
+ metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, null);
+ }
+
+ @Test
+ public void noopAddDerivedDoubleGauge_WithNullElement() {
+ List<LabelKey> labelKeys = Collections.singletonList(null);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelKey element should not be null.");
+ metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, labelKeys);
+ }
+
+ @Test
+ public void noopSameAs() {
+ LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ assertThat(longGauge.getDefaultTimeSeries()).isSameAs(longGauge.getDefaultTimeSeries());
+ assertThat(longGauge.getDefaultTimeSeries())
+ .isSameAs(longGauge.getOrCreateTimeSeries(LABEL_VALUES));
+
+ DoubleGauge doubleGauge = metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY);
+ assertThat(doubleGauge.getDefaultTimeSeries()).isSameAs(doubleGauge.getDefaultTimeSeries());
+ assertThat(doubleGauge.getDefaultTimeSeries())
+ .isSameAs(doubleGauge.getOrCreateTimeSeries(LABEL_VALUES));
+ }
+
+ @Test
+ public void noopInstanceOf() {
+ assertThat(metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY))
+ .isInstanceOf(LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY).getClass());
+ assertThat(metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY))
+ .isInstanceOf(
+ DoubleGauge.newNoopDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY).getClass());
+ assertThat(metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, LABEL_KEY))
+ .isInstanceOf(
+ DerivedLongGauge.newNoopDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, LABEL_KEY)
+ .getClass());
+ assertThat(metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, LABEL_KEY))
+ .isInstanceOf(
+ DerivedDoubleGauge.newNoopDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, LABEL_KEY)
+ .getClass());
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/MetricsComponentTest.java b/api/src/test/java/io/opencensus/metrics/MetricsComponentTest.java
new file mode 100644
index 00000000..1c4e70f7
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/MetricsComponentTest.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import io.opencensus.metrics.export.ExportComponent;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link MetricsComponent}. */
+@RunWith(JUnit4.class)
+public class MetricsComponentTest {
+ @Test
+ public void defaultExportComponent() {
+ assertThat(MetricsComponent.newNoopMetricsComponent().getExportComponent())
+ .isInstanceOf(ExportComponent.newNoopExportComponent().getClass());
+ }
+
+ @Test
+ public void defaultMetricRegistry() {
+ assertThat(MetricsComponent.newNoopMetricsComponent().getMetricRegistry())
+ .isInstanceOf(MetricRegistry.newNoopMetricRegistry().getClass());
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/MetricsTest.java b/api/src/test/java/io/opencensus/metrics/MetricsTest.java
new file mode 100644
index 00000000..9e0eee1f
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/MetricsTest.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import io.opencensus.metrics.export.ExportComponent;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link Metrics}. */
+@RunWith(JUnit4.class)
+public class MetricsTest {
+ @Rule public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void loadMetricsComponent_UsesProvidedClassLoader() {
+ final RuntimeException toThrow = new RuntimeException("UseClassLoader");
+ thrown.expect(RuntimeException.class);
+ thrown.expectMessage("UseClassLoader");
+ Metrics.loadMetricsComponent(
+ new ClassLoader() {
+ @Override
+ public Class<?> loadClass(String name) {
+ throw toThrow;
+ }
+ });
+ }
+
+ @Test
+ public void loadMetricsComponent_IgnoresMissingClasses() {
+ ClassLoader classLoader =
+ new ClassLoader() {
+ @Override
+ public Class<?> loadClass(String name) throws ClassNotFoundException {
+ throw new ClassNotFoundException();
+ }
+ };
+ assertThat(Metrics.loadMetricsComponent(classLoader).getClass().getName())
+ .isEqualTo("io.opencensus.metrics.MetricsComponent$NoopMetricsComponent");
+ }
+
+ @Test
+ public void defaultExportComponent() {
+ assertThat(Metrics.getExportComponent())
+ .isInstanceOf(ExportComponent.newNoopExportComponent().getClass());
+ }
+
+ @Test
+ public void defaultMetricRegistry() {
+ assertThat(Metrics.getMetricRegistry())
+ .isInstanceOf(MetricRegistry.newNoopMetricRegistry().getClass());
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/export/DistributionTest.java b/api/src/test/java/io/opencensus/metrics/export/DistributionTest.java
new file mode 100644
index 00000000..85b31498
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/export/DistributionTest.java
@@ -0,0 +1,331 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics.export;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.testing.EqualsTester;
+import io.opencensus.common.Function;
+import io.opencensus.common.Functions;
+import io.opencensus.common.Timestamp;
+import io.opencensus.metrics.export.Distribution.Bucket;
+import io.opencensus.metrics.export.Distribution.BucketOptions;
+import io.opencensus.metrics.export.Distribution.BucketOptions.ExplicitOptions;
+import io.opencensus.metrics.export.Distribution.Exemplar;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link Distribution}. */
+@RunWith(JUnit4.class)
+public class DistributionTest {
+
+ @Rule public final ExpectedException thrown = ExpectedException.none();
+
+ private static final Timestamp TIMESTAMP = Timestamp.create(1, 0);
+ private static final Map<String, String> ATTACHMENTS = Collections.singletonMap("key", "value");
+ private static final double TOLERANCE = 1e-6;
+
+ @Test
+ public void createAndGet_Bucket() {
+ Bucket bucket = Bucket.create(98);
+ assertThat(bucket.getCount()).isEqualTo(98);
+ assertThat(bucket.getExemplar()).isNull();
+ }
+
+ @Test
+ public void createAndGet_BucketWithExemplar() {
+ Exemplar exemplar = Exemplar.create(12.2, TIMESTAMP, ATTACHMENTS);
+ Bucket bucket = Bucket.create(7, exemplar);
+ assertThat(bucket.getCount()).isEqualTo(7);
+ assertThat(bucket.getExemplar()).isEqualTo(exemplar);
+ }
+
+ @Test
+ public void createBucket_preventNullExemplar() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("exemplar");
+ Bucket.create(1, null);
+ }
+
+ @Test
+ public void createAndGet_Exemplar() {
+ Exemplar exemplar = Exemplar.create(-9.9, TIMESTAMP, ATTACHMENTS);
+ assertThat(exemplar.getValue()).isWithin(TOLERANCE).of(-9.9);
+ assertThat(exemplar.getTimestamp()).isEqualTo(TIMESTAMP);
+ assertThat(exemplar.getAttachments()).isEqualTo(ATTACHMENTS);
+ }
+
+ @Test
+ public void createAndGet_ExplicitBuckets() {
+ List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 3.0);
+
+ BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
+ final List<Double> actual = new ArrayList<Double>();
+ bucketOptions.match(
+ new Function<ExplicitOptions, Object>() {
+ @Override
+ public Object apply(ExplicitOptions arg) {
+ actual.addAll(arg.getBucketBoundaries());
+ return null;
+ }
+ },
+ Functions.throwAssertionError());
+
+ assertThat(actual).containsExactlyElementsIn(bucketBounds).inOrder();
+ }
+
+ @Test
+ public void createAndGet_ExplicitBucketsNegativeBounds() {
+ List<Double> bucketBounds = Collections.singletonList(-1.0);
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("bucket boundary should be > 0");
+ BucketOptions.explicitOptions(bucketBounds);
+ }
+
+ @Test
+ public void createAndGet_PreventNullExplicitBuckets() {
+ thrown.expect(NullPointerException.class);
+ BucketOptions.explicitOptions(Arrays.asList(1.0, null, 3.0));
+ }
+
+ @Test
+ public void createAndGet_ExplicitBucketsEmptyBounds() {
+ List<Double> bucketBounds = new ArrayList<Double>();
+ BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
+
+ final List<Double> actual = new ArrayList<Double>();
+ bucketOptions.match(
+ new Function<ExplicitOptions, Object>() {
+ @Override
+ public Object apply(ExplicitOptions arg) {
+ actual.addAll(arg.getBucketBoundaries());
+ return null;
+ }
+ },
+ Functions.throwAssertionError());
+
+ assertThat(actual).isEmpty();
+ }
+
+ @Test
+ public void createBucketOptions_UnorderedBucketBounds() {
+ List<Double> bucketBounds = Arrays.asList(1.0, 5.0, 2.0);
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("bucket boundaries not sorted.");
+ BucketOptions.explicitOptions(bucketBounds);
+ }
+
+ @Test
+ public void createAndGet_PreventNullBucketOptions() {
+ thrown.expect(NullPointerException.class);
+ BucketOptions.explicitOptions(null);
+ }
+
+ @Test
+ public void createAndGet_Distribution() {
+ Exemplar exemplar = Exemplar.create(15.0, TIMESTAMP, ATTACHMENTS);
+ List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 5.0);
+ BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
+ List<Bucket> buckets =
+ Arrays.asList(
+ Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4, exemplar));
+ Distribution distribution = Distribution.create(10, 6.6, 678.54, bucketOptions, buckets);
+ assertThat(distribution.getCount()).isEqualTo(10);
+ assertThat(distribution.getSum()).isWithin(TOLERANCE).of(6.6);
+ assertThat(distribution.getSumOfSquaredDeviations()).isWithin(TOLERANCE).of(678.54);
+
+ final List<Double> actual = new ArrayList<Double>();
+ distribution
+ .getBucketOptions()
+ .match(
+ new Function<ExplicitOptions, Object>() {
+ @Override
+ public Object apply(ExplicitOptions arg) {
+ actual.addAll(arg.getBucketBoundaries());
+ return null;
+ }
+ },
+ Functions.throwAssertionError());
+
+ assertThat(actual).containsExactlyElementsIn(bucketBounds).inOrder();
+
+ assertThat(distribution.getBuckets()).containsExactlyElementsIn(buckets).inOrder();
+ }
+
+ @Test
+ public void createBucket_NegativeCount() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("bucket count should be non-negative.");
+ Bucket.create(-5);
+ }
+
+ @Test
+ public void createExemplar_PreventNullAttachments() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("attachments");
+ Exemplar.create(15, TIMESTAMP, null);
+ }
+
+ @Test
+ public void createExemplar_PreventNullAttachmentKey() {
+ Map<String, String> attachments = Collections.singletonMap(null, "value");
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("key of attachment");
+ Exemplar.create(15, TIMESTAMP, attachments);
+ }
+
+ @Test
+ public void createExemplar_PreventNullAttachmentValue() {
+ Map<String, String> attachments = Collections.singletonMap("key", null);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("value of attachment");
+ Exemplar.create(15, TIMESTAMP, attachments);
+ }
+
+ @Test
+ public void createDistribution_NegativeCount() {
+ List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 5.0);
+ BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
+
+ List<Bucket> buckets =
+ Arrays.asList(Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4));
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("count should be non-negative.");
+ Distribution.create(-10, 6.6, 678.54, bucketOptions, buckets);
+ }
+
+ @Test
+ public void createDistribution_NegativeSumOfSquaredDeviations() {
+ List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 5.0);
+ BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
+
+ List<Bucket> buckets =
+ Arrays.asList(Bucket.create(0), Bucket.create(0), Bucket.create(0), Bucket.create(0));
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("sum of squared deviations should be non-negative.");
+ Distribution.create(0, 6.6, -678.54, bucketOptions, buckets);
+ }
+
+ @Test
+ public void createDistribution_ZeroCountAndPositiveMean() {
+ List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 5.0);
+ BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
+
+ List<Bucket> buckets =
+ Arrays.asList(Bucket.create(0), Bucket.create(0), Bucket.create(0), Bucket.create(0));
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("sum should be 0 if count is 0.");
+ Distribution.create(0, 6.6, 0, bucketOptions, buckets);
+ }
+
+ @Test
+ public void createDistribution_ZeroCountAndSumOfSquaredDeviations() {
+ List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 5.0);
+ BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
+ List<Bucket> buckets =
+ Arrays.asList(Bucket.create(0), Bucket.create(0), Bucket.create(0), Bucket.create(0));
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("sum of squared deviations should be 0 if count is 0.");
+ Distribution.create(0, 0, 678.54, bucketOptions, buckets);
+ }
+
+ @Test
+ public void createDistribution_NullBucketBoundaries() {
+ List<Bucket> buckets =
+ Arrays.asList(Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4));
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("bucketBoundaries");
+ Distribution.create(10, 6.6, 678.54, BucketOptions.explicitOptions(null), buckets);
+ }
+
+ @Test
+ public void createDistribution_NullBucketBoundary() {
+ List<Bucket> buckets =
+ Arrays.asList(Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4));
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("bucketBoundary");
+ Distribution.create(
+ 10, 6.6, 678.54, BucketOptions.explicitOptions(Arrays.asList(2.5, null)), buckets);
+ }
+
+ @Test
+ public void createDistribution_NullBucketOptions() {
+ List<Bucket> buckets =
+ Arrays.asList(Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4));
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("bucketOptions");
+ Distribution.create(10, 6.6, 678.54, null, buckets);
+ }
+
+ @Test
+ public void createDistribution_NullBucketList() {
+ List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 5.0);
+ BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("buckets");
+ Distribution.create(10, 6.6, 678.54, bucketOptions, null);
+ }
+
+ @Test
+ public void createDistribution_NullBucket() {
+ List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 5.0);
+ BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
+ List<Bucket> buckets =
+ Arrays.asList(Bucket.create(3), Bucket.create(1), null, Bucket.create(4));
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("bucket");
+ Distribution.create(10, 6.6, 678.54, bucketOptions, buckets);
+ }
+
+ @Test
+ public void testEquals() {
+ List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 2.5);
+ new EqualsTester()
+ .addEqualityGroup(
+ Distribution.create(
+ 10,
+ 10,
+ 1,
+ BucketOptions.explicitOptions(bucketBounds),
+ Arrays.asList(
+ Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4))),
+ Distribution.create(
+ 10,
+ 10,
+ 1,
+ BucketOptions.explicitOptions(bucketBounds),
+ Arrays.asList(
+ Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4))))
+ .addEqualityGroup(
+ Distribution.create(
+ 7,
+ 10,
+ 23.456,
+ BucketOptions.explicitOptions(bucketBounds),
+ Arrays.asList(
+ Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4))))
+ .testEquals();
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/export/ExportComponentTest.java b/api/src/test/java/io/opencensus/metrics/export/ExportComponentTest.java
new file mode 100644
index 00000000..15c6e883
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/export/ExportComponentTest.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics.export;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link ExportComponent}. */
+@RunWith(JUnit4.class)
+public class ExportComponentTest {
+ @Test
+ public void defaultMetricExporter() {
+ assertThat(ExportComponent.newNoopExportComponent().getMetricProducerManager())
+ .isInstanceOf(MetricProducerManager.class);
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/export/MetricDescriptorTest.java b/api/src/test/java/io/opencensus/metrics/export/MetricDescriptorTest.java
new file mode 100644
index 00000000..502170c6
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/export/MetricDescriptorTest.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics.export;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.testing.EqualsTester;
+import io.opencensus.metrics.LabelKey;
+import io.opencensus.metrics.export.MetricDescriptor.Type;
+import java.util.Arrays;
+import java.util.List;
+import org.hamcrest.CoreMatchers;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link MetricDescriptor}. */
+@RunWith(JUnit4.class)
+public class MetricDescriptorTest {
+
+ @Rule public final ExpectedException thrown = ExpectedException.none();
+
+ private static final String METRIC_NAME_1 = "metric1";
+ private static final String METRIC_NAME_2 = "metric2";
+ private static final String DESCRIPTION = "Metric description.";
+ private static final String UNIT = "kb/s";
+ private static final LabelKey KEY_1 = LabelKey.create("key1", "some key");
+ private static final LabelKey KEY_2 = LabelKey.create("key2", "some other key");
+
+ @Test
+ public void testGet() {
+ MetricDescriptor metricDescriptor =
+ MetricDescriptor.create(
+ METRIC_NAME_1, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, Arrays.asList(KEY_1, KEY_2));
+ assertThat(metricDescriptor.getName()).isEqualTo(METRIC_NAME_1);
+ assertThat(metricDescriptor.getDescription()).isEqualTo(DESCRIPTION);
+ assertThat(metricDescriptor.getUnit()).isEqualTo(UNIT);
+ assertThat(metricDescriptor.getType()).isEqualTo(Type.GAUGE_DOUBLE);
+ assertThat(metricDescriptor.getLabelKeys()).containsExactly(KEY_1, KEY_2).inOrder();
+ }
+
+ @Test
+ public void preventNullLabelKeyList() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage(CoreMatchers.equalTo("labelKeys"));
+ MetricDescriptor.create(METRIC_NAME_1, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, null);
+ }
+
+ @Test
+ public void preventNullLabelKey() {
+ List<LabelKey> keys = Arrays.asList(KEY_1, null);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage(CoreMatchers.equalTo("labelKey"));
+ MetricDescriptor.create(METRIC_NAME_1, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, keys);
+ }
+
+ @Test
+ public void testEquals() {
+ new EqualsTester()
+ .addEqualityGroup(
+ MetricDescriptor.create(
+ METRIC_NAME_1, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, Arrays.asList(KEY_1, KEY_2)),
+ MetricDescriptor.create(
+ METRIC_NAME_1, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, Arrays.asList(KEY_1, KEY_2)))
+ .addEqualityGroup(
+ MetricDescriptor.create(
+ METRIC_NAME_2, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, Arrays.asList(KEY_1, KEY_2)))
+ .addEqualityGroup(
+ MetricDescriptor.create(
+ METRIC_NAME_2, DESCRIPTION, UNIT, Type.GAUGE_INT64, Arrays.asList(KEY_1, KEY_2)))
+ .addEqualityGroup(
+ MetricDescriptor.create(
+ METRIC_NAME_1,
+ DESCRIPTION,
+ UNIT,
+ Type.CUMULATIVE_DISTRIBUTION,
+ Arrays.asList(KEY_1, KEY_2)))
+ .addEqualityGroup(
+ MetricDescriptor.create(
+ METRIC_NAME_1,
+ DESCRIPTION,
+ UNIT,
+ Type.CUMULATIVE_DISTRIBUTION,
+ Arrays.asList(KEY_1)))
+ .testEquals();
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/export/MetricProducerManagerTest.java b/api/src/test/java/io/opencensus/metrics/export/MetricProducerManagerTest.java
new file mode 100644
index 00000000..1025427f
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/export/MetricProducerManagerTest.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics.export;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+/** Unit tests for {@link MetricProducerManager}. */
+@RunWith(JUnit4.class)
+public class MetricProducerManagerTest {
+ private final MetricProducerManager metricProducerManager =
+ MetricProducerManager.newNoopMetricProducerManager();
+ @Mock private MetricProducer metricProducer;
+
+ @Rule public final ExpectedException thrown = ExpectedException.none();
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @Test
+ public void add_DisallowsNull() {
+ thrown.expect(NullPointerException.class);
+ metricProducerManager.add(null);
+ }
+
+ @Test
+ public void add() {
+ metricProducerManager.add(metricProducer);
+ assertThat(metricProducerManager.getAllMetricProducer()).isEmpty();
+ }
+
+ @Test
+ public void addAndRemove() {
+ metricProducerManager.add(metricProducer);
+ assertThat(metricProducerManager.getAllMetricProducer()).isEmpty();
+ metricProducerManager.remove(metricProducer);
+ assertThat(metricProducerManager.getAllMetricProducer()).isEmpty();
+ }
+
+ @Test
+ public void remove_DisallowsNull() {
+ thrown.expect(NullPointerException.class);
+ metricProducerManager.remove(null);
+ }
+
+ @Test
+ public void remove_FromEmpty() {
+ metricProducerManager.remove(metricProducer);
+ assertThat(metricProducerManager.getAllMetricProducer()).isEmpty();
+ }
+
+ @Test
+ public void getAllMetricProducer_empty() {
+ assertThat(metricProducerManager.getAllMetricProducer()).isEmpty();
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/export/MetricTest.java b/api/src/test/java/io/opencensus/metrics/export/MetricTest.java
new file mode 100644
index 00000000..ed205289
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/export/MetricTest.java
@@ -0,0 +1,180 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics.export;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.testing.EqualsTester;
+import io.opencensus.common.Timestamp;
+import io.opencensus.metrics.LabelKey;
+import io.opencensus.metrics.LabelValue;
+import io.opencensus.metrics.export.MetricDescriptor.Type;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link Metric}. */
+@RunWith(JUnit4.class)
+public class MetricTest {
+
+ @Rule public final ExpectedException thrown = ExpectedException.none();
+
+ private static final String METRIC_NAME_1 = "metric1";
+ private static final String METRIC_NAME_2 = "metric2";
+ private static final String DESCRIPTION = "Metric description.";
+ private static final String UNIT = "kb/s";
+ private static final LabelKey KEY_1 = LabelKey.create("key1", "some key");
+ private static final LabelKey KEY_2 = LabelKey.create("key1", "some other key");
+ private static final MetricDescriptor METRIC_DESCRIPTOR_1 =
+ MetricDescriptor.create(
+ METRIC_NAME_1, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, Arrays.asList(KEY_1, KEY_2));
+ private static final MetricDescriptor METRIC_DESCRIPTOR_2 =
+ MetricDescriptor.create(
+ METRIC_NAME_2,
+ DESCRIPTION,
+ UNIT,
+ Type.CUMULATIVE_INT64,
+ Collections.singletonList(KEY_1));
+ private static final LabelValue LABEL_VALUE_1 = LabelValue.create("value1");
+ private static final LabelValue LABEL_VALUE_2 = LabelValue.create("value1");
+ private static final LabelValue LABEL_VALUE_EMPTY = LabelValue.create("");
+ private static final Value VALUE_LONG = Value.longValue(12345678);
+ private static final Value VALUE_DOUBLE_1 = Value.doubleValue(-345.77);
+ private static final Value VALUE_DOUBLE_2 = Value.doubleValue(133.79);
+ private static final Timestamp TIMESTAMP_1 = Timestamp.fromMillis(1000);
+ private static final Timestamp TIMESTAMP_2 = Timestamp.fromMillis(2000);
+ private static final Timestamp TIMESTAMP_3 = Timestamp.fromMillis(3000);
+ private static final Point POINT_1 = Point.create(VALUE_DOUBLE_1, TIMESTAMP_2);
+ private static final Point POINT_2 = Point.create(VALUE_DOUBLE_2, TIMESTAMP_3);
+ private static final Point POINT_3 = Point.create(VALUE_LONG, TIMESTAMP_3);
+ private static final TimeSeries GAUGE_TIME_SERIES_1 =
+ TimeSeries.create(
+ Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Collections.singletonList(POINT_1), null);
+ private static final TimeSeries GAUGE_TIME_SERIES_2 =
+ TimeSeries.create(
+ Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Collections.singletonList(POINT_2), null);
+ private static final TimeSeries CUMULATIVE_TIME_SERIES =
+ TimeSeries.create(
+ Collections.singletonList(LABEL_VALUE_EMPTY),
+ Collections.singletonList(POINT_3),
+ TIMESTAMP_1);
+
+ @Test
+ public void testGet() {
+ Metric metric =
+ Metric.create(METRIC_DESCRIPTOR_1, Arrays.asList(GAUGE_TIME_SERIES_1, GAUGE_TIME_SERIES_2));
+ assertThat(metric.getMetricDescriptor()).isEqualTo(METRIC_DESCRIPTOR_1);
+ assertThat(metric.getTimeSeriesList())
+ .containsExactly(GAUGE_TIME_SERIES_1, GAUGE_TIME_SERIES_2)
+ .inOrder();
+ }
+
+ @Test
+ public void typeMismatch_GaugeDouble_Long() {
+ typeMismatch(
+ METRIC_DESCRIPTOR_1,
+ Collections.singletonList(CUMULATIVE_TIME_SERIES),
+ String.format("Type mismatch: %s, %s.", Type.GAUGE_DOUBLE, "ValueLong"));
+ }
+
+ @Test
+ public void typeMismatch_CumulativeInt64_Double() {
+ typeMismatch(
+ METRIC_DESCRIPTOR_2,
+ Collections.singletonList(GAUGE_TIME_SERIES_1),
+ String.format("Type mismatch: %s, %s.", Type.CUMULATIVE_INT64, "ValueDouble"));
+ }
+
+ private void typeMismatch(
+ MetricDescriptor metricDescriptor, List<TimeSeries> timeSeriesList, String errorMessage) {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage(errorMessage);
+ Metric.create(metricDescriptor, timeSeriesList);
+ }
+
+ @Test
+ public void create_WithNullMetricDescriptor() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("metricDescriptor");
+ Metric.create(null, Collections.<TimeSeries>emptyList());
+ }
+
+ @Test
+ public void create_WithNullTimeSeriesList() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("timeSeriesList");
+ Metric.create(METRIC_DESCRIPTOR_1, null);
+ }
+
+ @Test
+ public void create_WithNullTimeSeries() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("timeSeries");
+ Metric.create(METRIC_DESCRIPTOR_1, Arrays.asList(GAUGE_TIME_SERIES_1, null));
+ }
+
+ @Test
+ public void immutableTimeSeriesList() {
+ List<TimeSeries> timeSeriesList = new ArrayList<TimeSeries>();
+ timeSeriesList.add(GAUGE_TIME_SERIES_1);
+ Metric metric = Metric.create(METRIC_DESCRIPTOR_1, timeSeriesList);
+ timeSeriesList.add(GAUGE_TIME_SERIES_2);
+ assertThat(metric.getTimeSeriesList()).containsExactly(GAUGE_TIME_SERIES_1);
+ }
+
+ @Test
+ public void createWithOneTimeSeries_WithNullTimeSeries() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("timeSeries");
+ Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR_1, null);
+ }
+
+ @Test
+ public void createWithOneTimeSeries_WithNullMetricDescriptor() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("metricDescriptor");
+ Metric.createWithOneTimeSeries(null, GAUGE_TIME_SERIES_1);
+ }
+
+ @Test
+ public void testGet_WithOneTimeSeries() {
+ Metric metric = Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR_1, GAUGE_TIME_SERIES_1);
+ assertThat(metric.getMetricDescriptor()).isEqualTo(METRIC_DESCRIPTOR_1);
+ assertThat(metric.getTimeSeriesList()).containsExactly(GAUGE_TIME_SERIES_1);
+ }
+
+ @Test
+ public void testEquals() {
+ new EqualsTester()
+ .addEqualityGroup(
+ Metric.create(
+ METRIC_DESCRIPTOR_1, Arrays.asList(GAUGE_TIME_SERIES_1, GAUGE_TIME_SERIES_2)),
+ Metric.create(
+ METRIC_DESCRIPTOR_1, Arrays.asList(GAUGE_TIME_SERIES_1, GAUGE_TIME_SERIES_2)))
+ .addEqualityGroup(Metric.create(METRIC_DESCRIPTOR_1, Collections.<TimeSeries>emptyList()))
+ .addEqualityGroup(
+ Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR_2, CUMULATIVE_TIME_SERIES))
+ .addEqualityGroup(Metric.create(METRIC_DESCRIPTOR_2, Collections.<TimeSeries>emptyList()))
+ .testEquals();
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/export/PointTest.java b/api/src/test/java/io/opencensus/metrics/export/PointTest.java
new file mode 100644
index 00000000..cdfc7792
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/export/PointTest.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics.export;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.testing.EqualsTester;
+import io.opencensus.common.Timestamp;
+import io.opencensus.metrics.export.Distribution.Bucket;
+import io.opencensus.metrics.export.Distribution.BucketOptions;
+import java.util.Arrays;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link Point}. */
+@RunWith(JUnit4.class)
+public class PointTest {
+
+ private static final Value DOUBLE_VALUE = Value.doubleValue(55.5);
+ private static final Value LONG_VALUE = Value.longValue(9876543210L);
+ private static final Value DISTRIBUTION_VALUE =
+ Value.distributionValue(
+ Distribution.create(
+ 10,
+ 6.6,
+ 678.54,
+ BucketOptions.explicitOptions(Arrays.asList(1.0, 2.0, 5.0)),
+ Arrays.asList(
+ Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4))));
+ private static final Timestamp TIMESTAMP_1 = Timestamp.create(1, 2);
+ private static final Timestamp TIMESTAMP_2 = Timestamp.create(3, 4);
+ private static final Timestamp TIMESTAMP_3 = Timestamp.create(5, 6);
+
+ @Test
+ public void testGet() {
+ Point point = Point.create(DOUBLE_VALUE, TIMESTAMP_1);
+ assertThat(point.getValue()).isEqualTo(DOUBLE_VALUE);
+ assertThat(point.getTimestamp()).isEqualTo(TIMESTAMP_1);
+ }
+
+ @Test
+ public void testEquals() {
+ new EqualsTester()
+ .addEqualityGroup(
+ Point.create(DOUBLE_VALUE, TIMESTAMP_1), Point.create(DOUBLE_VALUE, TIMESTAMP_1))
+ .addEqualityGroup(Point.create(LONG_VALUE, TIMESTAMP_1))
+ .addEqualityGroup(Point.create(LONG_VALUE, TIMESTAMP_2))
+ .addEqualityGroup(
+ Point.create(DISTRIBUTION_VALUE, TIMESTAMP_2),
+ Point.create(DISTRIBUTION_VALUE, TIMESTAMP_2))
+ .addEqualityGroup(Point.create(DISTRIBUTION_VALUE, TIMESTAMP_3))
+ .testEquals();
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/export/SummaryTest.java b/api/src/test/java/io/opencensus/metrics/export/SummaryTest.java
new file mode 100644
index 00000000..c10df043
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/export/SummaryTest.java
@@ -0,0 +1,189 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics.export;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.testing.EqualsTester;
+import io.opencensus.metrics.export.Summary.Snapshot;
+import io.opencensus.metrics.export.Summary.Snapshot.ValueAtPercentile;
+import java.util.Collections;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link Summary}. */
+@RunWith(JUnit4.class)
+public class SummaryTest {
+
+ @Rule public final ExpectedException thrown = ExpectedException.none();
+ private static final double TOLERANCE = 1e-6;
+
+ @Test
+ public void createAndGet_ValueAtPercentile() {
+ ValueAtPercentile valueAtPercentile = ValueAtPercentile.create(99.5, 10.2);
+ assertThat(valueAtPercentile.getPercentile()).isWithin(TOLERANCE).of(99.5);
+ assertThat(valueAtPercentile.getValue()).isWithin(TOLERANCE).of(10.2);
+ }
+
+ @Test
+ public void createValueAtPercentile_InvalidValueAtPercentileInterval() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("percentile must be in the interval (0.0, 100.0]");
+ ValueAtPercentile.create(100.1, 10.2);
+ }
+
+ @Test
+ public void createValueAtPercentile_NegativeValue() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("value must be non-negative");
+ ValueAtPercentile.create(99.5, -10.2);
+ }
+
+ @Test
+ public void createAndGet_Snapshot() {
+ Snapshot snapshot =
+ Snapshot.create(
+ 10L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)));
+ assertThat(snapshot.getCount()).isEqualTo(10);
+ assertThat(snapshot.getSum()).isWithin(TOLERANCE).of(87.07);
+ assertThat(snapshot.getValueAtPercentiles())
+ .containsExactly(ValueAtPercentile.create(99.5, 10.2));
+ }
+
+ @Test
+ public void createAndGet_Snapshot_WithNullCountAndSum() {
+ Snapshot snapshot =
+ Snapshot.create(
+ null, null, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)));
+ assertThat(snapshot.getCount()).isNull();
+ assertThat(snapshot.getSum()).isNull();
+ assertThat(snapshot.getValueAtPercentiles())
+ .containsExactly(ValueAtPercentile.create(99.5, 10.2));
+ }
+
+ @Test
+ public void createSnapshot_NegativeCount() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("count must be non-negative");
+ Snapshot.create(-10L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)));
+ }
+
+ @Test
+ public void createSnapshot_NegativeSum() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("sum must be non-negative");
+ Snapshot.create(10L, -87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)));
+ }
+
+ @Test
+ public void createSnapshot_ZeroCountAndNonZeroSum() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("sum must be 0 if count is 0");
+ Snapshot.create(0L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)));
+ }
+
+ @Test
+ public void createSnapshot_NullValueAtPercentilesList() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("valueAtPercentiles");
+ Snapshot.create(10L, 87.07, null);
+ }
+
+ @Test
+ public void createSnapshot_OneNullValueAtPercentile() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("value in valueAtPercentiles");
+ Snapshot.create(10L, 87.07, Collections.<ValueAtPercentile>singletonList(null));
+ }
+
+ @Test
+ public void createAndGet_Summary() {
+ Snapshot snapshot =
+ Snapshot.create(
+ 10L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)));
+ Summary summary = Summary.create(10L, 6.6, snapshot);
+ assertThat(summary.getCount()).isEqualTo(10);
+ assertThat(summary.getSum()).isWithin(TOLERANCE).of(6.6);
+ assertThat(summary.getSnapshot()).isEqualTo(snapshot);
+ }
+
+ @Test
+ public void createSummary_NegativeCount() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("count must be non-negative");
+ Summary.create(
+ -10L, 6.6, Snapshot.create(null, null, Collections.<ValueAtPercentile>emptyList()));
+ }
+
+ @Test
+ public void createSummary_NegativeSum() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("sum must be non-negative");
+ Summary.create(
+ 10L, -6.6, Snapshot.create(null, null, Collections.<ValueAtPercentile>emptyList()));
+ }
+
+ @Test
+ public void createSummary_ZeroCountAndNonZeroSum() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("sum must be 0 if count is 0");
+ Summary.create(
+ 0L, 6.6, Snapshot.create(null, null, Collections.<ValueAtPercentile>emptyList()));
+ }
+
+ @Test
+ public void createSummary_NullSnapshot() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("snapshot");
+ Summary.create(10L, 6.6, null);
+ }
+
+ @Test
+ public void testEquals() {
+ new EqualsTester()
+ .addEqualityGroup(
+ Summary.create(
+ 10L,
+ 10.0,
+ Snapshot.create(
+ 10L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)))),
+ Summary.create(
+ 10L,
+ 10.0,
+ Snapshot.create(
+ 10L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)))))
+ .addEqualityGroup(
+ Summary.create(
+ 7L,
+ 10.0,
+ Snapshot.create(
+ 10L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)))))
+ .addEqualityGroup(
+ Summary.create(
+ 10L,
+ 7.0,
+ Snapshot.create(
+ 10L, 87.07, Collections.singletonList(ValueAtPercentile.create(99.5, 10.2)))))
+ .addEqualityGroup(
+ Summary.create(
+ 10L, 10.0, Snapshot.create(null, null, Collections.<ValueAtPercentile>emptyList())))
+ .testEquals();
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/export/TimeSeriesTest.java b/api/src/test/java/io/opencensus/metrics/export/TimeSeriesTest.java
new file mode 100644
index 00000000..92a2c8cf
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/export/TimeSeriesTest.java
@@ -0,0 +1,151 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics.export;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.testing.EqualsTester;
+import io.opencensus.common.Timestamp;
+import io.opencensus.metrics.LabelValue;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import org.hamcrest.CoreMatchers;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link TimeSeries}. */
+@RunWith(JUnit4.class)
+public class TimeSeriesTest {
+
+ @Rule public ExpectedException thrown = ExpectedException.none();
+
+ private static final LabelValue LABEL_VALUE_1 = LabelValue.create("value1");
+ private static final LabelValue LABEL_VALUE_2 = LabelValue.create("value2");
+ private static final Value VALUE_LONG = Value.longValue(12345678);
+ private static final Value VALUE_DOUBLE = Value.doubleValue(-345.77);
+ private static final Timestamp TIMESTAMP_1 = Timestamp.fromMillis(1000);
+ private static final Timestamp TIMESTAMP_2 = Timestamp.fromMillis(2000);
+ private static final Timestamp TIMESTAMP_3 = Timestamp.fromMillis(3000);
+ private static final Point POINT_1 = Point.create(VALUE_DOUBLE, TIMESTAMP_2);
+ private static final Point POINT_2 = Point.create(VALUE_LONG, TIMESTAMP_3);
+
+ @Test
+ public void testGet_TimeSeries() {
+ TimeSeries cumulativeTimeSeries =
+ TimeSeries.create(
+ Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), TIMESTAMP_1);
+ assertThat(cumulativeTimeSeries.getStartTimestamp()).isEqualTo(TIMESTAMP_1);
+ assertThat(cumulativeTimeSeries.getLabelValues())
+ .containsExactly(LABEL_VALUE_1, LABEL_VALUE_2)
+ .inOrder();
+ assertThat(cumulativeTimeSeries.getPoints()).containsExactly(POINT_1).inOrder();
+ }
+
+ @Test
+ public void create_WithNullLabelValueList() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage(CoreMatchers.equalTo("labelValues"));
+ TimeSeries.create(null, Collections.<Point>emptyList(), TIMESTAMP_1);
+ }
+
+ @Test
+ public void create_WithNullLabelValue() {
+ List<LabelValue> labelValues = Arrays.asList(LABEL_VALUE_1, null);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage(CoreMatchers.equalTo("labelValue"));
+ TimeSeries.create(labelValues, Collections.<Point>emptyList(), TIMESTAMP_1);
+ }
+
+ @Test
+ public void create_WithNullPointList() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage(CoreMatchers.equalTo("points"));
+ TimeSeries.create(Collections.<LabelValue>emptyList(), null, TIMESTAMP_1);
+ }
+
+ @Test
+ public void create_WithNullPoint() {
+ List<Point> points = Arrays.asList(POINT_1, null);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage(CoreMatchers.equalTo("point"));
+ TimeSeries.create(Collections.<LabelValue>emptyList(), points, TIMESTAMP_1);
+ }
+
+ @Test
+ public void testGet_WithOnePointTimeSeries() {
+ TimeSeries cumulativeTimeSeries =
+ TimeSeries.createWithOnePoint(
+ Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), POINT_1, TIMESTAMP_1);
+ assertThat(cumulativeTimeSeries.getStartTimestamp()).isEqualTo(TIMESTAMP_1);
+ assertThat(cumulativeTimeSeries.getLabelValues())
+ .containsExactly(LABEL_VALUE_1, LABEL_VALUE_2)
+ .inOrder();
+ assertThat(cumulativeTimeSeries.getPoints()).containsExactly(POINT_1).inOrder();
+ }
+
+ @Test
+ public void createWithOnePoint_WithNullLabelValueList() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage(CoreMatchers.equalTo("labelValues"));
+ TimeSeries.createWithOnePoint(null, POINT_1, TIMESTAMP_1);
+ }
+
+ @Test
+ public void createWithOnePoint_WithNullLabelValue() {
+ List<LabelValue> labelValues = Arrays.asList(LABEL_VALUE_1, null);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage(CoreMatchers.equalTo("labelValue"));
+ TimeSeries.createWithOnePoint(labelValues, POINT_1, TIMESTAMP_1);
+ }
+
+ @Test
+ public void createWithOnePoint_WithNullPointList() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage(CoreMatchers.equalTo("point"));
+ TimeSeries.createWithOnePoint(Collections.<LabelValue>emptyList(), null, TIMESTAMP_1);
+ }
+
+ @Test
+ public void testEquals() {
+ new EqualsTester()
+ .addEqualityGroup(
+ TimeSeries.create(
+ Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), TIMESTAMP_1),
+ TimeSeries.create(
+ Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), TIMESTAMP_1))
+ .addEqualityGroup(
+ TimeSeries.create(
+ Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), null),
+ TimeSeries.create(
+ Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), null))
+ .addEqualityGroup(
+ TimeSeries.create(
+ Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Arrays.asList(POINT_1), TIMESTAMP_2))
+ .addEqualityGroup(
+ TimeSeries.create(Arrays.asList(LABEL_VALUE_1), Arrays.asList(POINT_1), TIMESTAMP_2))
+ .addEqualityGroup(
+ TimeSeries.create(Arrays.asList(LABEL_VALUE_1), Arrays.asList(POINT_2), TIMESTAMP_2))
+ .addEqualityGroup(
+ TimeSeries.create(
+ Arrays.asList(LABEL_VALUE_1), Arrays.asList(POINT_1, POINT_2), TIMESTAMP_2))
+ .testEquals();
+ }
+}
diff --git a/api/src/test/java/io/opencensus/metrics/export/ValueTest.java b/api/src/test/java/io/opencensus/metrics/export/ValueTest.java
new file mode 100644
index 00000000..bf947692
--- /dev/null
+++ b/api/src/test/java/io/opencensus/metrics/export/ValueTest.java
@@ -0,0 +1,168 @@
+/*
+ * Copyright 2018, OpenCensus Authors
+ *
+ * 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.opencensus.metrics.export;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.testing.EqualsTester;
+import io.opencensus.common.Function;
+import io.opencensus.common.Functions;
+import io.opencensus.metrics.export.Distribution.Bucket;
+import io.opencensus.metrics.export.Distribution.BucketOptions;
+import io.opencensus.metrics.export.Distribution.BucketOptions.ExplicitOptions;
+import io.opencensus.metrics.export.Summary.Snapshot;
+import io.opencensus.metrics.export.Summary.Snapshot.ValueAtPercentile;
+import io.opencensus.metrics.export.Value.ValueDistribution;
+import io.opencensus.metrics.export.Value.ValueDouble;
+import io.opencensus.metrics.export.Value.ValueLong;
+import io.opencensus.metrics.export.Value.ValueSummary;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Unit tests for {@link Value}. */
+@RunWith(JUnit4.class)
+public class ValueTest {
+ private static final double TOLERANCE = 1e-6;
+
+ private static final Distribution DISTRIBUTION =
+ Distribution.create(
+ 10,
+ 10,
+ 1,
+ BucketOptions.explicitOptions(Arrays.asList(1.0, 2.0, 5.0)),
+ Arrays.asList(Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4)));
+ private static final Summary SUMMARY =
+ Summary.create(
+ 10L,
+ 10.0,
+ Snapshot.create(
+ 10L, 87.07, Collections.singletonList(ValueAtPercentile.create(0.98, 10.2))));
+
+ @Test
+ public void createAndGet_ValueDouble() {
+ Value value = Value.doubleValue(-34.56);
+ assertThat(value).isInstanceOf(ValueDouble.class);
+ assertThat(((ValueDouble) value).getValue()).isWithin(TOLERANCE).of(-34.56);
+ }
+
+ @Test
+ public void createAndGet_ValueLong() {
+ Value value = Value.longValue(123456789);
+ assertThat(value).isInstanceOf(ValueLong.class);
+ assertThat(((ValueLong) value).getValue()).isEqualTo(123456789);
+ }
+
+ @Test
+ public void createAndGet_ValueDistribution() {
+ Value value = Value.distributionValue(DISTRIBUTION);
+ assertThat(value).isInstanceOf(ValueDistribution.class);
+ assertThat(((ValueDistribution) value).getValue()).isEqualTo(DISTRIBUTION);
+ }
+
+ @Test
+ public void createAndGet_ValueSummary() {
+ Value value = Value.summaryValue(SUMMARY);
+ assertThat(value).isInstanceOf(ValueSummary.class);
+ assertThat(((ValueSummary) value).getValue()).isEqualTo(SUMMARY);
+ }
+
+ @Test
+ public void testEquals() {
+ new EqualsTester()
+ .addEqualityGroup(Value.doubleValue(1.0), Value.doubleValue(1.0))
+ .addEqualityGroup(Value.doubleValue(2.0))
+ .addEqualityGroup(Value.longValue(1L))
+ .addEqualityGroup(Value.longValue(2L))
+ .addEqualityGroup(
+ Value.distributionValue(
+ Distribution.create(
+ 7,
+ 10,
+ 23.456,
+ BucketOptions.explicitOptions(Arrays.asList(1.0, 2.0, 5.0)),
+ Arrays.asList(
+ Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4)))))
+ .testEquals();
+ }
+
+ @Test
+ public void testMatch() {
+ List<Value> values =
+ Arrays.asList(
+ ValueDouble.create(1.0),
+ ValueLong.create(-1),
+ ValueDistribution.create(DISTRIBUTION),
+ ValueSummary.create(SUMMARY));
+ List<Number> expected =
+ Arrays.<Number>asList(1.0, -1L, 10.0, 10L, 1.0, 1.0, 2.0, 5.0, 3L, 1L, 2L, 4L);
+ final List<Number> actual = new ArrayList<Number>();
+ for (Value value : values) {
+ value.match(
+ new Function<Double, Object>() {
+ @Override
+ public Object apply(Double arg) {
+ actual.add(arg);
+ return null;
+ }
+ },
+ new Function<Long, Object>() {
+ @Override
+ public Object apply(Long arg) {
+ actual.add(arg);
+ return null;
+ }
+ },
+ new Function<Distribution, Object>() {
+ @Override
+ public Object apply(Distribution arg) {
+ actual.add(arg.getSum());
+ actual.add(arg.getCount());
+ actual.add(arg.getSumOfSquaredDeviations());
+
+ arg.getBucketOptions()
+ .match(
+ new Function<ExplicitOptions, Object>() {
+ @Override
+ public Object apply(ExplicitOptions arg) {
+ actual.addAll(arg.getBucketBoundaries());
+ return null;
+ }
+ },
+ Functions.throwAssertionError());
+
+ for (Bucket bucket : arg.getBuckets()) {
+ actual.add(bucket.getCount());
+ }
+ return null;
+ }
+ },
+ new Function<Summary, Object>() {
+ @Override
+ public Object apply(Summary arg) {
+ return null;
+ }
+ },
+ Functions.throwAssertionError());
+ }
+ assertThat(actual).containsExactlyElementsIn(expected).inOrder();
+ }
+}