summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/federation/sourceref
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /container-search/src/test/java/com/yahoo/search/federation/sourceref
Publish
Diffstat (limited to 'container-search/src/test/java/com/yahoo/search/federation/sourceref')
-rw-r--r--container-search/src/test/java/com/yahoo/search/federation/sourceref/test/SearchChainResolverTestCase.java152
-rw-r--r--container-search/src/test/java/com/yahoo/search/federation/sourceref/test/SourceRefResolverTestCase.java114
2 files changed, 266 insertions, 0 deletions
diff --git a/container-search/src/test/java/com/yahoo/search/federation/sourceref/test/SearchChainResolverTestCase.java b/container-search/src/test/java/com/yahoo/search/federation/sourceref/test/SearchChainResolverTestCase.java
new file mode 100644
index 00000000000..e874c89b918
--- /dev/null
+++ b/container-search/src/test/java/com/yahoo/search/federation/sourceref/test/SearchChainResolverTestCase.java
@@ -0,0 +1,152 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.search.federation.sourceref.test;
+
+import com.yahoo.component.ComponentId;
+import com.yahoo.component.ComponentSpecification;
+import com.yahoo.processing.request.properties.PropertyMap;
+import com.yahoo.processing.request.Properties;
+import com.yahoo.search.federation.sourceref.SearchChainInvocationSpec;
+import com.yahoo.search.federation.sourceref.SearchChainResolver;
+import com.yahoo.search.federation.sourceref.Target;
+import com.yahoo.search.federation.sourceref.UnresolvedSearchChainException;
+import com.yahoo.search.searchchain.model.federation.FederationOptions;
+import org.junit.Test;
+
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.SortedSet;
+
+import static junit.framework.Assert.assertNull;
+import static junit.framework.Assert.fail;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * @author tonytv
+ */
+public class SearchChainResolverTestCase {
+
+ private static final FederationOptions federationOptions =
+ new FederationOptions().setTimeoutInMilliseconds(3000).setOptional(true);
+
+ private static final ComponentId searchChainId = ComponentId.fromString("search-chain");
+ private static final ComponentId providerId = ComponentId.fromString("provider");
+ private static final ComponentId provider2Id = ComponentId.fromString("provider2");
+
+ private static final ComponentId sourceId = ComponentId.fromString("source");
+ private static final ComponentId sourceChainInProviderId =
+ ComponentId.fromString("source-chain").nestInNamespace(providerId);
+ private static final ComponentId sourceChainInProvider2Id =
+ ComponentId.fromString("source-chain").nestInNamespace(provider2Id);
+
+ private static final SearchChainResolver searchChainResolver;
+
+ static {
+ SearchChainResolver.Builder builder = new SearchChainResolver.Builder();
+ builder.addSearchChain(searchChainId, federationOptions.setUseByDefault(true), Collections.<String>emptyList());
+ builder.addSearchChain(providerId, federationOptions.setUseByDefault(false), Collections.<String>emptyList());
+ builder.addSourceForProvider(sourceId, providerId, sourceChainInProviderId, true,
+ federationOptions.setUseByDefault(true), Collections.<String>emptyList());
+ builder.addSourceForProvider(sourceId, provider2Id, sourceChainInProvider2Id, false,
+ federationOptions.setUseByDefault(false), Collections.<String>emptyList());
+
+ searchChainResolver = builder.build();
+ }
+
+ @Test
+ public void check_default_search_chains() {
+ assertThat(searchChainResolver.defaultTargets().size(), is(2));
+
+ Iterator<Target> iterator = searchChainResolver.defaultTargets().iterator();
+ assertThat(iterator.next().searchRefDescription(), is(searchChainId.toString()));
+ assertThat(iterator.next().searchRefDescription(), is(sourceChainInProviderId.toString()));
+ }
+
+ @Test
+ public void require_error_message_for_invalid_source() {
+ try {
+ resolve("no-such-source");
+ fail("Expected exception.");
+ } catch (UnresolvedSearchChainException e) {
+ assertThat(e.getMessage(), is("Could not resolve source ref 'no-such-source'."));
+ }
+ }
+
+ @Test
+ public void lookup_search_chain() throws Exception {
+ SearchChainInvocationSpec res = resolve(searchChainId.getName());
+ assertThat(res.searchChainId, is(searchChainId));
+ }
+
+ //TODO: TVT: @Test()
+ public void lookup_provider() throws Exception {
+ SearchChainInvocationSpec res = resolve(providerId.getName());
+ assertThat(res.provider, is(providerId));
+ assertNull(res.source);
+ assertThat(res.searchChainId, is(providerId));
+ }
+
+ @Test
+ public void lookup_source() throws Exception {
+ SearchChainInvocationSpec res = resolve(sourceId.getName());
+ assertIsSourceInProvider(res);
+ }
+
+ @Test
+ public void lookup_source_search_chain_directly() throws Exception {
+ SearchChainInvocationSpec res = resolve(sourceChainInProviderId.stringValue());
+ assertIsSourceInProvider(res);
+ }
+
+ private void assertIsSourceInProvider(SearchChainInvocationSpec res) {
+ assertThat(res.provider, is(providerId));
+ assertThat(res.source, is(sourceId));
+ assertThat(res.searchChainId, is(sourceChainInProviderId));
+ }
+
+ @Test
+ public void lookup_source_for_provider2() throws Exception {
+ SearchChainInvocationSpec res = resolve(sourceId.getName(), provider2Id.getName());
+ assertThat(res.provider, is(provider2Id));
+ assertThat(res.source, is(sourceId));
+ assertThat(res.searchChainId, is(sourceChainInProvider2Id));
+ }
+
+ @Test
+ public void lists_source_ref_description_for_top_level_targets() {
+ SortedSet<Target> topLevelTargets = searchChainResolver.allTopLevelTargets();
+ assertThat(topLevelTargets.size(), is(3));
+
+ Iterator<Target> i = topLevelTargets.iterator();
+ assertSearchRefDescriptionIs(i.next(), providerId.toString());
+ assertSearchRefDescriptionIs(i.next(), searchChainId.toString());
+ assertSearchRefDescriptionIs(i.next(), "source[provider = provider, provider2]");
+ }
+
+ private void assertSearchRefDescriptionIs(Target target, String expected) {
+ assertThat(target.searchRefDescription(), is(expected));
+ }
+
+ static Properties emptySourceToProviderMap() {
+ return new PropertyMap();
+ }
+
+ private SearchChainInvocationSpec resolve(String sourceSpecification) throws UnresolvedSearchChainException {
+ return resolve(sourceSpecification, emptySourceToProviderMap());
+ }
+
+ private SearchChainInvocationSpec resolve(String sourceSpecification, String providerSpecification)
+ throws UnresolvedSearchChainException {
+ Properties sourceToProviderMap = emptySourceToProviderMap();
+ sourceToProviderMap.set("source." + sourceSpecification + ".provider", providerSpecification);
+ return resolve(sourceSpecification, sourceToProviderMap);
+ }
+
+ private SearchChainInvocationSpec resolve(String sourceSpecification, Properties sourceToProviderMap)
+ throws UnresolvedSearchChainException {
+ SearchChainInvocationSpec res = searchChainResolver.resolve(
+ ComponentSpecification.fromString(sourceSpecification), sourceToProviderMap);
+ assertThat(res.federationOptions, is(federationOptions));
+ return res;
+ }
+}
diff --git a/container-search/src/test/java/com/yahoo/search/federation/sourceref/test/SourceRefResolverTestCase.java b/container-search/src/test/java/com/yahoo/search/federation/sourceref/test/SourceRefResolverTestCase.java
new file mode 100644
index 00000000000..f8559745358
--- /dev/null
+++ b/container-search/src/test/java/com/yahoo/search/federation/sourceref/test/SourceRefResolverTestCase.java
@@ -0,0 +1,114 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.search.federation.sourceref.test;
+
+import com.yahoo.component.ComponentId;
+import com.yahoo.component.ComponentSpecification;
+import com.yahoo.prelude.IndexFacts;
+import com.yahoo.prelude.IndexModel;
+import com.yahoo.search.federation.sourceref.SearchChainInvocationSpec;
+import com.yahoo.search.federation.sourceref.SearchChainResolver;
+import com.yahoo.search.federation.sourceref.SourceRefResolver;
+import com.yahoo.search.federation.sourceref.UnresolvedSearchChainException;
+import com.yahoo.search.searchchain.model.federation.FederationOptions;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeMap;
+
+import static com.yahoo.search.federation.sourceref.test.SearchChainResolverTestCase.emptySourceToProviderMap;
+import static junit.framework.Assert.fail;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.matchers.JUnitMatchers.hasItems;
+
+
+/**
+ * Test for SourceRefResolver.
+ * @author tonytv
+ */
+public class SourceRefResolverTestCase {
+ private static final String cluster1 = "cluster1";
+ private static final String cluster2 = "cluster2";
+ private static final String cluster3 = "cluster3";
+ private static IndexFacts indexFacts;
+
+ private static final SourceRefResolver sourceRefResolver = createSourceRefResolver();
+
+ static {
+ setupIndexFacts();
+ }
+
+ private static SourceRefResolver createSourceRefResolver() {
+ SearchChainResolver.Builder builder = new SearchChainResolver.Builder();
+ builder.addSearchChain(ComponentId.fromString(cluster1), new FederationOptions().setUseByDefault(true),
+ Collections.<String>emptyList());
+ builder.addSearchChain(ComponentId.fromString(cluster2), new FederationOptions().setUseByDefault(true),
+ Collections.<String>emptyList());
+
+ return new SourceRefResolver(builder.build());
+ }
+
+ private static void setupIndexFacts() {
+ TreeMap<String, List<String>> masterClusters = new TreeMap<>();
+ masterClusters.put(cluster1, Arrays.asList("document1", "document2"));
+ masterClusters.put(cluster2, Arrays.asList("document1"));
+ masterClusters.put(cluster3, Arrays.asList("document3"));
+ indexFacts = new IndexFacts(new IndexModel(masterClusters, null, null));
+ }
+
+ @Test
+ public void check_test_assumptions() {
+ assertThat(indexFacts.clustersHavingSearchDefinition("document1"), hasItems("cluster1", "cluster2"));
+ }
+
+ @Test
+ public void lookup_search_chain() throws Exception {
+ Set<SearchChainInvocationSpec> searchChains = resolve(cluster1);
+ assertThat(searchChains.size(), is(1));
+ assertThat(searchChainIds(searchChains), hasItems(cluster1));
+ }
+
+ @Test
+ public void lookup_search_chains_for_document1() throws Exception {
+ Set<SearchChainInvocationSpec> searchChains = resolve("document1");
+ assertThat(searchChains.size(), is(2));
+ assertThat(searchChainIds(searchChains), hasItems(cluster1, cluster2));
+ }
+
+ @Test
+ public void error_when_document_gives_cluster_without_matching_search_chain() {
+ try {
+ resolve("document3");
+ fail("Expected exception");
+ } catch (UnresolvedSearchChainException e) {
+ assertThat(e.getMessage(), is("Failed to resolve cluster search chain 'cluster3' " +
+ "when using source ref 'document3' as a document name."));
+ }
+ }
+
+ @Test
+ public void error_when_no_document_or_search_chain() {
+ try {
+ resolve("document4");
+ fail("Expected exception");
+ } catch (UnresolvedSearchChainException e) {
+ assertThat(e.getMessage(), is("Could not resolve source ref 'document4'."));
+ }
+ }
+
+ private List<String> searchChainIds(Set<SearchChainInvocationSpec> searchChains) {
+ List<String> names = new ArrayList<>();
+ for (SearchChainInvocationSpec searchChain : searchChains) {
+ names.add(searchChain.searchChainId.stringValue());
+ }
+ return names;
+ }
+
+ private Set<SearchChainInvocationSpec> resolve(String documentName) throws UnresolvedSearchChainException {
+ return sourceRefResolver.resolve(ComponentSpecification.fromString(documentName), emptySourceToProviderMap(), indexFacts);
+ }
+}