From 83a1bbff60a642043260bfc61e238203a3bba769 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Tue, 5 May 2020 13:05:11 +0100 Subject: Policy delete should only return deleted policy The policy type delete and policy delete now only return what is deleted in the service template. Issue-ID: POLICY-2534 Change-Id: I1b8d10c22d4392b0bbb655aa6876ac900ebc81df Signed-off-by: liamfallon --- .../provider/impl/PolicyToscaPersistenceTest.java | 9 +++++-- .../provider/impl/PolicyTypePersistenceTest.java | 30 +++++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) (limited to 'models-provider/src') diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java index cbc57fe75..210c900c2 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java @@ -179,8 +179,11 @@ public class PolicyToscaPersistenceTest { assertEquals(3, namingServiceTemplate.getDataTypesAsMap().size()); long deleteStartTime = System.currentTimeMillis(); - databaseProvider.deletePolicy("SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP", "1.0.0"); + ToscaServiceTemplate deletedServiceTemplate = + databaseProvider.deletePolicy("SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP", "1.0.0"); LOGGER.trace("Naming policy delete time (ms): {}", System.currentTimeMillis() - deleteStartTime); + + assertEquals(1, deletedServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size()); } @Test @@ -228,7 +231,9 @@ public class PolicyToscaPersistenceTest { .next().getVersion()); for (int i = 1; i < 22; i++) { - databaseProvider.deletePolicy("SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP", i + ".0.0"); + ToscaServiceTemplate deletedServiceTemplate = + databaseProvider.deletePolicy("SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP", i + ".0.0"); + assertEquals(1, deletedServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().size()); } } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java index 6cda57b60..8001d536c 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java @@ -21,7 +21,7 @@ package org.onap.policy.models.provider.impl; import static org.assertj.core.api.Assertions.assertThatCode; -import static org.awaitility.Awaitility.await; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -40,6 +40,7 @@ import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; /** @@ -140,5 +141,32 @@ public class PolicyTypePersistenceTest { assertTrue(resOptPolicyTypeSt.getPolicyTypesAsMap().containsKey(optimizationPtKey)); assertEquals(2, resOptPolicyTypeSt.getDataTypesAsMap().size()); + + deleteUnreferencedPolicyTypes(); + + assertThatThrownBy(() -> { + databaseProvider.getPolicyTypes(null, null); + }).hasMessageContaining("policy types for null:null do not exist"); + } + + private void deleteUnreferencedPolicyTypes() throws PfModelException { + ToscaServiceTemplate allPolicyTypes = databaseProvider.getPolicyTypes(null, null); + + boolean deleteFailedDueToReference = false; + + for (ToscaPolicyType policyType : allPolicyTypes.getPolicyTypes().values()) { + try { + ToscaServiceTemplate deletedServiceTemplate = + databaseProvider.deletePolicyType(policyType.getName(), policyType.getVersion()); + assertEquals(1, deletedServiceTemplate.getPolicyTypesAsMap().size()); + } catch (Exception pfModelRuntimeException) { + assertTrue(pfModelRuntimeException.getMessage().contains("is in use, it is referenced in policy type")); + deleteFailedDueToReference = true; + } + } + + if (deleteFailedDueToReference) { + deleteUnreferencedPolicyTypes(); + } } } -- cgit 1.2.3-korg