Fix CloudConfig junits

...for some definition of the word "fix".  There is still a lot
that's less than ideal about how CloudConfig is handled, and with
how the unit tests are written.

Change-Id: Ic8c66c64d336f22c141687cf41a4828810bf1aec
Issue-ID: SO-584
Signed-off-by: Rob Daugherty <rd472p@att.com>
diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudConfigFactory.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudConfigFactory.java
index 3281f04..2b38591 100644
--- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudConfigFactory.java
+++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/cloud/CloudConfigFactory.java
@@ -92,11 +92,11 @@
     public CloudConfig getCloudConfig () {
         rwl.readLock ().lock ();
         try {
-            if (cloudConfigCache.isValidCloudConfig()) {
-                return cloudConfigCache.clone ();
-            } else {
-                return new CloudConfig();
+            if (!cloudConfigCache.isValidCloudConfig()) {
+            	// Not ideal, but better than returning an invalid object
+            	throw new IllegalStateException("No valid CloudConfig is loaded");
             }
+            return cloudConfigCache.clone ();
         } finally {
             rwl.readLock ().unlock ();
         }
diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtils.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtils.java
index 3f5da19..f7723b6 100644
--- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtils.java
+++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtils.java
@@ -100,9 +100,6 @@
     // The cache key is "tenantId:cloudId"
     private static Map <String, HeatCacheEntry> heatClientCache = new HashMap <> ();
 
-    // Fetch cloud configuration each time (may be cached in CloudConfig class)
-    protected CloudConfig cloudConfig;
-
     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
 
     protected MsoJavaProperties msoProps = null;
@@ -145,11 +142,12 @@
 		} catch (MsoPropertiesException e) {
 			LOGGER.error (MessageEnum.LOAD_PROPERTIES_FAIL, "Unknown. Mso Properties ID not found in cache: " + msoPropID, "", "", MsoLogger.ErrorCode.DataError, "Exception - Mso Properties ID not found in cache", e);
 		}
-        cloudConfig = cloudConfigFactory.getCloudConfig ();
         LOGGER.debug("MsoHeatUtils:" + msoPropID);
-
     }
 
+    protected CloudConfigFactory getCloudConfigFactory() {
+    	return cloudConfigFactory;
+    }
 
     /**
      * keep this old method signature here to maintain backwards compatibility. keep others as well.
@@ -325,7 +323,7 @@
         }
 
         // Obtain the cloud site information where we will create the stack
-        CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
+        CloudSite cloudSite = getCloudConfigFactory().getCloudConfig().getCloudSite(cloudSiteId).orElseThrow(
                 () -> new MsoCloudSiteNotFound(cloudSiteId));
         LOGGER.debug("Found: " + cloudSite.toString());
         // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
@@ -661,7 +659,7 @@
         LOGGER.debug ("Query HEAT stack: " + stackName + " in tenant " + tenantId);
 
         // Obtain the cloud site information where we will create the stack
-        CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
+        CloudSite cloudSite = getCloudConfigFactory().getCloudConfig().getCloudSite(cloudSiteId).orElseThrow(
                 () -> new MsoCloudSiteNotFound(cloudSiteId));
         LOGGER.debug("Found: " + cloudSite.toString());
 
@@ -723,7 +721,7 @@
                                   String stackName,
                                   boolean pollForCompletion) throws MsoException {
         // Obtain the cloud site information where we will create the stack
-        CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
+        CloudSite cloudSite = getCloudConfigFactory().getCloudConfig().getCloudSite(cloudSiteId).orElseThrow(
                 () -> new MsoCloudSiteNotFound(cloudSiteId));
         LOGGER.debug("Found: " + cloudSite.toString());
 
@@ -863,7 +861,7 @@
      */
     public List <StackInfo> queryAllStacks (String tenantId, String cloudSiteId) throws MsoException {
         // Obtain the cloud site information where we will create the stack
-        CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
+        CloudSite cloudSite = getCloudConfigFactory().getCloudConfig().getCloudSite(cloudSiteId).orElseThrow(
                 () -> new MsoCloudSiteNotFound(cloudSiteId));
         // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
         Heat heatClient = getHeatClient (cloudSite, tenantId);
diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtilsWithUpdate.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtilsWithUpdate.java
index 595da58..08ae9df 100644
--- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtilsWithUpdate.java
+++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatUtilsWithUpdate.java
@@ -193,7 +193,7 @@
         }
 
         // Obtain the cloud site information where we will create the stack
-        CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
+        CloudSite cloudSite = getCloudConfigFactory().getCloudConfig().getCloudSite(cloudSiteId).orElseThrow(
                 () -> new MsoCloudSiteNotFound(cloudSiteId));
         // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
         // This could throw MsoTenantNotFound or MsoOpenstackException (both propagated)
diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java
index 018396d..6bec917 100644
--- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java
+++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java
@@ -27,6 +27,8 @@
 import java.util.Map;
 
 import java.util.Optional;
+
+import org.openecomp.mso.cloud.CloudConfigFactory;
 import org.openecomp.mso.cloud.CloudIdentity;
 import org.openecomp.mso.cloud.CloudSite;
 import org.openecomp.mso.logger.MsoAlarmLogger;
