Github user geomacy commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/734#discussion_r124009325
  
    --- Diff: 
core/src/test/java/org/apache/brooklyn/core/config/ConfigKeyDeprecationTest.java
 ---
    @@ -0,0 +1,328 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you 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.
    + */
    +package org.apache.brooklyn.core.config;
    +
    +import static org.testng.Assert.assertEquals;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.entity.EntityInitializer;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.entity.EntitySpec;
    +import org.apache.brooklyn.api.entity.ImplementedBy;
    +import org.apache.brooklyn.api.location.LocationSpec;
    +import org.apache.brooklyn.api.policy.PolicySpec;
    +import org.apache.brooklyn.api.sensor.EnricherSpec;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.core.entity.AbstractEntity;
    +import org.apache.brooklyn.core.entity.EntityInternal;
    +import org.apache.brooklyn.core.entity.internal.ConfigUtilsInternal;
    +import org.apache.brooklyn.core.location.AbstractLocation;
    +import org.apache.brooklyn.core.policy.AbstractPolicy;
    +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
    +import org.apache.brooklyn.core.test.entity.TestEntity;
    +import org.apache.brooklyn.entity.stock.BasicEntity;
    +import org.apache.brooklyn.test.LogWatcher;
    +import org.apache.brooklyn.test.LogWatcher.EventPredicates;
    +import org.apache.brooklyn.util.core.config.ConfigBag;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.testng.annotations.Test;
    +
    +import com.google.common.base.Predicate;
    +import com.google.common.collect.ImmutableMap;
    +
    +import ch.qos.logback.classic.spi.ILoggingEvent;
    +
    +public class ConfigKeyDeprecationTest extends BrooklynAppUnitTestSupport {
    +
    +    @SuppressWarnings("unused")
    +    private static final Logger log = 
LoggerFactory.getLogger(ConfigKeyDeprecationTest.class);
    +    
    +    public static final ConfigKey<String> KEY_1 = 
ConfigKeys.builder(String.class, "key1")
    +            .deprecatedNames("oldKey1", "oldKey1b")
    +            .build();
    +
    +    @Test
    +    public void testUsingDeprecatedName() throws Exception {
    +        EntityInternal entity = 
app.addChild(EntitySpec.create(MyBaseEntity.class)
    +                .configure("oldSuperKey1", "myval"));
    +        EntityInternal entity2 = 
app.addChild(EntitySpec.create(MyBaseEntity.class)
    +                .configure("oldSuperKey1b", "myval"));
    +        assertEquals(entity.config().get(MyBaseEntity.SUPER_KEY_1), 
"myval");
    +        assertEquals(entity2.config().get(MyBaseEntity.SUPER_KEY_1), 
"myval");
    +    }
    +
    +    @Test
    +    public void testPrefersNonDeprecatedName() throws Exception {
    +        EntityInternal entity = 
app.addChild(EntitySpec.create(MyBaseEntity.class)
    +                .configure("superKey1", "myval")
    +                .configure("oldSuperKey1", "mywrongval"));
    +        assertEquals(entity.config().get(MyBaseEntity.SUPER_KEY_1), 
"myval");
    +    }
    +    
    +    @Test
    +    public void testPrefersFirstDeprecatedNameIfMultiple() throws 
Exception {
    +        EntityInternal entity = 
app.addChild(EntitySpec.create(MyBaseEntity.class)
    +                .configure("oldSuperKey1", "myval1")
    +                .configure("oldSuperKey1b", "myval2"));
    +        assertEquals(entity.config().get(MyBaseEntity.SUPER_KEY_1), 
"myval1");
    +    }
    +    
    +    @Test
    +    public void testInheritsDeprecatedKeyFromRuntimeParent() throws 
Exception {
    +        EntityInternal entity = 
app.addChild(EntitySpec.create(TestEntity.class)
    +                .configure("oldSuperKey1", "myval"));
    +        EntityInternal child = 
entity.addChild(EntitySpec.create(MyBaseEntity.class));
    +        assertEquals(child.config().get(MyBaseEntity.SUPER_KEY_1), 
"myval");
    +    }
    +
    +    @Test
    +    public void testInheritsDeprecatedKeyFromSuperType() throws Exception {
    +        EntityInternal entity = 
app.addChild(EntitySpec.create(MySubEntity.class)
    +                .configure("oldSuperKey1", "myval")
    +                .configure("oldSuperKey2", "myval2")
    +                .configure("oldSubKey2", "myval3")
    +                .configure("oldInterfaceKey1", "myval4"));
    +        assertEquals(entity.config().get(MySubEntity.SUPER_KEY_1), 
"myval");
    +        assertEquals(entity.config().get(MySubEntity.SUPER_KEY_2), 
"myval2");
    +        assertEquals(entity.config().get(MySubEntity.SUB_KEY_2), "myval3");
    +        assertEquals(entity.config().get(MyInterface.INTERFACE_KEY_1), 
"myval4");
    +    }
    +
    +    @Test
    +    public void testUsingDeprecatedNameInLocation() throws Exception {
    +        MyLocation loc = 
mgmt.getLocationManager().createLocation(LocationSpec.create(MyLocation.class)
    +                .configure("oldKey1", "myval"));
    +        assertEquals(loc.config().get(MyLocation.KEY_1), "myval");
    +    }
    +
    +    @Test
    +    public void testUsingDeprecatedNameInPolicy() throws Exception {
    +        MyPolicy policy = 
app.policies().add(PolicySpec.create(MyPolicy.class)
    +                .configure("oldKey1", "myval"));
    +        assertEquals(policy.config().get(MyPolicy.KEY_1), "myval");
    +    }
    +
    +    @Test
    +    public void testUsingDeprecatedNameInEnricher() throws Exception {
    +        MyEnricher enricher = 
app.enrichers().add(EnricherSpec.create(MyEnricher.class)
    +                .configure("oldKey1", "myval"));
    +        assertEquals(enricher.config().get(MyEnricher.KEY_1), "myval");
    +    }
    +
    +    @Test
    +    public void testUsingDeprecatedNameInEntityInitializer() throws 
Exception {
    +        Entity entity = app.addChild(EntitySpec.create(BasicEntity.class)
    +                .addInitializer(new 
MyEntityInitializer(ConfigBag.newInstance(ImmutableMap.of("oldKey1", 
"myval")))));
    +        assertEquals(entity.config().get(MyEntityInitializer.KEY_1), 
"myval");
    +    }
    +
    +    @Test
    +    public void testSetConfigUsingDeprecatedName() throws Exception {
    +        EntityInternal entity = 
app.addChild(EntitySpec.create(MyBaseEntity.class));
    +        
    +        // Set using strongly typed key
    +        entity.config().set(MyBaseEntity.SUPER_KEY_1, "myval");
    +        assertEquals(entity.config().get(MyBaseEntity.SUPER_KEY_1), 
"myval");
    +        
    +        // Set using correct string
    +        entity.config().putAll(ImmutableMap.of("superKey1", "myval2"));
    +        assertEquals(entity.config().get(MyBaseEntity.SUPER_KEY_1), 
"myval2");
    +        
    +        // Set using deprecated name
    +        entity.config().putAll(ImmutableMap.of("oldSuperKey1", "myval3"));
    +        assertEquals(entity.config().get(MyBaseEntity.SUPER_KEY_1), 
"myval3");
    +        
    +        // Set using pseudo-generated strongly typed key with deprecated 
name
    +        entity.config().set(ConfigKeys.newConfigKey(Object.class, 
"oldSuperKey1"), "myval4");
    +        assertEquals(entity.config().get(MyBaseEntity.SUPER_KEY_1), 
"myval4");
    +    }
    +
    +    @Test
    +    public void testSetAndGetDynamicConfigUsingDeprecatedName() throws 
Exception {
    +        // Using BasicEntity, which doesn't know about KEY_1
    +        EntityInternal entity = (EntityInternal) 
app.addChild(EntitySpec.create(BasicEntity.class));
    +        
    +        // Set using deprecated name
    +        entity.config().putAll(ImmutableMap.of("oldKey1", "myval3"));
    +        assertEquals(entity.config().get(KEY_1), "myval3");
    +        
    +        // Set using pseudo-generated strongly typed key with deprecated 
name
    +        entity.config().set(ConfigKeys.newConfigKey(Object.class, 
"oldKey1"), "myval4");
    +        assertEquals(entity.config().get(KEY_1), "myval4");
    +    }
    +
    +    /*
    +    // Setting the value of a "dynamic config key" when using the 
deprecated key will not overwrite
    +    // any existing value that was set using the real config key name. I 
think this is because 
    +    // EntityDynamicType.addConfigKey() is not called when KEY_1 is first 
set, so it doesn't have
    +    // access to the deprecated names on subsequent calls to set(String, 
val). It therefore stores
    +    // in EntityConfigMap (backed by a ConfigBag) the original key-value 
and the deprecated key-value.
    +    // When we subsequently call get(key), it retrieved the original 
key-value.
    +    //
    +    // Contrast this with EntityDynamicType.addSensorIfAbsent().
    +    //
    +     * However, it's (probably) not straight forward to just add and call 
a addConfigKeyIfAbsent. This
    +     * is because config().set() is used for dynamic config declard in 
yaml, which the entity doesn't
    --- End diff --
    
    typo `declard`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to