Add DataNodeNotFoundException to deleteDataNodes
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsDataPersistenceServiceImpl.java
index 2159cea..f634008 100644 (file)
@@ -57,6 +57,7 @@ import org.onap.cps.spi.exceptions.ConcurrencyException;
 import org.onap.cps.spi.exceptions.CpsAdminException;
 import org.onap.cps.spi.exceptions.CpsPathException;
 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
+import org.onap.cps.spi.exceptions.DataNodeNotFoundExceptionBatch;
 import org.onap.cps.spi.model.DataNode;
 import org.onap.cps.spi.model.DataNodeBuilder;
 import org.onap.cps.spi.repository.AnchorRepository;
@@ -621,23 +622,30 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
 
-        final Collection<String> normalizedXPathsToDelete = new ArrayList<>(xpathsToDelete.size());
-        final Collection<String> normalizedXpathsToPotentialLists = new ArrayList<>();
+        final Collection<String> deleteChecklist = new HashSet<>(xpathsToDelete.size());
         for (final String xpath : xpathsToDelete) {
             try {
-                final CpsPathQuery cpsPathQuery = CpsPathUtil.getCpsPathQuery(xpath);
-                final String normalizedXpath = cpsPathQuery.getNormalizedXpath();
-                normalizedXPathsToDelete.add(normalizedXpath);
-                if (!cpsPathQuery.isPathToListElement()) {
-                    normalizedXpathsToPotentialLists.add(normalizedXpath);
-                }
+                deleteChecklist.add(CpsPathUtil.getNormalizedXpath(xpath));
             } catch (final PathParsingException e) {
                 log.debug("Error parsing xpath \"{}\": {}", xpath, e.getMessage());
             }
         }
 
-        fragmentRepository.deleteByAnchorIdAndXpaths(anchorEntity.getId(), normalizedXPathsToDelete);
-        fragmentRepository.deleteListsByAnchorIdAndXpaths(anchorEntity.getId(), normalizedXpathsToPotentialLists);
+        final Collection<String> xpathsToExistingContainers =
+            fragmentRepository.findAllXpathByAnchorAndXpathIn(anchorEntity, deleteChecklist);
+        deleteChecklist.removeAll(xpathsToExistingContainers);
+
+        final Collection<String> xpathsToExistingLists = deleteChecklist.stream()
+            .filter(xpath -> fragmentRepository.existsByAnchorAndXpathStartsWith(anchorEntity, xpath + "["))
+            .collect(Collectors.toList());
+        deleteChecklist.removeAll(xpathsToExistingLists);
+
+        if (!deleteChecklist.isEmpty()) {
+            throw new DataNodeNotFoundExceptionBatch(dataspaceName, anchorName, deleteChecklist);
+        }
+
+        fragmentRepository.deleteByAnchorIdAndXpaths(anchorEntity.getId(), xpathsToExistingContainers);
+        fragmentRepository.deleteListsByAnchorIdAndXpaths(anchorEntity.getId(), xpathsToExistingLists);
     }
 
     @Override