aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/query/profile/types/test/NameTestCase.java
blob: d3dcb6ff38d2ae974b9bb27a22ed8cbe896cf60e (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
// Copyright Vespa.ai. 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.processing.request.CompoundName;
import com.yahoo.search.query.profile.QueryProfileRegistry;
import com.yahoo.yolean.Exceptions;
import com.yahoo.search.query.profile.QueryProfile;
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 org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * tests creating invalid names
 *
 * @author bratseth
 */
public class NameTestCase {

    @Test
    void testNames() {
        assertLegalName("aB");
        assertIllegalName("a.");
        assertLegalName("_a_b");
        assertLegalName("a_b");
        assertLegalName("a/b");
        assertLegalName("/a/b");
        assertLegalName("/a/b/");
        assertIllegalName("");
    }

    @Test
    void testFieldNames() {
        assertLegalFieldName("aB");
        try {
            QueryProfile profile = new QueryProfile("test");
            profile.set("a.", "anyValue", (QueryProfileRegistry) null);
            fail("Should have failed");
        } catch (IllegalArgumentException e) {
            assertEquals("'a.' is not a legal compound name. Names can not end with a dot.", e.getMessage());
        }
        assertLegalFieldName("_a_b");
        assertLegalFieldName("a_b");
        assertLegalFieldName("a/b");
        assertLegalFieldName("/a/b");
        assertLegalFieldName("/a/b/");
        assertIllegalFieldName("aBc.dooEee.ce_d.-some-other.moreHere",
                "Could not set 'aBc.dooEee.ce_d.-some-other.moreHere' to 'anyValue'",
                "Illegal name '-some-other'");
    }

    @Test
    void testComponentIdAsCompoundName() {
        String name = "a/b";
        assertEquals(CompoundName.from(name), new QueryProfileType(name).getComponentIdAsCompoundName());
    }

    private void assertLegalName(String name) {
        new QueryProfile(name);
        new QueryProfileType(name);
    }

    private void assertLegalFieldName(String name) {
        new QueryProfile(name).set(name, "value", (QueryProfileRegistry)null);
        new FieldDescription(name,FieldType.stringType);
    }

    /** Checks that this is illegal both for profiles and types */
    private void assertIllegalName(String name) {
        try {
            new QueryProfile(name);
            fail("Should have failed");
        }
        catch (IllegalArgumentException e) {
            if (!name.isEmpty())
                assertEquals("Illegal name '" + name + "'",e.getMessage());
        }

        try {
            new QueryProfileType(name);
            fail("Should have failed");
        }
        catch (IllegalArgumentException e) {
            if (!name.isEmpty())
                assertEquals("Illegal name '" + name + "'",e.getMessage());
        }
    }

    /** Checks that this is illegal both for profiles and types */
    private void assertIllegalFieldName(String name, String expectedHighError, String expectedLowError) {
        try {
            QueryProfile profile=new QueryProfile("test");
            profile.set(name, "anyValue", (QueryProfileRegistry)null);
            fail("Should have failed");
        }
        catch (IllegalArgumentException e) {
            assertEquals(expectedHighError + ": " + expectedLowError, Exceptions.toMessageString(e));
        }

        try {
            new FieldDescription(name, FieldType.stringType);
            fail("Should have failed");
        }
        catch (IllegalArgumentException e) {
            assertEquals(expectedLowError, e.getMessage());
        }
    }

}