/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.iceberg.util;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Map;
import org.junit.jupiter.api.Test;

public class TestPropertyUtil {

  @Test
  void propertiesWithPrefixHandlesRegexSpecialChars() {
    // prefix[0]. is a valid regex where [0] matches char '0' and . matches any char,
    // so replaceFirst (used by the older propertiesWithPrefix) would silently fail to strip the
    // literal prefix "prefix[0]."
    Map<String, String> properties = Map.of("prefix[0].key", "value");

    Map<String, String> result = PropertyUtil.propertiesWithPrefix(properties, "prefix[0].");

    assertThat(result).containsExactly(Map.entry("key", "value"));
  }

  @Test
  void propertiesWithPrefixHandlesUnclosedRegexChars() {
    // prefix[0. contains an unclosed character class which would cause
    // PatternSyntaxException with replaceFirst (used by the older propertiesWithPrefix) but works
    // correctly with substring
    Map<String, String> properties = Map.of("prefix[0.key", "value");

    Map<String, String> result = PropertyUtil.propertiesWithPrefix(properties, "prefix[0.");

    assertThat(result).containsExactly(Map.entry("key", "value"));
  }

  @Test
  void mergeProperties() {
    Map<String, String> properties = Map.of("k1", "v1", "k2", "v2");
    Map<String, String> overrides = Map.of("k1", "v11", "k3", "v3");

    assertThat(PropertyUtil.mergeProperties(null, null)).isNull();
    assertThat(PropertyUtil.mergeProperties(properties, null)).isEqualTo(properties);
    assertThat(PropertyUtil.mergeProperties(properties, Map.of())).isEqualTo(properties);
    assertThat(PropertyUtil.mergeProperties(null, overrides)).isEqualTo(overrides);
    assertThat(PropertyUtil.mergeProperties(Map.of(), overrides)).isEqualTo(overrides);
    assertThat(PropertyUtil.mergeProperties(properties, overrides))
        .containsExactlyInAnyOrderEntriesOf(Map.of("k1", "v11", "k2", "v2", "k3", "v3"));
  }
}
