Back to Repositories

Testing JPA Map Field JSON Conversion in Apollo Config

This test suite validates the JpaMapFieldJsonConverter class functionality in Apollo, focusing on JSON conversion operations for database persistence and entity mapping. The tests ensure proper handling of map-to-JSON conversions and vice versa, with special attention to null values and varying map sizes.

Test Coverage Overview

The test suite provides comprehensive coverage of the JpaMapFieldJsonConverter functionality.

Key areas tested include:
  • Null value handling for both database and entity conversions
  • Empty map conversions
  • Single and multiple element map conversions
  • Bi-directional conversion verification
Integration points focus on database column conversion and entity attribute mapping.

Implementation Analysis

The testing approach uses JUnit Jupiter to systematically verify the converter’s behavior. The implementation employs a combination of direct assertion testing and file-based expected result comparison.

Key patterns include:
  • Separate test methods for each conversion scenario
  • Use of LinkedHashMap for ordered element testing
  • File-based JSON comparison for complex cases
  • StandardCharsets.UTF_8 for consistent encoding

Technical Details

Testing tools and configuration:
  • JUnit Jupiter test framework
  • ClassPathResource for test file loading
  • Files utility for content reading
  • HashMap and LinkedHashMap for test data structure
  • JSON test files stored in classpath resources
  • Static helper method for file content reading

Best Practices Demonstrated

The test suite exhibits several testing best practices for Java persistence testing.

Notable practices include:
  • Isolated test methods for specific scenarios
  • Consistent test method naming convention
  • External test data management
  • Proper resource handling
  • Clear separation of test cases
  • Comprehensive null and edge case handling

apolloconfig/apollo

apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/entity/JpaMapFieldJsonConverterTest.java

            
/*
 * Copyright 2024 Apollo Authors
 *
 * 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
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.ctrip.framework.apollo.biz.entity;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;

class JpaMapFieldJsonConverterTest {

  private final JpaMapFieldJsonConverter converter = new JpaMapFieldJsonConverter();

  static String readAllContentOf(String path) throws IOException {
    ClassPathResource classPathResource = new ClassPathResource(path);
    byte[] bytes = Files.readAllBytes(classPathResource.getFile().toPath());
    return new String(bytes, StandardCharsets.UTF_8);
  }

  @Test
  void convertToDatabaseColumn_null() {
    assertEquals("null", this.converter.convertToDatabaseColumn(null));
  }

  @Test
  void convertToDatabaseColumn_empty() {
    assertEquals("{}", this.converter.convertToDatabaseColumn(new HashMap<>(4)));
  }

  @Test
  void convertToDatabaseColumn_oneElement() throws IOException {
    Map<String, String> map = new HashMap<>(8);
    map.put("a", "1");

    String expected = readAllContentOf("json/converter/element.1.json");
    assertEquals(expected, this.converter.convertToDatabaseColumn(map));
  }

  @Test
  void convertToDatabaseColumn_twoElement() throws IOException {
    Map<String, String> map = new LinkedHashMap<>(8);
    map.put("a", "1");
    map.put("disableCheck", "true");

    String expected = readAllContentOf("json/converter/element.2.json");
    assertEquals(expected, this.converter.convertToDatabaseColumn(map));
  }

  @Test
  void convertToEntityAttribute_null() {
    assertNull(this.converter.convertToEntityAttribute(null));
    assertNull(this.converter.convertToEntityAttribute("null"));
  }

  @Test
  void convertToEntityAttribute_null_oneElement() throws IOException {
    Map<String, String> map = new HashMap<>(8);
    map.put("a", "1");

    String content = readAllContentOf("json/converter/element.1.json");
    assertEquals(map, this.converter.convertToEntityAttribute(content));
  }

  @Test
  void convertToEntityAttribute_null_twoElement() throws IOException {
    Map<String, String> map = new HashMap<>(8);
    map.put("a", "1");
    map.put("disableCheck", "true");

    String content = readAllContentOf("json/converter/element.2.json");
    assertEquals(map, this.converter.convertToEntityAttribute(content));
  }
}