'How to get AppSearch private api key in integration test

I am using EnterpriseElasticSearch (AppSearch) and ElasticSearch for my web application. In integration test I use testcontainer to setup real database. To call api of AppSearch you need to provide AppSearch api private key. My question is how to get private api key in my integration test?

Below is my config for AppSearch & ElasticSearch by using testcontainer

/**
 * The base class for all integration test classes.
 */
public abstract class AbstractTest {

    private static final Logger log = LoggerFactory.getLogger(AbstractTest.class);

    // ElasticSearch
    public static final String ELASTICSEARCH_VERSION = "7.12.0";
    public static final String ELASTICSEARCH_USERNAME = "elastic";
    private static final String ELASTICSEARCH_PASSWORD = "elasticsearch";
    private static final String ELASTICSEARCH_HOSTNAME = "elasticsearch";
    private static final int ELASTICSEARCH_PORT = 9200;
    private static final Duration ELASTICSEARCH_STARTUP_TIMEOUT = Duration.ofMinutes(10);
    private static final DockerImageName ELASTICSEARCH_IMAGE = DockerImageName
            .parse("docker.elastic.co/elasticsearch/elasticsearch")
            .withTag(ELASTICSEARCH_VERSION);
    protected static final ElasticsearchContainer ELASTICSEARCH =
        new ElasticsearchContainer(ELASTICSEARCH_IMAGE);

    // EnterpriseSearch
    private static final String APPSEARCH_HOSTNAME = "enterprisesearch";
    private static final Duration APPSEARCH_STARTUP_TIMEOUT = Duration.ofMinutes(10);
    private static final DockerImageName APPSEARCH_IMAGE = DockerImageName
        .parse("docker.elastic.co/enterprise-search/enterprise-search")
        .withTag(ELASTICSEARCH_VERSION);
    protected static final GenericContainer APPSEARCH =
        new GenericContainer(APPSEARCH_IMAGE);


    private static void startElasticSearch() {
        ELASTICSEARCH.withPassword(ELASTICSEARCH_PASSWORD)
            .withNetworkAliases(ELASTICSEARCH_HOSTNAME)
            .withStartupTimeout(ELASTICSEARCH_STARTUP_TIMEOUT)
            .start();
    }

    private static void startAppSearch() {
        APPSEARCH.withNetworkAliases(APPSEARCH_HOSTNAME)
            .withEnv("elasticsearch.host", String.format("http://%s:%s", ELASTICSEARCH_HOSTNAME, ELASTICSEARCH_PORT))
            .withEnv("ent_search.auth.source", "standard")
            .withEnv("elasticsearch.username", ELASTICSEARCH_USERNAME)
            .withEnv("elasticsearch.password", ELASTICSEARCH_PASSWORD)
            .withEnv("JAVA_OPTS", " $JAVA_OPTS -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8")
            .withEnv("ENT_SEARCH_DEFAULT_PASSWORD", ELASTICSEARCH_PASSWORD)
            .withEnv("allow_es_settings_modification", "true")
            .withStartupTimeout(APPSEARCH_STARTUP_TIMEOUT)
            .start();
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source