ClientConfigTest.java 10.4 KB
Newer Older
A
Allen Wang 已提交
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
18
package com.netflix.client.config;
A
Allen Wang 已提交
19 20 21 22 23 24 25

import static org.junit.Assert.*;

import com.netflix.config.ConfigurationManager;

import org.junit.AfterClass;
import org.junit.Assert;
E
elandau 已提交
26
import org.junit.Before;
A
Allen Wang 已提交
27
import org.junit.BeforeClass;
E
elandau 已提交
28 29
import org.junit.FixMethodOrder;
import org.junit.Rule;
A
Allen Wang 已提交
30
import org.junit.Test;
E
elandau 已提交
31 32
import org.junit.rules.TestName;
import org.junit.runners.MethodSorters;
33 34
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
A
Allen Wang 已提交
35

E
elandau 已提交
36
import java.util.Objects;
A
Allen Wang 已提交
37 38 39
import java.util.Properties;

/**
40
 * Test cases to verify the correctness of the Client Configuration settings
A
Allen Wang 已提交
41 42 43 44
 * 
 * @author stonse
 * 
 */
E
elandau 已提交
45
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
A
Allen Wang 已提交
46
public class ClientConfigTest {
47 48
    private static final Logger LOG = LoggerFactory.getLogger(ClientConfigTest.class);

E
elandau 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61
    @Rule
    public TestName testName = new TestName();

    IClientConfigKey<Integer> INTEGER_PROPERTY;

    IClientConfigKey<Integer> DEFAULT_INTEGER_PROPERTY;

    @Before
    public void setUp() throws Exception {
        INTEGER_PROPERTY = new CommonClientConfigKey<Integer>(
                "niws.loadbalancer.%s." + testName.getMethodName(), 10) {};
        DEFAULT_INTEGER_PROPERTY = new CommonClientConfigKey<Integer>(
                "niws.loadbalancer.default." + testName.getMethodName(), 30) {};
A
Allen Wang 已提交
62 63 64 65 66 67 68 69
    }

    @AfterClass
    public static void shutdown() throws Exception {
    }

    @Test
    public void testNiwsConfigViaProperties() throws Exception {
A
Allen Wang 已提交
70
    	DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
A
Allen Wang 已提交
71
    	DefaultClientConfigImpl override = new DefaultClientConfigImpl();
A
Allen Wang 已提交
72
    	clientConfig.loadDefaultValues();
A
Allen Wang 已提交
73 74 75 76 77 78 79
        Properties props = new Properties();
        
        final String restClientName = "testRestClient";
        
        props.setProperty("netflix.appinfo.stack","xbox");
        props.setProperty("netflix.environment","test");
        props.setProperty("appname", "movieservice");
80 81 82 83
        props.setProperty(restClientName + ".ribbon." + CommonClientConfigKey.AppName.key(), "movieservice");
        props.setProperty(restClientName + ".ribbon." + CommonClientConfigKey.DeploymentContextBasedVipAddresses.key(), "${appname}-${netflix.appinfo.stack}-${netflix.environment},movieservice--${netflix.environment}");
        props.setProperty(restClientName + ".ribbon." + CommonClientConfigKey.EnableZoneAffinity.key(), "false");

A
Allen Wang 已提交
84
        ConfigurationManager.loadProperties(props);
A
Allen Wang 已提交
85
        ConfigurationManager.getConfigInstance().setProperty("testRestClient.ribbon.customProperty", "abc");
A
Allen Wang 已提交
86 87
        
        clientConfig.loadProperties(restClientName);
E
elandau 已提交
88 89 90
        clientConfig.set(CommonClientConfigKey.ConnectTimeout, 1000);
        override.set(CommonClientConfigKey.Port, 8000);
        override.set(CommonClientConfigKey.ConnectTimeout, 5000);
A
Allen Wang 已提交
91
        clientConfig.applyOverride(override);
A
Allen Wang 已提交
92
        
E
elandau 已提交
93 94
        Assert.assertEquals("movieservice", clientConfig.get(CommonClientConfigKey.AppName));
        Assert.assertEquals(false, clientConfig.get(CommonClientConfigKey.EnableZoneAffinity));
A
Allen Wang 已提交
95
        Assert.assertEquals("movieservice-xbox-test,movieservice--test", clientConfig.resolveDeploymentContextbasedVipAddresses());
E
elandau 已提交
96
        Assert.assertEquals(5000, clientConfig.get(CommonClientConfigKey.ConnectTimeout).longValue());
A
Allen Wang 已提交
97

E
elandau 已提交
98
        Assert.assertEquals(8000, clientConfig.get(CommonClientConfigKey.Port).longValue());
A
Allen Wang 已提交
99 100
        System.out.println("AutoVipAddress:" + clientConfig.resolveDeploymentContextbasedVipAddresses());
        
A
Allen Wang 已提交
101
        ConfigurationManager.getConfigInstance().setProperty("testRestClient.ribbon.EnableZoneAffinity", "true");
E
elandau 已提交
102
        assertEquals(true, clientConfig.get(CommonClientConfigKey.EnableZoneAffinity));
A
Allen Wang 已提交
103 104 105 106
    }
    
