|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.apachedubbo.v2_7; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static org.mockito.Mockito.when; |
| 10 | + |
| 11 | +import java.net.InetSocketAddress; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.Iterator; |
| 14 | +import java.util.Map; |
| 15 | +import org.apache.dubbo.common.URL; |
| 16 | +import org.apache.dubbo.rpc.RpcContext; |
| 17 | +import org.apache.dubbo.rpc.RpcInvocation; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 20 | +import org.mockito.Mock; |
| 21 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 22 | + |
| 23 | +@ExtendWith(MockitoExtension.class) |
| 24 | +class DubboHeadersGetterTest { |
| 25 | + |
| 26 | + @Mock RpcContext context; |
| 27 | + @Mock RpcInvocation rpcInvocation; |
| 28 | + |
| 29 | + @Test |
| 30 | + void testKeys() throws Exception { |
| 31 | + when(context.getUrl()).thenReturn(new URL("http", "localhost", 1)); |
| 32 | + when(context.getRemoteAddress()).thenReturn(new InetSocketAddress(1)); |
| 33 | + when(context.getLocalAddress()).thenReturn(new InetSocketAddress(1)); |
| 34 | + |
| 35 | + // for latest dep tests call getObjectAttachments, otherwise call getAttachments |
| 36 | + if (Boolean.getBoolean("testLatestDeps")) { |
| 37 | + when(getObjectAttachments()).thenReturn(Collections.singletonMap("key", "value")); |
| 38 | + } else { |
| 39 | + when(rpcInvocation.getAttachments()).thenReturn(Collections.singletonMap("key", "value")); |
| 40 | + } |
| 41 | + DubboRequest request = DubboRequest.create(rpcInvocation, context); |
| 42 | + |
| 43 | + Iterator<String> iterator = DubboHeadersGetter.INSTANCE.keys(request).iterator(); |
| 44 | + assertThat(iterator.hasNext()).isTrue(); |
| 45 | + assertThat(iterator.next()).isEqualTo("key"); |
| 46 | + assertThat(iterator.hasNext()).isFalse(); |
| 47 | + } |
| 48 | + |
| 49 | + @SuppressWarnings("unchecked") |
| 50 | + private Map<Object, Object> getObjectAttachments() throws Exception { |
| 51 | + return (Map<Object, Object>) |
| 52 | + RpcInvocation.class.getMethod("getObjectAttachments").invoke(rpcInvocation); |
| 53 | + } |
| 54 | +} |
0 commit comments