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

import com.yahoo.component.ComponentId;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.container.core.ChainsConfig;
import org.junit.jupiter.api.Test;

import java.util.Map;
import java.util.Set;

import static com.yahoo.container.core.ChainsConfig.Components;
import static com.yahoo.container.core.ChainsConfig.Chains;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author gjoranv
 */
public class ChainsModelBuilderTest {

    @Test
    void components_are_added_to_componentModels() throws Exception {
        ChainsModel model = chainsModel();
        assertEquals(2, model.allComponents().size());
        assertTrue(model.componentModels().containsKey(new ComponentId("componentA")));
    }

    @Test
    void components_are_added_to_chainSpecification() throws Exception {
        ChainsModel model = chainsModel();
        ChainSpecification chainSpec = model.chainSpecifications().get(new ComponentId("chain1")).model();
        assertTrue(getComponentsByName(chainSpec.componentReferences).containsKey("componentA"));
    }

    @Test
    void inherited_chains_are_added_to_chainSpecification() throws Exception {
        ChainsModel model = chainsModel();
        ChainSpecification inheritsChain1 = model.chainSpecifications().get(new ComponentId("inheritsChain1")).model();
        assertEquals(2, model.allChainsFlattened().size());
        assertTrue(getComponentsByName(inheritsChain1.inheritance.chainSpecifications).containsKey("chain1"));
        assertTrue(getComponentsByName(inheritsChain1.inheritance.excludedComponents).containsKey("componentA"));
    }

    private ChainsModel chainsModel() {
        ChainsConfig.Builder builder = new ChainsConfig.Builder()
                .components(new Components.Builder()
                        .id("componentA"))
                .components(new Components.Builder()
                        .id("componentB"))
                .chains(new Chains.Builder()
                        .id("chain1")
                        .components("componentA")
                        .components("componentB"))
                .chains(new Chains.Builder()
                        .id("inheritsChain1")
                        .inherits("chain1")
                        .excludes("componentA"));
        ChainsConfig config = new ChainsConfig(builder);

        ChainsModel model = ChainsModelBuilder.buildFromConfig(config);
        model.validate();
        return model;
    }

    private static Map<String, ComponentSpecification>
    getComponentsByName(Set<ComponentSpecification> componentSpecifications) {
        return ChainSpecification.componentsByName(componentSpecifications);
    }

}