aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/schema/derived/AbstractExportingTestCase.java
blob: 7d7ed2b23eb04377a6638c718484432de5f793fc (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.derived;

import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.config.model.application.provider.MockFileRegistry;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.deploy.TestProperties;
import com.yahoo.document.config.DocumenttypesConfig;
import com.yahoo.document.config.DocumentmanagerConfig;
import com.yahoo.schema.Schema;
import com.yahoo.schema.ApplicationBuilder;
import com.yahoo.schema.AbstractSchemaTestCase;
import com.yahoo.schema.parser.ParseException;
import com.yahoo.vespa.configmodel.producers.DocumentManager;
import com.yahoo.vespa.configmodel.producers.DocumentTypes;

import java.io.File;
import java.io.IOException;

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

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

    private DerivedConfiguration derive(String dirName,
                                        String schemaName,
                                        TestProperties properties,
                                        DeployLogger logger) throws IOException, ParseException {
        File toDir = new File(tempDir + dirName);
        toDir.mkdirs();
        deleteContent(toDir);

        ApplicationBuilder builder = ApplicationBuilder.createFromDirectory(schemaRoot + dirName + "/",
                                                                            new MockFileRegistry(),
                                                                            logger,
                                                                            properties);
        return derive(dirName, schemaName, properties, builder, logger);
    }

    private DerivedConfiguration derive(String dirName,
                                        String schemaName,
                                        TestProperties properties,
                                        ApplicationBuilder builder,
                                        DeployLogger logger) throws IOException {
        DerivedConfiguration config = new DerivedConfiguration(builder.getSchema(schemaName),
                                                               new DeployState.Builder().properties(properties)
                                                                                        .deployLogger(logger)
                                                                                        .rankProfileRegistry(builder.getRankProfileRegistry())
                                                                                        .queryProfiles(builder.getQueryProfileRegistry())
                                                                                        .build(), false);
        return export(dirName, builder, config);
    }

    DerivedConfiguration derive(String dirName, ApplicationBuilder builder, Schema schema) throws IOException {
        DerivedConfiguration config = new DerivedConfiguration(schema,
                                                               builder.getRankProfileRegistry(),
                                                               builder.getQueryProfileRegistry());
        return export(dirName, builder, config);
    }

    private DerivedConfiguration export(String name, ApplicationBuilder 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);
        DerivedConfiguration.exportQueryProfiles(builder.getQueryProfileRegistry(), path);
        config.exportConstants(path);
        config.exportOnnxModels(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, new TestableDeployLogger());
    }
    protected DerivedConfiguration assertCorrectDeriving(String dirName, DeployLogger logger) throws IOException, ParseException {
        return assertCorrectDeriving(dirName, null, logger);
    }

    protected DerivedConfiguration assertCorrectDeriving(String dirName,
                                                         String searchDefinitionName,
                                                         DeployLogger logger) throws IOException, ParseException {
        return assertCorrectDeriving(dirName, searchDefinitionName, new TestProperties(), logger);
    }

    protected DerivedConfiguration assertCorrectDeriving(String dirName,
                                                         TestProperties properties) throws IOException, ParseException {
        return assertCorrectDeriving(dirName, null, properties, new TestableDeployLogger());
    }

    protected DerivedConfiguration assertCorrectDeriving(String dirName,
                                                         String schemaName,
                                                         TestProperties properties,
                                                         DeployLogger logger) throws IOException, ParseException {
        DerivedConfiguration derived = derive(dirName, schemaName, properties, logger);
        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(ApplicationBuilder builder, String dirName, DeployLogger logger) throws IOException {
        builder.build(true);
        DerivedConfiguration derived = derive(dirName, null, new TestProperties(), builder, logger);
        assertCorrectConfigFiles(dirName);
        return derived;
    }

    protected DerivedConfiguration assertCorrectDeriving(ApplicationBuilder builder, Schema schema, String name) throws IOException {
        DerivedConfiguration derived = derive(name, builder, schema);
        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
     */
    void assertCorrectConfigFiles(String name) throws IOException {
        File[] files = new File(schemaRoot, name).listFiles();
        if (files == null) return;
        for (File file : files) {
            if ( ! file.getName().endsWith(".cfg")) continue;
            boolean orderMatters = file.getName().equals("ilscripts.cfg");
            assertEqualFiles(file.getPath(), tempDir + name + "/" + file.getName(), orderMatters);
        }
    }

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

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

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

}