• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 DerivedDoubleCumulative}. */
30 @RunWith(JUnit4.class)
31 public class DerivedDoubleCumulativeTest {
32   @Rule public ExpectedException thrown = ExpectedException.none();
33 
34   private static final String NAME = "name";
35   private static final String DESCRIPTION = "description";
36   private static final String UNIT = "1";
37   private static final List<LabelKey> LABEL_KEY =
38       Collections.singletonList(LabelKey.create("key", "key description"));
39   private static final List<LabelValue> LABEL_VALUES =
40       Collections.singletonList(LabelValue.create("value"));
41   private static final List<LabelValue> EMPTY_LABEL_VALUES = new ArrayList<LabelValue>();
42 
43   private final DerivedDoubleCumulative derivedDoubleCumulative =
44       DerivedDoubleCumulative.newNoopDerivedDoubleCumulative(NAME, DESCRIPTION, UNIT, LABEL_KEY);
45   private static final ToDoubleFunction<Object> doubleFunction =
46       new ToDoubleFunction<Object>() {
47         @Override
48         public double applyAsDouble(Object value) {
49           return 5.0;
50         }
51       };
52 
53   @Test
noopCreateTimeSeries_WithNullLabelValues()54   public void noopCreateTimeSeries_WithNullLabelValues() {
55     thrown.expect(NullPointerException.class);
56     thrown.expectMessage("labelValues");
57     derivedDoubleCumulative.createTimeSeries(null, null, doubleFunction);
58   }
59 
60   @Test
noopCreateTimeSeries_WithNullElement()61   public void noopCreateTimeSeries_WithNullElement() {
62     List<LabelValue> labelValues = Collections.singletonList(null);
63     thrown.expect(NullPointerException.class);
64     thrown.expectMessage("labelValue");
65     derivedDoubleCumulative.createTimeSeries(labelValues, null, doubleFunction);
66   }
67 
68   @Test
noopCreateTimeSeries_WithInvalidLabelSize()69   public void noopCreateTimeSeries_WithInvalidLabelSize() {
70     thrown.expect(IllegalArgumentException.class);
71     thrown.expectMessage("Label Keys and Label Values don't have same size.");
72     derivedDoubleCumulative.createTimeSeries(EMPTY_LABEL_VALUES, null, doubleFunction);
73   }
74 
75   @Test
createTimeSeries_WithNullFunction()76   public void createTimeSeries_WithNullFunction() {
77     thrown.expect(NullPointerException.class);
78     thrown.expectMessage("function");
79     derivedDoubleCumulative.createTimeSeries(LABEL_VALUES, null, null);
80   }
81 
82   @Test
noopRemoveTimeSeries_WithNullLabelValues()83   public void noopRemoveTimeSeries_WithNullLabelValues() {
84     thrown.expect(NullPointerException.class);
85     thrown.expectMessage("labelValues");
86     derivedDoubleCumulative.removeTimeSeries(null);
87   }
88 }
89