summaryrefslogtreecommitdiffstats
path: root/container-di/src/test/scala/com/yahoo/container/di/componentgraph/core/ComponentGraphTest.scala
blob: 867b5b792038f767dfe2412893477821a854d746 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.di.componentgraph.core

import org.junit.Test
import org.junit.Assert._
import org.hamcrest.CoreMatchers.{is, sameInstance, equalTo, not, containsString}

import com.yahoo.component.provider.ComponentRegistry
import com.google.inject.name.{Names, Named}
import com.yahoo.component.{ComponentId, AbstractComponent}
import org.hamcrest.Matcher
import com.yahoo.vespa.config.ConfigKey
import com.yahoo.config.{ConfigInstance}
import com.yahoo.config.test.{TestConfig, Test2Config}
import java.util.concurrent.{Executors, Executor}
import com.google.inject.{Guice, Key, AbstractModule, Inject, Provider=>GuiceProvider}
import com.yahoo.container.di._
import com.yahoo.config.subscription.ConfigGetter
import com.yahoo.container.di.config.{JerseyInjectionConfig, JerseyBundlesConfig, RestApiContext}
import com.yahoo.container.di.componentgraph.Provider

/**
 * @author gjoranv
 * @author tonytv
 */
class ComponentGraphTest {
  import ComponentGraphTest._

  private def keyAndConfig[T <: ConfigInstance](clazz: Class[T], configId: String): (ConfigKey[T], T) = {
    val key = new ConfigKey(clazz, configId)
    key -> ConfigGetter.getConfig(key.getConfigClass, key.getConfigId.toString)
  }

  @Test
  def component_taking_config_can_be_instantiated() {
    val componentGraph = new ComponentGraph
    val configId = "raw:stringVal \"test-value\""
    val componentNode = mockComponentNode(classOf[ComponentTakingConfig], configId)

    componentGraph.add(componentNode)
    componentGraph.complete()
    componentGraph.setAvailableConfigs(Map(keyAndConfig(classOf[TestConfig], configId)))

    val instance = componentGraph.getInstance(classOf[ComponentTakingConfig])
    assertNotNull(instance)
    assertThat(instance.config.stringVal(), is("test-value"))
  }

  @Test
  def component_can_be_injected_into_another_component() {
    val injectedComponent = mockComponentNode(classOf[SimpleComponent])
    val targetComponent = mockComponentNode(classOf[ComponentTakingComponent])
    targetComponent.inject(injectedComponent)

    val destroyGlobalLookupComponent = mockComponentNode(classOf[SimpleComponent])

    val componentGraph = new ComponentGraph
    componentGraph.add(injectedComponent)
    componentGraph.add(targetComponent)
    componentGraph.add(destroyGlobalLookupComponent)
    componentGraph.complete()


    val instance = componentGraph.getInstance(classOf[ComponentTakingComponent])
    assertNotNull(instance)
  }

  @Test
  def all_components_of_a_type_can_be_injected() {
    val componentGraph = new ComponentGraph
    componentGraph.add(mockComponentNode(classOf[SimpleComponent]))
    componentGraph.add(mockComponentNode(classOf[SimpleComponent]))
    componentGraph.add(mockComponentNode(classOf[SimpleDerivedComponent]))
    componentGraph.add(mockComponentNode(classOf[ComponentTakingAllSimpleComponents]))
    componentGraph.complete()

    val instance = componentGraph.getInstance(classOf[ComponentTakingAllSimpleComponents])
    assertThat(instance.simpleComponents.allComponents().size(), is(3))
  }

  @Test
  def empty_component_registry_can_be_injected() {
    val componentGraph = new ComponentGraph
    componentGraph.add(mockComponentNode(classOf[ComponentTakingAllSimpleComponents]))
    componentGraph.complete()

    val instance = componentGraph.getInstance(classOf[ComponentTakingAllSimpleComponents])
    assertThat(instance.simpleComponents.allComponents().size(), is(0))
  }

