aboutsummaryrefslogtreecommitdiffstats
path: root/application/src/test/java/com/yahoo/application/container/ContainerTest.java
blob: 31e0b71d186362b49fe20b5d53cf885d0ea779ed (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
// Copyright Yahoo. 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.search.Query;
import com.yahoo.search.Result;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

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

import static com.yahoo.application.container.JDisc.fromServicesXml;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * @author Tony Vaagenes
 * @author gjoranv
 * @author ollivir
 */
public class ContainerTest {

    @Test
    void container_can_be_used_as_top_level_element() {
        try (JDisc container = fromServicesXml("<container version=\"1.0\">" + //
                "<search />" + //
                "</container>", Networking.disable)) {
            assertNotNull(container.search());
        }
    }

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

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

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

    @Test
    void handleRequest_yields_response_from_correct_request_handler() {
        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 {
                assertEquals(TestHandler.RESPONSE, response.getBodyAsString());
            } catch (CharacterCodingException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Test
    void load_searcher_from_bundle() {
        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"));
            assertEquals("Heal the World!", result.hits().get(0).getField("title").toString());
        }
    }

    @Test
    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("container");
            DocumentProcessing processing = container.documentProcessing();
            assertTrue(processing.getDocumentTypes().containsKey("example"));
        }
    }

    @Test
    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("container");
            DocumentProcessing processing = container.documentProcessing();
            assertTrue(processing.getAnnotationTypes().containsKey("exampleAnnotation"));
        }
    }

    @Disabled // Enable this when static state has been removed.
    @Test
    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"));
        assertEquals(TestHandler.RESPONSE, response.getBodyAsString());
    }

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

    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 = //
                "<container version=\"1.0\">" + //
                        "<handler id=" + handlerId + " />" + //
                        "<http>\n" + //
                        "<server id=\"main\" port=\"9999\" />\n" + //
                        "</http>\n" + //
                        "<accesslog type=\"disabled\" />" +
                        "</container>";
        return JDisc.fromServicesXml(xml, Networking.disable);
    }

}