mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
framework/config: make logic in ::value() defensive (#9108)
This adds a NPE check on the s_depot.global() which can cause NPE in case of unit tests, where s_depot is not null but the underlying config dao is null (not mocked or initialised) via `s_depot.global()` becomes null. This reverts commit 5f73172bcbe975e4ef416e525dc95bad63fa6d3a. Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
This commit is contained in:
parent
5f73172bcb
commit
2a63483b4c
@ -211,7 +211,7 @@ public class ConfigKey<T> {
|
|||||||
|
|
||||||
public T value() {
|
public T value() {
|
||||||
if (_value == null || isDynamic()) {
|
if (_value == null || isDynamic()) {
|
||||||
ConfigurationVO vo = s_depot != null ? s_depot.global().findById(key()) : null;
|
ConfigurationVO vo = (s_depot != null && s_depot.global() != null) ? s_depot.global().findById(key()) : null;
|
||||||
final String value = (vo != null && vo.getValue() != null) ? vo.getValue() : defaultValue();
|
final String value = (vo != null && vo.getValue() != null) ? vo.getValue() : defaultValue();
|
||||||
_value = ((value == null) ? (T)defaultValue() : valueOf(value));
|
_value = ((value == null) ? (T)defaultValue() : valueOf(value));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,7 +20,6 @@ import com.cloud.utils.concurrency.NamedThreadFactory;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@ -59,8 +58,8 @@ public class ConfigKeyScheduledExecutionWrapperTest {
|
|||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void invalidConfigKeyTest() {
|
public void invalidConfigKeyTest() {
|
||||||
TestRunnable runnable = new TestRunnable();
|
TestRunnable runnable = new TestRunnable();
|
||||||
ConfigKey<String> configKey = Mockito.mock(ConfigKey.class);
|
ConfigKey<String> configKey = new ConfigKey<>(String.class, "test", "test", "test", "test", true,
|
||||||
when(configKey.value()).thenReturn("test");
|
ConfigKey.Scope.Global, null, null, null, null, null, ConfigKey.Kind.CSV, null);
|
||||||
ConfigKeyScheduledExecutionWrapper runner = new ConfigKeyScheduledExecutionWrapper(executorService, runnable, configKey, TimeUnit.SECONDS);
|
ConfigKeyScheduledExecutionWrapper runner = new ConfigKeyScheduledExecutionWrapper(executorService, runnable, configKey, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user