summaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/container/search/SemanticRulesTest.java
blob: e20e14f9465935143d0aac87d721ddb1ced0f38d (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.search;

import com.yahoo.config.model.application.provider.FilesApplicationPackage;
import com.yahoo.prelude.semantics.RuleBase;
import com.yahoo.prelude.semantics.SemanticRulesConfig;
import com.yahoo.prelude.semantics.parser.ParseException;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * @author bratseth
 */
public class SemanticRulesTest {

    private static final String basePath = "src/test/java/com/yahoo/vespa/model/container/search/";
    private static final String root = basePath + "semanticrules";
    private static final String rootWithErrors = basePath + "semanticrules_with_errors";
    private static final String rootWithDuplicateDefault = basePath + "semanticrules_with_duplicate_default_rule";

    @Test
    void semanticRulesTest() throws ParseException, IOException {
        SemanticRuleBuilder ruleBuilder = new SemanticRuleBuilder();
        SemanticRules rules = ruleBuilder.build(FilesApplicationPackage.fromFile(new File(root)));
        SemanticRulesConfig.Builder configBuilder = new SemanticRulesConfig.Builder();
        rules.getConfig(configBuilder);
        SemanticRulesConfig config = new SemanticRulesConfig(configBuilder);
        Map<String, RuleBase> ruleBases = SemanticRuleBuilder.toMap(config);
        assertEquals(2, ruleBases.size());
        assertTrue(ruleBases.containsKey("common"));
        assertTrue(ruleBases.containsKey("other"));
        assertFalse(ruleBases.get("common").isDefault());
        assertTrue(ruleBases.get("other").isDefault());
        assertTrue(ruleBases.get("other").includes("common"));
        assertNotNull(ruleBases.get("other").getCondition("stopword"));
    }

    @Test
    void rulesWithErrors() {
        try {
            new SemanticRuleBuilder().build(FilesApplicationPackage.fromFile(new File(rootWithErrors)));
            fail("should fail with exception");
        } catch (Exception e) {
            assertEquals("com.yahoo.prelude.semantics.parser.ParseException: Could not parse 'semantic-rules.cfg'", e.getMessage());
        }
    }

    @Test
    void rulesWithDuplicateDefault() {
        try {
            new SemanticRuleBuilder().build(FilesApplicationPackage.fromFile(new File(rootWithDuplicateDefault)));
            fail("should fail with exception");
        } catch (Exception e) {
            assertEquals("Rules [one, other] are both marked as the default rule, there can only be one", e.getMessage());
        }
    }

}