aboutsummaryrefslogtreecommitdiffstats
path: root/application/src/main/java/com/yahoo/application/container/DocumentAccesses.java
blob: 709b804fadebab5ecd04f40f21321b8a977133a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright Yahoo. 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.schema.derived.Deriver;

import java.io.File;
import java.util.stream.Stream;

/**
 * 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)
                                                                              .toList()).build();
        return new LocalDocumentAccess(new DocumentAccessParams().setDocumentmanagerConfig(config));
    }

}