1 /* 2 * Copyright 2018, OpenCensus Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.opencensus.metrics; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import java.util.ArrayList; 22 import java.util.Collections; 23 import java.util.List; 24 import org.junit.Rule; 25 import org.junit.Test; 26 import org.junit.rules.ExpectedException; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 /** Unit tests for {@link DoubleGauge}. */ 31 @RunWith(JUnit4.class) 32 public class DoubleGaugeTest { 33 @Rule public ExpectedException thrown = ExpectedException.none(); 34 35 private static final String NAME = "name"; 36 private static final String DESCRIPTION = "description"; 37 private static final String UNIT = "1"; 38 private static final List<LabelKey> LABEL_KEY = 39 Collections.singletonList(LabelKey.create("key", "key description")); 40 private static final List<LabelValue> LABEL_VALUES = 41 Collections.singletonList(LabelValue.create("value")); 42 private static final List<LabelKey> EMPTY_LABEL_KEYS = new ArrayList<LabelKey>(); 43 private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<LabelValue>(); 44 45 @Test noopGetOrCreateTimeSeries_WithNullLabelValues()46 public void noopGetOrCreateTimeSeries_WithNullLabelValues() { 47 DoubleGauge doubleGauge = 48 DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, EMPTY_LABEL_KEYS); 49 thrown.expect(NullPointerException.class); 50 thrown.expectMessage("labelValues"); 51 doubleGauge.getOrCreateTimeSeries(null); 52 } 53 54 @Test noopGetOrCreateTimeSeries_WithNullElement()55 public void noopGetOrCreateTimeSeries_WithNullElement() { 56 List<LabelValue> labelValues = Collections.singletonList(null); 57 DoubleGauge doubleGauge = DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); 58 thrown.expect(NullPointerException.class); 59 thrown.expectMessage("labelValue"); 60 doubleGauge.getOrCreateTimeSeries(labelValues); 61 } 62 63 @Test noopGetOrCreateTimeSeries_WithInvalidLabelSize()64 public void noopGetOrCreateTimeSeries_WithInvalidLabelSize() { 65 DoubleGauge doubleGauge = DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); 66 thrown.expect(IllegalArgumentException.class); 67 thrown.expectMessage("Label Keys and Label Values don't have same size."); 68 doubleGauge.getOrCreateTimeSeries(EMPTY_LABEL_VALUES); 69 } 70 71 @Test noopRemoveTimeSeries_WithNullLabelValues()72 public void noopRemoveTimeSeries_WithNullLabelValues() { 73 DoubleGauge doubleGauge = DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); 74 thrown.expect(NullPointerException.class); 75 thrown.expectMessage("labelValues"); 76 doubleGauge.removeTimeSeries(null); 77 } 78 79 @Test noopSameAs()80 public void noopSameAs() { 81 DoubleGauge doubleGauge = DoubleGauge.newNoopDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); 82 assertThat(doubleGauge.getDefaultTimeSeries()) 83 .isSameInstanceAs(doubleGauge.getDefaultTimeSeries()); 84 assertThat(doubleGauge.getDefaultTimeSeries()) 85 .isSameInstanceAs(doubleGauge.getOrCreateTimeSeries(LABEL_VALUES)); 86 } 87 } 88