Back to Repositories

Testing Elasticsearch REST DAO Operations in conductor-oss

This test suite provides a base testing framework for Elasticsearch REST DAO operations in Conductor OSS. It establishes test containers, manages client connections, and handles cleanup of Elasticsearch indices between tests.

Test Coverage Overview

The test suite covers essential Elasticsearch REST client operations and index management functionality.

  • Setup and teardown of Elasticsearch test containers
  • REST client connection management
  • Index cleanup and maintenance
  • DAO initialization and configuration

Implementation Analysis

The implementation uses JUnit’s Before/After hooks to manage test lifecycle and resources. It leverages Spring’s RetryTemplate for resilient operations and implements a robust cleanup mechanism for Elasticsearch indices.

The test suite utilizes the Elasticsearch REST client API for direct index management and demonstrates proper resource handling patterns.

Technical Details

  • Elasticsearch 7.x REST client
  • JUnit test framework
  • Spring RetryTemplate
  • TestContainers for Elasticsearch
  • HTTP-based index management
  • Buffered I/O operations

Best Practices Demonstrated

The test suite exemplifies several testing best practices for distributed systems.

  • Proper resource cleanup after tests
  • Containerized test environment
  • Abstract base class for shared functionality
  • Robust error handling
  • Clean separation of setup and teardown logic

conductor-oss/conductor

es7-persistence/src/test/java/com/netflix/conductor/es7/dao/index/ElasticSearchRestDaoBaseTest.java

            
/*
 * Copyright 2023 Conductor Authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package com.netflix.conductor.es7.dao.index;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.http.HttpHost;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.junit.After;
import org.junit.Before;
import org.springframework.retry.support.RetryTemplate;

public abstract class ElasticSearchRestDaoBaseTest extends ElasticSearchTest {

    protected RestClient restClient;
    protected ElasticSearchRestDAOV7 indexDAO;

    @Before
    public void setup() throws Exception {
        String httpHostAddress = container.getHttpHostAddress();
        String host = httpHostAddress.split(":")[0];
        int port = Integer.parseInt(httpHostAddress.split(":")[1]);

        properties.setUrl("http://" + httpHostAddress);

        RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(host, port, "http"));
        restClient = restClientBuilder.build();

        indexDAO =
                new ElasticSearchRestDAOV7(
                        restClientBuilder, new RetryTemplate(), properties, objectMapper);
        indexDAO.setup();
    }

    @After
    public void tearDown() throws Exception {
        deleteAllIndices();

        if (restClient != null) {
            restClient.close();
        }
    }

    private void deleteAllIndices() throws IOException {
        Response beforeResponse = restClient.performRequest(new Request("GET", "/_cat/indices"));

        Reader streamReader = new InputStreamReader(beforeResponse.getEntity().getContent());
        BufferedReader bufferedReader = new BufferedReader(streamReader);

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            String[] fields = line.split("\\s");
            String endpoint = String.format("/%s", fields[2]);

            restClient.performRequest(new Request("DELETE", endpoint));
        }
    }
}