summaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/searchdefinition/derived/AbstractExportingTestCase.java
blob: f39896f57791d4ef61502e4ff094861db74c2c5f (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.derived;

import com.yahoo.document.DocumenttypesConfig;
import com.yahoo.document.config.DocumentmanagerConfig;
import com.yahoo.io.IOUtils;
import com.yahoo.searchdefinition.Search;
import com.yahoo.searchdefinition.SearchBuilder;
import com.yahoo.searchdefinition.SearchDefinitionTestCase;
import com.yahoo.searchdefinition.parser.ParseException;
import com.yahoo.searchlib.rankingexpression.integration.ml.ImportedModels;
import com.yahoo.vespa.configmodel.producers.DocumentManager;
import com.yahoo.vespa.configmodel.producers.DocumentTypes;

import java.io.*;

/**
 * Superclass of tests needing file comparisons
 *
 * @author bratseth
 */
public abstract class AbstractExportingTestCase extends SearchDefinitionTestCase {

    static final String tempDir = "temp/";
    static final String searchDefRoot = "src/test/derived/";

    protected DerivedConfiguration derive(String dirName, String searchDefinitionName) throws IOException, ParseException {
        File toDir = new File(tempDir + dirName);
        toDir.mkdirs();
        deleteContent(toDir);

        SearchBuilder builder = SearchBuilder.createFromDirectory(searchDefRoot + dirName + "/");
        //SearchBuilder builder = SearchBuilder.createFromFile(searchDefDir + name + ".sd");
        return derive(dirName, searchDefinitionName, builder);
    }

    protected DerivedConfiguration derive(String dirName, String searchDefinitionName, SearchBuilder builder) throws IOException {
        DerivedConfiguration config = new DerivedConfiguration(builder.getSearch(searchDefinitionName),
                                                               builder.getRankProfileRegistry(),
                                                               builder.getQueryProfileRegistry(),
                                                               new ImportedModels());
        return export(dirName, builder, config);
    }

    protected DerivedConfiguration derive(String dirName, SearchBuilder builder, Search search) throws IOException {
        DerivedConfiguration config = new DerivedConfiguration(search,
                                                               builder.getRankProfileRegistry(),
                                                               builder.getQueryProfileRegistry(),
                                                               new ImportedModels());
        return export(dirName, builder, config);
    }

    private DerivedConfiguration export(String name, SearchBuilder builder, DerivedConfiguration config) throws IOException {
        String path = exportConfig(name, config);
        DerivedConfiguration.exportDocuments(new DocumentManager().produce(builder.getModel(), new DocumentmanagerConfig.Builder()), path);
        DerivedConfiguration.exportDocuments(new DocumentTypes().produce(builder.getModel(), new DocumenttypesConfig.Builder()), path);
        return config;
    }

    private String exportConfig(String name, DerivedConfiguration config) throws IOException {
        String path = tempDir + name;
        config.export(path);
        return path;
    }

    /**
     * Derives a config from name/name.sd below the test dir and verifies that every .cfg file in name/ has a
     * corresponding file with the same content in temp/name. Versions can and should be omitted from the .cfg file
     * names. This will fail if the search definition dir has multiple search definitions.
     *
     * @param dirName the name of the directory containing the searchdef file to verify.
     * @throws ParseException if the .sd file could not be parsed.
     * @throws IOException    if file access failed.
     */
    protected DerivedConfiguration assertCorrectDeriving(String dirName) throws IOException, ParseException {
        return assertCorrectDeriving(dirName, null);
    }

    protected DerivedConfiguration assertCorrectDeriving(String dirName, String searchDefinitionName) throws IOException, ParseException {
        DerivedConfiguration derived = derive(dirName, searchDefinitionName);
        assertCorrectConfigFiles(dirName);
        return derived;
    }

    /**
     * Asserts config is correctly derived given a builder.
     * This will fail if the builder contains multiple search definitions.
     */
    protected DerivedConfiguration assertCorrectDeriving(SearchBuilder builder, String dirName) throws IOException, ParseException {
        builder.build();
        DerivedConfiguration derived = derive(dirName, null, builder);
        assertCorrectConfigFiles(dirName);
        return derived;
    }

    protected DerivedConfiguration assertCorrectDeriving(SearchBuilder builder, Search search, String name) throws IOException, ParseException {
        DerivedConfiguration derived = derive(name, builder, search);
        assertCorrectConfigFiles(name);
        return derived;
    }

    /**
     * Assert that search is derived into the files in the directory given by name.
     *
     * @param name the local name of the directory containing the files to check
     * @throws IOException If file access failed.
     */
    protected void assertCorrectConfigFiles(String name) throws IOException {
        File[] files = new File(searchDefRoot, name).listFiles();
        if (files == null) return;
        for (File file : files) {
            if ( ! file.getName().endsWith(".cfg")) continue;
            assertEqualFiles(file.getPath(), tempDir + name + "/" + file.getName());
        }
    }

    public static void assertEqualFiles(String correctFileName, String checkFileName) throws IOException {
        // Set updateOnAssert to true if you want update the files with correct answer.
        assertConfigFiles(correctFileName, checkFileName, false);
    }

    protected void deleteContent(File dir) {
        File[] files = dir.listFiles();
        if (files == null) return;

        for (File file : files) {
            file.delete();
        }
    }

}