aboutsummaryrefslogtreecommitdiffstats
path: root/application/src/test/java/com/yahoo/application/container/JDiscContainerSearchTest.java
blob: 729439e7e5aac3a008d1efcbaf1ba159e1f13b70 (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.application.container;

import com.yahoo.application.Networking;
import com.yahoo.application.container.searchers.AddHitSearcher;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
 * @author gjoranv
 * @author ollivir
 */
public class JDiscContainerSearchTest {
    @Test
    public void processing_and_rendering_works() throws Exception {
        final String searcherId = AddHitSearcher.class.getName();

        try (JDisc container = containerWithSearch(searcherId)) {
            byte[] rendered = container.search().processAndRender(ComponentSpecification.fromString("mychain"),
                    ComponentSpecification.fromString("DefaultRenderer"), new Query(""));
            String renderedAsString = new String(rendered, "utf-8");
            assertThat(renderedAsString, containsString(searcherId));
        }
    }

    @Test
    public void searching_works() throws Exception {
        final String searcherId = AddHitSearcher.class.getName();

        try (JDisc container = containerWithSearch(searcherId)) {
            Search searching = container.search();
            Result result = searching.process(ComponentSpecification.fromString("mychain"), new Query(""));
            String hitTitle = result.hits().get(0).getField("title").toString();
            assertThat(hitTitle, is(searcherId));
        }
    }

    public JDisc containerWithSearch(String searcherId) {
        return JDisc.fromServicesXml("<container version=\"1.0\">" + //
                "<search>" + //
                "<chain id=\"mychain\">" + //
                "<searcher id=\"" + searcherId + "\"/>" + //
                "</chain>" + //
                "</search>" + //
                "</container>", Networking.disable);
    }

    @Test(expected = UnsupportedOperationException.class)
    public void retrieving_search_from_container_without_search_is_illegal() throws Exception {
        try (JDisc container = JDisc.fromServicesXml("<container version=\"1.0\" />", Networking.disable)) {
            container.search(); // throws
        }
    }
}