content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.jms.v1_1;
  
  import static io.opentelemetry.api.trace.SpanKind.CONSUMER;
  import static io.opentelemetry.api.trace.SpanKind.PRODUCER;
  import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
  import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME;
  import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_MESSAGE_ID;
  import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_OPERATION;
  import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_SYSTEM;
  import static org.assertj.core.api.Assertions.assertThat;
  
  import javax.jms.Destination;
  import javax.jms.JMSException;
  import javax.jms.MessageConsumer;
  import javax.jms.MessageProducer;
  import javax.jms.TextMessage;
  import org.junit.jupiter.params.ParameterizedTest;
  import org.junit.jupiter.params.provider.MethodSource;
  
  class Jms1SuppressReceiveSpansTest extends AbstractJms1Test {
  
    @SuppressWarnings("deprecation") // using deprecated semconv
    @ParameterizedTest
    @MethodSource("destinationArguments")
    void testMessageConsumer(
        DestinationFactory destinationFactory, String destinationName, boolean isTemporary)
        throws JMSException {
  
      // given
      Destination destination = destinationFactory.create(session);
      TextMessage sentMessage = session.createTextMessage("a message");
  
      MessageProducer producer = session.createProducer(destination);
      cleanup.deferCleanup(producer::close);
      MessageConsumer consumer = session.createConsumer(destination);
      cleanup.deferCleanup(consumer::close);
  
      // when
      testing.runWithSpan("producer parent", () -> producer.send(sentMessage));
  
      TextMessage receivedMessage =
          testing.runWithSpan("consumer parent", () -> (TextMessage) consumer.receive());
  
      // then
      assertThat(receivedMessage.getText()).isEqualTo(sentMessage.getText());
  
      String messageId = receivedMessage.getJMSMessageID();
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span -> span.hasName("producer parent").hasNoParent(),
                  span ->
                      span.hasName(destinationName + " publish")
                          .hasKind(PRODUCER)
                          .hasParent(trace.getSpan(0))
                          .hasAttributesSatisfyingExactly(
                              equalTo(MESSAGING_SYSTEM, "jms"),
                              equalTo(MESSAGING_DESTINATION_NAME, destinationName),
                              equalTo(MESSAGING_OPERATION, "publish"),
                              equalTo(MESSAGING_MESSAGE_ID, messageId),
                              messagingTempDestination(isTemporary)),
                  span ->
                      span.hasName(destinationName + " receive")
                          .hasKind(CONSUMER)
                          .hasParent(trace.getSpan(1))
                          .hasTotalRecordedLinks(0)
                          .hasAttributesSatisfyingExactly(
                              equalTo(MESSAGING_SYSTEM, "jms"),
                              equalTo(MESSAGING_DESTINATION_NAME, destinationName),
                              equalTo(MESSAGING_OPERATION, "receive"),
                              equalTo(MESSAGING_MESSAGE_ID, messageId),
                              messagingTempDestination(isTemporary))),
          trace ->
              trace.hasSpansSatisfyingExactly(span -> span.hasName("consumer parent").hasNoParent()));
    }
  }
  