public void createNamespace(Namespace namespace, Map<String, String> metadata) {
    checkNamespaceIsValid(namespace);
    getRef().checkMutable();
    ContentKey key = ContentKey.of(namespace.levels());
    org.projectnessie.model.Namespace content =
        org.projectnessie.model.Namespace.of(key.getElements(), metadata);
    try {
      Content existing = api.getContent().reference(getReference()).key(key).get().get(key);
      if (existing != null) {
        throw namespaceAlreadyExists(key, existing, null);
      }
      try {
        commitRetry("create namespace " + key, Operation.Put.of(key, content));
      } catch (NessieReferenceConflictException e) {
        Optional<Conflict> conflict =
            NessieUtil.extractSingleConflict(
                e,
                EnumSet.of(
                    Conflict.ConflictType.KEY_EXISTS, Conflict.ConflictType.NAMESPACE_ABSENT));
        if (conflict.isPresent()) {
          switch (conflict.get().conflictType()) {
            case KEY_EXISTS:
              Content conflicting = withReference(api.getContent()).key(key).get().get(key);
              throw namespaceAlreadyExists(key, conflicting, e);
            case NAMESPACE_ABSENT:
              throw new NoSuchNamespaceException(
                  e,
                  "Cannot create namespace '%s': parent namespace '%s' does not exist",
                  namespace,
                  conflict.get().key());
          }
        }
        throw new RuntimeException(
            String.format("Cannot create namespace '%s': %s", namespace, e.getMessage()));
      }
    } catch (NessieNotFoundException e) {
      throw new RuntimeException(
          String.format(
              "Cannot create namespace '%s': ref '%s' is no longer valid.",
              namespace, getRef().getName()),
          e);
    } catch (BaseNessieClientServerException e) {
      throw new RuntimeException(
          String.format("Cannot create namespace '%s': %s", namespace, e.getMessage()), e);
    }
  }
@Test
  public void testCreateNamespaceExternalConflict()
      throws NessieConflictException, NessieNotFoundException {
    String branch = "createNamespaceExternalConflictBranch";
    createBranch(branch);
    NessieIcebergClient client = new NessieIcebergClient(api, branch, null, Map.of());

    org.projectnessie.model.Namespace nessieNs =
        org.projectnessie.model.Namespace.of(ContentKey.of("a"));
    commit(branch, "create namespace a", Operation.Put.of(ContentKey.of("a"), nessieNs));

    assertThatThrownBy(() -> client.createNamespace(Namespace.of("a"), Map.of()))
        .isInstanceOf(AlreadyExistsException.class)
        .hasMessageContaining("Namespace already exists: a");

    IcebergTable table = IcebergTable.of("file:///tmp/iceberg", 1, 1, 1, 1);
    commit(branch, "create table a.tbl2", Operation.Put.of(ContentKey.of("a", "tbl"), table));

    assertThatThrownBy(() -> client.createNamespace(Namespace.of("a", "tbl"), Map.of()))
        .isInstanceOf(AlreadyExistsException.class)
        .hasMessageContaining("Another content object with name 'a.tbl' already exists");
  }