summaryrefslogtreecommitdiffstats
path: root/application/src/test/scala/com/yahoo/application/container/JDiscTest.scala
blob: c84e8a17a0ddbae065e9790637889e0ed35da44a (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
// Copyright 2017 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.container.Container
import com.yahoo.jdisc.http.server.jetty.JettyHttpServer

import scala.language.implicitConversions
import handler.Request
import org.junit.{Ignore, Test}
import org.junit.Assert.{assertThat, assertNotNull, fail}
import org.hamcrest.CoreMatchers.is
import java.nio.file.FileSystems
import com.yahoo.search.Query
import com.yahoo.component.ComponentSpecification
import handlers.TestHandler
import xml.{Node, Elem}
import JDiscTest._
import org.junit.matchers.JUnitMatchers.{containsString, hasItem}
import com.yahoo.application.{Networking, ApplicationBuilder}

import scala.collection.convert.wrapAsScala._


/**
 * @author tonytv
 * @author gjoranv
 */
class JDiscTest {
  @Test
  def jdisc_can_be_used_as_top_level_element() {
    using(fromServicesXml(
      <jdisc version="1.0">
        <search />
      </jdisc>))
    { container =>
      assertNotNull(container.search())
    }
  }

  @Test
  def jdisc_id_can_be_set() {
    using(fromServicesXml(
      <jdisc version="1.0" id="my-service-id">
        <search />
      </jdisc>))
    { container =>
      assertNotNull(container.search())
    }
  }

  @Test
  def jdisc_can_be_embedded_in_services_tag() {
    using(fromServicesXml(
      <services>
        <jdisc version="1.0" id="my-service-id">
          <search />
        </jdisc>
      </services>))
    { container =>
      assertNotNull(container.search())
    }
  }

  @Test
  def multiple_jdisc_elements_gives_exception() {
    try {
      using(fromServicesXml(
        <services>
          <jdisc version="1.0" id="id1" />
          <jdisc version="1.0" />
          <container version="1.0"/>
        </services>))
      { container => fail("expected exception")}
    } catch {
      case e: Exception => assertThat(e.getMessage, containsString("container id='', jdisc id='id1', jdisc id=''"))
    }
  }

  @Test
  def handleRequest_yields_response_from_correct_request_handler() {
    using(fromServicesXml(
        <container version="1.0">
          <handler id="test-handler" class={classOf[TestHandler].getName}>
            <binding>http://*/TestHandler</binding>
          </handler>
        </container>))
    { container =>
      val response = container.handleRequest(new Request("http://foo/TestHandler"))
      assertThat(response.getBodyAsString, is(TestHandler.RESPONSE))
    }
  }

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

  @Test
  def document_types_can_be_accessed() {
    using(new ApplicationBuilder().documentType("example", exampleDocument).
      servicesXml(containerWithDocumentProcessing).
      build())
    { application =>
      val container = application.getJDisc("jdisc")
      val processing = container.documentProcessing()
      assertThat(processing.getDocumentTypes.keySet(), hasItem("example"))
    }
  }

  @Test
  def annotation_types_can_be_accessed() {
    using(new ApplicationBuilder().documentType("example",
      s"""
          |search example {
          |  ${exampleDocument}
          |  annotation exampleAnnotation {}
          |}
          """.stripMargin).
      servicesXml(containerWithDocumentProcessing).
      build())
    { application =>
      val container = application.getJDisc("jdisc")
      val processing = container.documentProcessing()
      assertThat(processing.getAnnotationTypes.keySet(), hasItem("exampleAnnotation"))
    }
  }

  @Ignore //Enable this when static state has been removed.
  @Test
  def multiple_containers_can_be_run_in_parallel() {
    def sendRequest(jdisc: JDisc) {
      val response = jdisc.handleRequest(new Request("http://foo/TestHandler"))
      assertThat(response.getBodyAsString, is(TestHandler.RESPONSE))
    }

    using(jdiscWithHttp()) { jdisc1 =>
      using(jdiscWithHttp()) { jdisc2 =>
        sendRequest(jdisc1)
        sendRequest(jdisc2)
      }
    }
  }
}

object JDiscTest {

  def fromServicesXml(elem: Elem, networking: Networking = Networking.disable) =
    JDisc.fromServicesXml(elem.toString(), networking)

  def using[T <: AutoCloseable, U](t: T)(f: T => U ) = {
    try {
      f(t)
    } finally {
      t.close()
    }
  }

  implicit def xmlToString(xml: Node): String = xml.toString()

  val containerWithDocumentProcessing =
    <jdisc version="1.0">
      <http />
      <document-processing />
    </jdisc>

  val exampleDocument =
    """
      |document example {
      |
      |  field title type string {
      |    indexing: summary | index   # How this field should be indexed
      |    weight: 75 # Ranking importancy of this field, used by the built in nativeRank feature
      |    header
      |  }
      |}
    """.stripMargin


  def jdiscWithHttp() = {
    fromServicesXml(
      <jdisc version="1.0">
        <handler id={classOf[TestHandler].getName} />
        <http>
          <server id="main" port="9999" />
        </http>
      </jdisc>)
  }

  def getListenPort: Int =
    Container.get.getServerProviderRegistry.allComponents().collectFirst {
      case server: JettyHttpServer => server.getListenPort
    } getOrElse {
      throw new RuntimeException("No http server found")
    }
}