TOSCA Compliant Guard Policies
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / DatabasePolicyModelsProviderTest.java
index f8a3490..388b6ad 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2020 Nordix Foundation.
  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,7 +25,6 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
 import java.util.ArrayList;
 import java.util.Base64;
@@ -34,6 +33,7 @@ import java.util.List;
 
 import org.junit.Before;
 import org.junit.Test;
+import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.pdp.concepts.Pdp;
 import org.onap.policy.models.pdp.concepts.PdpGroup;
 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
@@ -45,13 +45,11 @@ 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.ToscaPolicyFilter;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
-import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Test the database models provider implementation.
@@ -61,21 +59,17 @@ import org.slf4j.LoggerFactory;
 public class DatabasePolicyModelsProviderTest {
     private static final String NAME = "name";
 
-    private static final String TEMPLATE_IS_NULL = "serviceTemplate is marked @NonNull but is null";
+    private static final String TEMPLATE_IS_NULL = "^serviceTemplate is marked .*on.*ull but is null$";
 
-    private static final String POLICY_ID_IS_NULL = "policyId is marked @NonNull but is null";
+    private static final String POLICY_ID_IS_NULL = "^policyId is marked .*on.*ull but is null$";
 
-    private static final String PDP_TYPE_IS_NULL = "pdpType is marked @NonNull but is null";
+    private static final String SUBGROUP_IS_NULL = "^pdpSubGroup is marked .*on.*ull but is null$";
 
-    private static final String SUBGROUP_IS_NULL = "pdpSubGroup is marked @NonNull but is null";
+    private static final String GROUP_IS_NULL = "^pdpGroupName is marked .*on.*ull but is null$";
 
-    private static final String GROUP_IS_NULL = "pdpGroupName is marked @NonNull but is null";
+    private static final String NAME_IS_NULL = "^name is marked .*on.*ull but is null$";
 
-    private static final String NAME_IS_NULL = "name is marked @NonNull but is null";
-
-    private static final String FILTER_IS_NULL = "filter is marked @NonNull but is null";
-
-    private static final String INSTANCE = "Instance";
+    private static final String FILTER_IS_NULL = "^filter is marked .*on.*ull but is null$";
 
     private static final String POLICY_ID = "policy_id";
 
@@ -85,7 +79,7 @@ public class DatabasePolicyModelsProviderTest {
 
     private static final Date TIMESTAMP = new Date();
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderTest.class);
+    private static final String ORDER = "DESC";
 
     PolicyModelsProviderParameters parameters;
 
@@ -106,7 +100,7 @@ public class DatabasePolicyModelsProviderTest {
     public void testInitAndClose() throws Exception {
         assertThatThrownBy(() -> {
             new DatabasePolicyModelsProviderImpl(null);
-        }).hasMessage("parameters is marked @NonNull but is null");
+        }).hasMessageMatching("^parameters is marked .*on.*ull but is null$");
 
         PolicyModelsProvider databaseProvider =
                 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
@@ -142,383 +136,427 @@ public class DatabasePolicyModelsProviderTest {
     @Test
     public void testProviderMethodsNull() throws Exception {
 
-        try (PolicyModelsProvider databaseProvider =
-                        new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
+        PolicyModelsProvider databaseProvider =
+                new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.getFilteredPolicyTypes(null);
-            }).hasMessage(FILTER_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.getFilteredPolicyTypes(null);
+        }).hasMessageMatching(FILTER_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.getFilteredPolicyTypeList(null);
-            }).hasMessage(FILTER_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.getFilteredPolicyTypeList(null);
+        }).hasMessageMatching(FILTER_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.createPolicyTypes(null);
-            }).hasMessage(TEMPLATE_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.createPolicyTypes(null);
+        }).hasMessageMatching(TEMPLATE_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePolicyTypes(null);
-            }).hasMessage(TEMPLATE_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePolicyTypes(null);
+        }).hasMessageMatching(TEMPLATE_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.deletePolicyType(null, null);
-            }).hasMessage(NAME_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePolicyType(null, null);
+        }).hasMessageMatching(NAME_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.deletePolicyType("aaa", null);
-            }).hasMessage("version is marked @NonNull but is null");
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePolicyType("aaa", null);
+        }).hasMessageMatching("^version is marked .*on.*ull but is null$");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.deletePolicyType(null, "aaa");
-            }).hasMessage(NAME_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePolicyType(null, "aaa");
+        }).hasMessageMatching(NAME_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.getFilteredPolicies(null);
-            }).hasMessage(FILTER_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.getFilteredPolicies(null);
+        }).hasMessageMatching(FILTER_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.getFilteredPolicyList(null);
-            }).hasMessage(FILTER_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.getFilteredPolicyList(null);
+        }).hasMessageMatching(FILTER_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.createPolicies(null);
-            }).hasMessage(TEMPLATE_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.createPolicies(null);
+        }).hasMessageMatching(TEMPLATE_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePolicies(null);
-            }).hasMessage(TEMPLATE_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePolicies(null);
+        }).hasMessageMatching(TEMPLATE_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.deletePolicy(null, null);
-            }).hasMessage(NAME_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePolicy(null, null);
+        }).hasMessageMatching(NAME_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.deletePolicy(null, "aaa");
-            }).hasMessage(NAME_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePolicy(null, "aaa");
+        }).hasMessageMatching(NAME_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.deletePolicy("aaa", null);
-            }).hasMessage("version is marked @NonNull but is null");
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePolicy("aaa", null);
+        }).hasMessageMatching("^version is marked .*on.*ull but is null$");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.getOperationalPolicy(null, null);
-            }).hasMessage(POLICY_ID_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.getOperationalPolicy(null, null);
+        }).hasMessageMatching(POLICY_ID_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.getOperationalPolicy(null, "");
-            }).hasMessage(POLICY_ID_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.getOperationalPolicy(null, "");
+        }).hasMessageMatching(POLICY_ID_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.getOperationalPolicy("", null);
-            }).hasMessage("no policy found for policy: :null");
+        assertThatThrownBy(() -> {
+            databaseProvider.getOperationalPolicy("", null);
+        }).hasMessage("no policy found for policy: :null");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.createOperationalPolicy(null);
-            }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
+        assertThatThrownBy(() -> {
+            databaseProvider.createOperationalPolicy(null);
+        }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updateOperationalPolicy(null);
-            }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null");
+        assertThatThrownBy(() -> {
+            databaseProvider.updateOperationalPolicy(null);
+        }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.deleteOperationalPolicy(null, null);
-            }).hasMessage(POLICY_ID_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.deleteOperationalPolicy(null, null);
+        }).hasMessageMatching(POLICY_ID_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.deleteOperationalPolicy(null, "");
-            }).hasMessage(POLICY_ID_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.deleteOperationalPolicy(null, "");
+        }).hasMessageMatching(POLICY_ID_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.deleteOperationalPolicy("", null);
-            }).hasMessage("policyVersion is marked @NonNull but is null");
+        assertThatThrownBy(() -> {
+            databaseProvider.deleteOperationalPolicy("", null);
+        }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.getGuardPolicy(null, null);
-            }).hasMessage(POLICY_ID_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.getFilteredPdpGroups(null);
+        }).hasMessageMatching(FILTER_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.getGuardPolicy(null, "");
-            }).hasMessage(POLICY_ID_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.createPdpGroups(null);
+        }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.getGuardPolicy("", null);
-            }).hasMessage("no policy found for policy: :null");
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePdpGroups(null);
+        }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.createGuardPolicy(null);
-            }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePdpSubGroup(null, null);
+        }).hasMessageMatching(GROUP_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updateGuardPolicy(null);
-            }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePdpSubGroup(null, new PdpSubGroup());
+        }).hasMessageMatching(GROUP_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.deleteGuardPolicy(null, null);
-            }).hasMessage(POLICY_ID_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePdpSubGroup(NAME, null);
+        }).hasMessageMatching(SUBGROUP_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.deleteGuardPolicy(null, "");
-            }).hasMessage(POLICY_ID_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePdpSubGroup(NAME, new PdpSubGroup());
+        }).hasMessage("parameter \"localName\" is null");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.deleteGuardPolicy("", null);
-            }).hasMessage("policyVersion is marked @NonNull but is null");
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePdp(null, null, null);
+        }).hasMessageMatching(GROUP_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.getFilteredPdpGroups(null);
-            }).hasMessage(FILTER_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePdp(null, null, new Pdp());
+        }).hasMessageMatching(GROUP_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.createPdpGroups(null);
-            }).hasMessage("pdpGroups is marked @NonNull but is null");
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePdp(null, "sub", null);
+        }).hasMessageMatching(GROUP_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePdpGroups(null);
-            }).hasMessage("pdpGroups is marked @NonNull but is null");
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePdp(null, "sub", new Pdp());
+        }).hasMessageMatching(GROUP_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePdpSubGroup(null, null);
-            }).hasMessage(GROUP_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePdp(NAME, null, null);
+        }).hasMessageMatching(SUBGROUP_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePdpSubGroup(null, new PdpSubGroup());
-            }).hasMessage(GROUP_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePdp(NAME, null, new Pdp());
+        }).hasMessageMatching(SUBGROUP_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePdpSubGroup(NAME, null);
-            }).hasMessage(SUBGROUP_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePdp(NAME, "sub", null);
+        }).hasMessageMatching("^pdp is marked .*on.*ull but is null$");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePdpSubGroup(NAME, new PdpSubGroup());
-            }).hasMessage("parameter \"localName\" is null");
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePdp(NAME, "sub", new Pdp());
+        }).hasMessage("parameter \"localName\" is null");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePdp(null, null, null);
-            }).hasMessage(GROUP_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePdpGroup(null);
+        }).hasMessageMatching(NAME_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePdp(null, null, new Pdp());
-            }).hasMessage(GROUP_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.getFilteredPdpStatistics(NAME, null, "sub", TIMESTAMP, TIMESTAMP, ORDER, 0);
+        }).hasMessageMatching(GROUP_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePdp(null, "sub", null);
-            }).hasMessage(GROUP_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.createPdpStatistics(null);
+        }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePdp(null, "sub", new Pdp());
-            }).hasMessage(GROUP_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePdpStatistics(null);
+        }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePdp(NAME, null, null);
-            }).hasMessage(SUBGROUP_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePdpStatistics(null, TIMESTAMP);
+        }).hasMessageMatching(NAME_IS_NULL);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePdp(NAME, null, new Pdp());
-            }).hasMessage(SUBGROUP_IS_NULL);
+        databaseProvider.close();
+    }
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePdp(NAME, "sub", null);
-            }).hasMessage("pdp is marked @NonNull but is null");
+    @Test
+    public void testProviderMethodsNotInit() throws Exception {
+        PolicyModelsProvider databaseProvider =
+                new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePdp(NAME, "sub", new Pdp());
-            }).hasMessage("parameter \"localName\" is null");
+        databaseProvider.close();
 
