code: /*
   * Copyright (c) 2022 Airbyte, Inc., all rights reserved.
   */
  
  package io.airbyte.integrations.source.tidb;
  
  import com.fasterxml.jackson.databind.JsonNode;
  import com.google.common.collect.ImmutableMap;
  import com.google.common.collect.Lists;
  import io.airbyte.commons.json.Jsons;
  import io.airbyte.db.Database;
  import io.airbyte.db.factory.DSLContextFactory;
  import io.airbyte.db.factory.DatabaseDriver;
  import io.airbyte.db.jdbc.JdbcUtils;
  import io.airbyte.integrations.base.ssh.SshHelpers;
  import io.airbyte.integrations.standardtest.source.SourceAcceptanceTest;
  import io.airbyte.integrations.standardtest.source.TestDestinationEnv;
  import io.airbyte.integrations.util.HostPortResolver;
  import io.airbyte.protocol.models.CatalogHelpers;
  import io.airbyte.protocol.models.ConfiguredAirbyteCatalog;
  import io.airbyte.protocol.models.ConfiguredAirbyteStream;
  import io.airbyte.protocol.models.ConnectorSpecification;
  import io.airbyte.protocol.models.DestinationSyncMode;
  import io.airbyte.protocol.models.Field;
  import io.airbyte.protocol.models.JsonSchemaType;
  import io.airbyte.protocol.models.SyncMode;
  import java.util.HashMap;
  import org.jooq.DSLContext;
  import org.jooq.SQLDialect;
  import org.testcontainers.containers.GenericContainer;
  import org.testcontainers.utility.DockerImageName;
  
  public class TiDBSourceAcceptanceTest extends SourceAcceptanceTest {
  
    private static final String STREAM_NAME = "id_and_name";
    private static final String STREAM_NAME2 = "public.starships";
  
    protected GenericContainer container;
    protected JsonNode config;
  
    @Override
    protected void setupEnvironment(final TestDestinationEnv testEnv) throws Exception {
      container = new GenericContainer(DockerImageName.parse("pingcap/tidb:nightly"))
          .withExposedPorts(4000);
      container.start();
  
      config = Jsons.jsonNode(ImmutableMap.builder()
          .put(JdbcUtils.HOST_KEY, HostPortResolver.resolveHost(container))
          .put(JdbcUtils.PORT_KEY, HostPortResolver.resolvePort(container))
          .put(JdbcUtils.USERNAME_KEY, "root")
          .put(JdbcUtils.DATABASE_KEY, "test")
          .build());
  
      try (final DSLContext dslContext = DSLContextFactory.create(
          config.get(JdbcUtils.USERNAME_KEY).asText(),
          "",
          DatabaseDriver.MYSQL.getDriverClassName(),
          String.format(DatabaseDriver.MYSQL.getUrlFormatString(),
              container.getHost(),
              container.getFirstMappedPort(),
              config.get(JdbcUtils.DATABASE_KEY).asText()),
          SQLDialect.MYSQL)) {
        final Database database = new Database(dslContext);
  
        database.query(ctx -> {
          ctx.fetch("CREATE TABLE id_and_name(id INTEGER, name VARCHAR(200));");
          ctx.fetch("INSERT INTO id_and_name (id, name) VALUES (1,'picard'),  (2, 'crusher'), (3, 'vash');");
          ctx.fetch("CREATE TABLE starships(id INTEGER, name VARCHAR(200));");
          ctx.fetch("INSERT INTO starships (id, name) VALUES (1,'enterprise-d'),  (2, 'defiant'), (3, 'yamato');");
          return null;
        });
      }
    }
  
    @Override
    protected void tearDown(final TestDestinationEnv testEnv) {
      container.close();
    }
  
    @Override
    protected String getImageName() {
      return "airbyte/source-tidb:dev";
    }
  
    @Override
    protected ConnectorSpecification getSpec() throws Exception {
      return SshHelpers.getSpecAndInjectSsh();
    }
  
    @Override
    protected JsonNode getConfig() {
      return config;
    }
  
    @Override
    protected ConfiguredAirbyteCatalog getConfiguredCatalog() {
      return new ConfiguredAirbyteCatalog().withStreams(Lists.newArrayList(
          new ConfiguredAirbyteStream()
              .withSyncMode(SyncMode.INCREMENTAL)
              .withCursorField(Lists.newArrayList("id"))
              .withDestinationSyncMode(DestinationSyncMode.APPEND)
              .withStream(CatalogHelpers.createAirbyteStream(
                  String.format("%s.%s", config.get(JdbcUtils.DATABASE_KEY).asText(), STREAM_NAME),
                  Field.of("id", JsonSchemaType.NUMBER),
                  Field.of("name", JsonSchemaType.STRING))
                  .withSupportedSyncModes(Lists.newArrayList(SyncMode.FULL_REFRESH, SyncMode.INCREMENTAL))),
          new ConfiguredAirbyteStream()
              .withSyncMode(SyncMode.INCREMENTAL)
              .withCursorField(Lists.newArrayList("id"))
              .withDestinationSyncMode(DestinationSyncMode.APPEND)
              .withStream(CatalogHelpers.createAirbyteStream(
                  String.format("%s.%s", config.get(JdbcUtils.DATABASE_KEY).asText(), STREAM_NAME2),
                  Field.of("id", JsonSchemaType.NUMBER),
                  Field.of("name", JsonSchemaType.STRING))
                  .withSupportedSyncModes(Lists.newArrayList(SyncMode.FULL_REFRESH, SyncMode.INCREMENTAL)))));
  
    }
  
    @Override
    protected JsonNode getState() {
      return Jsons.jsonNode(new HashMap<>());
    }
  
  }
  