• 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.data;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import io.opencensus.common.Timestamp;
22 import io.opencensus.metrics.data.AttachmentValue.AttachmentValueString;
23 import java.util.Collections;
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 io.opencensus.metrics.data.Exemplar}. */
32 @RunWith(JUnit4.class)
33 public class ExemplarTest {
34 
35   private static final double TOLERANCE = 1e-6;
36   private static final Timestamp TIMESTAMP_1 = Timestamp.create(1, 0);
37   private static final AttachmentValue ATTACHMENT_VALUE = AttachmentValueString.create("value");
38   private static final Map<String, AttachmentValue> ATTACHMENTS =
39       Collections.singletonMap("key", ATTACHMENT_VALUE);
40 
41   @Rule public ExpectedException thrown = ExpectedException.none();
42 
43   @Test
testExemplar()44   public void testExemplar() {
45     Exemplar exemplar = Exemplar.create(15.0, TIMESTAMP_1, ATTACHMENTS);
46     assertThat(exemplar.getValue()).isWithin(TOLERANCE).of(15.0);
47     assertThat(exemplar.getTimestamp()).isEqualTo(TIMESTAMP_1);
48     assertThat(exemplar.getAttachments()).isEqualTo(ATTACHMENTS);
49   }
50 
51   @Test
testExemplar_PreventNullTimestamp()52   public void testExemplar_PreventNullTimestamp() {
53     thrown.expect(NullPointerException.class);
54     Exemplar.create(15, null, ATTACHMENTS);
55   }
56 
57   @Test
testExemplar_PreventNullAttachments()58   public void testExemplar_PreventNullAttachments() {
59     thrown.expect(NullPointerException.class);
60     thrown.expectMessage("attachments");
61     Exemplar.create(15, TIMESTAMP_1, null);
62   }
63 
64   @Test
testExemplar_PreventNullAttachmentKey()65   public void testExemplar_PreventNullAttachmentKey() {
66     Map<String, AttachmentValue> attachments = Collections.singletonMap(null, ATTACHMENT_VALUE);
67     thrown.expect(NullPointerException.class);
68     thrown.expectMessage("key of attachment");
69     Exemplar.create(15, TIMESTAMP_1, attachments);
70   }
71 
72   @Test
testExemplar_PreventNullAttachmentValue()73   public void testExemplar_PreventNullAttachmentValue() {
74     Map<String, AttachmentValue> attachments = Collections.singletonMap("key", null);
75     thrown.expect(NullPointerException.class);
76     thrown.expectMessage("value of attachment");
77     Exemplar.create(15, TIMESTAMP_1, attachments);
78   }
79 }
80