  @Test
  def component_registry_with_wildcard_upper_bound_can_be_injected() {
    val componentGraph = new ComponentGraph
    componentGraph.add(mockComponentNode(classOf[SimpleComponent]))
    componentGraph.add(mockComponentNode(classOf[SimpleDerivedComponent]))
    componentGraph.add(mockComponentNode(classOf[ComponentTakingAllSimpleComponentsUpperBound]))
    componentGraph.complete()

    val instance = componentGraph.getInstance(classOf[ComponentTakingAllSimpleComponentsUpperBound])
    assertThat(instance.simpleComponents.allComponents().size(), is(2))
  }

  @Test(expected = classOf[RuntimeException])
  def require_exception_when_injecting_registry_with_unknown_type_variable() {
    val clazz = classOf[ComponentTakingAllComponentsWithTypeVariable[_]]

    val componentGraph = new ComponentGraph
    componentGraph.add(mockComponentNode(classOf[SimpleComponent]))
    componentGraph.add(mockComponentNode(classOf[SimpleDerivedComponent]))
    componentGraph.add(mockComponentNode(clazz))
    componentGraph.complete()

    componentGraph.getInstance(clazz)
  }

  @Test
  def components_are_shared() {
    val componentGraph = new ComponentGraph
    componentGraph.add(mockComponentNode(classOf[SimpleComponent]))
    componentGraph.complete()

    val instance1 = componentGraph.getInstance(classOf[SimpleComponent])
    val instance2 = componentGraph.getInstance(classOf[SimpleComponent])
    assertThat(instance1, sameInstance(instance2))
  }

  @Test
  def singleton_components_can_be_injected() {
    val componentGraph = new ComponentGraph
    val configId = """raw:stringVal "test-value" """

    componentGraph.add(mockComponentNode(classOf[ComponentTakingComponent]))
    componentGraph.add(mockComponentNode(classOf[ComponentTakingConfig], configId))
    componentGraph.add(mockComponentNode(classOf[SimpleComponent2]))
    componentGraph.complete()
    componentGraph.setAvailableConfigs(Map(keyAndConfig(classOf[TestConfig], configId)))

    val instance = componentGraph.getInstance(classOf[ComponentTakingComponent])
    assertThat(instance.injectedComponent.asInstanceOf[ComponentTakingConfig].config.stringVal(), is("test-value"))
  }

  @Test(expected = classOf[RuntimeException])
  def require_error_when_multiple_components_match_a_singleton_dependency() {
    val componentGraph = new ComponentGraph
    componentGraph.add(mockComponentNode(classOf[SimpleDerivedComponent]))
    componentGraph.add(mockComponentNode(classOf[SimpleComponent]))
    componentGraph.add(mockComponentNode(classOf[ComponentTakingComponent]))
    componentGraph.complete()
  }

  @Test
  def named_component_can_be_injected() {
    val componentGraph = new ComponentGraph
    componentGraph.add(mockComponentNode(classOf[SimpleComponent]))
    componentGraph.add(mockComponentNode(classOf[SimpleComponent], key = Names.named("named-test")))
    componentGraph.add(mockComponentNode(classOf[ComponentTakingNamedComponent]))
    componentGraph.complete()
  }

  @Test
  def config_keys_can_be_retrieved() {
    val componentGraph = new ComponentGraph
    componentGraph.add(mockComponentNode(classOf[ComponentTakingConfig], configId = """raw:stringVal "component1" """""))
    componentGraph.add(mockComponentNode(classOf[ComponentTakingConfig], configId = """raw:stringVal "component2" """""))
    componentGraph.add(new ComponentRegistryNode(classOf[ComponentTakingConfig]))
    componentGraph.complete()

    val configKeys = componentGraph.configKeys
    assertThat(configKeys.size, is(2))

    configKeys.foreach{ key =>
      assertThat(key.getConfigClass, equalTo(classOf[TestConfig]))
      assertThat(key.getConfigId.toString, containsString("component"))
    }
  }

  @Test
  def providers_can_be_instantiated() {
    val componentGraph = new ComponentGraph
    componentGraph.add(mockComponentNode(classOf[ExecutorProvider]))
    componentGraph.complete()

    assertNotNull(componentGraph.getInstance(classOf[Executor]))
  }

