Removed db-based statistics feature
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / persistence / provider / PdpProviderTest.java
index 2bf942a..620b818 100644 (file)
@@ -1,7 +1,8 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019-2021 Nordix Foundation.
+ *  Copyright (C) 2019-2021,2023 Nordix Foundation.
  *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ *  Modifications Copyright (C) 2023 Bell Canada. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -32,7 +33,6 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.Properties;
-import org.eclipse.persistence.config.PersistenceUnitProperties;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -52,11 +52,11 @@ import org.onap.policy.models.pdp.concepts.PdpGroups;
 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.PdpPolicyStatusBuilder;
 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
-import org.onap.policy.models.pdp.concepts.PdpStatistics;
 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
 import org.onap.policy.models.pdp.enums.PdpState;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
 
 /**
@@ -73,11 +73,13 @@ public class PdpProviderTest {
     private static final String PDP_GROUP0 = "PdpGroup0";
     private static final String GROUP_A = "groupA";
     private static final String GROUP_B = "groupB";
+    private static final ToscaConceptIdentifier MY_POLICY = new ToscaConceptIdentifier("MyPolicy", "1.2.3");
+    private static final ToscaConceptIdentifier MY_POLICY2 = new ToscaConceptIdentifier("MyPolicyB", "2.3.4");
+
     private PfDao pfDao;
     private StandardCoder standardCoder;
     private PdpPolicyStatusBuilder statusBuilder;
 
-
     /**
      * Set up the DAO towards the database.
      *
@@ -91,12 +93,16 @@ public class PdpProviderTest {
         daoParameters.setPersistenceUnit("ToscaConceptTest");
 
         Properties jdbcProperties = new Properties();
-        jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
-        jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
+        jdbcProperties.setProperty("javax.persistence.jdbc.user", "policy");
+        jdbcProperties.setProperty("javax.persistence.jdbc.password", "P01icY");
 
-        // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
-        jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
-        jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
+        if (System.getProperty("USE-MARIADB") != null) {
+            jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
+            jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/policy");
+        } else {
+            jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
+            jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:PdpProviderTest");
+        }
 
         daoParameters.setJdbcProperties(jdbcProperties);
 
@@ -117,11 +123,10 @@ public class PdpProviderTest {
      */
     @Before
     public void setupBuilder() {
-        ToscaConceptIdentifier policy = new ToscaConceptIdentifier("MyPolicy", "1.2.3");
         ToscaConceptIdentifier policyType = new ToscaConceptIdentifier("MyPolicyType", "1.2.4");
 
-        statusBuilder = PdpPolicyStatus.builder().deploy(true).pdpType("MyPdpType").policy(policy)
-                        .policyType(policyType).state(State.SUCCESS);
+        statusBuilder = PdpPolicyStatus.builder().deploy(true).pdpType("MyPdpType").policy(MY_POLICY)
+            .policyType(policyType).state(State.SUCCESS);
     }
 
     @After
@@ -227,7 +232,7 @@ public class PdpProviderTest {
         assertThatThrownBy(() -> {
             new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups());
         }).hasMessageContaining("PDP group").hasMessageContaining("pdpGroupState")
-                        .hasMessageContaining(Validated.IS_NULL);
+            .hasMessageContaining(Validated.IS_NULL);
     }
 
     @Test
@@ -293,7 +298,7 @@ public class PdpProviderTest {
         assertThatThrownBy(() -> {
             new PdpProvider().updatePdpGroups(pfDao, pdpGroups0.getGroups());
         }).hasMessageContaining("PDP group").hasMessageContaining("pdpGroupState")
-                    .hasMessageContaining(Validated.IS_NULL);
+            .hasMessageContaining(Validated.IS_NULL);
     }
 
     @Test
@@ -400,7 +405,7 @@ public class PdpProviderTest {
         assertThatThrownBy(() -> {
             new PdpProvider().updatePdpSubGroup(pfDao, PDP_GROUP0, existingSubGroup);
         }).hasMessageContaining("PDP sub group").hasMessageContaining("desiredInstanceCount")
-                        .hasMessageContaining("below the minimum value");
+            .hasMessageContaining("below the minimum value");
         existingSubGroup.setDesiredInstanceCount(10);
     }
 
