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:
Rohit Yadav 2024-05-22 20:20:15 +05:30 committed by GitHub
parent 5f73172bcb
commit 2a63483b4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 4 deletions

View File

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

View File

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