-            assertThatThrownBy(() -> {
-                databaseProvider.deletePdpGroup(null);
-            }).hasMessage(NAME_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.getPolicyTypes(NAME, "version");
+        }).hasMessage("policy models provider is not initilaized");
+    }
 
-            assertThatThrownBy(() -> {
-                databaseProvider.getFilteredPdpStatistics(NAME, null, "sub", TIMESTAMP, TIMESTAMP);
-            }).hasMessage(GROUP_IS_NULL);
+    @Test
+    public void testProviderMethods() throws PfModelException {
+        PolicyModelsProvider databaseProvider =
+                new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
+
+        assertThatThrownBy(() -> databaseProvider.getPolicyTypes(NAME, VERSION_100))
+                .hasMessage("service template not found in database");
+
+        assertTrue(databaseProvider.getPolicyTypeList(NAME, VERSION_100).isEmpty());
 
-            assertThatThrownBy(() -> {
-                databaseProvider.createPdpStatistics(null);
-            }).hasMessage("pdpStatisticsList is marked @NonNull but is null");
+        assertThatThrownBy(() -> databaseProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build()))
+                .hasMessage("service template not found in database");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePdpStatistics(null);
-            }).hasMessage("pdpStatisticsList is marked @NonNull but is null");
+        assertTrue(databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()).isEmpty());
+
+        assertThatThrownBy(() -> {
+            databaseProvider.createPolicyTypes(new ToscaServiceTemplate());
+        }).hasMessage("no policy types specified on service template");
+
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePolicyTypes(new ToscaServiceTemplate());
+        }).hasMessage("no policy types specified on service template");
+
+        assertThatThrownBy(() -> databaseProvider.deletePolicyType(NAME, VERSION_100))
+                .hasMessage("service template not found in database");
+
+        assertThatThrownBy(() -> databaseProvider.getPolicies(NAME, VERSION_100))
+                .hasMessage("service template not found in database");
+
+        assertTrue(databaseProvider.getPolicyList(NAME, VERSION_100).isEmpty());
+
+        assertThatThrownBy(() -> databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build()))
+                .hasMessage("service template not found in database");
+
+        assertTrue(databaseProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build()).isEmpty());
+
+        assertThatThrownBy(() -> {
+            databaseProvider.createPolicies(new ToscaServiceTemplate());
+        }).hasMessage("topology template not specified on service template");
 