  @Test
  def providers_can_be_inherited() {
    val componentGraph = new ComponentGraph
    componentGraph.add(mockComponentNode(classOf[DerivedExecutorProvider]))
    componentGraph.complete()

    assertNotNull(componentGraph.getInstance(classOf[Executor]))
  }

  @Test
  def providers_can_deliver_a_new_instance_for_each_component() {
    val componentGraph = new ComponentGraph
    componentGraph.add(mockComponentNode(classOf[NewIntProvider]))
    componentGraph.complete()

    val instance1 = componentGraph.getInstance(classOf[Int])
    val instance2 = componentGraph.getInstance(classOf[Int])
    assertThat(instance1, not(equalTo(instance2)))
  }

  @Test
  def providers_can_be_injected_explicitly() {
    val componentGraph = new ComponentGraph

    val componentTakingExecutor = mockComponentNode(classOf[ComponentTakingExecutor])
    val executorProvider = mockComponentNode(classOf[ExecutorProvider])
    componentTakingExecutor.inject(executorProvider)

    componentGraph.add(executorProvider)
    componentGraph.add(mockComponentNode(classOf[ExecutorProvider]))

    componentGraph.add(componentTakingExecutor)

    componentGraph.complete()
    assertNotNull(componentGraph.getInstance(classOf[ComponentTakingExecutor]))
  }

  @Test
  def global_providers_can_be_injected() {
    val componentGraph = new ComponentGraph

    componentGraph.add(mockComponentNode(classOf[ComponentTakingExecutor]))
    componentGraph.add(mockComponentNode(classOf[ExecutorProvider]))
    componentGraph.add(mockComponentNode(classOf[IntProvider]))
    componentGraph.complete()

    assertNotNull(componentGraph.getInstance(classOf[ComponentTakingExecutor]))
  }

  @Test(expected = classOf[RuntimeException])
  def throw_if_multiple_global_providers_exist(): Unit = {
    val componentGraph = new ComponentGraph

    componentGraph.add(mockComponentNode(classOf[ExecutorProvider]))
    componentGraph.add(mockComponentNode(classOf[ExecutorProvider]))
    componentGraph.add(mockComponentNode(classOf[ComponentTakingExecutor]))
    componentGraph.complete()
  }

  @Test
  def provider_is_not_used_when_component_of_provided_class_exists() {
    val componentGraph = new ComponentGraph

    componentGraph.add(mockComponentNode(classOf[SimpleComponent]))
    componentGraph.add(mockComponentNode(classOf[SimpleComponentProviderThatThrows]))
    componentGraph.add(mockComponentNode(classOf[ComponentTakingComponent]))
    componentGraph.complete()

    val injectedComponent = componentGraph.getInstance(classOf[ComponentTakingComponent]).injectedComponent
    assertNotNull(injectedComponent)
  }

  //TODO: move
  @Test
  def check_if_annotation_is_a_binding_annotation() {
    import ComponentGraph.isBindingAnnotation

    assertTrue(isBindingAnnotation(Names.named("name")))
    assertFalse(isBindingAnnotation(classOf[Named].getAnnotations.head))
  }

  @Test
  def cycles_gives_exception() {
    val componentGraph = new ComponentGraph

    def mockNode = mockComponentNode(classOf[ComponentCausingCycle])

    val node1 = mockNode
    val node2 = mockNode

    node1.inject(node2)
    node2.inject(node1)

    componentGraph.add(node1)
    componentGraph.add(node2)

    try {
      componentGraph.complete()
      fail("Cycle exception expected.")
    } catch {
      case e : Throwable => assertThat(e.getMessage, containsString("cycle"))
    }
  }

  @Test(expected = classOf[IllegalArgumentException])
  def abstract_classes_are_rejected() {
    new ComponentNode(ComponentId.fromString("Test"), "", classOf[AbstractClass])
  }

  @Test
  def inject_constructor_is_preferred() {
    assertThatComponentCanBeCreated(classOf[ComponentWithInjectConstructor])
  }

  @Test
  def constructor_with_most_parameters_is_preferred() {
    assertThatComponentCanBeCreated(classOf[ComponentWithMultipleConstructors])
  }

