Back to Repositories

Testing Elasticsearch 7.x Connection and Mapping Validation in Canal

This test suite validates the Elasticsearch 7.x connection functionality in Canal’s client adapter, focusing on mapping metadata retrieval and validation. It ensures proper connection establishment and mapping structure verification for Elasticsearch indices.

Test Coverage Overview

The test suite covers essential Elasticsearch connection and mapping functionality.

Key areas tested include:
  • Connection initialization with localhost configuration
  • Mapping metadata retrieval for specific indices
  • Validation of mapping structure and properties
  • Null checking for column names and types

Implementation Analysis

The testing approach utilizes JUnit 4 framework with Spring assertions for validation. The implementation follows a setup-execute-verify pattern, using ESConnection class to interface with Elasticsearch 7.x.

Notable patterns include:
  • Before setup for connection initialization
  • Map-based property configuration
  • Recursive mapping structure traversal
  • Type-safe casting for nested properties

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • Spring Assert utilities
  • Elasticsearch 7.x client libraries
  • REST client mode configuration
  • Local Elasticsearch instance (127.0.0.1:9200)
  • Custom ESConnection wrapper class

Best Practices Demonstrated

The test implementation showcases several testing best practices and quality approaches.

Notable practices include:
  • Proper test initialization and setup separation
  • Explicit assertion messages for better error reporting
  • Structured connection configuration management
  • Thorough null checking for critical properties
  • Clear separation of connection and testing logic

alibaba/canal

client-adapter/es7x/src/test/java/com/alibaba/otter/canal/client/adapter/es7x/test/ESConnectionTest.java

            
package com.alibaba.otter.canal.client.adapter.es7x.test;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.util.Assert;

import com.alibaba.otter.canal.client.adapter.es7x.support.ESConnection;

@Ignore
public class ESConnectionTest {

    ESConnection esConnection;

    @Before
    public void init() throws UnknownHostException {
        String[] hosts = new String[] { "127.0.0.1:9200" };
        Map<String, String> properties = new HashMap<>();
        properties.put("cluster.name", "elasticsearch");
        esConnection = new ESConnection(hosts, properties, ESConnection.ESClientMode.REST);
    }

    @Test
    public void test01() {
        MappingMetaData mappingMetaData = esConnection.getMapping("mytest_user");

        Map<String, Object> sourceMap = mappingMetaData.getSourceAsMap();
        Map<String, Object> esMapping = (Map<String, Object>) sourceMap.get("properties");
        for (Map.Entry<String, Object> entry : esMapping.entrySet()) {
            Map<String, Object> value = (Map<String, Object>) entry.getValue();
            if (value.containsKey("properties")) {
                System.out.println(entry.getKey() + " object");
            } else {
                System.out.println(entry.getKey() + " " + value.get("type"));
                Assert.notNull(entry.getKey(), "null column name");
                Assert.notNull(value.get("type"), "null column type");
            }
        }
    }
}