aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/builder/xml/dom/chains/search/DomSchemaChainsBuilderTest.java
blob: d470f1843400d822aff9ed0cfce9385847fab953 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.builder.xml.dom.chains.search;

import com.yahoo.component.ComponentId;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.config.model.builder.xml.test.DomBuilderTest;
import com.yahoo.config.model.test.MockRoot;
import com.yahoo.container.core.ChainsConfig;
import com.yahoo.search.searchchain.model.federation.FederationOptions;
import com.yahoo.vespa.model.container.component.chain.ChainedComponent;
import com.yahoo.vespa.model.container.search.searchchain.GenericTarget;
import com.yahoo.vespa.model.container.search.searchchain.Provider;
import com.yahoo.vespa.model.container.search.searchchain.SearchChain;
import com.yahoo.vespa.model.container.search.searchchain.SearchChains;
import com.yahoo.vespa.model.container.search.searchchain.Source;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Element;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static com.yahoo.container.core.ChainsConfig.Chains;
import static com.yahoo.container.core.ChainsConfig.Components;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;

/**
 * Test of Search chains builder.
 *
 * @author Tony Vaagenes
 */
public class DomSchemaChainsBuilderTest extends DomBuilderTest {

    private SearchChains searchChains;

    private static final Element element = parse(
            "<search>",
            "  <searcher id='searcher:1'/>",

            "  <provider id='provider:1' inherits='parentChain1 parentChain2' excludes='ExcludedSearcher1 ExcludedSearcher2'",
            "             cacheweight='2.3'>",
            "    <federationoptions optional='true' timeout='2.3 s' />",
            "    <nodes>",
            "      <node host='sourcehost' port='12'/>",
            "    </nodes>",

            "    <source id='source:1' inherits='parentChain3 parentChain4' excludes='ExcludedSearcher3 ExcludedSearcher4'>",
            "      <federationoptions timeout='12 ms' />",
            "    </source>",

            "  </provider>",

            "  <chain id='default'>",
            "    <federation id='federationSearcher'>",
            "      <source id='mysource'>",
            "        <federationoptions optional='false' />",
            "      </source>",
            "    </federation>",
            "  </chain>",

            "</search>");


    @BeforeEach
    public void createSearchChains() {
        searchChains = new DomSearchChainsBuilder().build(root.getDeployState(), root, element);
    }

    @Test
    void referToFederationAsSearcher() {
        final Element element = parse(
                "<search>",
                "  <federation id='federationSearcher'>",
                "    <source id='mysource'>",
                "      <federationoptions optional='false' />",
                "    </source>",
                "  </federation>",

                "  <chain id='default'>",
                "    <searcher id='federationSearcher'/>",
                "  </chain>",
                "</search>");

        try {
            MockRoot root = new MockRoot();
            new DomSearchChainsBuilder().build(root.getDeployState(), root, element);
            fail("Expected exception when referring to an outer 'federation' as a 'searcher'.");
        } catch (RuntimeException e) {
            assertTrue(e.getMessage().contains("Two different types declared for the component with name 'federationSearcher'"));
        }
    }

    @Test
    void ensureSearchChainsExists() {
        for (String id : Arrays.asList("provider:1", "source:1@provider:1", "default")) {
            assertNotNull(getSearchChain(id), "Missing search chain " + id);
        }
    }

    @Test
    void ensureSearcherExists() {
        assertThat(searchChains.allComponents(), hasItem(searcherWithId("searcher:1")));
    }

    private Matcher<ChainedComponent<?>> searcherWithId(final String componentId) {
        return new BaseMatcher<>() {
            @Override
            public boolean matches(Object o) {
                return o instanceof ChainedComponent &&
                        ((ChainedComponent) o).getComponentId().equals(new ComponentId(componentId));
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("a searcher with id ").appendValue(componentId);
            }
        };
    }

    @Test
    void checkProviderFederationOptions() {
        FederationOptions options = getProvider().federationOptions();

        assertTrue(options.getOptional());
        assertEquals(2300, options.getTimeoutInMilliseconds());
    }

    @Test
    void checkSourceFederationOptions() {
        FederationOptions options = getSource().federationOptions();

        assertTrue(options.getOptional()); //inherited
        assertEquals(12, options.getTimeoutInMilliseconds());
    }

    @Test
    void checkDefaultTargets() {
        Collection<? extends GenericTarget> defaultTargets =
                getProvider().defaultFederationTargets();

        assertEquals(1, defaultTargets.size());
        assertEquals(getSearchChain("source:1@provider:1"), first(defaultTargets));
    }

    @Test
    void checkInnerSearcherIdIsNestedInSearchChainId() {
        ChainsConfig.Builder builder = new ChainsConfig.Builder();
        searchChains.getConfig(builder);
        ChainsConfig config = new ChainsConfig(builder);

        checkInnerSearcherIdIsNestedInSearchChainId(config, "federationSearcher", "default");
    }

    private void checkInnerSearcherIdIsNestedInSearchChainId(ChainsConfig config,
                                                             String partOfSearcherName,
                                                             String searchChainName) {
        Components searcher = getSearcherConfig(config.components(), partOfSearcherName);
        ComponentId searcherId = ComponentId.fromString(searcher.id());

        assertEquals(getSearchChain(searchChainName).getComponentId(), searcherId.getNamespace());

        Chains searchChain = getSearchChainConfig(config.chains(), searchChainName);
        assertEquals(searcherId, ComponentId.fromString(searchChain.components(0)));
    }

    private Chains getSearchChainConfig(List<Chains> searchChains,
                                                                String searchChainName) {
        for (Chains searchChain : searchChains) {
            if (ComponentId.fromString(searchChain.id()).getName().equals(searchChainName))
                return searchChain;
        }
        fail("No search chain matching " + searchChainName);
        return null;
    }

    private Components getSearcherConfig(List<Components> searchers, String partOfId) {
        for (Components searcher : searchers) {
            if (searcher.id().contains(partOfId))
                return searcher;
        }
        fail("No searcher matching " + partOfId);
        return null;
    }

    private static <T> T first(Iterable<T> coll) {
        return coll.iterator().next();
    }

    private Provider getProvider() {
        return (Provider)getSearchChain("provider:1");
    }

    private Source getSource() {
        return first(getProvider().getSources());
    }

    private SearchChain getSearchChain(String componentSpecification) {
        return searchChains.allChains().getComponent(new ComponentSpecification(componentSpecification));
    }

}