aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/schema/RankingConstantTest.java
blob: c963f086ac416d2b99e2955e207a97092a4fd525 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema;

import com.yahoo.schema.parser.ParseException;
import org.junit.jupiter.api.Test;

import java.util.Iterator;

import static com.yahoo.config.model.test.TestUtil.joinLines;
import static org.junit.jupiter.api.Assertions.*;

/**
 * @author gjoranv
 */
public class RankingConstantTest {

    @Test
    void tensor_constant_properties_are_set() throws Exception {
        final String TENSOR_NAME = "my_global_tensor";
        final String TENSOR_FILE = "path/my-tensor-file.json";
        final String TENSOR_TYPE = "tensor(x{})";
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        ApplicationBuilder schemaBuilder = new ApplicationBuilder(rankProfileRegistry);
        schemaBuilder.addSchema(joinLines(
                "schema test {",
                "  document test { }",
                "  rank-profile my_rank_profile {",
                "    first-phase {",
                "      expression: sum(constant(my_global_tensor))",
                "    }",
                "  }",
                "  constant " + TENSOR_NAME + " {",
                "    file: " + TENSOR_FILE,
                "    type: " + TENSOR_TYPE,
                "  }",
                "}"
        ));
        schemaBuilder.build(true);
        Schema schema = schemaBuilder.getSchema();

        Iterator<RankProfile.Constant> constantIterator = schema.constants().values().iterator();
        RankProfile.Constant constant = constantIterator.next();
        assertEquals(TENSOR_NAME, constant.name().simpleArgument().get());
        assertEquals(TENSOR_FILE, constant.valuePath().get());
        assertEquals(TENSOR_TYPE, constant.type().toString());
        assertEquals(DistributableResource.PathType.FILE, constant.pathType().get());

        assertFalse(constantIterator.hasNext());
    }

    @Test
    void tensor_constant_must_have_a_type() throws Exception {
        Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
            RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
            ApplicationBuilder schemaBuilder = new ApplicationBuilder(rankProfileRegistry);
            schemaBuilder.addSchema(joinLines(
                    "schema test {",
                    "  document test { }",
                    "  constant foo {",
                    "    file: bar.baz",
                    "  }",
                    "}"
            ));
        });
        assertTrue(exception.getMessage().contains("must have a type"));
    }

    @Test
    void tensor_constant_must_have_a_file() throws Exception {
        Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
            RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
            ApplicationBuilder schemaBuilder = new ApplicationBuilder(rankProfileRegistry);
            schemaBuilder.addSchema(joinLines(
                    "schema test {",
                    "  document test { }",
                    "  constant foo {",
                    "    type: tensor(x[])",
                    "  }",
                    "}"
            ));
        });
        assertTrue(exception.getMessage().contains("must have a file"));
    }

    @Test
    void constant_file_does_not_need_path_or_ending() throws Exception {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        ApplicationBuilder schemaBuilder = new ApplicationBuilder(rankProfileRegistry);
        schemaBuilder.addSchema(joinLines(
                "schema test {",
                "  document test { }",
                "  constant foo {",
                "    type: tensor(x{})",
                "    file: simplename",
                "  }",
                "}"
        ));
        schemaBuilder.build(true);
        Schema schema = schemaBuilder.getSchema();
        RankProfile.Constant constant = schema.constants().values().iterator().next();
        assertEquals("simplename", constant.valuePath().get());
    }

    @Test
    void constant_uri_is_allowed() throws Exception {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        ApplicationBuilder schemaBuilder = new ApplicationBuilder(rankProfileRegistry);
        schemaBuilder.addSchema(joinLines(
                "schema test {",
                "  document test { }",
                "  constant foo {",
                "    type: tensor(x{})",
                "    uri: http://somewhere.far.away/in/another-galaxy",
                "  }",
                "}"
        ));
        schemaBuilder.build(true);
        Schema schema = schemaBuilder.getSchema();
        RankProfile.Constant constant = schema.constants().values().iterator().next();
        assertEquals(DistributableResource.PathType.URI, constant.pathType().get());
        assertEquals("http://somewhere.far.away/in/another-galaxy", constant.valuePath().get());
    }

    @Test
    void constant_https_uri_is_allowed() throws Exception {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        ApplicationBuilder schemaBuilder = new ApplicationBuilder(rankProfileRegistry);
        schemaBuilder.addSchema(joinLines(
                "schema test {",
                "  document test { }",
                "  constant foo {",
                "    type: tensor(x{})",
                "    uri: https://somewhere.far.away:4443/in/another-galaxy",
                "  }",
                "}"
        ));
        schemaBuilder.build(true);
        Schema schema = schemaBuilder.getSchema();
        RankProfile.Constant constant = schema.constants().values().iterator().next();
        assertEquals(DistributableResource.PathType.URI, constant.pathType().get());
        assertEquals("https://somewhere.far.away:4443/in/another-galaxy", constant.valuePath().get());
    }

    @Test
    void constant_uri_with_port_is_allowed() throws Exception {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        ApplicationBuilder schemaBuilder = new ApplicationBuilder(rankProfileRegistry);
        schemaBuilder.addSchema(joinLines(
                "schema test {",
                "  document test { }",
                "  constant foo {",
                "    type: tensor(x{})",
                "    uri: http://somewhere.far.away:4080/in/another-galaxy",
                "  }",
                "}"
        ));
        schemaBuilder.build(true);
        Schema schema = schemaBuilder.getSchema();
        RankProfile.Constant constant = schema.constants().values().iterator().next();
        assertEquals(DistributableResource.PathType.URI, constant.pathType().get());
        assertEquals("http://somewhere.far.away:4080/in/another-galaxy", constant.valuePath().get());
    }

    @Test
    void constant_uri_no_dual_slashes_is_allowed() throws Exception {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        ApplicationBuilder schemaBuilder = new ApplicationBuilder(rankProfileRegistry);
        schemaBuilder.addSchema(joinLines(
                "schema test {",
                "  document test { }",
                "  constant foo {",
                "    type: tensor(x{})",
                "    uri: http:somewhere.far.away/in/another-galaxy",
                "  }",
                "}"
        ));
        schemaBuilder.build(true);
        Schema schema = schemaBuilder.getSchema();
        RankProfile.Constant constant = schema.constants().values().iterator().next();
        assertEquals(DistributableResource.PathType.URI, constant.pathType().get());
        assertEquals("http:somewhere.far.away/in/another-galaxy", constant.valuePath().get());
    }

    @Test
    void constant_uri_only_supports_http_and_https() {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        ApplicationBuilder schemaBuilder = new ApplicationBuilder(rankProfileRegistry);
        String expectedMessage = "Encountered \" <IDENTIFIER> \"ftp\"\" at line 5, column 10.\n\n" +
                "Was expecting:\n\n" +
                "<URI_PATH> ...";
        try {
            schemaBuilder.addSchema(joinLines(
                    "schema test {",
                    "  document test { }",
                    "  constant foo {",
                    "    type: tensor(x{})",
                    "    uri: ftp:somewhere.far.away/in/another-galaxy",
                    "  }",
                    "}"
            ));
        } catch (ParseException e) {
            if (!e.getMessage().startsWith(expectedMessage))
                fail("Expected exception with message starting with:\n'" + expectedMessage + "\nBut got:\n'" + e.getMessage());
        }
    }

}