config in place

This commit is contained in:
Alex Huang 2013-08-06 07:37:14 -07:00
parent 31509cf6da
commit e4f20c7ced
7 changed files with 180 additions and 13 deletions

View File

@ -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

View File

@ -34,5 +34,12 @@
<artifactId>cloud-framework-config</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>
</project>

View File

@ -34,5 +34,12 @@
<artifactId>cloud-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-api</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -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,15 +50,29 @@ 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.
}
}
@Override

View File

@ -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;
}

View File

@ -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);
}
}
}
}

View File

@ -341,11 +341,6 @@
<artifactId>spring-beans</artifactId>
<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>