aboutsummaryrefslogtreecommitdiffstats
path: root/application/src/test/java/com/yahoo/application/container/JDiscTest.java
blob: 86a96d04848953a9a987acecccdf06893e04eb66 (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
// 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.Application;
import com.yahoo.application.ApplicationBuilder;
import com.yahoo.application.Networking;
import com.yahoo.application.container.handler.Request;
import com.yahoo.application.container.handler.Response;
import com.yahoo.application.container.handlers.TestHandler;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.container.Container;
import com.yahoo.jdisc.http.server.jetty.JettyHttpServer;
import com.yahoo.jdisc.service.ServerProvider;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import org.junit.Ignore;
import org.junit.Test;

import java.nio.charset.CharacterCodingException;
import java.nio.file.FileSystems;

import static com.yahoo.application.container.JDisc.fromServicesXml;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

/**
 * @author Tony Vaagenes
 * @author gjoranv
 * @author ollivir
 */
public class JDiscTest {
    @Test
    public void jdisc_can_be_used_as_top_level_element() throws Exception {
        try (JDisc container = fromServicesXml("<jdisc version=\"1.0\">" + //
                "<search />" + //
                "</jdisc>", Networking.disable)) {
            assertNotNull(container.search());
        }
    }

    @Test
    public void jdisc_id_can_be_set() throws Exception {
        try (JDisc container = fromServicesXml("<jdisc version=\"1.0\" id=\"my-service-id\">" + //
                "<search />" + //
                "</jdisc>", Networking.disable)) {
            assertNotNull(container.search());
        }
    }

    @Test
    public void jdisc_can_be_embedded_in_services_tag() throws Exception {
        try (JDisc container = fromServicesXml("<services>" + //
                "<jdisc version=\"1.0\" id=\"my-service-id\">" + //
                "<search />" + //
                "</jdisc>" + //
                "</services>", Networking.disable)) {
            assertNotNull(container.search());
        }
    }

    @Test
    @SuppressWarnings("try") // container is unused inside the try block
    public void multiple_jdisc_elements_gives_exception() {
        try (JDisc container = fromServicesXml("<services>" + //
                "<jdisc version=\"1.0\" id=\"id1\" />" + //
                "<jdisc version=\"1.0\" />" + //
                "<container version=\"1.0\"/>" + //
                "</services>", Networking.disable)) {
            fail("expected exception");
        } catch (Exception e) {
            assertThat(e.getMessage(), containsString("container id='', jdisc id='id1', jdisc id=''"));
        }
    }

    @Test
    public void handleRequest_yields_response_from_correct_request_handler() throws Exception {
        final String handlerClass = TestHandler.class.getName();
        try (JDisc container = fromServicesXml("<container version=\"1.0\">" + //
                "<handler id=\"test-handler\" class=\"" + handlerClass + "\">" + //
                "<binding>http://*/TestHandler</binding>" + //
                "</handler>" + //
                "</container>", Networking.disable)) {
            Response response = container.handleRequest(new Request("http://foo/TestHandler"));
            try {
                assertThat(response.getBodyAsString(), is(TestHandler.RESPONSE));
            } catch (CharacterCodingException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Test
    public void load_searcher_from_bundle() throws Exception {
        try (JDisc container = JDisc.fromPath(FileSystems.getDefault().getPath("src/test/app-packages/searcher-app"),
                Networking.disable)) {
            Result result = container.search().process(ComponentSpecification.fromString("default"),
                    new Query("?query=ignored"));
            assertThat(result.hits().get(0).getField("title").toString(), is("Heal the World!"));
        }
    }

    @Test
    public void document_types_can_be_accessed() throws Exception {
        try (Application application = new ApplicationBuilder().documentType("example", EXAMPLE_DOCUMENT)
                .servicesXml(CONTAINER_WITH_DOCUMENT_PROCESSING).build()) {
            JDisc container = application.getJDisc("jdisc");
            DocumentProcessing processing = container.documentProcessing();
            assertThat(processing.getDocumentTypes().keySet(), hasItem("example"));
        }
    }

    @Test
    public void annotation_types_can_be_accessed() throws Exception {
        try (Application application = new ApplicationBuilder().documentType("example", "search example {\n" + //
                "  " + EXAMPLE_DOCUMENT + "\n" + //
                "  annotation exampleAnnotation {}\n" + //
                "}\n").//
                servicesXml(CONTAINER_WITH_DOCUMENT_PROCESSING).build()) {
            JDisc container = application.getJDisc("jdisc");
            DocumentProcessing processing = container.documentProcessing();
            assertThat(processing.getAnnotationTypes().keySet(), hasItem("exampleAnnotation"));
        }
    }

    @Ignore // Enable this when static state has been removed.
    @Test
    public void multiple_containers_can_be_run_in_parallel() throws Exception {
        try (JDisc jdisc1 = jdiscWithHttp(); JDisc jdisc2 = jdiscWithHttp()) {
            sendRequest(jdisc1);
            sendRequest(jdisc2);
        }
    }

    private void sendRequest(JDisc jdisc) throws CharacterCodingException {
        Response response = jdisc.handleRequest(new Request("http://foo/TestHandler"));
        assertThat(response.getBodyAsString(), is(TestHandler.RESPONSE));
    }

    public static final String CONTAINER_WITH_DOCUMENT_PROCESSING = //
            "<jdisc version=\"1.0\">" + //
                    "<http />" + //
                    "<document-processing />" + //
                    "</jdisc>";

    public static final String EXAMPLE_DOCUMENT = //
            "document example {\n" + //
                    "\n" + //
                    "  field title type string {\n" + //
                    "    indexing: summary | index   # How this field should be indexed\n" + //
                    "    weight: 75 # Ranking importancy of this field, used by the built in nativeRank feature\n" + //
                    "  }\n" + //
                    "}\n";

    protected JDisc jdiscWithHttp() {
        final String handlerId = TestHandler.class.getName();
        final String xml = //
                "<jdisc version=\"1.0\">" + //
                        "<handler id=" + handlerId + " />" + //
                        "<http>\n" + //
                        "<server id=\"main\" port=\"9999\" />\n" + //
                        "</http>\n" + //
                        "</jdisc>";
        return JDisc.fromServicesXml(xml, Networking.disable);
    }

    public static int getListenPort() {
        for (ServerProvider server : Container.get().getServerProviderRegistry().allComponents()) {
            if (null != server && server instanceof JettyHttpServer) {
                return ((JettyHttpServer) server).getListenPort();
            }
        }
        throw new RuntimeException("No http server found");
    }
}