aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/query/profile/config/test/MultiProfileTestCase.java
blob: ebe9c9d3c0482bc283088ef89d7dcd564d37014a (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 Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile.config.test;

import java.util.HashMap;
import java.util.Map;

import com.yahoo.search.query.profile.QueryProfile;
import com.yahoo.search.query.profile.QueryProfileRegistry;
import com.yahoo.search.query.profile.config.QueryProfileXMLReader;
import org.junit.jupiter.api.Test;

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

/**
 * @author bratseth
 */
public class MultiProfileTestCase {

    @Test
    void testValid() {
        QueryProfileRegistry registry =
                new QueryProfileXMLReader().read("src/test/java/com/yahoo/search/query/profile/config/test/multiprofile");

        QueryProfile multiprofile1 = registry.getComponent("multiprofile1");
        assertNotNull(multiprofile1);
        assertGet("general-a", "a", new String[]{null, null, null}, multiprofile1);
        assertGet("us-nokia-test1-a", "a", new String[]{"us", "nok ia", "test1"}, multiprofile1);
        assertGet("us-nokia-b", "b", new String[]{"us", "nok ia", "test1"}, multiprofile1);
        assertGet("us-a", "a", new String[]{"us", null, null}, multiprofile1);
        assertGet("us-b", "b", new String[]{"us", null, null}, multiprofile1);
        assertGet("us-nokia-a", "a", new String[]{"us", "nok ia", null}, multiprofile1);
        assertGet("us-test1-a", "a", new String[]{"us", null, "test1"}, multiprofile1);
        assertGet("us-test1-b", "b", new String[]{"us", null, "test1"}, multiprofile1);

        assertGet("us-a", "a", new String[]{"us", "unspecified", "unspecified"}, multiprofile1);
        assertGet("us-nokia-a", "a", new String[]{"us", "nok ia", "unspecified"}, multiprofile1);
        assertGet("us-test1-a", "a", new String[]{"us", "unspecified", "test1"}, multiprofile1);
        assertGet("us-nokia-b", "b", new String[]{"us", "nok ia", "test1"}, multiprofile1);

        // ...inherited
        assertGet("parent1-value", "parent1", new String[]{"us", "nok ia", "-"}, multiprofile1);
        assertGet("parent2-value", "parent2", new String[]{"us", "nok ia", "-"}, multiprofile1);
        assertGet(null, "parent1", new String[]{"us", "-", "-"}, multiprofile1);
        assertGet(null, "parent2", new String[]{"us", "-", "-"}, multiprofile1);
    }

    private void assertGet(String expectedValue,String parameter,String[] dimensionValues,QueryProfile profile) {
        Map<String,String> context=new HashMap<>();
        context.put("region",dimensionValues[0]);
        context.put("model",dimensionValues[1]);
        context.put("bucket",dimensionValues[2]);
        assertEquals(expectedValue,profile.get(parameter,context,null),"Looking up '" + parameter + "' for '" + toString(dimensionValues) + "'");
    }

    private String toString(String[] array) {
        StringBuilder b=new StringBuilder("[");
        for (String value : array) {
            b.append(value);
            b.append(",");
        }
        b.deleteCharAt(b.length()-1); // Remove last comma  :-)
        b.append("]");
        return b.toString();
    }

}