1 /* 2 * Copyright 2017, 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 com.google.common.testing.EqualsTester; 22 import io.opencensus.trace.Link.Type; 23 import java.util.HashMap; 24 import java.util.Map; 25 import java.util.Random; 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 /** Unit tests for {@link Link}. */ 32 @RunWith(JUnit4.class) 33 public class LinkTest { 34 private final Map<String, AttributeValue> attributesMap = new HashMap<String, AttributeValue>(); 35 private final Random random = new Random(1234); 36 private final SpanContext spanContext = 37 SpanContext.create( 38 TraceId.generateRandomId(random), SpanId.generateRandomId(random), TraceOptions.DEFAULT); 39 40 @Before setUp()41 public void setUp() { 42 attributesMap.put("MyAttributeKey0", AttributeValue.stringAttributeValue("MyStringAttribute")); 43 attributesMap.put("MyAttributeKey1", AttributeValue.longAttributeValue(10)); 44 attributesMap.put("MyAttributeKey2", AttributeValue.booleanAttributeValue(true)); 45 } 46 47 @Test fromSpanContext_ChildLink()48 public void fromSpanContext_ChildLink() { 49 Link link = Link.fromSpanContext(spanContext, Type.CHILD_LINKED_SPAN); 50 assertThat(link.getTraceId()).isEqualTo(spanContext.getTraceId()); 51 assertThat(link.getSpanId()).isEqualTo(spanContext.getSpanId()); 52 assertThat(link.getType()).isEqualTo(Type.CHILD_LINKED_SPAN); 53 } 54 55 @Test fromSpanContext_ChildLink_WithAttributes()56 public void fromSpanContext_ChildLink_WithAttributes() { 57 Link link = Link.fromSpanContext(spanContext, Type.CHILD_LINKED_SPAN, attributesMap); 58 assertThat(link.getTraceId()).isEqualTo(spanContext.getTraceId()); 59 assertThat(link.getSpanId()).isEqualTo(spanContext.getSpanId()); 60 assertThat(link.getType()).isEqualTo(Type.CHILD_LINKED_SPAN); 61 assertThat(link.getAttributes()).isEqualTo(attributesMap); 62 } 63 64 @Test fromSpanContext_ParentLink()65 public void fromSpanContext_ParentLink() { 66 Link link = Link.fromSpanContext(spanContext, Type.PARENT_LINKED_SPAN); 67 assertThat(link.getTraceId()).isEqualTo(spanContext.getTraceId()); 68 assertThat(link.getSpanId()).isEqualTo(spanContext.getSpanId()); 69 assertThat(link.getType()).isEqualTo(Type.PARENT_LINKED_SPAN); 70 } 71 72 @Test fromSpanContext_ParentLink_WithAttributes()73 public void fromSpanContext_ParentLink_WithAttributes() { 74 Link link = Link.fromSpanContext(spanContext, Type.PARENT_LINKED_SPAN, attributesMap); 75 assertThat(link.getTraceId()).isEqualTo(spanContext.getTraceId()); 76 assertThat(link.getSpanId()).isEqualTo(spanContext.getSpanId()); 77 assertThat(link.getType()).isEqualTo(Type.PARENT_LINKED_SPAN); 78 assertThat(link.getAttributes()).isEqualTo(attributesMap); 79 } 80 81 @Test link_EqualsAndHashCode()82 public void link_EqualsAndHashCode() { 83 EqualsTester tester = new EqualsTester(); 84 tester 85 .addEqualityGroup( 86 Link.fromSpanContext(spanContext, Type.PARENT_LINKED_SPAN), 87 Link.fromSpanContext(spanContext, Type.PARENT_LINKED_SPAN)) 88 .addEqualityGroup( 89 Link.fromSpanContext(spanContext, Type.CHILD_LINKED_SPAN), 90 Link.fromSpanContext(spanContext, Type.CHILD_LINKED_SPAN)) 91 .addEqualityGroup(Link.fromSpanContext(SpanContext.INVALID, Type.CHILD_LINKED_SPAN)) 92 .addEqualityGroup(Link.fromSpanContext(SpanContext.INVALID, Type.PARENT_LINKED_SPAN)) 93 .addEqualityGroup( 94 Link.fromSpanContext(spanContext, Type.PARENT_LINKED_SPAN, attributesMap), 95 Link.fromSpanContext(spanContext, Type.PARENT_LINKED_SPAN, attributesMap)); 96 tester.testEquals(); 97 } 98 99 @Test link_ToString()100 public void link_ToString() { 101 Link link = Link.fromSpanContext(spanContext, Type.CHILD_LINKED_SPAN, attributesMap); 102 assertThat(link.toString()).contains(spanContext.getTraceId().toString()); 103 assertThat(link.toString()).contains(spanContext.getSpanId().toString()); 104 assertThat(link.toString()).contains("CHILD_LINKED_SPAN"); 105 assertThat(link.toString()).contains(attributesMap.toString()); 106 link = Link.fromSpanContext(spanContext, Type.PARENT_LINKED_SPAN, attributesMap); 107 assertThat(link.toString()).contains(spanContext.getTraceId().toString()); 108 assertThat(link.toString()).contains(spanContext.getSpanId().toString()); 109 assertThat(link.toString()).contains("PARENT_LINKED_SPAN"); 110 assertThat(link.toString()).contains(attributesMap.toString()); 111 } 112 } 113