CPVM: Read console proxy management state from DB
diff --git a/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManager.java b/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManager.java
index 4e71d78..af971d7 100644
--- a/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManager.java
+++ b/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManager.java
@@ -92,10 +92,10 @@
 
     String consoleProxyManagementStates = Arrays.stream(com.cloud.consoleproxy.ConsoleProxyManagementState.values()).map(Enum::name).collect(Collectors.joining(","));
     ConfigKey<String> ConsoleProxyServiceManagementState = new ConfigKey<String>(ConfigKey.CATEGORY_ADVANCED, String.class, "consoleproxy.management.state", com.cloud.consoleproxy.ConsoleProxyManagementState.Auto.toString(),
-            "console proxy service management state", false, ConfigKey.Kind.Select, consoleProxyManagementStates);
+            "console proxy service management state", true, ConfigKey.Kind.Select, consoleProxyManagementStates);
 
     ConfigKey<String> ConsoleProxyManagementLastState = new ConfigKey<String>(ConfigKey.CATEGORY_ADVANCED, String.class, "consoleproxy.management.state.last", com.cloud.consoleproxy.ConsoleProxyManagementState.Auto.toString(),
-            "last console proxy service management state", false, ConfigKey.Kind.Select, consoleProxyManagementStates);
+            "last console proxy service management state", true, ConfigKey.Kind.Select, consoleProxyManagementStates);
 
     ConfigKey<String> ConsoleProxyVmUserData = new ConfigKey<>(String.class, "console.proxy.vm.userdata",
             ConfigKey.CATEGORY_ADVANCED, "",
diff --git a/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java b/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
index 796fa7b..5a8a064 100644
--- a/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
+++ b/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
@@ -1011,18 +1011,21 @@
 
     @Override
     public ConsoleProxyManagementState getManagementState() {
-        String configKey = ConsoleProxyServiceManagementState.key();
-        String value = ConsoleProxyServiceManagementState.value();
+        return readManagementState(ConsoleProxyServiceManagementState);
+    }
+
+    private ConsoleProxyManagementState readManagementState(ConfigKey<String> configKey) {
+        String value = configurationDao.getValue(configKey.key());
 
         if (value != null) {
-            ConsoleProxyManagementState state = ConsoleProxyManagementState.valueOf(value);
-
-            if (state != null) {
-                return state;
+            try {
+                return ConsoleProxyManagementState.valueOf(value);
+            } catch (IllegalArgumentException e) {
+                logger.debug("Unable to parse console proxy management state [{}]", value, e);
             }
         }
 
-        logger.error(String.format("Value [%s] set in global configuration [%s] is not a valid console proxy management state.", value, configKey));
+        logger.error("Value [{}] set in global configuration [{}] is not a valid console proxy management state.", value, configKey.key());
         return null;
     }
 
@@ -1045,19 +1048,7 @@
     }
 
     private ConsoleProxyManagementState getLastManagementState() {
-        String configKey = ConsoleProxyManagementLastState.key();
-        String value = ConsoleProxyManagementLastState.value();
-
-        if (value != null) {
-            ConsoleProxyManagementState state = ConsoleProxyManagementState.valueOf(value);
-
-            if (state != null) {
-                return state;
-            }
-        }
-
-        logger.error(String.format("Value [%s] set in global configuration [%s] is not a valid console proxy management state.", value, configKey));
-        return null;
+        return readManagementState(ConsoleProxyManagementLastState);
     }
 
     @Override
diff --git a/server/src/test/java/com/cloud/consoleproxy/ConsoleProxyManagerImplTest.java b/server/src/test/java/com/cloud/consoleproxy/ConsoleProxyManagerImplTest.java
index 1feced5..28e0b4a9 100644
--- a/server/src/test/java/com/cloud/consoleproxy/ConsoleProxyManagerImplTest.java
+++ b/server/src/test/java/com/cloud/consoleproxy/ConsoleProxyManagerImplTest.java
@@ -18,10 +18,12 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -50,6 +52,9 @@
     private AccountManager accountManager;
 
     @Mock
+    private ConfigurationDao configurationDao;
+
+    @Mock
     private ServiceOffering serviceOffering;
     @Mock
     private VMTemplateVO template;
@@ -104,4 +109,33 @@
         assertEquals(template.getGuestOSId(), result.getGuestOSId());
         assertEquals(template.isDynamicallyScalable(), result.isDynamicallyScalable());
     }
+
+    @Test
+    public void testGetManagementStateReadsCurrentValueFromDb() {
+        when(configurationDao.getValue(ConsoleProxyManager.ConsoleProxyServiceManagementState.key()))
+                .thenReturn(ConsoleProxyManagementState.ResetSuspending.toString(), ConsoleProxyManagementState.Auto.toString());
+
+        assertEquals(ConsoleProxyManagementState.ResetSuspending, consoleProxyManager.getManagementState());
+        assertEquals(ConsoleProxyManagementState.Auto, consoleProxyManager.getManagementState());
+    }
+
+    @Test
+    public void testGetManagementStateInvalidValueReturnsNull() {
+        when(configurationDao.getValue(ConsoleProxyManager.ConsoleProxyServiceManagementState.key())).thenReturn("Bogus");
+
+        assertNull(consoleProxyManager.getManagementState());
+    }
+
+    @Test
+    public void testResumeLastManagementStateUpdatesDb() {
+        when(configurationDao.getValue(ConsoleProxyManager.ConsoleProxyServiceManagementState.key()))
+                .thenReturn(ConsoleProxyManagementState.ResetSuspending.toString());
+        when(configurationDao.getValue(ConsoleProxyManager.ConsoleProxyManagementLastState.key()))
+                .thenReturn(ConsoleProxyManagementState.Auto.toString());
+
+        consoleProxyManager.resumeLastManagementState();
+
+        verify(configurationDao).update(ConsoleProxyManager.ConsoleProxyServiceManagementState.key(),
+                ConsoleProxyManager.ConsoleProxyServiceManagementState.category(), ConsoleProxyManagementState.Auto.toString());
+    }
 }