config in place
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/e4f20c7c Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/e4f20c7c Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/e4f20c7c Branch: refs/heads/master Commit: e4f20c7cedd2910e515530bbab1167b05b93f58c Parents: 31509cf Author: Alex Huang <alex.hu...@citrix.com> Authored: Tue Aug 6 07:37:14 2013 -0700 Committer: Alex Huang <alex.hu...@citrix.com> Committed: Wed Aug 7 16:41:03 2013 -0700 ---------------------------------------------------------------------- client/tomcatconf/applicationContext.xml.in | 2 +- framework/cluster/pom.xml | 11 +- framework/config/pom.xml | 11 +- .../framework/config/ConfigDepotImpl.java | 15 +++ .../framework/config/ConfigurationVO.java | 18 ++- .../framework/config/ConfigDepotAdminTest.java | 131 +++++++++++++++++++ pom.xml | 5 - 7 files changed, 180 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e4f20c7c/client/tomcatconf/applicationContext.xml.in ---------------------------------------------------------------------- diff --git a/client/tomcatconf/applicationContext.xml.in b/client/tomcatconf/applicationContext.xml.in index 0d5d0fa..26cd047 100644 --- a/client/tomcatconf/applicationContext.xml.in +++ b/client/tomcatconf/applicationContext.xml.in @@ -81,7 +81,7 @@ </bean> <bean id="messageBus" class = "org.apache.cloudstack.framework.messagebus.MessageBusBase" /> - <bean id="configDepot" class = "org.apache.cloudstack.config.ConfigDepotImpl" /> + <bean id="configDepot" class = "org.apache.cloudstack.framework.config.ConfigDepotImpl" /> <!-- DAO with customized configuration http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e4f20c7c/framework/cluster/pom.xml ---------------------------------------------------------------------- diff --git a/framework/cluster/pom.xml b/framework/cluster/pom.xml index 3e9f5ae..fe8af4c 100644 --- a/framework/cluster/pom.xml +++ b/framework/cluster/pom.xml @@ -28,11 +28,18 @@ <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-framework-db</artifactId> <version>${project.version}</version> - </dependency> + </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-framework-config</artifactId> <version>${project.version}</version> - </dependency> + </dependency> + <dependency> + <groupId>org.apache.cloudstack</groupId> + <artifactId>cloud-api</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e4f20c7c/framework/config/pom.xml ---------------------------------------------------------------------- diff --git a/framework/config/pom.xml b/framework/config/pom.xml index d0e8367..0d4344e 100644 --- a/framework/config/pom.xml +++ b/framework/config/pom.xml @@ -28,11 +28,18 @@ <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-framework-db</artifactId> <version>${project.version}</version> - </dependency> + </dependency> + <dependency> + <groupId>org.apache.cloudstack</groupId> + <artifactId>cloud-api</artifactId> + <version>${project.version}</version> + </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-api</artifactId> <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> </dependency> -</dependencies> + </dependencies> </project> http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e4f20c7c/framework/config/src/org/apache/cloudstack/framework/config/ConfigDepotImpl.java ---------------------------------------------------------------------- diff --git a/framework/config/src/org/apache/cloudstack/framework/config/ConfigDepotImpl.java b/framework/config/src/org/apache/cloudstack/framework/config/ConfigDepotImpl.java index ce5ee8e..42b4ab6 100644 --- a/framework/config/src/org/apache/cloudstack/framework/config/ConfigDepotImpl.java +++ b/framework/config/src/org/apache/cloudstack/framework/config/ConfigDepotImpl.java @@ -17,6 +17,7 @@ package org.apache.cloudstack.framework.config; import java.util.ArrayList; +import java.util.Date; import java.util.List; import javax.inject.Inject; @@ -49,14 +50,28 @@ class ConfigDepotImpl implements ConfigDepot, ConfigDepotAdmin { @Override public void populateConfigurations() { + Date date = new Date(); for (Configurable configurable : _configurables) { for (ConfigKey<?> key : configurable.getConfigKeys()) { ConfigurationVO vo = _configDao.findById(key.key()); if (vo == null) { vo = new ConfigurationVO(configurable.getConfigComponentName(), key); + vo.setUpdated(date); _configDao.persist(vo); + } else { + if (vo.isDynamic() != key.isDynamic() || + !vo.getDescription().equals(key.description()) || + !vo.getDefaultValue().equals(key.defaultValue())) { + vo.setDynamic(key.isDynamic()); + vo.setDescription(key.description()); + vo.setDefaultValue(key.defaultValue()); + vo.setUpdated(date); + _configDao.persist(vo); + } } } + + // TODO: Missing code to remove the updated field if the a configurationVO's name cannot be found any more. } } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e4f20c7c/framework/config/src/org/apache/cloudstack/framework/config/ConfigurationVO.java ---------------------------------------------------------------------- diff --git a/framework/config/src/org/apache/cloudstack/framework/config/ConfigurationVO.java b/framework/config/src/org/apache/cloudstack/framework/config/ConfigurationVO.java index 5f5152d..ba33df2 100644 --- a/framework/config/src/org/apache/cloudstack/framework/config/ConfigurationVO.java +++ b/framework/config/src/org/apache/cloudstack/framework/config/ConfigurationVO.java @@ -55,7 +55,7 @@ public class ConfigurationVO implements Configuration { private String category; @Column(name = "is_dynamic") - private boolean isDynamic; + private boolean dynamic; @Column(name = "scope") private String scope; @@ -135,18 +135,30 @@ public class ConfigurationVO implements Configuration { this.description = description; } - public String getScope(String scope) { + public String getScope() { return scope; } public boolean isDynamic() { - return isDynamic; + return dynamic; + } + + public void setDynamic(boolean dynamic) { + this.dynamic = dynamic; } public String getDefaultValue() { return defaultValue; } + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } + + public void setScope(String scope) { + this.scope = scope; + } + public Date getUpdated() { return updated; } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e4f20c7c/framework/config/test/org/apache/cloudstack/framework/config/ConfigDepotAdminTest.java ---------------------------------------------------------------------- diff --git a/framework/config/test/org/apache/cloudstack/framework/config/ConfigDepotAdminTest.java b/framework/config/test/org/apache/cloudstack/framework/config/ConfigDepotAdminTest.java new file mode 100644 index 0000000..2a3add8 --- /dev/null +++ b/framework/config/test/org/apache/cloudstack/framework/config/ConfigDepotAdminTest.java @@ -0,0 +1,131 @@ +// 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.cloudstack.framework.config; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.IOException; + +import javax.inject.Inject; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.ComponentScan.Filter; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; +import org.springframework.core.type.filter.TypeFilter; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import org.apache.cloudstack.framework.config.dao.ConfigurationDao; +import org.apache.cloudstack.test.utils.SpringUtils; + +import com.cloud.utils.component.ComponentContext; +import com.cloud.utils.db.EntityManager; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(loader = AnnotationConfigContextLoader.class) +public class ConfigDepotAdminTest { + private final ConfigKey<Integer> DynamicIntCK = new ConfigKey<Integer>(Integer.class, "dynIntKey", "Advance", "10", "Test Key", true); + private final ConfigKey<Integer> StaticIntCK = new ConfigKey<Integer>(Integer.class, "statIntKey", "Advance", "10", "Test Key", false); + + @Inject + Configurable configurable; + + @Inject + ConfigDepot _configDepot; + + @Inject + ConfigDepotAdmin _depotAdmin; + + @Inject + EntityManager _entityMgr; + + @Inject + ConfigurationDao _configDao; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + ComponentContext.initComponentsLifeCycle(); // NOTE #3 + } + + @Test + public void testAutoPopulation() { + ConfigurationVO dynamicIntCV = new ConfigurationVO("UnitTestComponent", DynamicIntCK); + dynamicIntCV.setValue("100"); + ConfigurationVO staticIntCV = new ConfigurationVO("UnitTestComponent", StaticIntCK); + dynamicIntCV.setValue("200"); + + when(configurable.getConfigComponentName()).thenReturn("UnitTestComponent"); + when(configurable.getConfigKeys()).thenReturn(new ConfigKey<?>[] {DynamicIntCK, StaticIntCK}); + when(_entityMgr.findById(org.apache.cloudstack.config.Configuration.class, DynamicIntCK.key())).thenReturn(dynamicIntCV); + when(_entityMgr.findById(org.apache.cloudstack.config.Configuration.class, StaticIntCK.key())).thenReturn(staticIntCV); + when(_configDao.findById(StaticIntCK.key())).thenReturn(null); + when(_configDao.findById(DynamicIntCK.key())).thenReturn(dynamicIntCV); + when(_configDao.persist(any(ConfigurationVO.class))).thenReturn(dynamicIntCV); + + _depotAdmin.populateConfigurations(); + + // This is once because DynamicIntCK is returned. + verify(_configDao, times(1)).persist(Mockito.any(ConfigurationVO.class)); + + when(_configDao.findById(DynamicIntCK.key())).thenReturn(dynamicIntCV); + _depotAdmin.populateConfigurations(); + // This is two because DynamicIntCK also returns null. + verify(_configDao, times(2)).persist(Mockito.any(ConfigurationVO.class)); + } + + @Configuration + @ComponentScan(basePackageClasses = {ConfigDepotImpl.class}, includeFilters = {@Filter(value = TestConfiguration.Library.class, type = FilterType.CUSTOM)}, useDefaultFilters = false) + static class TestConfiguration extends SpringUtils.CloudStackTestConfiguration { + @Bean + public Configurable configurable() { + return Mockito.mock(Configurable.class); + } + + @Bean + public EntityManager entityMgr() { + return Mockito.mock(EntityManager.class); + } + + @Bean + public ConfigurationDao configurationDao() { + return Mockito.mock(ConfigurationDao.class); + } + + public static class Library implements TypeFilter { + @Override + public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException { + ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class); + return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs); + } + } + } +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e4f20c7c/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index ce956e3..404c82e 100644 --- a/pom.xml +++ b/pom.xml @@ -342,11 +342,6 @@ <version>${org.springframework.version}</version> </dependency> <dependency> - <groupId>javax.inject</groupId> - <artifactId>javax.inject</artifactId> - <version>1</version> - </dependency> - <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>${cs.gson.version}</version>