summaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/searchdefinition/processing/ImportedFieldsTestCase.java
blob: 434cfd72136a16e9b32b6a47ff72b48f4d3db3e1 (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
// Copyright 2017 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.processing;

import com.yahoo.searchdefinition.Search;
import com.yahoo.searchdefinition.SearchBuilder;
import com.yahoo.searchdefinition.document.TemporaryImportedField;
import com.yahoo.searchdefinition.parser.ParseException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;
import static com.yahoo.searchdefinition.TestUtils.joinLines;

/**
 * @author geirst
 */
public class ImportedFieldsTestCase {

    @Test
    public void require_that_imported_fields_can_be_parsed_from_sd_file() throws ParseException {
        Search search = build(joinLines(
                "search ad {",
                "  document ad {}",
                "  import field campaign.budget as budget {}",
                "  import field person.name as sales_person {}",
                "}"));
        assertEquals(2, search.importedFields().fields().size());
        assertSearchContainsTemporaryImportedField("budget", "campaign", "budget", search);
        assertSearchContainsTemporaryImportedField("sales_person", "person", "name", search);
    }

    @Rule
    public ExpectedException exception = ExpectedException.none();

    @Test
    public void require_that_field_reference_spec_must_include_dot() throws ParseException {
        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("Illegal field reference spec 'campaignbudget': Does not include a single '.'");
        build(joinLines(
                "search ad {",
                "  document ad {}",
                "  import field campaignbudget as budget {}",
                "}"));
    }

    private static Search build(String sdContent) throws ParseException {
        SearchBuilder builder = new SearchBuilder();
        builder.importString(sdContent);
        builder.build();
        return builder.getSearch();
    }

    private static void assertSearchContainsTemporaryImportedField(String fieldName, String refFieldName, String fieldNameInRefType, Search search) {
        TemporaryImportedField importedField = search.importedFields().fields().get(fieldName);
        assertEquals(fieldName, importedField.fieldName());
        assertEquals(refFieldName, importedField.reference().refFieldName());
        assertEquals(fieldNameInRefType, importedField.reference().fieldNameInRefType());
    }
}