content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.jedis.v4_0;
  
  import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable;
  import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
  import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_ADDRESS;
  import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_PORT;
  import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_TYPE;
  import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION;
  import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT;
  import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
  import static org.assertj.core.api.Assertions.assertThat;
  
  import io.opentelemetry.api.trace.SpanKind;
  import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
  import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
  import java.net.InetAddress;
  import java.net.UnknownHostException;
  import org.junit.jupiter.api.AfterAll;
  import org.junit.jupiter.api.BeforeAll;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Test;
  import org.junit.jupiter.api.extension.RegisterExtension;
  import org.testcontainers.containers.GenericContainer;
  import redis.clients.jedis.Jedis;
  
  @SuppressWarnings("deprecation") // using deprecated semconv
  class Jedis40ClientTest {
    @RegisterExtension
    static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
  
    static GenericContainer<?> redisServer =
        new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379);
  
    static String ip;
  
    static int port;
  
    static Jedis jedis;
  
    @BeforeAll
    static void setup() throws UnknownHostException {
      redisServer.start();
      port = redisServer.getMappedPort(6379);
      ip = InetAddress.getByName(redisServer.getHost()).getHostAddress();
      jedis = new Jedis(redisServer.getHost(), port);
    }
  
    @AfterAll
    static void cleanup() {
      redisServer.stop();
      jedis.close();
    }
  
    @BeforeEach
    void reset() {
      jedis.flushAll();
      testing.clearData();
    }
  
    @Test
    void setCommand() {
      jedis.set("foo", "bar");
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("SET")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(maybeStable(DB_SYSTEM), "redis"),
                              equalTo(maybeStable(DB_STATEMENT), "SET foo ?"),
                              equalTo(maybeStable(DB_OPERATION), "SET"),
                              equalTo(NETWORK_TYPE, "ipv4"),
                              equalTo(NETWORK_PEER_PORT, port),
                              equalTo(NETWORK_PEER_ADDRESS, ip))));
    }
  
    @Test
    void getCommand() {
      jedis.set("foo", "bar");
      String value = jedis.get("foo");
  
      assertThat(value).isEqualTo("bar");
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("SET")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(maybeStable(DB_SYSTEM), "redis"),
                              equalTo(maybeStable(DB_STATEMENT), "SET foo ?"),
                              equalTo(maybeStable(DB_OPERATION), "SET"),
                              equalTo(NETWORK_TYPE, "ipv4"),
                              equalTo(NETWORK_PEER_PORT, port),
                              equalTo(NETWORK_PEER_ADDRESS, ip))),
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("GET")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(maybeStable(DB_SYSTEM), "redis"),
                              equalTo(maybeStable(DB_STATEMENT), "GET foo"),
                              equalTo(maybeStable(DB_OPERATION), "GET"),
                              equalTo(NETWORK_TYPE, "ipv4"),
                              equalTo(NETWORK_PEER_PORT, port),
                              equalTo(NETWORK_PEER_ADDRESS, ip))));
    }
  
    @Test
    void commandWithNoArguments() {
      jedis.set("foo", "bar");
      String value = jedis.randomKey();
  
      assertThat(value).isEqualTo("foo");
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("SET")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(maybeStable(DB_SYSTEM), "redis"),
                              equalTo(maybeStable(DB_STATEMENT), "SET foo ?"),
                              equalTo(maybeStable(DB_OPERATION), "SET"),
                              equalTo(NETWORK_TYPE, "ipv4"),
                              equalTo(NETWORK_PEER_PORT, port),
                              equalTo(NETWORK_PEER_ADDRESS, ip))),
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("RANDOMKEY")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(maybeStable(DB_SYSTEM), "redis"),
                              equalTo(maybeStable(DB_STATEMENT), "RANDOMKEY"),
                              equalTo(maybeStable(DB_OPERATION), "RANDOMKEY"),
                              equalTo(NETWORK_TYPE, "ipv4"),
                              equalTo(NETWORK_PEER_PORT, port),
                              equalTo(NETWORK_PEER_ADDRESS, ip))));
    }
  }
  