  def assertThatComponentCanBeCreated(clazz: Class[AnyRef]) {
    val componentGraph = new ComponentGraph
    val configId = """raw:stringVal "dummy" """"

    componentGraph.add(mockComponentNode(clazz, configId))
    componentGraph.complete()

    componentGraph.setAvailableConfigs(Map(
      keyAndConfig(classOf[TestConfig], configId),
      keyAndConfig(classOf[Test2Config], configId)))

    assertNotNull(componentGraph.getInstance(clazz))
  }

  @Test
  def require_fallback_to_child_injector() {
    val componentGraph = new ComponentGraph

    componentGraph.add(mockComponentNode(classOf[ComponentTakingExecutor]))

    componentGraph.complete(singletonExecutorInjector)
    assertNotNull(componentGraph.getInstance(classOf[ComponentTakingExecutor]))
  }

  @Test
  def child_injector_can_inject_multiple_instances_for_same_key() {
    def executorProvider() = Executors.newSingleThreadExecutor()

    val (graphSize, executorA, executorB)  = buildGraphWithChildInjector(executorProvider)

    assertThat(graphSize, is(4))
    assertThat(executorA, not(sameInstance(executorB)))
  }

  @Test
  def components_injected_via_child_injector_can_be_shared() {
    val commonExecutor = Executors.newSingleThreadExecutor()
    val (graphSize, executorA, executorB) = buildGraphWithChildInjector(() => commonExecutor)

    assertThat(graphSize, is(3))
    assertThat(executorA, sameInstance(executorB))
  }

  def buildGraphWithChildInjector(executorProvider: () => Executor) = {
    val childInjector = Guice.createInjector(new AbstractModule {
      override def configure() {
        bind(classOf[Executor]).toProvider(new GuiceProvider[Executor] {
          def get() = executorProvider()
        })
      }
    })

    val componentGraph = new ComponentGraph

    def key(name: String) = Key.get(classOf[ComponentTakingExecutor], Names.named(name))
    val keyA = key("A")
    val keyB = key("B")

    componentGraph.add(mockComponentNode(keyA))
    componentGraph.add(mockComponentNode(keyB))

    componentGraph.complete(childInjector)

    (componentGraph.size, componentGraph.getInstance(keyA).executor, componentGraph.getInstance(keyB).executor)
  }

  @Test
  def providers_can_be_reused() {
    def createGraph() = {
      val graph = new ComponentGraph()
      graph.add(mockComponentNodeWithId(classOf[ExecutorProvider], "dummyId"))
      graph.complete()
      graph.setAvailableConfigs(Map())
      graph
    }

    val oldGraph = createGraph()
    val executor = oldGraph.getInstance(classOf[Executor])

    val newGraph = createGraph()
    newGraph.reuseNodes(oldGraph)

    val newExecutor = newGraph.getInstance(classOf[Executor])
    assertThat(executor, sameInstance(newExecutor))
  }

  @Test
  def component_id_can_be_injected() {
    val componentId: String = "myId:1.2@namespace"

    val componentGraph = new ComponentGraph
    componentGraph.add(mockComponentNodeWithId(classOf[ComponentTakingComponentId], componentId))
    componentGraph.complete()

    assertThat(componentGraph.getInstance(classOf[ComponentTakingComponentId]).componentId,
      is(ComponentId.fromString(componentId)))
  }

  @Test
  def rest_api_context_can_be_instantiated() {
    val configId = """raw:"" """

    val clazz = classOf[RestApiContext]
    val jerseyNode = new JerseyNode(uniqueComponentId(clazz.getName), configId, clazz, new Osgi {})

    val componentGraph = new ComponentGraph
    componentGraph.add(jerseyNode)
    componentGraph.complete()
    componentGraph.setAvailableConfigs(Map(keyAndConfig(classOf[JerseyBundlesConfig], configId),
                                           keyAndConfig(classOf[JerseyInjectionConfig], configId)))

    val restApiContext = componentGraph.getInstance(clazz)
    assertNotNull(restApiContext)
    assertThat(restApiContext.getBundles.size, is(0))
  }

}