-            assertThatThrownBy(() -> {
-                databaseProvider.deletePdpStatistics(null, TIMESTAMP);
-            }).hasMessage(NAME_IS_NULL);
+        assertThatThrownBy(() -> {
+            databaseProvider.updatePolicies(new ToscaServiceTemplate());
+        }).hasMessage("topology template not specified on service template");
+
+        assertThatThrownBy(() -> databaseProvider.deletePolicy("Policy", "0.0.0").getToscaTopologyTemplate())
+                .hasMessage("service template not found in database");
+
+        assertThatThrownBy(() -> {
+            databaseProvider.getOperationalPolicy(POLICY_ID, null);
+        }).hasMessage("no policy found for policy: policy_id:null");
+
+        assertThatThrownBy(() -> {
+            databaseProvider.getOperationalPolicy(POLICY_ID, "10");
+        }).hasMessage("no policy found for policy: policy_id:10");
+
+        assertThatThrownBy(() -> {
+            databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy());
+        }).hasMessageMatching(NAME_IS_NULL);
+
+        assertThatThrownBy(() -> {
+            databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy());
+        }).hasMessageMatching(NAME_IS_NULL);
+
+        assertThatThrownBy(() -> {
+            databaseProvider.deleteOperationalPolicy(POLICY_ID, "55");
+        }).hasMessage("no policy found for policy: policy_id:55");
+
+        assertEquals(0, databaseProvider.getPdpGroups(NAME).size());
+        assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
+
+        assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
+        assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
+
+        PdpGroup pdpGroup = new PdpGroup();
+        pdpGroup.setName(GROUP);
+        pdpGroup.setVersion("1.2.3");
+        pdpGroup.setPdpGroupState(PdpState.ACTIVE);
+        pdpGroup.setPdpSubgroups(new ArrayList<>());
+        List<PdpGroup> groupList = new ArrayList<>();
+        groupList.add(pdpGroup);
+
+        PdpSubGroup pdpSubGroup = new PdpSubGroup();
+        pdpSubGroup.setPdpType("type");
+        pdpSubGroup.setDesiredInstanceCount(123);
+        pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
+        pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("type", "7.8.9"));
+        pdpGroup.getPdpSubgroups().add(pdpSubGroup);
+
+        Pdp pdp = new Pdp();
+        pdp.setInstanceId("type-0");
+        pdp.setMessage("Hello");
+        pdp.setPdpState(PdpState.ACTIVE);
+        pdp.setHealthy(PdpHealthStatus.UNKNOWN);
+        pdpSubGroup.setPdpInstances(new ArrayList<>());
+        pdpSubGroup.getPdpInstances().add(pdp);
+
+        PdpStatistics pdpStatistics = new PdpStatistics();
+        pdpStatistics.setPdpInstanceId(NAME);
+        pdpStatistics.setTimeStamp(new Date());
+        pdpStatistics.setPdpGroupName(GROUP);
+        pdpStatistics.setPdpSubGroupName("type");
+        ArrayList<PdpStatistics> statisticsArrayList = new ArrayList<>();
+        statisticsArrayList.add(pdpStatistics);
+
+        assertEquals(123,
+                databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getDesiredInstanceCount());
+        assertEquals(1, databaseProvider.getPdpGroups(GROUP).size());
+
+        pdpSubGroup.setDesiredInstanceCount(234);
+        databaseProvider.updatePdpSubGroup(GROUP, pdpSubGroup);
+        assertEquals(234,
+                databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getDesiredInstanceCount());
+
+        assertEquals("Hello", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances()
+                .get(0).getMessage());
+        pdp.setMessage("Howdy");
+        databaseProvider.updatePdp(GROUP, "type", pdp);
+        assertEquals("Howdy", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances()
+                .get(0).getMessage());
+
+        assertThatThrownBy(() -> {
+            databaseProvider.deletePdpGroup(NAME);
+        }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist");
+
+        assertEquals(pdpGroup.getName(), databaseProvider.deletePdpGroup(GROUP).getName());
+
+        assertEquals(0, databaseProvider.getPdpStatistics(null, null).size());
+        assertEquals(1, databaseProvider.createPdpStatistics(statisticsArrayList).size());
+        assertEquals(1, databaseProvider.updatePdpStatistics(statisticsArrayList).size());
+
+        assertEquals(NAME, databaseProvider.getPdpStatistics(null, null).get(0).getPdpInstanceId());
+        assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, null, ORDER, 0).get(0)
+                .getPdpInstanceId());
+        assertEquals(0,
+                databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), null, ORDER, 0).size());
+        assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, new Date(), ORDER, 0)
+                .get(0).getPdpInstanceId());
+        assertEquals(0,
+                databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), new Date(), ORDER, 0).size());
+
+        assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, null, null, ORDER, 0).get(0)
+                .getPdpInstanceId());
+        assertEquals(0,
+                databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, new Date(), new Date(), ORDER, 0).size());
+
+        assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 0).get(0)
+                .getPdpInstanceId());
+        assertEquals(0, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 0)
+                .size());
+
+        assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 1).get(0)
+                .getPdpInstanceId());
+        assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 5).get(0)
+                .getPdpInstanceId());
+        assertEquals(0, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 5)
+                .size());
+
+        assertEquals(NAME, databaseProvider.deletePdpStatistics(NAME, null).get(0).getPdpInstanceId());
+        assertEquals(0, databaseProvider.getPdpStatistics(null, null).size());
 
