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 io.opencensus.common.ToDoubleFunction; 20 import java.util.ArrayList; 21 import java.util.Collections; 22 import java.util.List; 23 import org.junit.Rule; 24 import org.junit.Test; 25 import org.junit.rules.ExpectedException; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 /** Unit tests for {@link DerivedDoubleGauge}. */ 30 // TODO(mayurkale): Add more tests, once DerivedDoubleGauge plugs-in into the registry. 31 @RunWith(JUnit4.class) 32 public class DerivedDoubleGaugeTest { 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<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<LabelValue>(); 43 44 private final DerivedDoubleGauge derivedDoubleGauge = 45 DerivedDoubleGauge.newNoopDerivedDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY); 46 private static final ToDoubleFunction<Object> doubleFunction = 47 new ToDoubleFunction<Object>() { 48 @Override 49 public double applyAsDouble(Object value) { 50 return 5.0; 51 } 52 }; 53 54 @Test noopCreateTimeSeries_WithNullLabelValues()55 public void noopCreateTimeSeries_WithNullLabelValues() { 56 thrown.expect(NullPointerException.class); 57 thrown.expectMessage("labelValues"); 58 derivedDoubleGauge.createTimeSeries(null, null, doubleFunction); 59 } 60 61 @Test noopCreateTimeSeries_WithNullElement()62 public void noopCreateTimeSeries_WithNullElement() { 63 List<LabelValue> labelValues = Collections.singletonList(null); 64 thrown.expect(NullPointerException.class); 65 thrown.expectMessage("labelValue element should not be null."); 66 derivedDoubleGauge.createTimeSeries(labelValues, null, doubleFunction); 67 } 68 69 @Test noopCreateTimeSeries_WithInvalidLabelSize()70 public void noopCreateTimeSeries_WithInvalidLabelSize() { 71 thrown.expect(IllegalArgumentException.class); 72 thrown.expectMessage("Incorrect number of labels."); 73 derivedDoubleGauge.createTimeSeries(EMPTY_LABEL_VALUES, null, doubleFunction); 74 } 75 76 @Test createTimeSeries_WithNullFunction()77 public void createTimeSeries_WithNullFunction() { 78 thrown.expect(NullPointerException.class); 79 thrown.expectMessage("function"); 80 derivedDoubleGauge.createTimeSeries(LABEL_VALUES, null, null); 81 } 82 83 @Test noopRemoveTimeSeries_WithNullLabelValues()84 public void noopRemoveTimeSeries_WithNullLabelValues() { 85 thrown.expect(NullPointerException.class); 86 thrown.expectMessage("labelValues"); 87 derivedDoubleGauge.removeTimeSeries(null); 88 } 89 } 90