@@ -491,9 +496,9 @@ public class PdpProviderTest {
 
         List<PdpGroup> afterUpdatePdpGroups = new PdpProvider().getPdpGroups(pfDao, PDP_GROUP0);
         assertEquals(PdpState.TEST,
-                afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).getPdpState());
+            afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).getPdpState());
         assertEquals(PdpHealthStatus.TEST_IN_PROGRESS,
-                afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).getHealthy());
+            afterUpdatePdpGroups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).getHealthy());
 
         existingPdp.setMessage("");
         assertThatThrownBy(() -> {
@@ -503,145 +508,50 @@ public class PdpProviderTest {
     }
 
     @Test
-    public void testGetPdpStatistics() throws PfModelException {
+    public void testGetAllPolicyStatusPfDao() throws PfModelException {
         assertThatThrownBy(() -> {
-            new PdpProvider().getPdpStatistics(null, null);
+            new PdpProvider().getAllPolicyStatus(null);
         }).hasMessageMatching(DAO_IS_NULL);
 
-        assertThatThrownBy(() -> {
-            new PdpProvider().getPdpStatistics(null, "name");
-        }).hasMessageMatching(DAO_IS_NULL);
+        assertThat(new PdpProvider().getAllPolicyStatus(pfDao)).isEmpty();
 
-        assertEquals(0, new PdpProvider().getPdpStatistics(pfDao, "name").size());
+        PdpProvider provider = loadDeployments();
+        assertThat(provider.getAllPolicyStatus(pfDao)).hasSize(5);
     }
 
-    @Test
-    public void testUpdatePdpStatistics() throws PfModelException {
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, null, null, null, null);
-        }).hasMessageMatching(DAO_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, null, null, null, new PdpStatistics());
-        }).hasMessageMatching(DAO_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, null, null, "inst", null);
-        }).hasMessageMatching(DAO_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, null, null, "inst", new PdpStatistics());
-        }).hasMessageMatching(DAO_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, null, "TYPE", null, null);
-        }).hasMessageMatching(DAO_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, null, "TYPE", null, new PdpStatistics());
-        }).hasMessageMatching(DAO_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, null, "TYPE", "inst", null);
-        }).hasMessageMatching(DAO_IS_NULL);
+    private PdpProvider loadDeployments() {
+        PdpProvider provider = new PdpProvider();
 
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, null, "TYPE", "inst", new PdpStatistics());
-        }).hasMessageMatching(DAO_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, "name", null, null, null);
-        }).hasMessageMatching(DAO_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, "name", null, null, new PdpStatistics());
-        }).hasMessageMatching(DAO_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, "name", null, "inst", null);
-        }).hasMessageMatching(DAO_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, "name", null, "inst", new PdpStatistics());
-        }).hasMessageMatching(DAO_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, "name", "TYPE", null, null);
-        }).hasMessageMatching(DAO_IS_NULL);
+        // same name, different version
+        final ToscaConceptIdentifier policy3 = new ToscaConceptIdentifier(MY_POLICY.getName(), "10.20.30");
 
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, "name", "TYPE", null, new PdpStatistics());
-        }).hasMessageMatching(DAO_IS_NULL);
+        PdpPolicyStatus id1 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp1").policy(MY_POLICY).build();
+        PdpPolicyStatus id2 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp2").policy(MY_POLICY2).build();
+        PdpPolicyStatus id3 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp3").policy(policy3).build();
+        PdpPolicyStatus id4 = statusBuilder.pdpGroup(GROUP_B).pdpId("pdp4").policy(MY_POLICY).build();
+        PdpPolicyStatus id5 = statusBuilder.pdpGroup(GROUP_B).pdpId("pdp5").policy(MY_POLICY2).build();
+        provider.cudPolicyStatus(pfDao, List.of(id1, id2, id3, id4, id5), null, null);
 
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, "name", "TYPE", "inst", null);
-        }).hasMessageMatching(DAO_IS_NULL);
+        return provider;
+    }
 
+    @Test
+    public void testGetAllPolicyStatusPfDaoToscaConceptIdentifierOptVersion() throws PfModelException {
         assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(null, "name", "TYPE", "inst", new PdpStatistics());
+            new PdpProvider().getAllPolicyStatus(null, new ToscaConceptIdentifierOptVersion("somePdp", null));
         }).hasMessageMatching(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, null, null, null, null);
-        }).hasMessageMatching(GROUP_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, null, null, null, new PdpStatistics());
-        }).hasMessageMatching(GROUP_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, null, null, "inst", null);
-        }).hasMessageMatching(GROUP_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, null, null, "inst", new PdpStatistics());
-        }).hasMessageMatching(GROUP_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", null, null);
-        }).hasMessageMatching(GROUP_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", null, new PdpStatistics());
-        }).hasMessageMatching(GROUP_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", "inst", null);
-        }).hasMessageMatching(GROUP_IS_NULL);
+            new PdpProvider().getAllPolicyStatus(pfDao, null);
+        }).hasMessageContaining("policy").hasMessageContaining("null");
 
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, null, "TYPE", "inst", new PdpStatistics());
-        }).hasMessageMatching(GROUP_IS_NULL);
+        assertThat(new PdpProvider().getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion("somePdp", null)))
+            .isEmpty();
 
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, "name", null, null, null);
-        }).hasMessageMatching(PDP_TYPE_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, "name", null, null, new PdpStatistics());
-        }).hasMessageMatching(PDP_TYPE_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, "name", null, "inst", null);
-        }).hasMessageMatching(PDP_TYPE_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, "name", null, "inst", new PdpStatistics());
-        }).hasMessageMatching(PDP_TYPE_IS_NULL);
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", null, null);
-        }).hasMessageMatching("pdpInstanceId is marked .*ull but is null");
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", null, new PdpStatistics());
-        }).hasMessageMatching("pdpInstanceId is marked .*ull but is null");
-
-        assertThatThrownBy(() -> {
-            new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", "inst", null);
-        }).hasMessageMatching("pdpStatistics is marked .*ull but is null");
-
-        new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", "inst", new PdpStatistics());
+        PdpProvider provider = loadDeployments();
+        assertThat(provider.getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion(MY_POLICY))).hasSize(2);
+        assertThat(provider.getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion(MY_POLICY.getName(), null)))
+            .hasSize(3);
     }
 
     @Test
@@ -655,6 +565,9 @@ public class PdpProviderTest {
         }).hasMessageContaining("group").hasMessageContaining("null");
 
         assertThat(new PdpProvider().getGroupPolicyStatus(pfDao, PDP_GROUP0)).isEmpty();
+
+        PdpProvider provider = loadDeployments();
+        assertThat(provider.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(3);
     }
 
     @Test
@@ -662,7 +575,7 @@ public class PdpProviderTest {
         PdpProvider prov = new PdpProvider();
 
         assertThatThrownBy(() -> prov.cudPolicyStatus(null, List.of(), List.of(), List.of()))
-                        .hasMessageMatching(DAO_IS_NULL);
+            .hasMessageMatching(DAO_IS_NULL);
 
         // null collections should be OK
         assertThatCode(() -> prov.cudPolicyStatus(pfDao, null, null, null)).doesNotThrowAnyException();