add junit for MsoHeatEnvironmentEntry 53/30253/5
authorLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Fri, 2 Feb 2018 17:06:01 +0000 (18:06 +0100)
committerLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Wed, 7 Feb 2018 15:51:31 +0000 (16:51 +0100)
Change-Id: Ibc2b9562de8fbfdb60ee09add404bd3923431501
Issue-ID: SO-360
Signed-off-by: Lukasz Muszkieta <lukasz.muszkieta@nokia.com>
adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoHeatEnvironmentEntry.java
adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/openstack/utils/MsoHeatEnvironmentEntryTest.java [new file with mode: 0644]
adapters/mso-vnf-adapter/src/main/java/org/openecomp/mso/adapters/vnf/MsoVnfAdapterImpl.java

index 3aa77c2..69da437 100644 (file)
 
 package org.openecomp.mso.openstack.utils;
 
-
-
-import java.util.HashSet;
-import java.util.ArrayList;
 import java.util.Set;
-import org.openecomp.mso.db.catalog.beans.HeatTemplateParam;
 import org.openecomp.mso.logger.MsoLogger;
 
 public class MsoHeatEnvironmentEntry {
 
-    private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
-    
-       private Set<MsoHeatEnvironmentParameter> parameters = null;
-       private Set<MsoHeatEnvironmentResource> resources = null;
-       private StringBuilder rawEntry = null;
+       private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
+       private Set<MsoHeatEnvironmentParameter> parameters;
+       private String rawEntry;
        private boolean valid = true;
-       private String errorString = null;
-       private StringBuilder resourceRegistryEntryRaw = null;
-       
-       public MsoHeatEnvironmentEntry() {
-               super();
-       }
-       
-       public MsoHeatEnvironmentEntry(StringBuilder sb) {
-               this();
-               this.rawEntry = sb;
-               this.processRawEntry();
+       private String errorString;
+       private String resourceRegistryEntryRaw;
+
+       private MsoHeatEnvironmentEntry(String rawEntry) {
+               this.rawEntry = rawEntry;
        }
-       
-       private void processRawEntry() {
-               try {
-                       if (this.rawEntry == null || "".equals(this.rawEntry))
-                               return;
-                       byte[] b = this.rawEntry.toString().getBytes();
-                       MsoYamlEditorWithEnvt yaml = new MsoYamlEditorWithEnvt(b);
-                       this.parameters = yaml.getParameterListFromEnvt();
-                       //this.resources = yaml.getResourceListFromEnvt();
-                       StringBuilder sb = this.getResourceRegistryRawEntry();
-                       if (sb == null) {
-                               this.resourceRegistryEntryRaw = new StringBuilder("");
-                       } else {
-                               this.resourceRegistryEntryRaw = sb;
-                       }
-               } catch (Exception e) {
-                   LOGGER.debug("Exception:", e);
-                       this.valid = false;
-                       this.errorString = e.getMessage();
-                       //e.printStackTrace();
-               }
+
+       private MsoHeatEnvironmentEntry(Set<MsoHeatEnvironmentParameter> parameters, String rawEntry, boolean valid,
+                       String errorString, String resourceRegistryEntryRaw) {
+               this.parameters = parameters;
+               this.rawEntry = rawEntry;
+               this.valid = valid;
+               this.errorString = errorString;
+               this.resourceRegistryEntryRaw = resourceRegistryEntryRaw;
        }
-       
+
        public boolean isValid() {
                return this.valid;
        }
        public String getErrorString() {
                return this.errorString;
        }
-       
-       public Set<MsoHeatEnvironmentParameter> getParameters() {
-               return this.parameters;
-       }
-       public Set<MsoHeatEnvironmentResource> getResources() {
-               return this.resources;
-       }
-       public void setParameters(Set<MsoHeatEnvironmentParameter> paramSet) {
-               if (paramSet == null) {
-                       this.parameters = null;
-               } else {
-                       this.parameters = paramSet;
-               }
-       }
-       public void setResources(Set<MsoHeatEnvironmentResource> resourceSet) {
-               if (resourceSet == null) {
-                       this.resources = null;
-               } else {
-                       this.resources = resourceSet;
-               }
-       }
-       
-       public void addParameter(MsoHeatEnvironmentParameter hep) {
-               if (this.parameters == null) {
-                       this.parameters = new HashSet<>();
-               }
-               this.parameters.add(hep);
-       }
-       public void addResource(MsoHeatEnvironmentResource her) {
-               if (this.resources == null) {
-                       this.resources = new HashSet<>();
-               }
-               this.resources.add(her);
-       }
-       
-       public int getNumberOfParameters() {
-               return this.parameters.size();
-       }
-       public int getNumberOfResources() {
-               return this.resources.size();
-       }
-       
-       public boolean hasResources() {
-               if (this.resources != null && this.resources.size() > 0) {
-                       return true;
-               } 
-               return false;
-       }
-       public boolean hasParameters() {
-               if (this.parameters != null && this.parameters.size() > 0) {
-                       return true;
-               }
-               return false;
-       }
-       
+
        public boolean containsParameter(String paramName) {
-               boolean contains = false;
                if (this.parameters == null || this.parameters.size() < 1) {
                        return false;
                }
                if (this.parameters.contains(new MsoHeatEnvironmentParameter(paramName))) {
-                       contains = true;
-               }
-               return contains;
-       }
-       
-       public boolean containsParameter(String paramName, String paramAlias) {
-               if (this.containsParameter(paramName)) {
-                       return true;
-               }
-               if (this.containsParameter(paramAlias)) {
                        return true;
                }
                return false;
        }
-       
-       public StringBuilder toFullStringExcludeNonParams(Set<HeatTemplateParam> params) {
-               // Basically give back the envt - but exclude the params that aren't in the HeatTemplate 
-               
-               StringBuilder sb = new StringBuilder();
-               ArrayList<String> paramNameList = new ArrayList<String>(params.size());
-               for (HeatTemplateParam htp : params) {
-                       paramNameList.add(htp.getParamName());
-               }
-               
-               if (this.hasParameters()) {
-                       sb.append("parameters:\n");
-                       for (MsoHeatEnvironmentParameter hep : this.parameters) {
-                               String paramName = hep.getName();
-                               if (paramNameList.contains(paramName)) {
-                                       // This parameter *is* in the Heat Template - so include it:
-                                       sb.append("  " + hep.getName() + ": " + hep.getValue() + "\n");
-                                       // New - 1607 - if any of the params mapped badly - JUST RETURN THE ORIGINAL ENVT!
-                                       if (hep.getValue().startsWith("_BAD")) {
-                                               return this.rawEntry;
-                                       }
-                               } 
-                       }
-                       sb.append("\n");
-               }
-//             if (this.hasResources()) {
-//                     sb.append("resource_registry:\n");
-//                     for (MsoHeatEnvironmentResource her : this.resources) {
-//                             sb.append("   \"" + her.getName() + "\": " + her.getValue() + "\n");
-//                     }
-//             }
-               sb.append("\n");
-               sb.append(this.resourceRegistryEntryRaw);                               
-               return sb;
+
+       @Override
+       public String toString() {
+               final StringBuilder sb = new StringBuilder("MsoHeatEnvironmentEntry{");
+               sb.append("parameters=").append(parameters);
+               sb.append(", resourceRegistryEntryRaw='").append(resourceRegistryEntryRaw).append('\'');
+               sb.append('}');
+               return sb.toString();
+       }
+
+       public String getRawEntry() {
+               return rawEntry;
        }
        
-       public StringBuilder toFullString() {
-               StringBuilder sb = new StringBuilder();
-               
-               if (this.hasParameters()) {
-                       sb.append("parameters:\n");
-                       for (MsoHeatEnvironmentParameter hep : this.parameters) {
-                               sb.append("   " + hep.getName() + ":  " + hep.getValue() + "\n");
-                       }
-                       sb.append("\n");
+       private static String getResourceRegistryRawEntry(String rawEntry) {
+               int indexOf = rawEntry.indexOf("resource_registry:");
+               if (indexOf < 0) {
+                       return "";
                }
-//             if (this.hasResources()) {
-//                     sb.append("resource_registry:\n");
-//                     for (MsoHeatEnvironmentResource her : this.resources) {
-//                             sb.append("   \"" + her.getName() + "\": " + her.getValue() + "\n");
-//                     }
-//             }
-               sb.append("\n");
-               sb.append(this.resourceRegistryEntryRaw);                       
-               return sb;
+               return rawEntry.substring(indexOf);
        }
 
-       public StringBuilder getRawEntry() {
-               return this.rawEntry;
-       }
-       
-       private StringBuilder getResourceRegistryRawEntry() {
-               
-               if (this.rawEntry == null) {
-                       return null;
+       public static MsoHeatEnvironmentEntry create(String rawEntry) {
+               if (rawEntry == null || rawEntry.isEmpty()) {
+                       return new MsoHeatEnvironmentEntry(rawEntry);
                }
-               
-               StringBuilder sb = new StringBuilder();
-               int indexOf = this.rawEntry.indexOf("resource_registry:");
-               if (indexOf < 0) { // no resource_registry:
-                       return null;
+               try {
+                       Set<MsoHeatEnvironmentParameter> parameters = new MsoYamlEditorWithEnvt(rawEntry.getBytes())
+                                       .getParameterListFromEnvt();
+                       return new MsoHeatEnvironmentEntry(parameters, rawEntry, true, null,
+                                       getResourceRegistryRawEntry(rawEntry));
+               } catch (Exception e) {
+                       LOGGER.debug(String.format("An exception occurred during processing the following raw entry: %s", rawEntry),
+                                       e);
+                       return new MsoHeatEnvironmentEntry(null, rawEntry, false, e.getMessage(), null);
                }
-               sb.append(this.rawEntry.substring(indexOf));
-               return sb;
        }
-       
+
 }
diff --git a/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/openstack/utils/MsoHeatEnvironmentEntryTest.java b/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/openstack/utils/MsoHeatEnvironmentEntryTest.java
new file mode 100644 (file)
index 0000000..60faa76
--- /dev/null
@@ -0,0 +1,79 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.mso.openstack.utils;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.Test;
+
+public class MsoHeatEnvironmentEntryTest {
+
+    private static final String PARAMETER_NAME = "keyTest";
+    private static final String VALUE_NAME = "valueTest";
+    private static final String NOT_EXISTING_PARAM = "notExistingParam";
+    private static final String RAW_ENTRY_WITH_NO_RESOURCE_REGISTRY = "parameters: {"
+            + PARAMETER_NAME + ": " + VALUE_NAME + "}";
+    private static final String RAW_ENTRY_WITH_RESOURCE_REGISTRY = "resource_registry: resourceTest";
+    private static final String RAW_ENTRY_INVALID = "invalidRawEntry";
+    private static final String WHITESPACE = " ";
+
+    @Test
+    public void createObjectWithNullStringBuilder() {
+        MsoHeatEnvironmentEntry testedObject = MsoHeatEnvironmentEntry.create(null);
+        assertThat(testedObject.getRawEntry()).isNull();
+        assertThat(testedObject.containsParameter(PARAMETER_NAME)).isFalse();
+        assertThat(testedObject.isValid()).isTrue();
+    }
+
+    @Test
+    public void toFullString_ResourceRegistryNotPresentInRawEntry() {
+        MsoHeatEnvironmentEntry testedObject = MsoHeatEnvironmentEntry.create(RAW_ENTRY_WITH_NO_RESOURCE_REGISTRY);
+        assertThat(testedObject.getRawEntry()).isEqualTo(RAW_ENTRY_WITH_NO_RESOURCE_REGISTRY);
+        assertThat(testedObject.isValid()).isTrue();
+        assertThat(testedObject.containsParameter(PARAMETER_NAME)).isTrue();
+        assertThat(testedObject.toString()).contains(PARAMETER_NAME).contains(VALUE_NAME);
+    }
+
+    @Test
+    public void toFullString_ResourceRegistryPresentInRawEntry() {
+        MsoHeatEnvironmentEntry testedObject = MsoHeatEnvironmentEntry.create(RAW_ENTRY_WITH_RESOURCE_REGISTRY);
+        assertThat(testedObject.getRawEntry()).isEqualTo(RAW_ENTRY_WITH_RESOURCE_REGISTRY);
+        assertThat(testedObject.containsParameter(PARAMETER_NAME)).isFalse();
+        assertThat(testedObject.isValid()).isTrue();
+        assertThat(testedObject.toString()).contains(RAW_ENTRY_WITH_RESOURCE_REGISTRY);
+    }
+
+    @Test
+    public void toFullString_ExceptionOccurred() {
+        MsoHeatEnvironmentEntry testedObject = MsoHeatEnvironmentEntry.create(RAW_ENTRY_INVALID);
+        assertThat(testedObject.getRawEntry()).isEqualTo(RAW_ENTRY_INVALID);
+        assertThat(testedObject.isValid()).isFalse();
+        assertThat(testedObject.getErrorString()).isNotNull().isNotEmpty();
+    }
+
+    @Test
+    public void checkIfContainsTheParameter() {
+        MsoHeatEnvironmentEntry testedObject = MsoHeatEnvironmentEntry.create(RAW_ENTRY_WITH_NO_RESOURCE_REGISTRY);
+        assertThat(testedObject.containsParameter(PARAMETER_NAME)).isTrue();
+        assertThat(testedObject.containsParameter(NOT_EXISTING_PARAM)).isFalse();
+    }
+
+}
index fb86b8c..f5a47bc 100644 (file)
@@ -1200,9 +1200,7 @@ public class MsoVnfAdapterImpl implements MsoVnfAdapter {
             if (heatEnvironmentString != null && heatEnvironmentString.contains ("parameters:")) {
                 //LOGGER.debug ("Have an Environment argument with a parameters: section - will bypass checking for valid params - but will still check for aliases");
                 LOGGER.debug("Enhanced environment checking enabled - 1604");
-                StringBuilder sb = new StringBuilder(heatEnvironmentString);
-                //LOGGER.debug("About to create MHEE with " + sb);
-                mhee = new MsoHeatEnvironmentEntry(sb);
+                mhee = MsoHeatEnvironmentEntry.create(heatEnvironmentString);
                 StringBuilder sb2 = new StringBuilder("\nHeat Template Parameters:\n");
                 for (HeatTemplateParam parm : heatTemplate.getParameters()) {
                     sb2.append("\t" + parm.getParamName() + ", required=" + parm.isRequired());
@@ -1211,7 +1209,7 @@ public class MsoVnfAdapterImpl implements MsoVnfAdapter {
                     sb2.append("Environment says it's not valid! " + mhee.getErrorString());
                 } else {
                     sb2.append("\nEnvironment:");
-                    sb2.append(mhee.toFullString());
+                    sb2.append(mhee);
                 }
                 LOGGER.debug(sb2.toString());
             } else {
@@ -1292,7 +1290,7 @@ public class MsoVnfAdapterImpl implements MsoVnfAdapter {
             // and it causes problems with json that has arrays
             String newEnvironmentString = null;
             if (mhee != null) {
-                newEnvironmentString = mhee.getRawEntry().toString();
+                newEnvironmentString = mhee.getRawEntry();
             }
 
             // "Fix" the template if it has CR/LF (getting this from Oracle)
@@ -1904,10 +1902,7 @@ public class MsoVnfAdapterImpl implements MsoVnfAdapter {
             MsoHeatEnvironmentEntry mhee = null;
             if (heatEnvironmentString != null && heatEnvironmentString.toLowerCase ().contains ("parameters:")) {
                 LOGGER.debug("Enhanced environment checking enabled - 1604");
-                haveEnvironmentParameters = true;
-                StringBuilder sb = new StringBuilder(heatEnvironmentString);
-                //LOGGER.debug("About to create MHEE with " + sb);
-                mhee = new MsoHeatEnvironmentEntry(sb);
+                mhee = MsoHeatEnvironmentEntry.create(heatEnvironmentString);
                 StringBuilder sb2 = new StringBuilder("\nHeat Template Parameters:\n");
                 for (HeatTemplateParam parm : heatTemplate.getParameters()) {
                     sb2.append("\t" + parm.getParamName() + ", required=" + parm.isRequired());
@@ -1916,7 +1911,7 @@ public class MsoVnfAdapterImpl implements MsoVnfAdapter {
                     sb2.append("Environment says it's not valid! " + mhee.getErrorString());
                 } else {
                     sb2.append("\nEnvironment:");
-                    sb2.append(mhee.toFullString());
+                    sb2.append(mhee);
                 }
                 LOGGER.debug(sb2.toString());
             } else {
@@ -2041,7 +2036,7 @@ public class MsoVnfAdapterImpl implements MsoVnfAdapter {
             // Just submit the envt entry as received from the database
             String newEnvironmentString = null;
             if (mhee != null) {
-                newEnvironmentString = mhee.getRawEntry().toString();
+                newEnvironmentString = mhee.getRawEntry();
             }
 
             // Remove any extraneous parameters (don't throw an error)