-        }
+        databaseProvider.close();
     }
 
     @Test
-    public void testProviderMethodsNotInit() throws Exception {
+    public void testDeletePolicyDeployedInSubgroup() throws PfModelException {
+        List<ToscaPolicyIdentifier> policies = new ArrayList<>();
+
+        policies.add(new ToscaPolicyIdentifier("p0", "0.0.1"));
+        policies.add(new ToscaPolicyIdentifier("p1", "0.0.1"));
+
+        List<ToscaPolicyTypeIdentifier> supportedPolicyTypes = new ArrayList<>();
+        supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier("pt2", "0.0.1"));
+
+        PdpSubGroup subGroup = new PdpSubGroup();
+        subGroup.setPdpType("pdpType");
+        subGroup.setSupportedPolicyTypes(supportedPolicyTypes);
+        subGroup.setPolicies(policies);
+
+        List<PdpSubGroup> pdpSubgroups = new ArrayList<>();
+        pdpSubgroups.add(subGroup);
+
+        PdpGroup pdpGroup = new PdpGroup();
+        pdpGroup.setName("pdpGroup");
+        pdpGroup.setPdpGroupState(PdpState.PASSIVE);
+        pdpGroup.setPdpSubgroups(pdpSubgroups);
+
+        List<PdpGroup> pdpGroups = new ArrayList<>();
+        pdpGroups.add(pdpGroup);
+
         PolicyModelsProvider databaseProvider =
                 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
 
-        databaseProvider.close();
+        databaseProvider.createPdpGroups(pdpGroups);
 
-        assertThatThrownBy(() -> {
-            databaseProvider.getPolicyTypes(NAME, "version");
-        }).hasMessage("policy models provider is not initilaized");
+        assertThatThrownBy(() -> databaseProvider.deletePolicy("p0", "0.0.1"))
+                .hasMessageContaining("policy is in use, it is deployed in PDP group pdpGroup subgroup pdpType");
+
+        assertThatThrownBy(() -> databaseProvider.deletePolicy("p3", "0.0.1"))
+                .hasMessageContaining("service template not found in database");
+
+        databaseProvider.close();
     }
 
     @Test
