@Override
  public boolean setProperties(Namespace namespace, Map<String, String> properties)
      throws NoSuchNamespaceException {
    if (!namespaceExists(namespace)) {
      throw new NoSuchNamespaceException("Namespace does not exist: %s", namespace);
    }

    Preconditions.checkNotNull(properties, "Invalid properties to set: null");

    if (properties.isEmpty()) {
      return false;
    }

    Preconditions.checkArgument(
        !properties.containsKey(NAMESPACE_EXISTS_PROPERTY),
        "Cannot set reserved property: %s",
        NAMESPACE_EXISTS_PROPERTY);

    Map<String, String> startingProperties = fetchProperties(namespace);
    Map<String, String> inserts = Maps.newHashMap();
    Map<String, String> updates = Maps.newHashMap();

    for (String key : properties.keySet()) {
      String value = properties.get(key);
      if (startingProperties.containsKey(key)) {
        updates.put(key, value);
      } else {
        inserts.put(key, value);
      }
    }

    boolean hadInserts = false;
    if (!inserts.isEmpty()) {
      hadInserts = insertProperties(namespace, inserts);
    }

    boolean hadUpdates = false;
    if (!updates.isEmpty()) {
      hadUpdates = updateProperties(namespace, updates);
    }

    return hadInserts || hadUpdates;
  }
@Test
  public void testSetProperties() {
    Namespace testNamespace = Namespace.of("testDb", "ns1", "ns2");
    Map<String, String> testMetadata =
        ImmutableMap.of("key_1", "value_1", "key_2", "value_2", "key_3", "value_3");
    catalog.createNamespace(testNamespace, testMetadata);

    // Add more properties to set to test insert and update
    Map<String, String> propertiesToSet =
        ImmutableMap.of(
            "key_5",
            "value_5",
            "key_3",
            "new_value_3",
            "key_1",
            "new_value_1",
            "key_4",
            "value_4",
            "key_2",
            "new_value_2");
    assertThat(catalog.namespaceExists(testNamespace)).isTrue();
    assertThat(catalog.setProperties(testNamespace, propertiesToSet)).isTrue();

    Map<String, String> allProperties = catalog.loadNamespaceMetadata(testNamespace);
    assertThat(allProperties).hasSize(6);

    Map<String, String> namespaceProperties = catalog.loadNamespaceMetadata(testNamespace);
    assertThat(propertiesToSet.keySet())
        .as("All new keys should be in the namespace properties")
        .isEqualTo(Sets.intersection(propertiesToSet.keySet(), namespaceProperties.keySet()));

    // values should match
    for (Map.Entry<String, String> keyValue : propertiesToSet.entrySet()) {
      assertThat(namespaceProperties)
          .as("Value for key " + keyValue.getKey() + " should match")
          .containsEntry(keyValue.getKey(), keyValue.getValue());
    }
  }