From 566162d1790d4ba851fc5e06bab07bf1d5699084 Mon Sep 17 00:00:00 2001 From: Jon Marius Venstad Date: Wed, 30 Sep 2020 11:21:43 +0200 Subject: Add convenience for creating a LocalDocumentAccess --- .../application/container/DocumentAccesses.java | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 application/src/main/java/com/yahoo/application/container/DocumentAccesses.java (limited to 'application/src') 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. + *
+ * Example usage: + *
+     * LocalDocumentAccess access = DocumentAccesses.ofSchemas("src/main/application/schemas");
+     * 
+ */ + 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)); + } + +} -- cgit v1.2.3