aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/component/provider/test/ComponentRegistryTestCase.java
blob: f5db47bb2b52acb07411a3c8dcbd8a92ee1a8050 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.component.provider.test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.yahoo.component.AbstractComponent;
import com.yahoo.component.ComponentId;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.component.provider.ComponentRegistry;

/**
 * Tests that ComponentRegistry handles namespaces correctly.
 * 
 * @author Tony Vaagenes
 */
public class ComponentRegistryTestCase {
    private static class TestComponent extends AbstractComponent {
        TestComponent(ComponentId componentId) {
            super(componentId);
        }
    }

    private static final String componentName = "component";

    private static final String namespace1 = "namespace1";
    private static final String namespace2 = "namespace2";
    private static final String namespace21 = "namespace2:1";

    private static final TestComponent component1 = componentInNamespace(namespace1);
    private static final TestComponent component2 = componentInNamespace(namespace2);
    private static final TestComponent component21 = componentInNamespace(namespace21);

    private ComponentRegistry<TestComponent> registry;

    private static ComponentSpecification specInNamespace(String namespace) {
        return new ComponentSpecification(componentName + "@" + namespace);
    }

    private static ComponentId idInNamespace(String namespace) {
        return specInNamespace(namespace).toId();
    }

    private static TestComponent componentInNamespace(String namespace) {
        return new TestComponent(idInNamespace(namespace));
    }

    @BeforeEach
    public void before() {
        registry = new ComponentRegistry<>();

        registry.register(component1.getId(), component1);
        registry.register(component2.getId(), component2);
        registry.register(component21.getId(), component21);
    }

    @Test
    void testAllPresent() {
        assertEquals(3, registry.getComponentCount());
    }

    @Test
    void testIdNamespaceLookup() {
        assertEquals(component1,  registry.getComponent(idInNamespace(namespace1)));
        assertEquals(component2,  registry.getComponent(idInNamespace(namespace2)));
        assertEquals(component21, registry.getComponent(idInNamespace(namespace21)));
    }

    @Test
    void testSpecNamespaceLookup() {
        assertEquals(component1, registry.getComponent(specInNamespace(namespace1)));

        // Version for namespace must match the specification exactly, so do not return version '1' when an
        // empty version is asked for.
        assertEquals(component2, registry.getComponent(specInNamespace(namespace2)));
        assertEquals(component21, registry.getComponent(specInNamespace(namespace21)));
    }

    @Test
    void testInnerComponentNotMixedWithTopLevelComponent() {
        assertNull(registry.getComponent(componentName));

        TestComponent topLevel = new TestComponent(new ComponentId(componentName));
        registry.register(topLevel.getId(), topLevel);
        assertEquals(topLevel, registry.getComponent(componentName));

        assertEquals(component1, registry.getComponent(specInNamespace(namespace1)));
        assertEquals(component1, registry.getComponent(idInNamespace(namespace1)));
    }

}