summaryrefslogtreecommitdiffstats
path: root/application
diff options
context:
space:
mode:
authorJon Marius Venstad <venstad@gmail.com>2020-09-30 11:21:43 +0200
committerJon Marius Venstad <venstad@gmail.com>2020-09-30 11:21:43 +0200
commit566162d1790d4ba851fc5e06bab07bf1d5699084 (patch)
tree751812c831f0b14c4fcbf404d2a9e3c37219cf32 /application
parenta1917d6cc083d413f7cddcf154f159d067476bd7 (diff)
Add convenience for creating a LocalDocumentAccess
Diffstat (limited to 'application')
-rw-r--r--application/src/main/java/com/yahoo/application/container/DocumentAccesses.java44
1 files changed, 44 insertions, 0 deletions
diff --git a/application/src/main/java/com/yahoo/application/container/DocumentAccesses.java b/application/src/main/java/com/yahoo/application/container/DocumentAccesses.java
new file mode 100644
index 00000000000..c0edad2baa6
--- /dev/null
+++ b/application/src/main/java/com/yahoo/application/container/DocumentAccesses.java
@@ -0,0 +1,44 @@
+// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.application.container;
+
+import com.yahoo.document.config.DocumentmanagerConfig;
+import com.yahoo.documentapi.DocumentAccess;
+import com.yahoo.documentapi.DocumentAccessParams;
+import com.yahoo.documentapi.local.LocalDocumentAccess;
+import com.yahoo.searchdefinition.derived.Deriver;
+
+import java.io.File;
+import java.util.stream.Stream;
+
+import static java.util.stream.Collectors.toList;
+
+/**
+ * Utility for working with a {@link LocalDocumentAccess} for unit testing components which require a {@link DocumentAccess}.
+ *
+ * @author jonmv
+ */
+public class DocumentAccesses {
+
+ private DocumentAccesses() { }
+
+ /**
+ * Reads the {@code .sd} files in the given directory, and returns a {@link LocalDocumentAccess} with these document types.
+ * <br>
+ * Example usage:
+ * <pre>
+ * LocalDocumentAccess access = DocumentAccesses.ofSchemas("src/main/application/schemas");
+ * </pre>
+ */
+ public static LocalDocumentAccess createFromSchemas(String schemaDirectory) {
+ File[] schemasFiles = new File(schemaDirectory).listFiles(name -> name.toString().endsWith(".sd"));
+ if (schemasFiles == null)
+ throw new IllegalArgumentException(schemaDirectory + " is not a directory");
+ if (schemasFiles.length == 0)
+ throw new IllegalArgumentException("No schema files found under " + schemaDirectory);
+ DocumentmanagerConfig config = Deriver.getDocumentManagerConfig(Stream.of(schemasFiles)
+ .map(File::toString)
+ .collect(toList())).build();
+ return new LocalDocumentAccess(new DocumentAccessParams().setDocumentmanagerConfig(config));
+ }
+
+}