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

import com.yahoo.component.ComponentId;
import com.yahoo.search.query.profile.QueryProfile;
import com.yahoo.search.query.profile.QueryProfileRegistry;
import com.yahoo.search.query.profile.compiled.CompiledQueryProfile;
import com.yahoo.search.query.profile.types.FieldDescription;
import com.yahoo.search.query.profile.types.FieldType;
import com.yahoo.search.query.profile.types.QueryProfileType;
import com.yahoo.search.query.profile.types.QueryProfileTypeRegistry;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

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

    private QueryProfileTypeRegistry registry;

    private QueryProfileType type, typeStrict, user, userStrict;

    @BeforeEach
    public void setUp() {
        type=new QueryProfileType(new ComponentId("testtype"));
        typeStrict=new QueryProfileType(new ComponentId("testtypeStrict"));
        typeStrict.setStrict(true);
        user=new QueryProfileType(new ComponentId("user"));
        userStrict=new QueryProfileType(new ComponentId("userStrict"));
        userStrict.setStrict(true);
        registry=new QueryProfileTypeRegistry();
        registry.register(type);
        registry.register(typeStrict);
        registry.register(user);
        registry.register(userStrict);

        addTypeFields(type);
        type.addField(new FieldDescription("myUserQueryProfile", FieldType.fromString("query-profile:user",registry)));
        addTypeFields(typeStrict);
        typeStrict.addField(new FieldDescription("myUserQueryProfile",FieldType.fromString("query-profile:userStrict",registry)));
        addUserFields(user);
        addUserFields(userStrict);
    }

    private void addTypeFields(QueryProfileType type) {
        type.addField(new FieldDescription("myString", FieldType.fromString("string",registry)));
        type.addField(new FieldDescription("myInteger",FieldType.fromString("integer",registry)));
        type.addField(new FieldDescription("myLong",FieldType.fromString("long",registry)));
        type.addField(new FieldDescription("myFloat",FieldType.fromString("float",registry)));
        type.addField(new FieldDescription("myDouble",FieldType.fromString("double",registry)));
        type.addField(new FieldDescription("myQueryProfile",FieldType.fromString("query-profile",registry)));
    }

    private void addUserFields(QueryProfileType user) {
        user.addField(new FieldDescription("myUserString",FieldType.fromString("string",registry),true,false));
        user.addField(new FieldDescription("myUserInteger",FieldType.fromString("integer",registry)));
    }

    @Test
    void testInheritance() {
        type.inherited().add(user);
        type.freeze();
        user.freeze();

        assertFalse(type.isOverridable("myUserString"));
        assertEquals("myUserInteger", type.getField("myUserInteger").getName());

        QueryProfile test = new QueryProfile("test");
        test.setType(type);

        test.set("myUserInteger", "37", (QueryProfileRegistry) null);
        test.set("myUnknownInteger", "38", (QueryProfileRegistry) null);
        CompiledQueryProfile ctest = test.compile(null);

        assertEquals(37, ctest.get("myUserInteger"));
        assertEquals("38", ctest.get("myUnknownInteger"));
    }

    @Test
    void testInheritanceStrict() {
        typeStrict.inherited().add(userStrict);
        typeStrict.freeze();
        userStrict.freeze();

        QueryProfile test = new QueryProfile("test");
        test.setType(typeStrict);

        test.set("myUserInteger", "37", (QueryProfileRegistry) null);
        try {
            test.set("myUnknownInteger", "38", (QueryProfileRegistry) null);
            fail("Should have failed");
        }
        catch (IllegalArgumentException e) {
            assertEquals("'myUnknownInteger' is not declared in query profile type 'testtypeStrict', and the type is strict",
                    e.getCause().getMessage());
        }

        assertEquals(37, test.get("myUserInteger"));
        assertNull(test.get("myUnknownInteger"));
    }

    @Test
    void testStrictIsInherited() {
        type.inherited().add(userStrict);
        type.freeze();
        userStrict.freeze();

        QueryProfile test = new QueryProfile("test");
        test.setType(type);

        test.set("myUserInteger", "37", (QueryProfileRegistry) null);
        try {
            test.set("myUnknownInteger", "38", (QueryProfileRegistry) null);
            fail("Should have failed");
        }
        catch (IllegalArgumentException e) {
            assertEquals("'myUnknownInteger' is not declared in query profile type 'testtype', and the type is strict",
                    e.getCause().getMessage());
        }

        CompiledQueryProfile ctest = test.compile(null);
        assertEquals(37, ctest.get("myUserInteger"));
        assertNull(ctest.get("myUnknownInteger"));
    }

}