private Metadata newMetadata() {
    int val = _incMetadata.incrementAndGet();
    Metadata metadata = new Metadata().setUuid("uuid" + val).setData("metadata" + val);
    metadata.getDataInfo().setSchemaId("customSchema" + val);
    metadata.getSourceInfo().setSourceId("source" + val);
    metadata.getSourceInfo().setOwner(1);
    metadata.getHarvestInfo().setUuid("huuid" + val);
    metadata.getHarvestInfo().setHarvested(val % 2 == 0);

    return metadata;
  }
  @Test
  public void testRetrieveDatasetUuidFromIdentifierNs() {
    _repo.deleteAll();

    Metadata metadata1 = newMetadata();
    metadata1 = _repoMetadata.save(metadata1);

    InspireAtomFeed feed1 = newInspireAtomFeed();
    feed1.setMetadataId(metadata1.getId());
    feed1 = _repo.save(feed1);

    String metadataUuid =
        _repo.retrieveDatasetUuidFromIdentifierNs(
            feed1.getAtomDatasetid(), feed1.getAtomDatasetns());
    assertEquals(metadata1.getUuid(), metadataUuid);

    // Test no existing values
    metadataUuid = _repo.retrieveDatasetUuidFromIdentifierNs("aaa", "aaaa");
    assertTrue(StringUtils.isEmpty(metadataUuid));
  }
  /**
   * Create a metadata folder according to MEF {@link Version} 2 specification. If current record is
   * based on an ISO profil, the stylesheet /convert/to19139.xsl is used to map to ISO. Both files
   * are included in MEF file. Export relevant information according to format parameter.
   *
   * @param context
   * @param uuid Metadata record to export
   * @param zipFs Zip file to add new record
   * @param skipUUID
   * @param stylePath
   * @param format
   * @throws Exception
   */
  private static void createMetadataFolder(
      ServiceContext context,
      String uuid,
      FileSystem zipFs,
      boolean skipUUID,
      Path stylePath,
      Format format,
      boolean resolveXlink,
      boolean removeXlinkAttribute)
      throws Exception {

    final Path metadataRootDir = zipFs.getPath(uuid);
    Files.createDirectories(metadataRootDir);

    Pair<Metadata, String> recordAndMetadataForExport =
        MEFLib.retrieveMetadata(context, uuid, resolveXlink, removeXlinkAttribute);
    Metadata record = recordAndMetadataForExport.one();
    String xmlDocumentAsString = recordAndMetadataForExport.two();

    String id = "" + record.getId();
    String isTemp = record.getDataInfo().getType().codeString;

    if (!"y".equals(isTemp) && !"n".equals(isTemp))
      throw new Exception("Cannot export sub template");

    Path pubDir = Lib.resource.getDir(context, "public", id);
    Path priDir = Lib.resource.getDir(context, "private", id);

    final Path metadataXmlDir = metadataRootDir.resolve(MD_DIR);
    Files.createDirectories(metadataXmlDir);

    Collection<ExportFormat> formats =
        context.getApplicationContext().getBeansOfType(ExportFormat.class).values();
    for (ExportFormat exportFormat : formats) {
      for (Pair<String, String> output : exportFormat.getFormats(context, record)) {
        Files.write(metadataXmlDir.resolve(output.one()), output.two().getBytes(CHARSET));
      }
    }

    // --- save native metadata
    Files.write(metadataXmlDir.resolve(FILE_METADATA), xmlDocumentAsString.getBytes(CHARSET));

    // --- save Feature Catalog
    String ftUUID = getFeatureCatalogID(context, record.getId());
    if (!ftUUID.equals("")) {
      Pair<Metadata, String> ftrecordAndMetadata =
          MEFLib.retrieveMetadata(context, ftUUID, resolveXlink, removeXlinkAttribute);
      Path featureMdDir = metadataRootDir.resolve(SCHEMA);
      Files.createDirectories(featureMdDir);
      Files.write(featureMdDir.resolve(FILE_METADATA), ftrecordAndMetadata.two().getBytes(CHARSET));
    }

    // --- save info file
    byte[] binData =
        MEFLib.buildInfoFile(context, record, format, pubDir, priDir, skipUUID)
            .getBytes(Constants.ENCODING);

    Files.write(metadataRootDir.resolve(FILE_INFO), binData);

    // --- save thumbnails and maps

    if (format == Format.PARTIAL || format == Format.FULL) {
      IO.copyDirectoryOrFile(pubDir, metadataRootDir, true);
    }

    if (format == Format.FULL) {
      try {
        Lib.resource.checkPrivilege(context, id, ReservedOperation.download);
        IO.copyDirectoryOrFile(priDir, metadataRootDir, true);
      } catch (Exception e) {
        // Current user could not download private data
      }
    }
  }