Skip to content

Commit a3ac7f1

Browse files
authored
[ISSUE #4995] unit tests for TraceUtils.java (#5046)
1 parent 294e4c2 commit a3ac7f1

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.eventmesh.runtime.util;
19+
20+
import org.apache.eventmesh.runtime.boot.EventMeshServer;
21+
import org.apache.eventmesh.runtime.mock.MockCloudEvent;
22+
import org.apache.eventmesh.runtime.trace.Trace;
23+
24+
import java.util.Map;
25+
26+
import org.junit.jupiter.api.Assertions;
27+
import org.junit.jupiter.api.Test;
28+
import org.mockito.MockedStatic;
29+
import org.mockito.Mockito;
30+
31+
import io.cloudevents.SpecVersion;
32+
import io.opentelemetry.api.trace.Span;
33+
34+
public class TraceUtilsTest {
35+
@Test
36+
public void testShouldPrepareClientSpan() throws Exception {
37+
Map<String, Object> cloudEventExtensionMap = EventMeshUtil.getCloudEventExtensionMap(SpecVersion.V1.toString(), new MockCloudEvent());
38+
try (MockedStatic<EventMeshServer> dummyStatic = Mockito.mockStatic(EventMeshServer.class)) {
39+
Trace trace = Trace.getInstance("zipkin", true);
40+
trace.init();
41+
dummyStatic.when(EventMeshServer::getTrace).thenReturn(trace);
42+
Span testClientSpan = TraceUtils.prepareClientSpan(
43+
cloudEventExtensionMap,
44+
"test client span",
45+
false
46+
);
47+
Assertions.assertNotNull(testClientSpan);
48+
}
49+
}
50+
51+
@Test
52+
public void testShouldPrepareServerSpan() throws Exception {
53+
Map<String, Object> cloudEventExtensionMap = EventMeshUtil.getCloudEventExtensionMap(SpecVersion.V1.toString(), new MockCloudEvent());
54+
try (MockedStatic<EventMeshServer> dummyStatic = Mockito.mockStatic(EventMeshServer.class)) {
55+
Trace trace = Trace.getInstance("zipkin", true);
56+
trace.init();
57+
dummyStatic.when(EventMeshServer::getTrace).thenReturn(trace);
58+
TraceUtils.prepareClientSpan(
59+
cloudEventExtensionMap,
60+
"test client span",
61+
false
62+
);
63+
Span testServerSpan = TraceUtils.prepareServerSpan(
64+
cloudEventExtensionMap,
65+
"test server span",
66+
false
67+
);
68+
Assertions.assertNotNull(testServerSpan);
69+
}
70+
}
71+
72+
@Test
73+
public void testShouldFinishSpan() throws Exception {
74+
MockCloudEvent cloudEvent = new MockCloudEvent();
75+
Map<String, Object> cloudEventExtensionMap = EventMeshUtil.getCloudEventExtensionMap(SpecVersion.V1.toString(), cloudEvent);
76+
try (MockedStatic<EventMeshServer> dummyStatic = Mockito.mockStatic(EventMeshServer.class)) {
77+
Trace trace = Trace.getInstance("zipkin", true);
78+
trace.init();
79+
dummyStatic.when(EventMeshServer::getTrace).thenReturn(trace);
80+
Span testClientSpan = TraceUtils.prepareClientSpan(
81+
cloudEventExtensionMap,
82+
"test client span",
83+
false
84+
);
85+
86+
TraceUtils.finishSpan(testClientSpan, cloudEvent);
87+
Assertions.assertFalse(testClientSpan.isRecording());
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)