    @Test
    public void testresolveDeploymentContextbasedVipAddresses() throws Exception {
107 108
        final String restClientName = "testRestClient2";

A
Allen Wang 已提交
109
    	DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
A
Allen Wang 已提交
110
    	clientConfig.loadDefaultValues();
111

A
Allen Wang 已提交
112
        Properties props = new Properties();
113 114 115 116 117
        props.setProperty(restClientName + ".ribbon." + CommonClientConfigKey.AppName.key(), "movieservice");
        props.setProperty(restClientName + ".ribbon." + CommonClientConfigKey.DeploymentContextBasedVipAddresses.key(), "${<appname>}-${netflix.appinfo.stack}-${netflix.environment}:${<port>},${<appname>}--${netflix.environment}:${<port>}");
        props.setProperty(restClientName + ".ribbon." + CommonClientConfigKey.Port.key(), "7001");
        props.setProperty(restClientName + ".ribbon." + CommonClientConfigKey.EnableZoneAffinity.key(), "true");

A
Allen Wang 已提交
118
        ConfigurationManager.loadProperties(props);
119

A
Allen Wang 已提交
120
        clientConfig.loadProperties(restClientName);
121

E
elandau 已提交
122 123
        Assert.assertEquals("movieservice", clientConfig.get(CommonClientConfigKey.AppName));
        Assert.assertEquals(true, clientConfig.get(CommonClientConfigKey.EnableZoneAffinity));
A
Allen Wang 已提交
124
        
A
Allen Wang 已提交
125
        ConfigurationManager.getConfigInstance().setProperty("testRestClient2.ribbon.DeploymentContextBasedVipAddresses", "movieservice-xbox-test:7001");
E
elandau 已提交
126
        assertEquals("movieservice-xbox-test:7001", clientConfig.get(CommonClientConfigKey.DeploymentContextBasedVipAddresses));
E
elandau 已提交
127

A
Allen Wang 已提交
128
        ConfigurationManager.getConfigInstance().clearProperty("testRestClient2.ribbon.EnableZoneAffinity");
E
elandau 已提交
129 130
        assertNull(clientConfig.get(CommonClientConfigKey.EnableZoneAffinity));
        assertFalse(clientConfig.getOrDefault(CommonClientConfigKey.EnableZoneAffinity));
A
Allen Wang 已提交
131
    }
E
elandau 已提交
132 133 134 135 136 137 138

    @Test
    public void testFallback_noneSet() {
        DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
        Property<Integer> prop = clientConfig.getGlobalProperty(INTEGER_PROPERTY.format(testName.getMethodName()))
                .fallbackWith(clientConfig.getGlobalProperty(DEFAULT_INTEGER_PROPERTY));

139
        Assert.assertEquals(30, prop.getOrDefault().intValue());
E
elandau 已提交
140 141 142 143 144 145 146 147 148
    }

