Skip to content

Commit 2a63483

Browse files
authored
framework/config: make logic in ::value() defensive (apache#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 5f73172. Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent 5f73172 commit 2a63483

2 files changed

Lines changed: 3 additions & 4 deletions

File tree

framework/config/src/main/java/org/apache/cloudstack/framework/config/ConfigKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public boolean isSameKeyAs(Object obj) {
211211

212212
public T value() {
213213
if (_value == null || isDynamic()) {
214-
ConfigurationVO vo = s_depot != null ? s_depot.global().findById(key()) : null;
214+
ConfigurationVO vo = (s_depot != null && s_depot.global() != null) ? s_depot.global().findById(key()) : null;
215215
final String value = (vo != null && vo.getValue() != null) ? vo.getValue() : defaultValue();
216216
_value = ((value == null) ? (T)defaultValue() : valueOf(value));
217217
}

framework/config/src/test/java/org/apache/cloudstack/framework/config/ConfigKeyScheduledExecutionWrapperTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.junit.Test;
2121
import org.junit.runner.RunWith;
2222
import org.mockito.Mock;
23-
import org.mockito.Mockito;
2423
import org.mockito.junit.MockitoJUnitRunner;
2524

2625
import java.util.concurrent.Executors;
@@ -59,8 +58,8 @@ public void nullConfigKeyTest() {
5958
@Test(expected = IllegalArgumentException.class)
6059
public void invalidConfigKeyTest() {
6160
TestRunnable runnable = new TestRunnable();
62-
ConfigKey<String> configKey = Mockito.mock(ConfigKey.class);
63-
when(configKey.value()).thenReturn("test");
61+
ConfigKey<String> configKey = new ConfigKey<>(String.class, "test", "test", "test", "test", true,
62+
ConfigKey.Scope.Global, null, null, null, null, null, ConfigKey.Kind.CSV, null);
6463
ConfigKeyScheduledExecutionWrapper runner = new ConfigKeyScheduledExecutionWrapper(executorService, runnable, configKey, TimeUnit.SECONDS);
6564
}
6665

0 commit comments

Comments
 (0)