-    public void testProviderMethods() {
-        try (PolicyModelsProvider databaseProvider =
-                new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
-
-            assertTrue(databaseProvider.getPolicyTypes(NAME, VERSION_100).getPolicyTypes().isEmpty());
-            assertTrue(databaseProvider.getPolicyTypeList(NAME, VERSION_100).isEmpty());
-            assertEquals(0, databaseProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build())
-                    .getPolicyTypes().size());
-            assertEquals(0, databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()).size());
-
-            assertThatThrownBy(() -> {
-                databaseProvider.createPolicyTypes(new ToscaServiceTemplate());
-            }).hasMessage("no policy types specified on service template");
-
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePolicyTypes(new ToscaServiceTemplate());
-            }).hasMessage("no policy types specified on service template");
-
-            assertTrue(databaseProvider.deletePolicyType(NAME, VERSION_100).getPolicyTypes().isEmpty());
-
-            assertTrue(databaseProvider.deletePolicyType(NAME, VERSION_100).getPolicyTypes().isEmpty());
-
-            assertTrue(
-                    databaseProvider.getPolicies(NAME, VERSION_100).getToscaTopologyTemplate().getPolicies().isEmpty());
-            assertTrue(databaseProvider.getPolicyList(NAME, VERSION_100).isEmpty());
-            assertEquals(0, databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build())
-                    .getToscaTopologyTemplate().getPolicies().size());
-            assertEquals(0, databaseProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build()).size());
-
-            assertThatThrownBy(() -> {
-                databaseProvider.createPolicies(new ToscaServiceTemplate());
-            }).hasMessage("topology template not specified on service template");
-
-            assertThatThrownBy(() -> {
-                databaseProvider.updatePolicies(new ToscaServiceTemplate());
-            }).hasMessage("topology template not specified on service template");
-
-            assertTrue(databaseProvider.deletePolicy("Policy", "0.0.0").getToscaTopologyTemplate().getPolicies()
-                    .isEmpty());
-
-            assertThatThrownBy(() -> {
-                databaseProvider.getOperationalPolicy(POLICY_ID, null);
-            }).hasMessage("no policy found for policy: policy_id:null");
-
-            assertThatThrownBy(() -> {
-                databaseProvider.getOperationalPolicy(POLICY_ID, "10");
-            }).hasMessage("no policy found for policy: policy_id:10");
-
-            assertThatThrownBy(() -> {
-                databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy());
-            }).hasMessage(NAME_IS_NULL);
-
-            assertThatThrownBy(() -> {
-                databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy());
-            }).hasMessage(NAME_IS_NULL);
-
-            assertThatThrownBy(() -> {
-                databaseProvider.deleteOperationalPolicy(POLICY_ID, "55");
-            }).hasMessage("no policy found for policy: policy_id:55");
-
-            assertThatThrownBy(() -> {
-                databaseProvider.getGuardPolicy(POLICY_ID, null);
-            }).hasMessage("no policy found for policy: policy_id:null");
-
-            assertThatThrownBy(() -> {
-                databaseProvider.getGuardPolicy(POLICY_ID, "6");
-            }).hasMessage("no policy found for policy: policy_id:6");
-
-            assertThatThrownBy(() -> {
-                databaseProvider.createGuardPolicy(new LegacyGuardPolicyInput());
-            }).hasMessage("policy type for guard policy \"null\" unknown");
-
-            assertThatThrownBy(() -> {
-                databaseProvider.updateGuardPolicy(new LegacyGuardPolicyInput());
-            }).hasMessage("policy type for guard policy \"null\" unknown");
-
-            assertThatThrownBy(() -> {
-                databaseProvider.deleteGuardPolicy(POLICY_ID, "33");
-            }).hasMessage("no policy found for policy: policy_id:33");
-
-            assertEquals(0, databaseProvider.getPdpGroups(NAME).size());
-            assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
-
-            assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
-            assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
-
-            PdpGroup pdpGroup = new PdpGroup();
-            pdpGroup.setName(GROUP);
-            pdpGroup.setVersion("1.2.3");
-            pdpGroup.setPdpGroupState(PdpState.ACTIVE);
-            pdpGroup.setPdpSubgroups(new ArrayList<>());
-            List<PdpGroup> groupList = new ArrayList<>();
-            groupList.add(pdpGroup);
-
-            PdpSubGroup pdpSubGroup = new PdpSubGroup();
-            pdpSubGroup.setPdpType("type");
-            pdpSubGroup.setDesiredInstanceCount(123);
-            pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
-            pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("type", "7.8.9"));
-            pdpGroup.getPdpSubgroups().add(pdpSubGroup);
-
-            Pdp pdp = new Pdp();
-            pdp.setInstanceId("type-0");
-            pdp.setMessage("Hello");
-            pdp.setPdpState(PdpState.ACTIVE);
-            pdp.setHealthy(PdpHealthStatus.UNKNOWN);
-            pdpSubGroup.setPdpInstances(new ArrayList<>());
-            pdpSubGroup.getPdpInstances().add(pdp);
-
-            PdpStatistics pdpStatistics = new PdpStatistics();
-            pdpStatistics.setPdpInstanceId("Pdp1");
-            pdpStatistics.setTimeStamp(new Date());
-            pdpStatistics.setPdpGroupName("DefaultGroup");
-            ArrayList<PdpStatistics> statisticsArrayList = new ArrayList<>();
-            statisticsArrayList.add(pdpStatistics);
-
-            assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
-                    .getDesiredInstanceCount());
-            assertEquals(1, databaseProvider.getPdpGroups(GROUP).size());
-
-            pdpSubGroup.setDesiredInstanceCount(234);
-            databaseProvider.updatePdpSubGroup(GROUP, pdpSubGroup);
-            assertEquals(234, databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups()
-                    .get(0).getDesiredInstanceCount());
-
-            assertEquals("Hello", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups()
-                    .get(0).getPdpInstances().get(0).getMessage());
-            pdp.setMessage("Howdy");
-            databaseProvider.updatePdp(GROUP, "type", pdp);
-            assertEquals("Howdy", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups()
-                    .get(0).getPdpInstances().get(0).getMessage());
-
-            assertThatThrownBy(() -> {
-                databaseProvider.deletePdpGroup(NAME);
-            }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist");
-
-            assertEquals(pdpGroup.getName(), databaseProvider.deletePdpGroup(GROUP).getName());
-
-            assertEquals(0, databaseProvider.getPdpStatistics(null,null).size());
-
-            databaseProvider.getFilteredPdpStatistics(null, GROUP,null, null, null);
-            databaseProvider.getFilteredPdpStatistics(null, GROUP,null, new Date(), null);
-            databaseProvider.getFilteredPdpStatistics(null, GROUP,null, null, new Date());
-            databaseProvider.getFilteredPdpStatistics(null, GROUP,null, new Date(), new Date());
-
-            databaseProvider.getFilteredPdpStatistics(NAME, GROUP,null, null, null);
-            databaseProvider.getFilteredPdpStatistics(NAME, GROUP,null, new Date(), new Date());
-
-            databaseProvider.getFilteredPdpStatistics(NAME, GROUP,"type", null, null);
-            databaseProvider.getFilteredPdpStatistics(NAME, GROUP,"type", new Date(), new Date());
-
-            databaseProvider.createPdpStatistics(statisticsArrayList);
-            databaseProvider.updatePdpStatistics(statisticsArrayList);
-
-            databaseProvider.deletePdpStatistics("pdp1",null);
-        } catch (Exception exc) {
-            LOGGER.warn("test should not throw an exception", exc);
-            fail("test should not throw an exception");
-        }
+    public void testDeletePolicyTypeSupportedInSubgroup() throws PfModelException {
+        List<ToscaPolicyTypeIdentifier> supportedPolicyTypes = new ArrayList<>();
+        supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier("pt1", "0.0.1"));
+        supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier("pt2", "0.0.1"));
+
+        PdpSubGroup subGroup = new PdpSubGroup();
+        subGroup.setPdpType("pdpType");
+        subGroup.setSupportedPolicyTypes(supportedPolicyTypes);
+
+        List<PdpSubGroup> pdpSubgroups = new ArrayList<>();
+        pdpSubgroups.add(subGroup);
+
+        PdpGroup pdpGroup = new PdpGroup();
+        pdpGroup.setName("pdpGroup");
+        pdpGroup.setPdpGroupState(PdpState.PASSIVE);
+        pdpGroup.setPdpSubgroups(pdpSubgroups);
+
+        List<PdpGroup> pdpGroups = new ArrayList<>();
+        pdpGroups.add(pdpGroup);
+
+        PolicyModelsProvider databaseProvider =
+                new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters);
+
+        databaseProvider.createPdpGroups(pdpGroups);
+
+        assertThatThrownBy(() -> databaseProvider.deletePolicyType("pt2", "0.0.1"))
+                .hasMessageContaining("policy type is in use, it is referenced in PDP group pdpGroup subgroup pdpType");
+
+        assertThatThrownBy(() -> databaseProvider.deletePolicyType("pt0", "0.0.1"))
+                .hasMessageContaining("service template not found in database");
+
+        databaseProvider.close();
     }
 }