|
| 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 | + |
| 19 | +package org.apache.skywalking.apm.plugin.spring.rabbitmq; |
| 20 | + |
| 21 | +import static org.hamcrest.CoreMatchers.is; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +import java.net.InetAddress; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import org.apache.skywalking.apm.agent.core.context.SW8CarrierItem; |
| 31 | +import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment; |
| 32 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 33 | +import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule; |
| 34 | +import org.apache.skywalking.apm.agent.test.tools.SegmentStorage; |
| 35 | +import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint; |
| 36 | +import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; |
| 37 | +import org.junit.Assert; |
| 38 | +import org.junit.Before; |
| 39 | +import org.junit.Rule; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.runner.RunWith; |
| 42 | +import org.mockito.Mock; |
| 43 | +import org.springframework.amqp.core.Message; |
| 44 | +import org.springframework.amqp.core.MessageProperties; |
| 45 | + |
| 46 | +import com.rabbitmq.client.Channel; |
| 47 | +import com.rabbitmq.client.Connection; |
| 48 | + |
| 49 | +@RunWith(TracingSegmentRunner.class) |
| 50 | +public class RabbitMQSpringConsumerInterceptorTest { |
| 51 | + |
| 52 | + @SegmentStoragePoint |
| 53 | + private SegmentStorage segmentStorage; |
| 54 | + |
| 55 | + private SpringRabbitMQConsumerInterceptor rabbitMQConsumerInterceptor; |
| 56 | + |
| 57 | + @Mock |
| 58 | + private EnhancedInstance enhancedInstance; |
| 59 | + |
| 60 | + @Rule |
| 61 | + public AgentServiceRule serviceRule = new AgentServiceRule(); |
| 62 | + |
| 63 | + @Before |
| 64 | + public void setUp() throws Exception { |
| 65 | + rabbitMQConsumerInterceptor = new SpringRabbitMQConsumerInterceptor(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testRabbitMQConsumerInterceptorWithNilHeaders() throws Throwable { |
| 70 | + Object[] args = prepareMockData(false); |
| 71 | + rabbitMQConsumerInterceptor.beforeMethod(enhancedInstance, null, args, new Class[0], null); |
| 72 | + rabbitMQConsumerInterceptor.afterMethod(enhancedInstance, null, args, new Class[0], null); |
| 73 | + List<TraceSegment> traceSegments = segmentStorage.getTraceSegments(); |
| 74 | + Assert.assertThat(traceSegments.size(), is(1)); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testRabbitMQConsumerInterceptor() throws Throwable { |
| 79 | + Object[] args = prepareMockData(true); |
| 80 | + rabbitMQConsumerInterceptor.beforeMethod(enhancedInstance, null, args, new Class[0], null); |
| 81 | + rabbitMQConsumerInterceptor.afterMethod(enhancedInstance, null, args, new Class[0], null); |
| 82 | + List<TraceSegment> traceSegments = segmentStorage.getTraceSegments(); |
| 83 | + Assert.assertThat(traceSegments.size(), is(1)); |
| 84 | + } |
| 85 | + |
| 86 | + private Object[] prepareMockData(boolean withHeaders) throws Exception { |
| 87 | + Channel channel = mock(Channel.class); |
| 88 | + Connection connection = mock(Connection.class); |
| 89 | + InetAddress address = mock(InetAddress.class); |
| 90 | + Message message = mock(Message.class); |
| 91 | + MessageProperties messageProperties = mock(MessageProperties.class); |
| 92 | + |
| 93 | + when(channel.getConnection()).thenReturn(connection); |
| 94 | + when(connection.getAddress()).thenReturn(address); |
| 95 | + when(address.getHostAddress()).thenReturn("127.0.0.1"); |
| 96 | + when(connection.getPort()).thenReturn(5672); |
| 97 | + when(message.getMessageProperties()).thenReturn(messageProperties); |
| 98 | + when(messageProperties.getReceivedExchange()).thenReturn("test-exchange"); |
| 99 | + when(messageProperties.getReceivedRoutingKey()).thenReturn("test-routing-key"); |
| 100 | + |
| 101 | + if (withHeaders) { |
| 102 | + Map<String, Object> headers = new HashMap<>(); |
| 103 | + headers.put(SW8CarrierItem.HEADER_NAME, |
| 104 | + "1-My40LjU=-MS4yLjM=-3-c2VydmljZQ==-aW5zdGFuY2U=-L2FwcA==-MTI3LjAuMC4xOjgwODA="); |
| 105 | + when(messageProperties.getHeader(SW8CarrierItem.HEADER_NAME)) |
| 106 | + .thenReturn(headers.get(SW8CarrierItem.HEADER_NAME)); |
| 107 | + } |
| 108 | + |
| 109 | + return new Object[] {channel, message}; |
| 110 | + } |
| 111 | +} |
0 commit comments