aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/searchers/test/QueryValidatorTestCase.java
blob: 64fb43540038dba499a9c6fcccaa8f0e808e5057 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.searchers.test;

import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.IndexModel;
import com.yahoo.prelude.SearchDefinition;
import com.yahoo.search.Query;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.searchers.QueryValidator;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

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

    @Test
    void testValidation() {
        SearchDefinition sd = new SearchDefinition("test");
        sd.addCommand("mytensor1", "type tensor(x[100]");
        sd.addCommand("mytensor2", "type tensor<float>(x[100]");
        sd.addCommand("mystring", "type string");
        IndexModel model = new IndexModel(sd);

        IndexFacts indexFacts = new IndexFacts(model);
        Execution execution = new Execution(Execution.Context.createContextStub(indexFacts));
        new QueryValidator().search(new Query("?query=mystring:foo"), execution);

        try {
            new QueryValidator().search(new Query("?query=mytensor1:foo"), execution);
            fail("Expected validation error");
        }
        catch (IllegalArgumentException e) {
            // success
            assertEquals("Cannot search 'mytensor1': It is a tensor field", e.getMessage());
        }

        try {
            new QueryValidator().search(new Query("?query=mytensor2:foo"), execution);
            fail("Expected validation error");
        }
        catch (IllegalArgumentException e) {
            // success
            assertEquals("Cannot search 'mytensor2': It is a tensor field", e.getMessage());
        }
    }

}