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

import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.document.DataType;
import com.yahoo.document.Field;
import com.yahoo.searchdefinition.DocumentReference;
import com.yahoo.searchdefinition.Search;
import com.yahoo.searchdefinition.document.ImportedField;
import com.yahoo.searchdefinition.document.ImportedFields;
import com.yahoo.searchdefinition.document.SDDocumentType;
import com.yahoo.searchdefinition.document.SDField;
import com.yahoo.vespa.documentmodel.DocumentSummary;
import com.yahoo.vespa.documentmodel.SummaryField;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.Collections;

/**
 * @author bjorncs
 */
public class ValidateFieldTypesTest {

    private static final String IMPORTED_FIELD_NAME = "imported_myfield";
    private static final String DOCUMENT_NAME = "my_doc";

    @Rule
    public final ExpectedException exceptionRule = ExpectedException.none();

    @Test
    public void throws_exception_if_type_of_document_field_does_not_match_summary_field() {
        Search search = createSearchWithDocument(DOCUMENT_NAME);
        search.setImportedFields(createSingleImportedField(IMPORTED_FIELD_NAME, DataType.INT));
        search.addSummary(createDocumentSummary(IMPORTED_FIELD_NAME, DataType.STRING));

        ValidateFieldTypes validator = new ValidateFieldTypes(search, null, null, null);
        exceptionRule.expect(IllegalArgumentException.class);
        exceptionRule.expectMessage(
                "For search '" + DOCUMENT_NAME + "', field '" + IMPORTED_FIELD_NAME + "': Incompatible types. " +
                "Expected int for summary field '" + IMPORTED_FIELD_NAME + "', got string.");
        validator.process(true);
    }

    private static Search createSearchWithDocument(String documentName) {
        Search search = new Search(documentName, MockApplicationPackage.createEmpty());
        SDDocumentType document = new SDDocumentType(documentName, search);
        search.addDocument(document);
        return search;
    }

    private static ImportedFields createSingleImportedField(String fieldName, DataType dataType) {
        Search targetSearch = new Search("target_doc", MockApplicationPackage.createEmpty());
        SDField targetField = new SDField("target_field", dataType);
        DocumentReference documentReference = new DocumentReference(new Field("reference_field"), targetSearch);
        ImportedField importedField = new ImportedField(fieldName, documentReference, targetField);
        return new ImportedFields(Collections.singletonMap(fieldName, importedField));
    }

    private static DocumentSummary createDocumentSummary(String fieldName, DataType dataType) {
        DocumentSummary summary = new DocumentSummary("mysummary");
        summary.add(new SummaryField(fieldName, dataType));
        return summary;
    }

}