aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/utils/internal/ReflectionUtilTest.java
blob: 2a7a019915bd1d0bf4710fa9f4cc786bbc47b07a (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.utils.internal;

import com.yahoo.config.model.producer.TreeConfigProducer;
import com.yahoo.test.ArraytypesConfig;
import com.yahoo.config.ChangesRequiringRestart;
import com.yahoo.config.ConfigInstance;
import com.yahoo.test.SimpletypesConfig;
import com.yahoo.vespa.config.ConfigKey;
import org.junit.jupiter.api.Test;

import java.util.Set;

import static com.yahoo.vespa.model.utils.internal.ReflectionUtil.getAllConfigsProduced;
import static org.junit.jupiter.api.Assertions.*;

/**
 * @author Ulf Lilleengen
 * @author bjorncs
 * @author gjoranv
 * @since 5.1
 */
public class ReflectionUtilTest {

    private static class SimpleProducer extends TreeConfigProducer implements SimpletypesConfig.Producer {
        SimpleProducer(TreeConfigProducer parent, String subId) { super(parent, subId); }

        @Override
        public void getConfig(SimpletypesConfig.Builder builder) { }
    }

    private interface ProducerInterface extends SimpletypesConfig.Producer, ArraytypesConfig.Producer { }

    private static class InterfaceImplementingProducer extends TreeConfigProducer implements ProducerInterface {
        InterfaceImplementingProducer(TreeConfigProducer parent, String subId) { super(parent, subId); }

        @Override
        public void getConfig(ArraytypesConfig.Builder builder) { }
        @Override
        public void getConfig(SimpletypesConfig.Builder builder) { }
    }

    private static abstract class MyAbstractProducer extends TreeConfigProducer implements SimpletypesConfig.Producer {
        MyAbstractProducer(TreeConfigProducer parent, String subId) { super(parent, subId); }
        @Override
        public void getConfig(SimpletypesConfig.Builder builder) { }
    }

    private static class ConcreteProducer extends MyAbstractProducer {
        ConcreteProducer(TreeConfigProducer parent, String subId) { super(parent, subId); }
    }

    private static class RestartConfig extends ConfigInstance {
        @SuppressWarnings("UnusedDeclaration")
        private static boolean containsFieldsFlaggedWithRestart() {
            return true;
        }

        @SuppressWarnings("UnusedDeclaration")
        private ChangesRequiringRestart getChangesRequiringRestart(RestartConfig newConfig) {
            return new ChangesRequiringRestart("testing");
        }
    }

    private static class NonRestartConfig extends ConfigInstance {}

    @Test
    void getAllConfigsProduced_includes_configs_produced_by_super_class() {
        Set<ConfigKey<?>> configs = getAllConfigsProduced(ConcreteProducer.class, "foo");
        assertEquals(1, configs.size());
        assertTrue(configs.contains(new ConfigKey<>(SimpletypesConfig.CONFIG_DEF_NAME, "foo", SimpletypesConfig.CONFIG_DEF_NAMESPACE)));
    }

    @Test
    void getAllConfigsProduced_includes_configs_produced_by_implemented_interface() {
        Set<ConfigKey<?>> configs = getAllConfigsProduced(InterfaceImplementingProducer.class, "foo");
        assertEquals(2, configs.size());
        assertTrue(configs.contains(new ConfigKey<>(SimpletypesConfig.CONFIG_DEF_NAME, "foo", SimpletypesConfig.CONFIG_DEF_NAMESPACE)));
        assertTrue(configs.contains(new ConfigKey<>(ArraytypesConfig.CONFIG_DEF_NAME, "foo", ArraytypesConfig.CONFIG_DEF_NAMESPACE)));
    }

    @Test
    void getAllConfigsProduced_includes_configs_directly_implemented_by_producer() {
        Set<ConfigKey<?>> configs = getAllConfigsProduced(SimpleProducer.class, "foo");
        assertEquals(1, configs.size());
        assertTrue(configs.contains(new ConfigKey<>(SimpletypesConfig.CONFIG_DEF_NAME, "foo", SimpletypesConfig.CONFIG_DEF_NAMESPACE)));
    }

    @Test
    void requireThatRestartMethodsAreDetectedProperly() {
        assertFalse(ReflectionUtil.hasRestartMethods(NonRestartConfig.class));
        assertTrue(ReflectionUtil.hasRestartMethods(RestartConfig.class));
    }

    @Test
    void requireThatRestartMethodsAreProperlyInvoked() {
        assertTrue(ReflectionUtil.containsFieldsFlaggedWithRestart(RestartConfig.class));
        assertEquals("testing", ReflectionUtil.getChangesRequiringRestart(new RestartConfig(), new RestartConfig()).getName());
    }

    @Test
    void requireThatGetChangesRequiringRestartValidatesParameterTypes() {
        assertThrows(IllegalArgumentException.class, () -> {
            ReflectionUtil.getChangesRequiringRestart(new RestartConfig(), new NonRestartConfig());
        });
    }


}