//Note that all Components must be defined in a static context,
//otherwise their constructor will take the outer class as the first parameter.
object ComponentGraphTest {
  var counter = 0


  class SimpleComponent extends AbstractComponent
  class SimpleComponent2 extends AbstractComponent
  class SimpleDerivedComponent extends SimpleComponent

  class ComponentTakingConfig(val config: TestConfig) extends SimpleComponent {
    require(config != null)
  }

  class ComponentTakingComponent(val injectedComponent: SimpleComponent) extends AbstractComponent {
    require(injectedComponent != null)
  }

  class ComponentTakingConfigAndComponent(val config: TestConfig, val injectedComponent: SimpleComponent) extends AbstractComponent {
    require(config != null)
    require(injectedComponent != null)
  }

  class ComponentTakingAllSimpleComponents(val simpleComponents: ComponentRegistry[SimpleComponent]) extends AbstractComponent {
    require(simpleComponents != null)
  }

  class ComponentTakingAllSimpleComponentsUpperBound(val simpleComponents: ComponentRegistry[_ <: SimpleComponent])
    extends AbstractComponent {

    require(simpleComponents != null)
  }

  class ComponentTakingAllComponentsWithTypeVariable[COMPONENT <: AbstractComponent](val simpleComponents: ComponentRegistry[COMPONENT])
    extends AbstractComponent {

    require(simpleComponents != null)
  }

  class ComponentTakingNamedComponent(@Named("named-test") injectedComponent: SimpleComponent) extends AbstractComponent {
    require(injectedComponent != null)
  }

  class ComponentCausingCycle(component: ComponentCausingCycle) extends AbstractComponent

  class SimpleComponentProviderThatThrows extends Provider[SimpleComponent] {
    def get() = throw new AssertionError("Should never be called.")
    def deconstruct() {}
  }

  class ExecutorProvider extends Provider[Executor] {
    val executor = Executors.newSingleThreadExecutor()
    def get() = executor
    def deconstruct() { /*TODO */ }
  }

  class DerivedExecutorProvider extends ExecutorProvider

  class IntProvider extends Provider[java.lang.Integer] {
    def get() = throw new AssertionError("Should never be called.")
    def deconstruct() {}
  }

  class NewIntProvider extends Provider[Integer] {
    var i: Int = 0
    def get() = {
      i += 1
      i
    }
    def deconstruct() {}
  }

  class ComponentTakingExecutor(val executor: Executor) extends AbstractComponent {
    require(executor != null)
  }

  class ComponentWithInjectConstructor private () {
    def this(c: TestConfig, c2: Test2Config) = { this(); sys.error("Should not be called") }
    @Inject
    def this(c: Test2Config) = { this() }
  }

  class ComponentWithMultipleConstructors private (dummy : Int) {
    def this(c: TestConfig, c2: Test2Config) = { this(0); }

    def this() = { this(0); sys.error("Should not be called") }
    def this(c: Test2Config) = { this() }
  }

  class ComponentTakingComponentId(val componentId: ComponentId)

  def uniqueComponentId(className: String): ComponentId = {
    counter += 1
    ComponentId.fromString(className + counter)
  }

  def mockComponentNode(key: Key[_ <: AnyRef]): Node =
    mockComponentNode(key.getTypeLiteral.getRawType.asInstanceOf[Class[AnyRef]], key=key.getAnnotation)

  def mockComponentNode(clazz: Class[_ <: AnyRef], configId: String = "", key: JavaAnnotation = null): Node =
    new ComponentNode(uniqueComponentId(clazz.getName), configId, clazz, key)

  def mockComponentNodeWithId(clazz: Class[_ <: AnyRef], componentId: String,  configId: String = "", key: JavaAnnotation = null): Node =
    new ComponentNode(ComponentId.fromString(componentId), configId, clazz, key)

  val singletonExecutorInjector = Guice.createInjector(new AbstractModule {
    override def configure() {
      bind(classOf[Executor]).toInstance(Executors.newSingleThreadExecutor())
    }
  })

  implicit def makeMatcherCovariant[T, U >: T](matcher: Matcher[T]) : Matcher[U] = matcher.asInstanceOf[Matcher[U]]

  abstract class AbstractClass
}