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.trace;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.junit.runners.JUnit4;
24 
25 /** Unit tests for {@link MessageEvent}. */
26 @RunWith(JUnit4.class)
27 public class MessageEventTest {
28   @Test(expected = NullPointerException.class)
buildMessageEvent_NullType()29   public void buildMessageEvent_NullType() {
30     MessageEvent.builder(null, 1L).build();
31   }
32 
33   @Test
buildMessageEvent_WithRequiredFields()34   public void buildMessageEvent_WithRequiredFields() {
35     MessageEvent messageEvent = MessageEvent.builder(MessageEvent.Type.SENT, 1L).build();
36     assertThat(messageEvent.getType()).isEqualTo(MessageEvent.Type.SENT);
37     assertThat(messageEvent.getMessageId()).isEqualTo(1L);
38     assertThat(messageEvent.getUncompressedMessageSize()).isEqualTo(0L);
39   }
40 
41   @Test
buildMessageEvent_WithUncompressedMessageSize()42   public void buildMessageEvent_WithUncompressedMessageSize() {
43     MessageEvent messageEvent =
44         MessageEvent.builder(MessageEvent.Type.SENT, 1L).setUncompressedMessageSize(123L).build();
45     assertThat(messageEvent.getType()).isEqualTo(MessageEvent.Type.SENT);
46     assertThat(messageEvent.getMessageId()).isEqualTo(1L);
47     assertThat(messageEvent.getUncompressedMessageSize()).isEqualTo(123L);
48   }
49 
50   @Test
buildMessageEvent_WithCompressedMessageSize()51   public void buildMessageEvent_WithCompressedMessageSize() {
52     MessageEvent messageEvent =
53         MessageEvent.builder(MessageEvent.Type.SENT, 1L).setCompressedMessageSize(123L).build();
54     assertThat(messageEvent.getType()).isEqualTo(MessageEvent.Type.SENT);
55     assertThat(messageEvent.getMessageId()).isEqualTo(1L);
56     assertThat(messageEvent.getCompressedMessageSize()).isEqualTo(123L);
57   }
58 
59   @Test
buildMessageEvent_WithAllValues()60   public void buildMessageEvent_WithAllValues() {
61     MessageEvent messageEvent =
62         MessageEvent.builder(MessageEvent.Type.RECEIVED, 1L)
63             .setUncompressedMessageSize(123L)
64             .setCompressedMessageSize(63L)
65             .build();
66     assertThat(messageEvent.getType()).isEqualTo(MessageEvent.Type.RECEIVED);
67     assertThat(messageEvent.getMessageId()).isEqualTo(1L);
68     assertThat(messageEvent.getUncompressedMessageSize()).isEqualTo(123L);
69     assertThat(messageEvent.getCompressedMessageSize()).isEqualTo(63L);
70   }
71 
72   @Test
messageEvent_ToString()73   public void messageEvent_ToString() {
74     MessageEvent messageEvent =
75         MessageEvent.builder(MessageEvent.Type.SENT, 1L)
76             .setUncompressedMessageSize(123L)
77             .setCompressedMessageSize(63L)
78             .build();
79     assertThat(messageEvent.toString()).contains("type=SENT");
80     assertThat(messageEvent.toString()).contains("messageId=1");
81     assertThat(messageEvent.toString()).contains("compressedMessageSize=63");
82     assertThat(messageEvent.toString()).contains("uncompressedMessageSize=123");
83   }
84 }
85