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 // TODO(mayurkale): Add more tests, once LongGauge plugs-in into the registry. 46 47 @Test noopGetOrCreateTimeSeries_WithNullLabelValues()48 public void noopGetOrCreateTimeSeries_WithNullLabelValues() { 49 LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, EMPTY_LABEL_KEYS); 50 thrown.expect(NullPointerException.class); 51 thrown.expectMessage("labelValues"); 52 longGauge.getOrCreateTimeSeries(null); 53 } 54 55 @Test noopGetOrCreateTimeSeries_WithNullElement()56 public void noopGetOrCreateTimeSeries_WithNullElement() { 57 List<LabelValue> labelValues = Collections.singletonList(null); 58 LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); 59 thrown.expect(NullPointerException.class); 60 thrown.expectMessage("labelValue element should not be null."); 61 longGauge.getOrCreateTimeSeries(labelValues); 62 } 63 64 @Test noopGetOrCreateTimeSeries_WithInvalidLabelSize()65 public void noopGetOrCreateTimeSeries_WithInvalidLabelSize() { 66 LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); 67 thrown.expect(IllegalArgumentException.class); 68 thrown.expectMessage("Incorrect number of labels."); 69 longGauge.getOrCreateTimeSeries(EMPTY_LABEL_VALUES); 70 } 71 72 @Test noopRemoveTimeSeries_WithNullLabelValues()73 public void noopRemoveTimeSeries_WithNullLabelValues() { 74 LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); 75 thrown.expect(NullPointerException.class); 76 thrown.expectMessage("labelValues"); 77 longGauge.removeTimeSeries(null); 78 } 79 80 @Test noopSameAs()81 public void noopSameAs() { 82 LongGauge longGauge = LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); 83 assertThat(longGauge.getDefaultTimeSeries()).isSameAs(longGauge.getDefaultTimeSeries()); 84 assertThat(longGauge.getDefaultTimeSeries()) 85 .isSameAs(longGauge.getOrCreateTimeSeries(LABEL_VALUES)); 86 } 87 } 88