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