content: /*
   * Copyright (c) 2023 Airbyte, Inc., all rights reserved.
   */
  
  package io.airbyte.integrations.destination.postgres;
  
  import com.fasterxml.jackson.databind.JsonNode;
  import com.fasterxml.jackson.databind.node.ObjectNode;
  import io.airbyte.cdk.integrations.base.JavaBaseConstants;
  import io.airbyte.cdk.integrations.destination.StandardNameTransformer;
  import io.airbyte.cdk.integrations.standardtest.destination.JdbcDestinationAcceptanceTest;
  import io.airbyte.cdk.integrations.standardtest.destination.comparator.TestDataComparator;
  import io.airbyte.commons.json.Jsons;
  import java.util.List;
  import java.util.stream.Collectors;
  
  public abstract class AbstractPostgresDestinationAcceptanceTest extends JdbcDestinationAcceptanceTest {
  
    public static final String DEFAULT_DEV_IMAGE = "airbyte/destination-postgres:dev";
  
    private final StandardNameTransformer namingResolver = new StandardNameTransformer();
  
    @Override
    protected String getImageName() {
      return DEFAULT_DEV_IMAGE;
    }
  
    @Override
    protected JsonNode getFailCheckConfig() throws Exception {
      final JsonNode clone = Jsons.clone(getConfig());
      ((ObjectNode) clone).put("password", "wrong password");
      return clone;
    }
  
    @Override
    protected List<JsonNode> retrieveNormalizedRecords(final TestDestinationEnv env, final String streamName, final String namespace)
        throws Exception {
      final String tableName = namingResolver.getIdentifier(streamName);
      return retrieveRecordsFromTable(tableName, namespace);
    }
  
    @Override
    // namingResolver.getRawTableName is deprecated
    @SuppressWarnings("deprecation")
    protected List<JsonNode> retrieveRecords(final TestDestinationEnv env,
                                             final String streamName,
                                             final String namespace,
                                             final JsonNode streamSchema)
        throws Exception {
      return retrieveRecordsFromTable(namingResolver.getRawTableName(streamName), namespace)
          .stream()
          .map(r -> r.get(JavaBaseConstants.COLUMN_NAME_DATA))
          .collect(Collectors.toList());
    }
  
    protected List<JsonNode> retrieveRecordsFromTable(final String tableName, final String schemaName) throws Exception {
      // TODO: Change emitted_at with DV2
      return getTestDb().query(ctx -> {
        ctx.execute("set time zone 'UTC';");
        return ctx.fetch(String.format("SELECT * FROM %s.%s ORDER BY %s ASC;", schemaName, tableName, JavaBaseConstants.COLUMN_NAME_EMITTED_AT))
            .stream()
            .map(this::getJsonFromRecord)
            .collect(Collectors.toList());
      });
    }
  
    protected abstract PostgresTestDatabase getTestDb();
  
    @Override
    protected boolean implementsNamespaces() {
      return true;
    }
  
    @Override
    protected TestDataComparator getTestDataComparator() {
      return new PostgresTestDataComparator();
    }
  
    @Override
    protected boolean supportBasicDataTypeTest() {
      return true;
    }
  
    @Override
    protected boolean supportArrayDataTypeTest() {
      return true;
    }
  
    @Override
    protected boolean supportObjectDataTypeTest() {
      return true;
    }
  
    @Override
    protected boolean supportsInDestinationNormalization() {
      return true;
    }
  
  }
  