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 LongGauge}. */ 31 @RunWith(JUnit4.class) 32 public class LongGaugeTest { 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 LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, EMPTY_LABEL_KEYS); 48 thrown.expect(NullPointerException.class); 49 thrown.expectMessage("labelValues"); 50 longGauge.getOrCreateTimeSeries(null); 51 } 52 53 @Test noopGetOrCreateTimeSeries_WithNullElement()54 public void noopGetOrCreateTimeSeries_WithNullElement() { 55 List<LabelValue> labelValues = Collections.singletonList(null); 56 LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); 57 thrown.expect(NullPointerException.class); 58 thrown.expectMessage("labelValue"); 59 longGauge.getOrCreateTimeSeries(labelValues); 60 } 61 62 @Test noopGetOrCreateTimeSeries_WithInvalidLabelSize()63 public void noopGetOrCreateTimeSeries_WithInvalidLabelSize() { 64 LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); 65 thrown.expect(IllegalArgumentException.class); 66 thrown.expectMessage("Label Keys and Label Values don't have same size."); 67 longGauge.getOrCreateTimeSeries(EMPTY_LABEL_VALUES); 68 } 69 70 @Test noopRemoveTimeSeries_WithNullLabelValues()71 public void noopRemoveTimeSeries_WithNullLabelValues() { 72 LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); 73 thrown.expect(NullPointerException.class); 74 thrown.expectMessage("labelValues"); 75 longGauge.removeTimeSeries(null); 76 } 77 78 @Test noopSameAs()79 public void noopSameAs() { 80 LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); 81 assertThat(longGauge.getDefaultTimeSeries()).isSameInstanceAs(longGauge.getDefaultTimeSeries()); 82 assertThat(longGauge.getDefaultTimeSeries()) 83 .isSameInstanceAs(longGauge.getOrCreateTimeSeries(LABEL_VALUES)); 84 } 85 } 86