@@ -64,8 +66,8 @@
 	private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
 	String msoPropID;
 	
-    public MsoKeystoneUtils (String msoPropID) {
-		super(msoPropID);
+    public MsoKeystoneUtils(String msoPropID, CloudConfigFactory cloudConfigFactory) {
+		super(msoPropID, cloudConfigFactory);
 		this.msoPropID = msoPropID;
 		LOGGER.debug("MsoKeyStoneUtils:" + msoPropID);
 	}
@@ -93,7 +95,7 @@
                                 Map <String, String> metadata,
                                 boolean backout) throws MsoException {
         // Obtain the cloud site information where we will create the tenant
-        Optional<CloudSite> cloudSiteOpt = cloudConfig.getCloudSite(cloudSiteId);
+        Optional<CloudSite> cloudSiteOpt = getCloudConfigFactory().getCloudConfig().getCloudSite(cloudSiteId);
         if (!cloudSiteOpt.isPresent()) {
         	LOGGER.error(MessageEnum.RA_CREATE_TENANT_ERR, "MSOCloudSite not found", "", "", MsoLogger.ErrorCode.DataError, "MSOCloudSite not found");
             throw new MsoCloudSiteNotFound (cloudSiteId);
@@ -197,7 +199,7 @@
      */
     public MsoTenant queryTenant (String tenantId, String cloudSiteId) throws MsoException {
         // Obtain the cloud site information where we will query the tenant
-        CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
+        CloudSite cloudSite = getCloudConfigFactory().getCloudConfig().getCloudSite(cloudSiteId).orElseThrow(
                 () -> new MsoCloudSiteNotFound(cloudSiteId));
 
         Keystone keystoneAdminClient = getKeystoneAdminClient (cloudSite);
@@ -245,7 +247,7 @@
      */
     public MsoTenant queryTenantByName (String tenantName, String cloudSiteId) throws MsoException {
         // Obtain the cloud site information where we will query the tenant
-        CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
+        CloudSite cloudSite = getCloudConfigFactory().getCloudConfig().getCloudSite(cloudSiteId).orElseThrow(
                 () -> new MsoCloudSiteNotFound(cloudSiteId));
         Keystone keystoneAdminClient = getKeystoneAdminClient (cloudSite);
 
@@ -290,7 +292,7 @@
      */
     public boolean deleteTenant (String tenantId, String cloudSiteId) throws MsoException {
         // Obtain the cloud site information where we will query the tenant
-        CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
+        CloudSite cloudSite = getCloudConfigFactory().getCloudConfig().getCloudSite(cloudSiteId).orElseThrow(
                 () -> new MsoCloudSiteNotFound(cloudSiteId));
         Keystone keystoneAdminClient = getKeystoneAdminClient (cloudSite);
 
diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoNeutronUtils.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoNeutronUtils.java
index 9eba799..df769ec 100644
--- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoNeutronUtils.java
+++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoNeutronUtils.java
@@ -65,8 +65,7 @@
 	// The cache key is "tenantId:cloudId"
 	private static Map<String,NeutronCacheEntry> neutronClientCache = new HashMap<>();
 
-	// Fetch cloud configuration each time (may be cached in CloudConfig class)
-	private CloudConfig cloudConfig;
+	private CloudConfigFactory cloudConfigFactory;
 
 	private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
 	private String msoPropID;
@@ -76,9 +75,13 @@
 	};
 
 	public MsoNeutronUtils(String msoPropID, CloudConfigFactory cloudConfigFactory) {
-		cloudConfig = cloudConfigFactory.getCloudConfig();
+		this.cloudConfigFactory = cloudConfigFactory;
 		this.msoPropID = msoPropID;
 	}
+	
+	protected CloudConfigFactory getCloudConfigFactory() {
+		return cloudConfigFactory;
+	}
 
 	/**
 	 * Create a network with the specified parameters in the given cloud/tenant.
@@ -101,7 +104,7 @@
             throws MsoException
 	{
 		// Obtain the cloud site information where we will create the stack
-        CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
+        CloudSite cloudSite = getCloudConfigFactory().getCloudConfig().getCloudSite(cloudSiteId).orElseThrow(
                 () -> new MsoCloudSiteNotFound(cloudSiteId));
 
 		Quantum neutronClient = getNeutronClient (cloudSite, tenantId);
@@ -179,7 +182,7 @@
 		LOGGER.debug("In queryNetwork");
 
 		// Obtain the cloud site information
-        CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
+        CloudSite cloudSite = getCloudConfigFactory().getCloudConfig().getCloudSite(cloudSiteId).orElseThrow(
                 () -> new MsoCloudSiteNotFound(cloudSiteId));
 
 		Quantum neutronClient = getNeutronClient (cloudSite, tenantId);
@@ -219,7 +222,7 @@
     public boolean deleteNetwork(String networkId, String tenantId, String cloudSiteId) throws MsoException
 	{
 		// Obtain the cloud site information where we will create the stack
-        CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
+        CloudSite cloudSite = getCloudConfigFactory().getCloudConfig().getCloudSite(cloudSiteId).orElseThrow(
                 () -> new MsoCloudSiteNotFound(cloudSiteId));
 		Quantum neutronClient = getNeutronClient (cloudSite, tenantId);
 		try {
@@ -276,7 +279,7 @@
             throws MsoException
 	{
 		// Obtain the cloud site information where we will create the stack
-        CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
+        CloudSite cloudSite = getCloudConfigFactory().getCloudConfig().getCloudSite(cloudSiteId).orElseThrow(
                 () -> new MsoCloudSiteNotFound(cloudSiteId));
 		Quantum neutronClient = getNeutronClient (cloudSite, tenantId);
 		// Check that the network exists
diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoTenantUtils.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoTenantUtils.java
index 2cd4597..964babd 100644
--- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoTenantUtils.java
+++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoTenantUtils.java
@@ -22,7 +22,6 @@
 
 
 import java.util.Map;
-import org.openecomp.mso.cloud.CloudConfig;
 import org.openecomp.mso.cloud.CloudConfigFactory;
 import org.openecomp.mso.cloud.CloudIdentity;
 import org.openecomp.mso.logger.MessageEnum;
@@ -36,16 +35,14 @@
 
 public abstract class MsoTenantUtils extends MsoCommonUtils {
 
-    protected CloudConfigFactory cloudConfigFactory;
+    private CloudConfigFactory cloudConfigFactory;
 	protected MsoPropertiesFactory msoPropFactory;
 	protected static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
 	protected MsoJavaProperties msoProps;
-    protected CloudConfig cloudConfig;
 
-    public MsoTenantUtils (String msoPropID) {
-    	cloudConfigFactory = new CloudConfigFactory();
+    public MsoTenantUtils (String msoPropID, CloudConfigFactory cloudConfigFactory) {
+    	this.cloudConfigFactory = cloudConfigFactory;
     	msoPropFactory = new MsoPropertiesFactory();
-    	cloudConfig = cloudConfigFactory.getCloudConfig ();
 
     	LOGGER.debug("msoTenantUtils:" + msoPropID);
 		
@@ -56,6 +53,10 @@
 		}
     }
 
+    public CloudConfigFactory getCloudConfigFactory() {
+    	return cloudConfigFactory;
+    }
+
     public abstract String createTenant (String tenantName, String cloudSiteId, Map <String, String> metadata, boolean backout) 
     		throws MsoException;
        
diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoTenantUtilsFactory.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoTenantUtilsFactory.java
index cc9e869..49c2622 100644
--- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoTenantUtilsFactory.java
+++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoTenantUtilsFactory.java
@@ -33,18 +33,25 @@
 public class MsoTenantUtilsFactory {
 
 	private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
-	private CloudConfigFactory cloudConfigFactory= new CloudConfigFactory(); 
-	private CloudConfig cloudConfig;
+	private CloudConfigFactory cloudConfigFactory = new CloudConfigFactory();
 	private String msoPropID;
 	
 	public MsoTenantUtilsFactory (String msoPropID) {
 		this.msoPropID = msoPropID;
 	}
 
+	public void setCloudConfigFactory(CloudConfigFactory cloudConfigFactory) {
+		this.cloudConfigFactory = cloudConfigFactory;
+	}
+
+	public CloudConfigFactory getCloudConfigFactory() {
+		return cloudConfigFactory;
+	}
+
 	//based on Cloud IdentityServerType returns ORM or KEYSTONE Utils
 	public MsoTenantUtils getTenantUtils(String cloudSiteId) throws MsoCloudSiteNotFound {
 		// Obtain the cloud site information 
-		cloudConfig = cloudConfigFactory.getCloudConfig();
+		CloudConfig cloudConfig = getCloudConfigFactory().getCloudConfig();
 		CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
 				() -> new MsoCloudSiteNotFound(cloudSiteId));
 		return getTenantUtilsByServerType(cloudSite.getIdentityService().getIdentityServerType().toString());
@@ -54,10 +61,11 @@
 
 		MsoTenantUtils tenantU = null;
 		if (CloudIdentity.IdentityServerType.KEYSTONE.toString().equals(serverType)) {
-			tenantU = new MsoKeystoneUtils (msoPropID);
+			tenantU = new MsoKeystoneUtils(msoPropID, getCloudConfigFactory());
 		} else {
 			try {
-				tenantU = CloudIdentity.IdentityServerType.valueOf(serverType).getMsoTenantUtilsClass().getConstructor(String.class).newInstance(msoPropID);
+				tenantU = CloudIdentity.IdentityServerType.valueOf(serverType).getMsoTenantUtilsClass()
+					.getConstructor(String.class, CloudConfigFactory.class).newInstance(msoPropID, getCloudConfigFactory());
 			} catch (InvocationTargetException | InstantiationException | NoSuchMethodException | IllegalAccessException e) {
 				throw new RuntimeException("Could not instantiate an MsoTenantUtils class for " + serverType, e);
 			}
diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/adapter_utils/tests/MsoHeatUtilsTest.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/adapter_utils/tests/MsoHeatUtilsTest.java
index 931f11e..93afbcc 100644
--- a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/adapter_utils/tests/MsoHeatUtilsTest.java
+++ b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/adapter_utils/tests/MsoHeatUtilsTest.java
@@ -70,9 +70,9 @@
 
 	@BeforeClass
 	public static final void loadClasses() throws MsoCloudIdentityNotFound, MsoPropertiesException {
-		ClassLoader classLoader = CloudConfigTest.class.getClassLoader();
-		String config = classLoader.getResource("cloud_config.json").toString().substring(5);
-		cloudConfigFactory.initializeCloudConfig(config, 1);
+		ClassLoader classLoader = MsoHeatUtilsTest.class.getClassLoader();
+		String cloudConfigJson = classLoader.getResource("cloud_config.json").getPath();
+		cloudConfigFactory.initializeCloudConfig(cloudConfigJson, 1);
 		msoPropertiesFactory.initializeMsoProperties("NO_PROP", classLoader.getResource("mso.properties").getPath());
 		msoHeatUtils = new MsoHeatUtils("NO_PROP", msoPropertiesFactory, cloudConfigFactory);
 	}
diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/CloudConfigFactoryTest.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/CloudConfigFactoryTest.java
index fe768b5..c6c6baf 100644
--- a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/CloudConfigFactoryTest.java
+++ b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/CloudConfigFactoryTest.java
@@ -30,6 +30,7 @@
 import java.util.HashMap;
 import java.util.Map;
 import javax.ws.rs.core.Response;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.openecomp.mso.openstack.exceptions.MsoCloudIdentityNotFound;
@@ -41,12 +42,23 @@
 
     private CloudConfigFactory testedObject;
     private CloudConfig cloudConfigMock;
+    private CloudConfig savedCloudConfig;
 
     @Before
     public void init() throws NoSuchFieldException, IllegalAccessException {
         cloudConfigMock = mock(CloudConfig.class);
         testedObject = new CloudConfigFactory();
-        setCloudConfig();
+        Field field = CloudConfigFactory.class.getDeclaredField(CLOUD_CONFIG_FIELD_NAME);
+        field.setAccessible(true);
+        savedCloudConfig = (CloudConfig) field.get(null);
+        field.set(null, cloudConfigMock);
+    }
+
+    @After
+    public void reset() throws NoSuchFieldException, IllegalAccessException {
+        Field field = CloudConfigFactory.class.getDeclaredField(CLOUD_CONFIG_FIELD_NAME);
+        field.setAccessible(true);
+        field.set(null, savedCloudConfig);
     }
 
     @Test
@@ -67,17 +79,6 @@
     }
 
     @Test
-    public void getNotValidCloudConfig() {
-        when(cloudConfigMock.isValidCloudConfig()).thenReturn(false);
-
-        CloudConfig result = testedObject.getCloudConfig();
-
-        assertThat(result).isNotNull();
-        assertThat(result.getCloudSites()).isEmpty();
-        assertThat(result.getIdentityServices()).isEmpty();
-    }
-
-    @Test
     public void reload_CloudConfigValid() throws IOException, MsoCloudIdentityNotFound {
         when(cloudConfigMock.isValidCloudConfig()).thenReturn(true);
 
@@ -156,9 +157,6 @@
 
     private void setCloudConfig()
             throws NoSuchFieldException, IllegalAccessException {
-        Field field = testedObject.getClass().getDeclaredField(CLOUD_CONFIG_FIELD_NAME);
-        field.setAccessible(true);
-        field.set(testedObject, cloudConfigMock);
     }
 
 }
diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/servertype/NewServerTypeUtils.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/servertype/NewServerTypeUtils.java
index aaa732c..40108b3 100644
--- a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/servertype/NewServerTypeUtils.java
+++ b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/servertype/NewServerTypeUtils.java
@@ -22,6 +22,7 @@
 

 import java.util.Map;

 

+import org.openecomp.mso.cloud.CloudConfigFactory;

 import org.openecomp.mso.cloud.CloudIdentity;

 import org.openecomp.mso.openstack.beans.MsoTenant;

 import org.openecomp.mso.openstack.exceptions.MsoCloudSiteNotFound;

@@ -31,11 +32,8 @@
 

 public class NewServerTypeUtils extends MsoTenantUtils {

 

-	/**

-	 * @param msoPropID

-	 */

-	public NewServerTypeUtils(String msoPropID) {

-		super(msoPropID);

+	public NewServerTypeUtils(String msoPropID, CloudConfigFactory cloudConfigFactory) {

+		super(msoPropID, cloudConfigFactory);

 	}

 

 	@Override

diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/servertype/ServerTypeTest.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/servertype/ServerTypeTest.java
index 4aaf379..69fab27 100644
--- a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/servertype/ServerTypeTest.java
+++ b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/servertype/ServerTypeTest.java
@@ -24,14 +24,24 @@
 import static org.junit.Assert.assertNotNull;

 import static org.junit.Assert.fail;

 

+import org.junit.BeforeClass;

 import org.junit.Ignore;

 import org.junit.Test;

+import org.openecomp.mso.cloud.CloudConfigFactory;

 import org.openecomp.mso.cloud.CloudIdentity;

 import org.openecomp.mso.cloud.CloudIdentity.IdentityServerType;

 import org.openecomp.mso.cloud.IdentityServerTypeAbstract;

 import org.openecomp.mso.openstack.exceptions.MsoException;

+import org.openecomp.mso.openstack.utils.MsoKeystoneUtilsTest;

 

 public class ServerTypeTest {

+	

+    @BeforeClass

+    public static void init() throws Exception {

+        String cloudConfigJson = ServerTypeTest.class.getClassLoader()

+            .getResource("cloud_config.json").getPath();

+        (new CloudConfigFactory()).initializeCloudConfig(cloudConfigJson, 0);

+    }

 

     @Test

     @Ignore // IGNORED FOR 1710 MERGE TO ONAP

diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/openstack/utils/MsoHeatUtilsTest2.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/openstack/utils/MsoHeatUtilsTest2.java
index a0ab8e6..74d512c 100644
--- a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/openstack/utils/MsoHeatUtilsTest2.java
+++ b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/openstack/utils/MsoHeatUtilsTest2.java
@@ -42,6 +42,7 @@
 import org.openecomp.mso.adapters.vdu.VduStateType;

 import org.openecomp.mso.adapters.vdu.VduStatus;

 import org.openecomp.mso.cloud.CloudConfig;

+import org.openecomp.mso.cloud.CloudConfigFactory;

 import org.openecomp.mso.cloud.CloudSite;

 import org.openecomp.mso.cloudify.beans.DeploymentInfo;

 import org.openecomp.mso.cloudify.beans.DeploymentStatus;

@@ -68,7 +69,9 @@
 		CloudSite site = new CloudSite();

 		Optional<CloudSite> opSite = Optional.ofNullable(site);

 		CloudConfig config = Mockito.mock(CloudConfig.class);

-		heatUtils.cloudConfig = config;

+		CloudConfigFactory cloudConfigFactory = Mockito.mock(CloudConfigFactory.class);

+		when(cloudConfigFactory.getCloudConfig()).thenReturn(config);

+		when(heatUtils.getCloudConfigFactory()).thenReturn(cloudConfigFactory);

 		CloudInfo cloudInfo = new CloudInfo();

 		cloudInfo.setCloudSiteId("cloudSiteId");

 		cloudInfo.setTenantId("tenantId");

diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtilsTest.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtilsTest.java
index 3e4758a..f2d14d8 100644
--- a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtilsTest.java
+++ b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtilsTest.java
@@ -29,9 +29,13 @@
 import java.util.HashMap;

 import java.util.Map;

 import org.junit.Assert;

+import org.junit.BeforeClass;

 import org.junit.Test;

 import org.junit.runner.RunWith;

 import org.mockito.Mock;

+import org.mockito.Mockito;

+import org.openecomp.mso.cloud.CloudConfig;

+import org.openecomp.mso.cloud.CloudConfigFactory;

 import org.openecomp.mso.cloud.CloudIdentity;

 import org.openecomp.mso.cloud.CloudSite;

 import org.openecomp.mso.openstack.beans.MsoTenant;

@@ -66,9 +70,19 @@
     @Mock

     MsoJavaProperties msoProps;

 

+    @Mock

+	private static CloudConfigFactory cloudConfigFactory;

+

+    @BeforeClass

+    public static final void prepare () {

+        cloudConfigFactory = Mockito.mock(CloudConfigFactory.class);

+        CloudConfig cloudConfig = Mockito.mock(CloudConfig.class);

+        Mockito.when(cloudConfigFactory.getCloudConfig()).thenReturn(cloudConfig);

+    }

+

     @Test

      public  void testcreateTenant() throws MsoException{

-        MsoKeystoneUtils msk = PowerMockito.spy(new MsoKeystoneUtils("ID"));

+        MsoKeystoneUtils msk = PowerMockito.spy(new MsoKeystoneUtils("ID", cloudConfigFactory));

         Map<String,String>metadata=new HashMap<>();

         metadata.put("1", "value");

         tenant = mock(Tenant.class);

@@ -81,19 +95,19 @@
     }

     @Test

     public  void testdeleteTenant() throws MsoException{

-        MsoKeystoneUtils msk = PowerMockito.spy(new MsoKeystoneUtils("ID"));

+        MsoKeystoneUtils msk = PowerMockito.spy(new MsoKeystoneUtils("ID", cloudConfigFactory));

         doReturn(true).when(msk).deleteTenant("tenantId", "cloudSiteId");

        assertTrue(msk.deleteTenant("tenantId", "cloudSiteId"));

     }

     @Test

     public  void testfindTenantByName() throws Exception{

-        MsoKeystoneUtils msk = PowerMockito.spy(new MsoKeystoneUtils("ID"));

+        MsoKeystoneUtils msk = PowerMockito.spy(new MsoKeystoneUtils("ID", cloudConfigFactory));

        doReturn(null).when(msk).findTenantByName(adminClient, "tenantName");

        assertNull(msk.findTenantByName(adminClient, "tenantName"));

     }

     @Test

     public  void testqueryTenant() throws MsoException{

-        MsoKeystoneUtils msk = PowerMockito.spy(new MsoKeystoneUtils("ID"));

+        MsoKeystoneUtils msk = PowerMockito.spy(new MsoKeystoneUtils("ID", cloudConfigFactory));

         Map<String,String>metadata=new HashMap<>();

         metadata.put("1", "value");  

         mst = mock(MsoTenant.class);

@@ -106,7 +120,7 @@
         

     @Test

     public  void testqueryTenantByName()throws MsoException {

-        MsoKeystoneUtils msk = PowerMockito.spy(new MsoKeystoneUtils("ID"));

+        MsoKeystoneUtils msk = PowerMockito.spy(new MsoKeystoneUtils("ID", cloudConfigFactory));

         Map<String,String>metadata=new HashMap<>();

         metadata.put("1", "value");  

         mst = mock(MsoTenant.class);

@@ -122,9 +136,8 @@
     public void testgetKeystoneAdminClient() throws MsoException{

     	cloudIdentity = mock(CloudIdentity.class);

         Keystone keystone = new Keystone (cloudIdentity.getKeystoneUrl ("region", "msoPropID"));

-        MsoKeystoneUtils msk = PowerMockito.spy(new MsoKeystoneUtils("ID"));

+        MsoKeystoneUtils msk = PowerMockito.spy(new MsoKeystoneUtils("ID", cloudConfigFactory));

         doReturn(keystone).when(msk).getKeystoneAdminClient(cs);

         assertNotNull(msk.getKeystoneAdminClient(cs));

     }

-    

-    }

+}
\ No newline at end of file
diff --git a/adapters/mso-network-adapter/src/main/java/org/openecomp/mso/adapters/network/MsoNetworkAdapterImpl.java b/adapters/mso-network-adapter/src/main/java/org/openecomp/mso/adapters/network/MsoNetworkAdapterImpl.java
index c0df4fe..7f0f988 100644
--- a/adapters/mso-network-adapter/src/main/java/org/openecomp/mso/adapters/network/MsoNetworkAdapterImpl.java
+++ b/adapters/mso-network-adapter/src/main/java/org/openecomp/mso/adapters/network/MsoNetworkAdapterImpl.java
@@ -91,7 +91,6 @@
     private static final String NEUTRON_MODE = "NEUTRON";
     private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
     private static MsoAlarmLogger alarmLogger = new MsoAlarmLogger ();
-    protected CloudConfig cloudConfig;
 
     /**
      * Health Check web method. Does nothing but return to show the adapter is deployed.
@@ -117,7 +116,6 @@
     public MsoNetworkAdapterImpl(MsoPropertiesFactory msoPropFactory,CloudConfigFactory cloudConfigFact) {
     	this.msoPropertiesFactory = msoPropFactory;
     	this.cloudConfigFactory=cloudConfigFact;
-    	cloudConfig = cloudConfigFactory.getCloudConfig ();
     	neutron = new MsoNeutronUtils(MSO_PROP_NETWORK_ADAPTER, cloudConfigFactory);
     	heat = new MsoHeatUtils(MSO_PROP_NETWORK_ADAPTER, msoPropertiesFactory, cloudConfigFactory);
     	heatWithUpdate = new MsoHeatUtilsWithUpdate(MSO_PROP_NETWORK_ADAPTER, msoPropertiesFactory,
@@ -282,7 +280,7 @@
         // If the tenant doesn’t exist, the Heat calls will fail anyway (when the HeatUtils try to obtain a token).
         // So this is just catching that error in a bit more obvious way up front.
 
-        cloudConfig = cloudConfigFactory.getCloudConfig ();
+        CloudConfig cloudConfig = getCloudConfigFactory().getCloudConfig ();
         Optional<CloudSite> cloudSiteOpt = cloudConfig.getCloudSite(cloudSiteId);
         if (!cloudSiteOpt.isPresent())
         {
@@ -829,7 +827,7 @@
         networkRollback.setTenantId (tenantId);
         networkRollback.setMsoRequest (msoRequest);
 
-        cloudConfig = cloudConfigFactory.getCloudConfig ();
+        CloudConfig cloudConfig = getCloudConfigFactory().getCloudConfig ();
         Optional<CloudSite> cloudSiteOpt = cloudConfig.getCloudSite (cloudSiteId);
         if (!cloudSiteOpt.isPresent()) {
         	   String error = "UpdateNetwork: Configuration Error. Stack " + networkName + " in "
@@ -1384,7 +1382,7 @@
             throw new NetworkException (error, MsoExceptionCategory.USERDATA);
         }
 
-        cloudConfig = cloudConfigFactory.getCloudConfig();
+        CloudConfig cloudConfig = getCloudConfigFactory().getCloudConfig();
         Optional<CloudSite> cloudSiteOpt = cloudConfig.getCloudSite(cloudSiteId);
         if (!cloudSiteOpt.isPresent())
         {
@@ -1646,6 +1644,10 @@
         return CatalogDatabase.getInstance();
     }
 
+    public CloudConfigFactory getCloudConfigFactory() {
+        return cloudConfigFactory;
+    }
+
     /**
      * This web service endpoint will rollback a previous Create VNF operation.
      * A rollback object is returned to the client in a successful creation
@@ -1703,7 +1705,7 @@
                 // Rolling back a newly created network, so delete it.
                 if (NEUTRON_MODE.equals(mode)) {
                     // Use MsoNeutronUtils for all NEUTRON commands
-                    MsoNeutronUtils neutron = new MsoNeutronUtils(MSO_PROP_NETWORK_ADAPTER, cloudConfigFactory);
+                    MsoNeutronUtils neutron = new MsoNeutronUtils(MSO_PROP_NETWORK_ADAPTER, getCloudConfigFactory());
                     long deleteNetworkStarttime = System.currentTimeMillis();
                     try {
                         // The deleteNetwork function in MsoNeutronUtils returns success if the network
@@ -1734,7 +1736,7 @@
                 } else { // DEFAULT to if ("HEAT".equals (mode))
                     // Use MsoHeatUtils for all HEAT commands
                     MsoHeatUtils heat = new MsoHeatUtils(MSO_PROP_NETWORK_ADAPTER, msoPropertiesFactory,
-                        cloudConfigFactory);
+                        getCloudConfigFactory());
                     long deleteStackStarttime = System.currentTimeMillis();
                     try {
                         // The deleteStack function in MsoHeatUtils returns success if the stack
diff --git a/adapters/mso-network-adapter/src/test/java/org/openecomp/mso/adapters/network/NetworkAdapterTest.java b/adapters/mso-network-adapter/src/test/java/org/openecomp/mso/adapters/network/NetworkAdapterTest.java
index 1106c55..417ea80 100644
--- a/adapters/mso-network-adapter/src/test/java/org/openecomp/mso/adapters/network/NetworkAdapterTest.java
+++ b/adapters/mso-network-adapter/src/test/java/org/openecomp/mso/adapters/network/NetworkAdapterTest.java
@@ -34,6 +34,8 @@
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.openecomp.mso.adapters.network.exceptions.NetworkException;
+import org.openecomp.mso.cloud.CloudConfig;
+import org.openecomp.mso.cloud.CloudConfigFactory;
 import org.openecomp.mso.db.catalog.CatalogDatabase;
 import org.openecomp.mso.db.catalog.beans.NetworkResource;
 import org.openecomp.mso.db.catalog.beans.NetworkResourceCustomization;
@@ -50,11 +52,15 @@
 
     @Mock
     private static CatalogDatabase db;
+    
+    @Mock
+	private static CloudConfigFactory cloudConfigFactory;
 
     @BeforeClass
     public static final void prepare () {
         adapter = Mockito.spy (new MsoNetworkAdapterImpl ());
         db = Mockito.mock (CatalogDatabase.class);
+        cloudConfigFactory = Mockito.mock(CloudConfigFactory.class);
         NetworkResource networkResource = new NetworkResource ();
         NetworkResourceCustomization nrc = new NetworkResourceCustomization();
         nrc.setNetworkResource(networkResource);
@@ -65,6 +71,9 @@
         networkResource.setOrchestrationMode ("toto");
         Mockito.when (db.getNetworkResource ("PROVIDER")).thenReturn (networkResource);
         Mockito.when (adapter.getCatalogDB ()).thenReturn (db);
+        CloudConfig cloudConfig = Mockito.mock(CloudConfig.class);
+        Mockito.when(cloudConfigFactory.getCloudConfig()).thenReturn(cloudConfig);
+        Mockito.when (adapter.getCloudConfigFactory()).thenReturn(cloudConfigFactory);
     }
 
     @Test
diff --git a/adapters/mso-tenant-adapter/src/main/java/org/openecomp/mso/adapters/tenant/MsoTenantAdapterImpl.java b/adapters/mso-tenant-adapter/src/main/java/org/openecomp/mso/adapters/tenant/MsoTenantAdapterImpl.java
index 58169f6..85d50ae 100644
--- a/adapters/mso-tenant-adapter/src/main/java/org/openecomp/mso/adapters/tenant/MsoTenantAdapterImpl.java
+++ b/adapters/mso-tenant-adapter/src/main/java/org/openecomp/mso/adapters/tenant/MsoTenantAdapterImpl.java
@@ -58,6 +58,11 @@
     WebServiceContext wsContext;
 
     private static MsoLogger logger = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
+
+    protected MsoTenantUtilsFactory getTenantUtilsFactory() {
+    	return tFactory;
+    }
+
     /**
      * Health Check web method. Does nothing but return to show the adapter is deployed.
      */
diff --git a/adapters/mso-tenant-adapter/src/test/java/org/openecomp/mso/adapters/tenant/MsoTenantAdapterImplTest.java b/adapters/mso-tenant-adapter/src/test/java/org/openecomp/mso/adapters/tenant/MsoTenantAdapterImplTest.java
index e60a143..821c114 100644
--- a/adapters/mso-tenant-adapter/src/test/java/org/openecomp/mso/adapters/tenant/MsoTenantAdapterImplTest.java
+++ b/adapters/mso-tenant-adapter/src/test/java/org/openecomp/mso/adapters/tenant/MsoTenantAdapterImplTest.java
@@ -19,8 +19,13 @@
  */
 package org.openecomp.mso.adapters.tenant;
 
+import org.junit.BeforeClass;
 import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
 import org.openecomp.mso.adapters.tenantrest.TenantRollback;
+import org.openecomp.mso.cloud.CloudConfig;
+import org.openecomp.mso.cloud.CloudConfigFactory;
 import org.openecomp.mso.entity.MsoRequest;
 
 import javax.validation.constraints.Null;
@@ -28,11 +33,24 @@
 import java.util.HashMap;
 
 public class MsoTenantAdapterImplTest {
+	
+    @Mock
+    private static MsoTenantAdapterImpl msoTenantAdapter;
 
+    @Mock
+	private static CloudConfigFactory cloudConfigFactory;
+    
     // TODO: following test case is done for coverage
     // later it should be modified for proper test.
-
-    MsoTenantAdapterImpl msoTenantAdapter = new MsoTenantAdapterImpl();
+    
+    @BeforeClass
+    public static final void prepare () {
+        msoTenantAdapter = Mockito.spy (new MsoTenantAdapterImpl());
+        cloudConfigFactory = Mockito.mock(CloudConfigFactory.class);
+        CloudConfig cloudConfig = Mockito.mock(CloudConfig.class);
+        Mockito.when(cloudConfigFactory.getCloudConfig()).thenReturn(cloudConfig);
+        msoTenantAdapter.getTenantUtilsFactory().setCloudConfigFactory(cloudConfigFactory);
+    }
 
     @Test
     public void healthCheck() throws Exception {
diff --git a/adapters/mso-vnf-adapter/src/main/java/org/openecomp/mso/adapters/vnf/MsoVnfCloudifyAdapterImpl.java b/adapters/mso-vnf-adapter/src/main/java/org/openecomp/mso/adapters/vnf/MsoVnfCloudifyAdapterImpl.java
index 1cd2c16..43021d2 100644
--- a/adapters/mso-vnf-adapter/src/main/java/org/openecomp/mso/adapters/vnf/MsoVnfCloudifyAdapterImpl.java
+++ b/adapters/mso-vnf-adapter/src/main/java/org/openecomp/mso/adapters/vnf/MsoVnfCloudifyAdapterImpl.java
@@ -33,7 +33,6 @@
 
 import org.openecomp.mso.adapters.vnf.exceptions.VnfAlreadyExists;
 import org.openecomp.mso.adapters.vnf.exceptions.VnfException;
-import org.openecomp.mso.cloud.CloudConfig;
 import org.openecomp.mso.cloud.CloudConfigFactory;
 import org.openecomp.mso.cloud.CloudSite;
 import org.openecomp.mso.cloudify.beans.DeploymentInfo;
@@ -72,7 +71,6 @@
 public class MsoVnfCloudifyAdapterImpl implements MsoVnfAdapter {
 
 	CloudConfigFactory cloudConfigFactory = new CloudConfigFactory();
-	protected CloudConfig cloudConfig = cloudConfigFactory.getCloudConfig();
 
 	MsoPropertiesFactory msoPropertiesFactory=new MsoPropertiesFactory();
 
@@ -310,7 +308,7 @@
         try {
         	// KLUDGE - Cloudify requires Tenant Name for Openstack.  We have the ID.
         	//          Go directly to Keystone until APIs could be updated to supply the name.
-        	MsoKeystoneUtils keystone = new MsoKeystoneUtils(MSO_PROP_VNF_ADAPTER);
+        	MsoKeystoneUtils keystone = new MsoKeystoneUtils(MSO_PROP_VNF_ADAPTER, cloudConfigFactory);
         	MsoTenant msoTenant = keystone.queryTenant(tenantId, cloudSiteId);
         	String tenantName = (msoTenant != null? msoTenant.getTenantName() : tenantId);
         	
@@ -655,7 +653,7 @@
 
         //  Perform a version check against cloudSite
         // Obtain the cloud site information where we will create the VF Module
-        Optional<CloudSite> cloudSite = cloudConfig.getCloudSite (cloudSiteId);
+        Optional<CloudSite> cloudSite = cloudConfigFactory.getCloudConfig().getCloudSite(cloudSiteId);
         if (!cloudSite.isPresent()) {
             throw new VnfException (new MsoCloudSiteNotFound (cloudSiteId));
         }
@@ -1117,7 +1115,7 @@
             try {
             	// KLUDGE - Cloudify requires Tenant Name for Openstack.  We have the ID.
             	//          Go directly to Keystone until APIs could be updated to supply the name.
-            	MsoKeystoneUtils keystone = new MsoKeystoneUtils(MSO_PROP_VNF_ADAPTER);
+            	MsoKeystoneUtils keystone = new MsoKeystoneUtils(MSO_PROP_VNF_ADAPTER, cloudConfigFactory);
             	MsoTenant msoTenant = keystone.queryTenant(tenantId, cloudSiteId);
             	String tenantName = (msoTenant != null? msoTenant.getTenantName() : tenantId);