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

import com.yahoo.documentmodel.NewDocumentType;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ReservedDocumentTypeNameValidatorTest {

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

    private static Map<String, NewDocumentType> asDocTypeMapping(List<String> typeNames) {
        return typeNames.stream().collect(Collectors.toMap(Function.identity(), n -> new NewDocumentType(new NewDocumentType.Name(n))));
    }

    @Test
    public void exception_thrown_on_reserved_names() {
        expectedException.expect(IllegalArgumentException.class);
        expectedException.expectMessage("The following document types conflict with reserved keyword names: " +
                "'and', 'false', 'id', 'not', 'null', 'or', 'true'. " +
                "Reserved keywords are 'and', 'false', 'id', 'not', 'null', 'or', 'true'");

        // Ensure ordering is consistent for testing
        Map<String, NewDocumentType> orderedDocTypes = new TreeMap<>(asDocTypeMapping(ReservedDocumentTypeNameValidator.ORDERED_RESERVED_NAMES));

        ReservedDocumentTypeNameValidator validator = new ReservedDocumentTypeNameValidator();
        validator.validate(orderedDocTypes);
    }

    @Test
    public void exception_is_not_thrown_on_unreserved_name() {
        ReservedDocumentTypeNameValidator validator = new ReservedDocumentTypeNameValidator();
        validator.validate(asDocTypeMapping(Collections.singletonList("foo")));
    }

    @Test
    public void validation_is_case_insensitive() {
        expectedException.expect(IllegalArgumentException.class);
        expectedException.expectMessage("The following document types conflict with reserved keyword names: " +
                                        "'NULL', 'True', 'anD'.");

        ReservedDocumentTypeNameValidator validator = new ReservedDocumentTypeNameValidator();
        Map<String, NewDocumentType> orderedDocTypes = new TreeMap<>(asDocTypeMapping(Arrays.asList("NULL", "True", "anD")));
        validator.validate(orderedDocTypes);
    }

}