code: /*
   * Copyright (c) 2022 Airbyte, Inc., all rights reserved.
   */
  
  package io.airbyte.integrations.source.cockroachdb;
  
  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.commons.resources.MoreResources;
  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.standardtest.source.SourceAcceptanceTest;
  import io.airbyte.integrations.standardtest.source.TestDestinationEnv;
  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 java.util.Objects;
  import org.jooq.DSLContext;
  import org.jooq.SQLDialect;
  import org.testcontainers.containers.CockroachContainer;
  
  public class CockroachDbSourceAcceptanceTest extends SourceAcceptanceTest {
  
    private static final String STREAM_NAME = "public.id_and_name";
    private static final String STREAM_NAME2 = "public.starships";
  
    private CockroachContainer container;
    private JsonNode config;
  
    @Override
    protected void setupEnvironment(final TestDestinationEnv environment) throws Exception {
      container = new CockroachContainer("cockroachdb/cockroach:v20.2.18");
      container.start();
  
      config = Jsons.jsonNode(ImmutableMap.builder()
          .put(JdbcUtils.HOST_KEY, Objects.requireNonNull(container.getContainerInfo()
              .getNetworkSettings()
              .getNetworks()
              .entrySet().stream()
              .findFirst()
              .get().getValue().getIpAddress()))
          // by some reason it return not a port number as exposed and mentioned in logs
          .put(JdbcUtils.PORT_KEY, container.getExposedPorts().get(1))
          .put(JdbcUtils.DATABASE_KEY, container.getDatabaseName())
          .put(JdbcUtils.USERNAME_KEY, container.getUsername())
          .put(JdbcUtils.PASSWORD_KEY, container.getPassword())
          .put(JdbcUtils.SSL_KEY, false)
          .build());
  
      try (final DSLContext dslContext = DSLContextFactory.create(
          config.get(JdbcUtils.USERNAME_KEY).asText(),
          config.get(JdbcUtils.PASSWORD_KEY).asText(),
          DatabaseDriver.POSTGRESQL.getDriverClassName(),
          String.format(DatabaseDriver.POSTGRESQL.getUrlFormatString(),
              config.get(JdbcUtils.HOST_KEY).asText(),
              config.get(JdbcUtils.PORT_KEY).asInt(),
              config.get(JdbcUtils.DATABASE_KEY).asText()),
          SQLDialect.POSTGRES)) {
        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-cockroachdb:dev";
    }
  
    @Override
    protected ConnectorSpecification getSpec() throws Exception {
      return Jsons.deserialize(MoreResources.readResource("spec.json"), ConnectorSpecification.class);
    }
  
    @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(
                  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(
                  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<>());
    }
  
  }
  