content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.javaagent.instrumentation.lettuce.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.ServerAttributes.SERVER_ADDRESS;
  import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT;
  import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION;
  import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
  import static org.assertj.core.api.Assertions.assertThat;
  import static org.assertj.core.api.Assertions.catchException;
  
  import com.google.common.collect.ImmutableMap;
  import com.lambdaworks.redis.ClientOptions;
  import com.lambdaworks.redis.RedisClient;
  import com.lambdaworks.redis.RedisConnectionException;
  import com.lambdaworks.redis.api.StatefulRedisConnection;
  import com.lambdaworks.redis.api.sync.RedisCommands;
  import io.opentelemetry.api.trace.SpanKind;
  import io.opentelemetry.instrumentation.test.utils.PortUtils;
  import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
  import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
  import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
  import java.util.Map;
  import org.junit.jupiter.api.AfterAll;
  import org.junit.jupiter.api.BeforeAll;
  import org.junit.jupiter.api.Test;
  import org.junit.jupiter.api.extension.RegisterExtension;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.testcontainers.containers.GenericContainer;
  import org.testcontainers.containers.output.Slf4jLogConsumer;
  import org.testcontainers.containers.wait.strategy.Wait;
  import org.testcontainers.utility.DockerImageName;
  
  @SuppressWarnings("deprecation") // using deprecated semconv
  class LettuceSyncClientTest {
    private static final Logger logger = LoggerFactory.getLogger(LettuceSyncClientTest.class);
  
    @RegisterExtension
    protected static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
  
    @RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create();
  
    static final DockerImageName containerImage = DockerImageName.parse("redis:6.2.3-alpine");
  
    private static final int DB_INDEX = 0;
  
    // Disable auto reconnect, so we do not get stray traces popping up on server shutdown
    private static final ClientOptions CLIENT_OPTIONS =
        new ClientOptions.Builder().autoReconnect(false).build();
  
    private static final GenericContainer<?> redisServer =
        new GenericContainer<>(containerImage)
            .withExposedPorts(6379)
            .withLogConsumer(new Slf4jLogConsumer(logger))
            .waitingFor(Wait.forLogMessage(".*Ready to accept connections.*", 1));
  
    private static String host;
    private static int port;
    private static int incorrectPort;
    private static String dbUriNonExistent;
    private static String embeddedDbUri;
  
    private static final ImmutableMap<String, String> testHashMap =
        ImmutableMap.of(
            "firstname", "John",
            "lastname", "Doe",
            "age", "53");
  
    static RedisClient redisClient;
  
    private static StatefulRedisConnection<String, String> connection;
    static RedisCommands<String, String> syncCommands;
  
    @BeforeAll
    static void setUp() {
      redisServer.start();
  
      host = redisServer.getHost();
      port = redisServer.getMappedPort(6379);
      embeddedDbUri = "redis://" + host + ":" + port + "/" + DB_INDEX;
  
      incorrectPort = PortUtils.findOpenPort();
      dbUriNonExistent = "redis://" + host + ":" + incorrectPort + "/" + DB_INDEX;
  
      redisClient = RedisClient.create(embeddedDbUri);
  
      connection = redisClient.connect();
      syncCommands = connection.sync();
  
      syncCommands.set("TESTKEY", "TESTVAL");
      syncCommands.hmset("TESTHM", testHashMap);
  
      // 2 sets + 1 connect trace
      testing.waitForTraces(3);
      testing.clearData();
    }
  
    @AfterAll
    static void cleanUp() {
      connection.close();
      redisClient.shutdown();
      redisServer.stop();
    }
  
    @Test
    void testConnect() {
      RedisClient testConnectionClient = RedisClient.create(embeddedDbUri);
      testConnectionClient.setOptions(CLIENT_OPTIONS);
  
      StatefulRedisConnection<String, String> testConnection = testConnectionClient.connect();
      cleanup.deferCleanup(() -> testConnection.close());
      cleanup.deferCleanup(testConnectionClient::shutdown);
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("CONNECT")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(SERVER_ADDRESS, host),
                              equalTo(SERVER_PORT, port),
                              equalTo(maybeStable(DB_SYSTEM), "redis"))));
    }
  
    @Test
    void testConnectException() {
      RedisClient testConnectionClient = RedisClient.create(dbUriNonExistent);
      testConnectionClient.setOptions(CLIENT_OPTIONS);
      cleanup.deferCleanup(testConnectionClient::shutdown);
  
      Exception exception = catchException(testConnectionClient::connect);
  
      assertThat(exception).isInstanceOf(RedisConnectionException.class);
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("CONNECT")
                          .hasKind(SpanKind.CLIENT)
                          .hasException(exception)
                          .hasAttributesSatisfyingExactly(
                              equalTo(SERVER_ADDRESS, host),
                              equalTo(SERVER_PORT, incorrectPort),
                              equalTo(maybeStable(DB_SYSTEM), "redis"))));
    }
  
    @Test
    void testSetCommand() {
      String res = syncCommands.set("TESTSETKEY", "TESTSETVAL");
      assertThat(res).isEqualTo("OK");
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("SET")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(maybeStable(DB_SYSTEM), "redis"),
                              equalTo(maybeStable(DB_OPERATION), "SET"))));
    }
  
    @Test
    void testGetCommand() {
      String res = syncCommands.get("TESTKEY");
      assertThat(res).isEqualTo("TESTVAL");
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("GET")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(maybeStable(DB_SYSTEM), "redis"),
                              equalTo(maybeStable(DB_OPERATION), "GET"))));
    }
  
    @Test
    void testGetNonExistentKeyCommand() {
      String res = syncCommands.get("NON_EXISTENT_KEY");
      assertThat(res).isNull();
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("GET")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(maybeStable(DB_SYSTEM), "redis"),
                              equalTo(maybeStable(DB_OPERATION), "GET"))));
    }
  
    @Test
    void testCommandWithNoArguments() {
      String res = syncCommands.randomkey();
      assertThat(res).isNotNull();
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("RANDOMKEY")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(maybeStable(DB_SYSTEM), "redis"),
                              equalTo(maybeStable(DB_OPERATION), "RANDOMKEY"))));
    }
  
    @Test
    void testListCommand() {
      long res = syncCommands.lpush("TESTLIST", "TESTLIST ELEMENT");
      assertThat(res).isEqualTo(1);
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("LPUSH")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(maybeStable(DB_SYSTEM), "redis"),
                              equalTo(maybeStable(DB_OPERATION), "LPUSH"))));
    }
  
    @Test
    void testHashSetCommand() {
      String res = syncCommands.hmset("user", testHashMap);
      assertThat(res).isEqualTo("OK");
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("HMSET")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(maybeStable(DB_SYSTEM), "redis"),
                              equalTo(maybeStable(DB_OPERATION), "HMSET"))));
    }
  
    @Test
    void testHashGetallCommand() {
      Map<String, String> res = syncCommands.hgetall("TESTHM");
      assertThat(res).isEqualTo(testHashMap);
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("HGETALL")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(maybeStable(DB_SYSTEM), "redis"),
                              equalTo(maybeStable(DB_OPERATION), "HGETALL"))));
    }
  
    @Test
    void testDebugSegfaultCommandWithNoArgumentShouldProduceSpan() {
      // Test Causes redis to crash therefore it needs its own container
      GenericContainer<?> server = new GenericContainer<>(containerImage).withExposedPorts(6379);
      server.start();
      cleanup.deferCleanup(server::stop);
  
      long serverPort = server.getMappedPort(6379);
      RedisClient client = RedisClient.create("redis://" + host + ":" + serverPort + "/" + DB_INDEX);
      client.setOptions(CLIENT_OPTIONS);
      StatefulRedisConnection<String, String> connection1 = client.connect();
      cleanup.deferCleanup(connection1);
      cleanup.deferCleanup(client::shutdown);
  
      RedisCommands<String, String> commands = connection1.sync();
      // 1 connect trace
      testing.waitForTraces(1);
      testing.clearData();
  
      commands.debugSegfault();
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("DEBUG")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(maybeStable(DB_SYSTEM), "redis"),
                              equalTo(maybeStable(DB_OPERATION), "DEBUG"))));
    }
  
    @Test
    void testShutdownCommandShouldProduceSpan() {
      // Test Causes redis to crash therefore it needs its own container
      GenericContainer<?> server = new GenericContainer<>(containerImage).withExposedPorts(6379);
      server.start();
      cleanup.deferCleanup(server::stop);
  
      long shutdownServerPort = server.getMappedPort(6379);
  
      RedisClient client =
          RedisClient.create("redis://" + host + ":" + shutdownServerPort + "/" + DB_INDEX);
      client.setOptions(CLIENT_OPTIONS);
      StatefulRedisConnection<String, String> connection1 = client.connect();
      cleanup.deferCleanup(connection1);
      cleanup.deferCleanup(client::shutdown);
  
      RedisCommands<String, String> commands = connection1.sync();
      // 1 connect trace
      testing.waitForTraces(1);
      testing.clearData();
  
      commands.shutdown(false);
  
      testing.waitAndAssertTraces(
          trace ->
              trace.hasSpansSatisfyingExactly(
                  span ->
                      span.hasName("SHUTDOWN")
                          .hasKind(SpanKind.CLIENT)
                          .hasAttributesSatisfyingExactly(
                              equalTo(maybeStable(DB_SYSTEM), "redis"),
                              equalTo(maybeStable(DB_OPERATION), "SHUTDOWN"))));
    }
  }
  