    @Test
    public void testFallback_fallbackSet() {
        ConfigurationManager.getConfigInstance().setProperty(DEFAULT_INTEGER_PROPERTY.key(), "100");
        DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
        Property<Integer> prop = clientConfig.getGlobalProperty(INTEGER_PROPERTY.format(testName.getMethodName()))
                .fallbackWith(clientConfig.getGlobalProperty(DEFAULT_INTEGER_PROPERTY));

149
        Assert.assertEquals(100, prop.getOrDefault().intValue());
E
elandau 已提交
150 151 152 153 154 155 156 157 158 159 160
    }

    @Test
    public void testFallback_primarySet() {

        ConfigurationManager.getConfigInstance().setProperty(INTEGER_PROPERTY.format(testName.getMethodName()).key(), "200");

        DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
        Property<Integer> prop = clientConfig.getGlobalProperty(INTEGER_PROPERTY.format(testName.getMethodName()))
                .fallbackWith(clientConfig.getGlobalProperty(DEFAULT_INTEGER_PROPERTY));

161
        Assert.assertEquals(200, prop.getOrDefault().intValue());
E
elandau 已提交
162
    }
E
elandau 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

    static class CustomValueOf {
        private final String value;

        public static CustomValueOf valueOf(String value) {
            return new CustomValueOf(value);
        }

        public CustomValueOf(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
E
elandau 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

        @Override
        public String toString() {
            return value;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            CustomValueOf that = (CustomValueOf) o;
            return Objects.equals(value, that.value);
        }

        @Override
        public int hashCode() {
            return Objects.hash(value);
        }
E
elandau 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    }

    public static IClientConfigKey<CustomValueOf> CUSTOM_KEY = new CommonClientConfigKey<CustomValueOf>("CustomValueOf", new CustomValueOf("default")) {};

    @Test
    public void testValueOfWithDefault() {
        DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();

        CustomValueOf prop = clientConfig.getOrDefault(CUSTOM_KEY);
        Assert.assertEquals("default", prop.getValue());
    }

    @Test
    public void testValueOf() {
        ConfigurationManager.getConfigInstance().setProperty("testValueOf.ribbon.CustomValueOf", "value");

        DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
213
        clientConfig.loadProperties("testValueOf");
E
elandau 已提交
214 215

        Property<CustomValueOf> prop = clientConfig.getDynamicProperty(CUSTOM_KEY);
216
        Assert.assertEquals("value", prop.getOrDefault().getValue());
E
elandau 已提交
217 218 219

        ConfigurationManager.getConfigInstance().setProperty("testValueOf.ribbon.CustomValueOf", "value2");
        Assert.assertEquals("value2", prop.getOrDefault().getValue());
E
elandau 已提交
220
    }
221 222 223 224 225 226 227

    @Test
    public void testScopedProperty() {
        ConfigurationManager.getConfigInstance().setProperty("ribbon.foo.ScopePropertyTimeout", "2000");
        ConfigurationManager.getConfigInstance().setProperty("testScopedProperty.ribbon.foo.ScopePropertyTimeout", "1000");

        DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
228
        clientConfig.loadProperties("testScopedProperty");
229 230 231 232

        Property<Integer> prop = clientConfig.getScopedProperty(new CommonClientConfigKey<Integer>("foo.ScopePropertyTimeout", 0) {});
        Assert.assertEquals(1000, prop.get().get().intValue());
    }
E
elandau 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

    @Test
    public void testDynamicConfig() {
        ConfigurationManager.getConfigInstance().setProperty("testValueOf.ribbon.CustomValueOf", "value");

        DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
        clientConfig.loadProperties("testValueOf");

        Assert.assertEquals("value", clientConfig.get(CUSTOM_KEY).getValue());

        ConfigurationManager.getConfigInstance().setProperty("testValueOf.ribbon.CustomValueOf", "value2");
        Assert.assertEquals("value2", clientConfig.get(CUSTOM_KEY).getValue());

        ConfigurationManager.getConfigInstance().clearProperty("testValueOf.ribbon.CustomValueOf");
        Assert.assertNull(clientConfig.get(CUSTOM_KEY));
    }
A
Allen Wang 已提交
249 250
}