• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Map;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.JUnit4;
30 
31 /** Unit tests for {@link MetricOptions}. */
32 @RunWith(JUnit4.class)
33 public class MetricOptionsTest {
34   @Rule public ExpectedException thrown = ExpectedException.none();
35 
36   private static final String DESCRIPTION = "test_description";
37   private static final String UNIT = "1";
38   private static final LabelKey LABEL_KEY = LabelKey.create("test_key", "test key description");
39   private static final List<LabelKey> LABEL_KEYS = Collections.singletonList(LABEL_KEY);
40   private static final LabelValue LABEL_VALUE = LabelValue.create("test_value");
41   private static final Map<LabelKey, LabelValue> CONSTANT_LABELS =
42       Collections.singletonMap(LabelKey.create("test_key_1", "test key description"), LABEL_VALUE);
43 
44   @Test
nullDescription()45   public void nullDescription() {
46     thrown.expect(NullPointerException.class);
47     thrown.expectMessage("description");
48     MetricOptions.builder().setDescription(null).build();
49   }
50 
51   @Test
nullUnit()52   public void nullUnit() {
53     thrown.expect(NullPointerException.class);
54     thrown.expectMessage("unit");
55     MetricOptions.builder().setUnit(null).build();
56   }
57 
58   @Test
nullLabelKeys()59   public void nullLabelKeys() {
60     thrown.expect(NullPointerException.class);
61     thrown.expectMessage("labelKeys");
62     MetricOptions.builder().setLabelKeys(null).build();
63   }
64 
65   @Test
labelKeys_WithNullElement()66   public void labelKeys_WithNullElement() {
67     List<LabelKey> labelKeys = Collections.singletonList(null);
68     thrown.expect(NullPointerException.class);
69     thrown.expectMessage("labelKeys elements");
70     MetricOptions.builder().setLabelKeys(labelKeys).build();
71   }
72 
73   @Test
sameLabelKeyInLabelsKey()74   public void sameLabelKeyInLabelsKey() {
75     thrown.expect(IllegalArgumentException.class);
76     thrown.expectMessage("Invalid LabelKey in labelKeys");
77     MetricOptions.builder()
78         .setLabelKeys(Arrays.asList(LABEL_KEY, LABEL_KEY))
79         .setConstantLabels(Collections.singletonMap(LABEL_KEY, LABEL_VALUE))
80         .build();
81   }
82 
83   @Test
nullConstantLabels()84   public void nullConstantLabels() {
85     thrown.expect(NullPointerException.class);
86     thrown.expectMessage("constantLabels");
87     MetricOptions.builder().setConstantLabels(null).build();
88   }
89 
90   @Test
constantLabels_WithNullKey()91   public void constantLabels_WithNullKey() {
92     Map<LabelKey, LabelValue> constantLabels = Collections.singletonMap(null, LABEL_VALUE);
93     thrown.expect(NullPointerException.class);
94     thrown.expectMessage("constantLabels elements");
95     MetricOptions.builder().setConstantLabels(constantLabels).build();
96   }
97 
98   @Test
constantLabels_WithNullValue()99   public void constantLabels_WithNullValue() {
100     Map<LabelKey, LabelValue> constantLabels = Collections.singletonMap(LABEL_KEY, null);
101     thrown.expect(NullPointerException.class);
102     thrown.expectMessage("constantLabels elements");
103     MetricOptions.builder().setConstantLabels(constantLabels).build();
104   }
105 
106   @Test
sameLabelKeyInConstantLabelsAndLabelsKey()107   public void sameLabelKeyInConstantLabelsAndLabelsKey() {
108     thrown.expect(IllegalArgumentException.class);
109     thrown.expectMessage("Invalid LabelKey in constantLabels");
110     MetricOptions.builder()
111         .setLabelKeys(LABEL_KEYS)
112         .setConstantLabels(Collections.singletonMap(LABEL_KEY, LABEL_VALUE))
113         .build();
114   }
115 
116   @Test
setAndGet()117   public void setAndGet() {
118     MetricOptions metricOptions =
119         MetricOptions.builder()
120             .setDescription(DESCRIPTION)
121             .setUnit(UNIT)
122             .setLabelKeys(LABEL_KEYS)
123             .setConstantLabels(CONSTANT_LABELS)
124             .build();
125     assertThat(metricOptions.getDescription()).isEqualTo(DESCRIPTION);
126     assertThat(metricOptions.getUnit()).isEqualTo(UNIT);
127     assertThat(metricOptions.getLabelKeys()).isEqualTo(LABEL_KEYS);
128     assertThat(metricOptions.getConstantLabels()).isEqualTo(CONSTANT_LABELS);
129   }
130 }
131