Heal version created based on old-unhealed version

Change-Id: I4d50b349988e8e31f46cc9727f84fced84876fea
Issue-ID: SDC-1366
Signed-off-by: talig <talig@amdocs.com>
diff --git a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-core/src/main/java/org/openecomp/sdc/healing/impl/HealingManagerImpl.java b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-core/src/main/java/org/openecomp/sdc/healing/impl/HealingManagerImpl.java
index eb3fd91..e51f427 100644
--- a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-core/src/main/java/org/openecomp/sdc/healing/impl/HealingManagerImpl.java
+++ b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-core/src/main/java/org/openecomp/sdc/healing/impl/HealingManagerImpl.java
@@ -67,7 +67,9 @@
   public Optional<Version> healItemVersion(final String itemId, final Version version,
                                            final ItemType itemType, final boolean force) {
     String user = getUser();
-    if (!force && !isPrivateHealingNeededByFlags(itemId, version.getId(), user)) {
+    populateVersionInfo(itemId, version);
+
+    if (!isHealingNeeded(itemId, version, force, user)) {
       return Optional.empty();
     }
 
@@ -102,8 +104,26 @@
     return healVersion;
   }
 
+  private void populateVersionInfo(String itemId, Version version) {
+    if (version.getStatus() != null) {
+      return;
+    }
+
+    Version retrievedVersion = versioningManager.get(itemId, version);
+    version.setStatus(retrievedVersion.getStatus());
+    version.setBaseId(retrievedVersion.getBaseId());
+  }
+
+  private boolean isHealingNeeded(String itemId, Version version, boolean force, String user) {
+    return force || isHealingFlagOn(itemId, version.getId(), user)
+        .orElse(isHealingFlagOn(itemId, version.getId(), PUBLIC_USER)
+            .orElse(version.getStatus() == VersionStatus.Draft && version.getBaseId() != null &&
+                isHealingFlagOn(itemId, version.getBaseId(), user)
+                    .orElse(isHealingFlagOn(itemId, version.getBaseId(), PUBLIC_USER)
+                        .orElse(false))));
+  }
+
   private Optional<Version> getHealVersion(String itemId, Version version) {
-    version.setStatus(versioningManager.get(itemId, version).getStatus());
     return version.getStatus() == VersionStatus.Certified
         ? createNewVersion(itemId, version.getId())
         : Optional.of(version);
@@ -124,9 +144,12 @@
                               List<Healer> structureHealersToRun,
                               List<Healer> dataHealersToRun, String user,
                               boolean force) {
+    boolean publicFlagOn = isHealingFlagOn(itemId, origVersion.getId(), PUBLIC_USER)
+        .orElse(origVersion.getBaseId() != null &&
+            isHealingFlagOn(itemId, origVersion.getBaseId(), PUBLIC_USER).orElse(false));
+
     List<String> failureMessages =
-        force || origVersion.getStatus() == VersionStatus.Certified ||
-            isPublicHealingNeededByFlag(itemId, origVersion.getId())
+        force || origVersion.getStatus() == VersionStatus.Certified || publicFlagOn
             ? healPublic(itemId, version, origVersion, structureHealersToRun, dataHealersToRun,
             user)
             : new LinkedList<>();
@@ -203,17 +226,7 @@
     return failureMessages;
   }
 
-  private boolean isPrivateHealingNeededByFlags(String itemId, String version, String user) {
-    Optional<Boolean> userHealingFlag = getHealingFlag(itemId, version, user);
-    return userHealingFlag.orElseGet(() -> isPublicHealingNeededByFlag(itemId, version));
-  }
-
-  private boolean isPublicHealingNeededByFlag(String itemId, String versionId) {
-    Optional<Boolean> publicHealingFlag = getHealingFlag(itemId, versionId, PUBLIC_USER);
-    return publicHealingFlag.isPresent() && publicHealingFlag.get();
-  }
-
-  private Optional<Boolean> getHealingFlag(String itemId, String version, String user) {
+  private Optional<Boolean> isHealingFlagOn(String itemId, String version, String user) {
     return healingDao.getItemHealingFlag(user, itemId, version);
   }
 
@@ -232,13 +245,13 @@
   private List<Healer> getHealersToRun(Collection<String> healersClassNames, String itemId,
                                        Version version, List<String> failureMessages) {
     return healersClassNames == null
-                   ? Collections.emptyList()
-                   : healersClassNames.stream()
-                      .map(healerClassName -> getHealerInstance(healerClassName, failureMessages))
-                      .filter(Optional::isPresent)
-                      .map(Optional::get)
-                      .filter(healer -> healer.isHealingNeeded(itemId, version))
-                      .collect(Collectors.toList());
+        ? Collections.emptyList()
+        : healersClassNames.stream()
+            .map(healerClassName -> getHealerInstance(healerClassName, failureMessages))
+            .filter(Optional::isPresent)
+            .map(Optional::get)
+            .filter(healer -> healer.isHealingNeeded(itemId, version))
+            .collect(Collectors.toList());
   }
 
   private Optional<Healer> getHealerInstance(String healerClassName, List<String> failureMessages) {
@@ -255,7 +268,8 @@
     Map healingConfig = FileUtils
         .readViaInputStream(HEALERS_BY_ENTITY_TYPE_FILE,
             stream -> JsonUtil.json2Object(stream, Map.class));
-    return (Map<String, Collection<String>>) healingConfig.getOrDefault(itemType.name(), Collections.emptyMap());
+    return (Map<String, Collection<String>>) healingConfig
+        .getOrDefault(itemType.name(), Collections.emptyMap());
   }
 
   private String getUser() {