Refactor Delete Anchor functionality
- Added a delete DataNode functionality in the CpsDataService
- CpsAdminService uses CpsDataService to delete DataNodes
Issue-ID: CPS-791
Signed-off-by: Renu Kumari <renu.kumari@bell.ca>
Change-Id: I090500dbc4a2ccf061dc105f979472137d43b06d
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java
index 51b2482..5b89d9f 100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java
@@ -24,9 +24,9 @@
import java.util.Collection;
import java.util.List;
-import java.util.Set;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
+import lombok.AllArgsConstructor;
import org.onap.cps.spi.CpsAdminPersistenceService;
import org.onap.cps.spi.entities.AnchorEntity;
import org.onap.cps.spi.entities.DataspaceEntity;
@@ -38,30 +38,19 @@
import org.onap.cps.spi.model.Anchor;
import org.onap.cps.spi.repository.AnchorRepository;
import org.onap.cps.spi.repository.DataspaceRepository;
-import org.onap.cps.spi.repository.FragmentRepository;
import org.onap.cps.spi.repository.SchemaSetRepository;
import org.onap.cps.spi.repository.YangResourceRepository;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Component;
@Component
+@AllArgsConstructor
public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
- @Autowired
- private DataspaceRepository dataspaceRepository;
-
- @Autowired
- private AnchorRepository anchorRepository;
-
- @Autowired
- private SchemaSetRepository schemaSetRepository;
-
- @Autowired
- private FragmentRepository fragmentRepository;
-
- @Autowired
- private YangResourceRepository yangResourceRepository;
+ private final DataspaceRepository dataspaceRepository;
+ private final AnchorRepository anchorRepository;
+ private final SchemaSetRepository schemaSetRepository;
+ private final YangResourceRepository yangResourceRepository;
@Override
public void createDataspace(final String dataspaceName) {
@@ -140,7 +129,6 @@
@Override
public void deleteAnchor(final String dataspaceName, final String anchorName) {
final var anchorEntity = getAnchorEntity(dataspaceName, anchorName);
- fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity));
anchorRepository.delete(anchorEntity);
}
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
index c6e28ab..85b0cb7 100644
--- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* Copyright (C) 2021-2022 Nordix Foundation
* Modifications Copyright (C) 2021 Pantheon.tech
- * Modifications Copyright (C) 2020-2021 Bell Canada.
+ * Modifications Copyright (C) 2020-2022 Bell Canada.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -319,6 +319,14 @@
fragmentRepository.save(parentEntity);
}
+ @Override
+ @Transactional
+ public void deleteDataNodes(final String dataspaceName, final String anchorName) {
+ final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
+ anchorRepository.findByDataspaceAndName(dataspaceEntity, anchorName)
+ .ifPresent(
+ anchorEntity -> fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity)));
+ }
@Override
@Transactional
diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy
index 2218014..063bd5b 100644
--- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy
+++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsAdminPersistenceServiceSpec.groovy
@@ -41,8 +41,7 @@
static final String SET_DATA = '/data/anchor.sql'
static final String SAMPLE_DATA_FOR_ANCHORS_WITH_MODULES = '/data/anchors-schemaset-modules.sql'
static final String DATASPACE_WITH_NO_DATA = 'DATASPACE-002-NO-DATA'
- static final Integer DELETED_ANCHOR_ID = 3001
- static final Long DELETED_FRAGMENT_ID = 4001
+ static final Integer DELETED_ANCHOR_ID = 3002
@Sql(CLEAR_DATA)
def 'Create and retrieve a new dataspace.'() {
@@ -150,10 +149,9 @@
@Sql([CLEAR_DATA, SET_DATA])
def 'Delete anchor'() {
when: 'delete anchor action is invoked'
- objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME1)
- then: 'anchor and associated data fragment are deleted'
+ objectUnderTest.deleteAnchor(DATASPACE_NAME, ANCHOR_NAME2)
+ then: 'anchor is deleted'
assert anchorRepository.findById(DELETED_ANCHOR_ID).isEmpty()
- assert fragmentRepository.findById(DELETED_FRAGMENT_ID).isEmpty()
}
@Sql([CLEAR_DATA, SET_DATA])
diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy
index 69e6aa8..3150188 100755
--- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy
+++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* Copyright (C) 2021-2022 Nordix Foundation
* Modifications Copyright (C) 2021 Pantheon.tech
- * Modifications Copyright (C) 2021 Bell Canada.
+ * Modifications Copyright (C) 2021-2022 Bell Canada.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,7 +34,6 @@
import org.onap.cps.utils.JsonObjectMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.jdbc.Sql
-
import javax.validation.ConstraintViolationException
import java.util.stream.Collectors
@@ -49,6 +48,8 @@
static final JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
static final String SET_DATA = '/data/fragment.sql'
+ static final int DATASPACE_1001_ID = 1001L
+ static final int ANCHOR_3003_ID = 3003L
static final long ID_DATA_NODE_WITH_DESCENDANTS = 4001
static final String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
static final String XPATH_DATA_NODE_WITH_LEAVES = '/parent-100'
@@ -540,6 +541,20 @@
'invalid list element' | '/parent-206/child-206/grand-child-206@key="A"]'
}
+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Delete data node for an anchor.'() {
+ given: 'a data-node exists for an anchor'
+ assert fragmentsExistInDB(DATASPACE_1001_ID, ANCHOR_3003_ID)
+ when: 'data nodes are deleted '
+ objectUnderTest.deleteDataNodes(DATASPACE_NAME, ANCHOR_NAME3)
+ then: 'all data-nodes are deleted successfully'
+ assert !fragmentsExistInDB(DATASPACE_1001_ID, ANCHOR_3003_ID)
+ }
+
+ def fragmentsExistInDB(dataSpaceId, anchorId) {
+ !fragmentRepository.findRootsByDataspaceAndAnchor(dataSpaceId, anchorId).isEmpty()
+ }
+
static Collection<DataNode> toDataNodes(xpaths) {
return xpaths.collect { new DataNodeBuilder().withXpath(it).build() }
}
diff --git a/cps-ri/src/test/resources/application.yml b/cps-ri/src/test/resources/application.yml
index 73292bb..18e6ed6 100644
--- a/cps-ri/src/test/resources/application.yml
+++ b/cps-ri/src/test/resources/application.yml
@@ -18,10 +18,12 @@
spring:
jpa:
ddl-auto: create
+ show-sql: true
properties:
hibernate:
enable_lazy_load_no_trans: true
dialect: org.hibernate.dialect.PostgreSQLDialect
+ format_sql: true
datasource:
url: ${DB_URL}