summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-06-10 17:38:58 -0400
committerJim Hahn <jrh3@att.com>2021-06-14 08:21:04 -0400
commit8cbad257df2d5f5c585ce37e61cfd16e402be738 (patch)
tree18027d19e9c6c231e41e4321d5938c8b0a5d5632
parent1b460194bc034443ba57fa690d294131836a3e9e (diff)
Fix sonars in policy-models
Fixed: - too many parameters in method call Fixed it by introducing PfFilterParameters. However, classes having the Builder annotation are not easily subclassed, so introduced an interface, too. Issue-ID: POLICY-3094 Change-Id: Ida99522a542b1296b367c55b7e8f8e83783c2e4f Signed-off-by: Jim Hahn <jrh3@att.com>
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java14
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/PfFilter.java71
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParameters.java40
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParametersIntfc.java40
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java38
-rw-r--r--models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java132
-rw-r--r--models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java68
-rw-r--r--models-pap/src/test/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProviderTest.java34
-rw-r--r--models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpFilterParameters.java64
-rw-r--r--models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpProvider.java7
-rw-r--r--models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProvider.java39
-rw-r--r--models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpFilterParametersTest.java53
-rw-r--r--models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProviderTest.java28
13 files changed, 451 insertions, 177 deletions
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java
index 378ae59b6..7358ab11b 100644
--- a/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java
@@ -24,7 +24,6 @@ package org.onap.policy.models.dao;
import java.time.Instant;
import java.util.Collection;
import java.util.List;
-import java.util.Map;
import org.onap.policy.models.base.PfConcept;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfGeneratedIdKey;
@@ -168,19 +167,10 @@ public interface PfDao {
* @param someClass the class of the object to get, a subclass of {@link PfConcept}, if name is null, all concepts
* of type T are returned, if name is not null and version is null, all versions of that concept matching the
* name are returned.
- * @param name the name of the object to get, null returns all objects
- * @param version the version the object to get, null returns all objects for a specified name
- * @param startTime the start timeStamp to filter from database, filter rule: startTime <= filteredRecord timeStamp
- * <= endTime. null for ignore start time.
- * @param endTime the end timeStamp to filter from database, filter rule: startTime <= filteredRecord timeStamp <=
- * endTime. null for ignore end time
- * @param filterMap Map store extra key/value used to filter from database, can be null.
- * @param sortOrder sortOrder to query database
- * @param getRecordNum Total query count from database
+ * @param filterParams filter parameters
* @return the objects that was retrieved from the database
*/
- <T extends PfConcept> List<T> getFiltered(Class<T> someClass, String name, String version, Instant startTime,
- Instant endTime, Map<String, Object> filterMap, String sortOrder, int getRecordNum);
+ <T extends PfConcept> List<T> getFiltered(Class<T> someClass, PfFilterParametersIntfc filterParams);
/**
* Get an object from the database, referred to by concept key.
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfFilter.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfFilter.java
index 9ba2b4ee6..e00e1d6bf 100644
--- a/models-dao/src/main/java/org/onap/policy/models/dao/PfFilter.java
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfFilter.java
@@ -21,9 +21,11 @@
package org.onap.policy.models.dao;
-import java.time.Instant;
+import java.sql.Timestamp;
import java.util.Map;
+import javax.persistence.TypedQuery;
import lombok.Data;
+import org.onap.policy.models.base.PfConcept;
/**
* This abstract class is used as a base for the filter implementations.
*
@@ -31,6 +33,7 @@ import lombok.Data;
@Data
public abstract class PfFilter {
+ private static final String WHERE = " WHERE ";
private static final String AND = " AND ";
private static final String ORDER = " ORDER BY ";
@@ -42,35 +45,22 @@ public abstract class PfFilter {
private String keyPrefix;
/**
- * Generates filter string.
- *
- * @param inputFilterString current filterString generated from FilterMap
- * @param name the pdpInstance name for the PDP statistics to get
- * @param startTime the start timeStamp to filter from database, filter rule:
- * startTime <= filteredRecord timeStamp <= endTime. null for ignore end time
- * @param endTime the end timeStamp to filter from database, filter rule:
- * startTime <= filteredRecord timeStamp <= endTime. null for ignore end time
- * @param filterMap Map store extra key/value used to filter from database, can be null *
- * @param sortOrder sortOrder to query database
- * @param getRecordNum Total query count from database
-
+ * Generates the "WHERE" (and "ORDER BY") clause for a JPA query.
*/
- public String addFilter(final String inputFilterString,
- final String name, final Instant startTime, final Instant endTime,
- final Map<String, Object> filterMap, final String sortOrder, final int getRecordNum) {
- var filterQueryString = new StringBuilder(inputFilterString);
- if (filterMap != null) {
- for (String key : filterMap.keySet()) {
+ public String genWhereClause(PfFilterParametersIntfc parameters) {
+ var filterQueryString = new StringBuilder(WHERE);
+ if (parameters.getFilterMap() != null) {
+ for (String key : parameters.getFilterMap().keySet()) {
filterQueryString.append(getKeyPrefix() + key + "= :" + key + AND);
}
}
- if (name != null) {
+ if (parameters.getName() != null) {
filterQueryString.append(getNameFilter() + AND);
}
- if (startTime != null) {
- if (endTime != null) {
+ if (parameters.getStartTime() != null) {
+ if (parameters.getEndTime() != null) {
filterQueryString.append(getTimeStampStartFilter());
filterQueryString.append(AND);
filterQueryString.append(getTimeStampEndFilter());
@@ -78,16 +68,47 @@ public abstract class PfFilter {
filterQueryString.append(getTimeStampStartFilter());
}
} else {
- if (endTime != null) {
+ if (parameters.getEndTime() != null) {
filterQueryString.append(getTimeStampEndFilter());
} else {
filterQueryString.delete(filterQueryString.length() - AND.length(), filterQueryString.length());
}
}
- if (getRecordNum > 0) {
- filterQueryString.append(ORDER + getTimeStampFilter() + sortOrder);
+ if (parameters.getRecordNum() > 0) {
+ filterQueryString.append(ORDER + getTimeStampFilter() + parameters.getSortOrder());
}
return filterQueryString.toString();
}
+
+ /**
+ * Sets the JPA query parameters, based on the filter parameters.
+ * @param query query to populate
+ */
+ public <T extends PfConcept> void setParams(TypedQuery<T> query, PfFilterParametersIntfc parameters) {
+
+ if (parameters.getFilterMap() != null) {
+ for (Map.Entry<String, Object> entry : parameters.getFilterMap().entrySet()) {
+ query.setParameter(entry.getKey(), entry.getValue());
+ }
+ }
+ if (parameters.getName() != null) {
+ query.setParameter(this.getNameParameter(), parameters.getName());
+ }
+ if (parameters.getStartTime() != null) {
+ if (parameters.getEndTime() != null) {
+ query.setParameter("startTime", Timestamp.from(parameters.getStartTime()));
+ query.setParameter("endTime", Timestamp.from(parameters.getEndTime()));
+ } else {
+ query.setParameter("startTime", Timestamp.from(parameters.getStartTime()));
+ }
+ } else {
+ if (parameters.getEndTime() != null) {
+ query.setParameter("endTime", Timestamp.from(parameters.getEndTime()));
+ }
+ }
+ if (parameters.getRecordNum() > 0) {
+ query.setMaxResults(parameters.getRecordNum());
+ }
+ }
}
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParameters.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParameters.java
new file mode 100644
index 000000000..24a2ad2e4
--- /dev/null
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParameters.java
@@ -0,0 +1,40 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 AT&T Intellectual Property. 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.dao;
+
+import java.time.Instant;
+import java.util.Map;
+import lombok.Builder;
+import lombok.Getter;
+
+@Getter
+@Builder
+public class PfFilterParameters implements PfFilterParametersIntfc {
+ private String name;
+ private String version;
+ private Instant startTime;
+ private Instant endTime;
+ private Map<String, Object> filterMap;
+
+ private int recordNum;
+ @Builder.Default
+ private String sortOrder = "DESC";
+}
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParametersIntfc.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParametersIntfc.java
new file mode 100644
index 000000000..7a383cd9c
--- /dev/null
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParametersIntfc.java
@@ -0,0 +1,40 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 AT&T Intellectual Property. 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.dao;
+
+import java.time.Instant;
+import java.util.Map;
+
+public interface PfFilterParametersIntfc {
+ String getName();
+
+ String getVersion();
+
+ Instant getStartTime();
+
+ Instant getEndTime();
+
+ Map<String, Object> getFilterMap();
+
+ int getRecordNum();
+
+ String getSortOrder();
+}
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java
index 336abf7df..95a5bf3fa 100644
--- a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java
@@ -26,7 +26,6 @@ import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
-import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
@@ -46,6 +45,7 @@ import org.onap.policy.models.dao.DaoParameters;
import org.onap.policy.models.dao.PfDao;
import org.onap.policy.models.dao.PfFilter;
import org.onap.policy.models.dao.PfFilterFactory;
+import org.onap.policy.models.dao.PfFilterParametersIntfc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -388,43 +388,15 @@ public class DefaultPfDao implements PfDao {
}
@Override
- public <T extends PfConcept> List<T> getFiltered(final Class<T> someClass, final String name, final String version,
- final Instant startTime, final Instant endTime, final Map<String, Object> filterMap, final String sortOrder,
- final int getRecordNum) {
+ public <T extends PfConcept> List<T> getFiltered(final Class<T> someClass, PfFilterParametersIntfc filterParams) {
final var mg = getEntityManager();
- var filterQueryString = SELECT_FROM_TABLE + WHERE;
-
try {
- PfFilter timeStampFilter = new PfFilterFactory().createFilter(someClass);
- filterQueryString = timeStampFilter.addFilter(filterQueryString, name, startTime, endTime, filterMap,
- sortOrder, getRecordNum);
+ PfFilter filter = new PfFilterFactory().createFilter(someClass);
+ String filterQueryString = SELECT_FROM_TABLE + filter.genWhereClause(filterParams);
TypedQuery<T> query = mg.createQuery(setQueryTable(filterQueryString, someClass), someClass);
-
- if (filterMap != null) {
- for (Map.Entry<String, Object> entry : filterMap.entrySet()) {
- query.setParameter(entry.getKey(), entry.getValue());
- }
- }
- if (name != null) {
- query.setParameter(timeStampFilter.getNameParameter(), name);
- }
- if (startTime != null) {
- if (endTime != null) {
- query.setParameter("startTime", Timestamp.from(startTime));
- query.setParameter("endTime", Timestamp.from(endTime));
- } else {
- query.setParameter("startTime", Timestamp.from(startTime));
- }
- } else {
- if (endTime != null) {
- query.setParameter("endTime", Timestamp.from(endTime));
- }
- }
- if (getRecordNum > 0) {
- query.setMaxResults(getRecordNum);
- }
+ filter.setParams(query, filterParams);
LOGGER.debug("filterQueryString is \"{}\"", filterQueryString);
return query.getResultList();
diff --git a/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java b/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java
index 59fc9b482..c0b0f2ad0 100644
--- a/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java
+++ b/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java
@@ -21,6 +21,7 @@
package org.onap.policy.models.dao;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
@@ -598,11 +599,11 @@ public class EntityTest {
pfDao.create(keyInfo4);
pfDao.create(keyInfo5);
- assertEquals(6, pfDao.getFiltered(DummyConceptEntity.class, null, null).size());
- assertEquals(3, pfDao.getFiltered(DummyConceptEntity.class, "AAA0", null).size());
- assertEquals(3, pfDao.getFiltered(DummyConceptEntity.class, "BBB0", null).size());
- assertEquals(1, pfDao.getFiltered(DummyConceptEntity.class, "BBB0", VERSION003).size());
- assertEquals(6, pfDao.getFiltered(DummyConceptEntity.class, null, VERSION003).size());
+ assertThat(pfDao.getFiltered(DummyConceptEntity.class, null, null)).hasSize(6);
+ assertThat(pfDao.getFiltered(DummyConceptEntity.class, "AAA0", null)).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyConceptEntity.class, "BBB0", null)).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyConceptEntity.class, "BBB0", VERSION003)).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyConceptEntity.class, null, VERSION003)).hasSize(6);
final PfTimestampKey atKey0 = new PfTimestampKey("AT-KEY0", VERSION001, TIMESTAMP0);
final PfTimestampKey atKey1 = new PfTimestampKey("AT-KEY1", VERSION001, TIMESTAMP1);
@@ -616,28 +617,25 @@ public class EntityTest {
pfDao.create(tkeyInfo2);
- assertEquals(1, pfDao
- .getFiltered(DummyTimestampEntity.class, "AT-KEY0", VERSION001, null, null, null, "DESC", 0).size());
- assertEquals(1,
- pfDao.getFiltered(DummyTimestampEntity.class, "AT-KEY0", null, null, null, null, "DESC", 0).size());
- assertEquals(3, pfDao
- .getFiltered(DummyTimestampEntity.class, null, VERSION001, TIMESTAMP0, TIMESTAMP2, null, "DESC", 0)
- .size());
- assertEquals(1, pfDao
- .getFiltered(DummyTimestampEntity.class, "AT-KEY0", VERSION001, TIMESTAMP0, TIMESTAMP2, null, "DESC", 0)
- .size());
- assertEquals(3, pfDao
- .getFiltered(DummyTimestampEntity.class, null, VERSION001, null, TIMESTAMP2, null, "DESC", 0).size());
- assertEquals(3, pfDao
- .getFiltered(DummyTimestampEntity.class, null, VERSION001, TIMESTAMP0, null, null, "DESC", 0).size());
- assertEquals(2,
- pfDao.getFiltered(DummyTimestampEntity.class, null, VERSION001, TIMESTAMP0, TIMESTAMP2, null, "DESC", 2)
- .size());
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class,
+ PfFilterParameters.builder().name("AT-KEY0").version(VERSION001).build())).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class, PfFilterParameters.builder().name("AT-KEY0").build()))
+ .hasSize(1);
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class, PfFilterParameters.builder().version(VERSION001)
+ .startTime(TIMESTAMP0).endTime(TIMESTAMP2).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class, PfFilterParameters.builder().name("AT-KEY0")
+ .version(VERSION001).startTime(TIMESTAMP0).endTime(TIMESTAMP2).build())).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class,
+ PfFilterParameters.builder().version(VERSION001).endTime(TIMESTAMP2).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class,
+ PfFilterParameters.builder().version(VERSION001).startTime(TIMESTAMP0).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class, PfFilterParameters.builder().version(VERSION001)
+ .startTime(TIMESTAMP0).endTime(TIMESTAMP2).sortOrder("DESC").recordNum(2).build())).hasSize(2);
Map<String, Object> filterMap = new HashMap<>();
filterMap.put("doubleValue", 200.1);
- assertEquals(1,
- pfDao.getFiltered(DummyTimestampEntity.class, null, null, null, null, filterMap, "DESC", 0).size());
+ assertThat(pfDao.getFiltered(DummyTimestampEntity.class,
+ PfFilterParameters.builder().filterMap(filterMap).build())).hasSize(1);
}
private void testgetFilteredOps2() {
@@ -656,29 +654,23 @@ public class EntityTest {
pfDao.create(gkeyInfo2);
- assertEquals(1, pfDao
- .getFiltered(DummyGeneratedIdEntity.class, "AT-KEY0", VERSION001, null, null, null, "DESC", 0).size());
- assertEquals(1,
- pfDao.getFiltered(DummyGeneratedIdEntity.class, "AT-KEY0", null, null, null, null, "DESC", 0).size());
- assertEquals(3, pfDao
- .getFiltered(DummyGeneratedIdEntity.class, null, VERSION001, TIMESTAMP0, TIMESTAMP2, null, "DESC", 0)
- .size());
- assertEquals(1, pfDao
- .getFiltered(DummyGeneratedIdEntity.class, "AT-KEY0", VERSION001,
- TIMESTAMP0, TIMESTAMP2, null, "DESC", 0)
- .size());
- assertEquals(3, pfDao
- .getFiltered(DummyGeneratedIdEntity.class, null, VERSION001, null, TIMESTAMP2, null, "DESC", 0).size());
- assertEquals(3, pfDao
- .getFiltered(DummyGeneratedIdEntity.class, null, VERSION001,
- TIMESTAMP0, null, null, "DESC", 0).size());
- assertEquals(2,
- pfDao.getFiltered(DummyGeneratedIdEntity.class, null, VERSION001,
- TIMESTAMP0, TIMESTAMP2, null, "DESC", 2)
- .size());
-
- assertEquals(1,
- pfDao.getFiltered(DummyGeneratedIdEntity.class, null, null, null, null, filterMap, "DESC", 0).size());
+ assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class,
+ PfFilterParameters.builder().name("AT-KEY0").version(VERSION001).build())).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class,
+ PfFilterParameters.builder().name("AT-KEY0").build())).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class, PfFilterParameters.builder().version(VERSION001)
+ .startTime(TIMESTAMP0).endTime(TIMESTAMP2).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class, PfFilterParameters.builder().name("AT-KEY0")
+ .version(VERSION001).startTime(TIMESTAMP0).endTime(TIMESTAMP2).build())).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class,
+ PfFilterParameters.builder().version(VERSION001).endTime(TIMESTAMP2).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class,
+ PfFilterParameters.builder().version(VERSION001).startTime(TIMESTAMP0).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class, PfFilterParameters.builder().version(VERSION001)
+ .startTime(TIMESTAMP0).endTime(TIMESTAMP2).sortOrder("DESC").recordNum(2).build())).hasSize(2);
+
+ assertThat(pfDao.getFiltered(DummyGeneratedIdEntity.class,
+ PfFilterParameters.builder().filterMap(filterMap).build())).hasSize(1);
}
private void testgetFilteredOps3() {
@@ -700,33 +692,23 @@ public class EntityTest {
pfDao.create(rkeyInfo2);
- assertEquals(1, pfDao
- .getFiltered(DummyReferenceTimestampEntity.class,
- "Owner0", VERSION001, null, null, null, "DESC", 0).size());
- assertEquals(1,
- pfDao.getFiltered(DummyReferenceTimestampEntity.class,
- "Owner0", null, null, null, null, "DESC", 0).size());
- assertEquals(3, pfDao
- .getFiltered(DummyReferenceTimestampEntity.class,
- null, VERSION001, TIMESTAMP0, TIMESTAMP2, null, "DESC", 0)
- .size());
- assertEquals(1, pfDao
- .getFiltered(DummyReferenceTimestampEntity.class,
- "Owner0", VERSION001, TIMESTAMP0, TIMESTAMP2, null, "DESC", 0)
- .size());
- assertEquals(3, pfDao
- .getFiltered(DummyReferenceTimestampEntity.class, null,
- VERSION001, null, TIMESTAMP2, null, "DESC", 0).size());
- assertEquals(3, pfDao
- .getFiltered(DummyReferenceTimestampEntity.class, null,
- VERSION001, TIMESTAMP0, null, null, "DESC", 0).size());
- assertEquals(2,
- pfDao.getFiltered(DummyReferenceTimestampEntity.class,
- null, VERSION001, TIMESTAMP0, TIMESTAMP2, null, "DESC", 2)
- .size());
-
- assertEquals(1,
- pfDao.getFiltered(DummyReferenceTimestampEntity.class,
- null, null, null, null, filterMap, "DESC", 0).size());
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class,
+ PfFilterParameters.builder().name("Owner0").version(VERSION001).build())).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class,
+ PfFilterParameters.builder().name("Owner0").build())).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class, PfFilterParameters.builder()
+ .version(VERSION001).startTime(TIMESTAMP0).endTime(TIMESTAMP2).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class, PfFilterParameters.builder().name("Owner0")
+ .version(VERSION001).startTime(TIMESTAMP0).endTime(TIMESTAMP2).build())).hasSize(1);
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class,
+ PfFilterParameters.builder().version(VERSION001).endTime(TIMESTAMP2).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class,
+ PfFilterParameters.builder().version(VERSION001).startTime(TIMESTAMP0).build())).hasSize(3);
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class,
+ PfFilterParameters.builder().version(VERSION001).startTime(TIMESTAMP0).endTime(TIMESTAMP2)
+ .sortOrder("DESC").recordNum(2).build())).hasSize(2);
+
+ assertThat(pfDao.getFiltered(DummyReferenceTimestampEntity.class,
+ PfFilterParameters.builder().filterMap(filterMap).build())).hasSize(1);
}
}
diff --git a/models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java b/models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java
index 8923fb23f..ca8f18dc6 100644
--- a/models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java
+++ b/models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java
@@ -1,6 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2021 Nordix Foundation.
+ * Modifications Copyright (C) 2021 AT&T Intellectual Property. 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.
@@ -33,6 +34,7 @@ import org.apache.commons.lang3.StringUtils;
import org.onap.policy.common.parameters.BeanValidationResult;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.dao.PfDao;
+import org.onap.policy.models.dao.PfFilterParametersIntfc;
import org.onap.policy.models.pap.concepts.PolicyAudit;
import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
import org.onap.policy.models.pap.persistence.concepts.JpaPolicyAudit;
@@ -46,7 +48,6 @@ import org.onap.policy.models.pap.persistence.concepts.JpaPolicyAudit;
public class PolicyAuditProvider {
private static final Integer DEFAULT_MAX_RECORDS = 100;
- private static final String DESCENDING_ORDER = "DESC";
/**
* Create audit records.
@@ -96,24 +97,22 @@ public class PolicyAuditProvider {
*/
public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter,
@NonNull Integer numRecords) {
- numRecords = numRecords > DEFAULT_MAX_RECORDS ? DEFAULT_MAX_RECORDS : numRecords;
- Map<String, Object> filter = new HashMap<>();
- if (StringUtils.isNotBlank(auditFilter.getPdpGroup())) {
- filter.put("pdpGroup", auditFilter.getPdpGroup());
- }
+ auditFilter.setRecordNum(Math.min(numRecords, DEFAULT_MAX_RECORDS));
- if (auditFilter.getAction() != null) {
- filter.put("action", auditFilter.getAction());
- }
+ return getAuditRecords(dao, auditFilter);
+ }
- // @formatter:off
- return dao.getFiltered(JpaPolicyAudit.class,
- auditFilter.getName(), auditFilter.getVersion(),
- auditFilter.getFromDate(), auditFilter.getToDate(),
- filter, DESCENDING_ORDER, numRecords)
- .stream().map(JpaPolicyAudit::toAuthorative).collect(Collectors.toList());
- // @formatter:on
+ /**
+ * Collect audit records based on filters at {@link AuditFilter}.
+ *
+ * @param auditFilter {@link AuditFilter} object with filters for search
+ * @return list of {@link PolicyAudit} records
+ */
+ public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter) {
+
+ return dao.getFiltered(JpaPolicyAudit.class, auditFilter)
+ .stream().map(JpaPolicyAudit::toAuthorative).collect(Collectors.toList());
}
/**
@@ -126,13 +125,19 @@ public class PolicyAuditProvider {
*/
@Data
@Builder
- public static class AuditFilter {
+ public static class AuditFilter implements PfFilterParametersIntfc {
private String name;
private String version;
private AuditAction action;
private String pdpGroup;
private Instant fromDate;
private Instant toDate;
+ private int recordNum;
+ @Builder.Default
+ private String sortOrder = "DESC";
+
+ // initialized lazily, if not set via the builder
+ private Map<String, Object> filterMap;
/**
* Check if even still using build(), none of the params were provided.
@@ -143,5 +148,34 @@ public class PolicyAuditProvider {
return StringUtils.isAllEmpty(name, version, pdpGroup) && action == null && fromDate == null
&& toDate == null;
}
+
+ @Override
+ public Instant getStartTime() {
+ return fromDate;
+ }
+
+ @Override
+ public Instant getEndTime() {
+ return toDate;
+ }
+
+ @Override
+ public Map<String, Object> getFilterMap() {
+ if (filterMap != null) {
+ return filterMap;
+ }
+
+ filterMap = new HashMap<>();
+
+ if (StringUtils.isNotBlank(pdpGroup)) {
+ filterMap.put("pdpGroup", pdpGroup);
+ }
+
+ if (action != null) {
+ filterMap.put("action", action);
+ }
+
+ return filterMap;
+ }
}
}
diff --git a/models-pap/src/test/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProviderTest.java b/models-pap/src/test/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProviderTest.java
index 228a3fc01..0540d4844 100644
--- a/models-pap/src/test/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProviderTest.java
+++ b/models-pap/src/test/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProviderTest.java
@@ -134,7 +134,37 @@ public class PolicyAuditProviderTest {
PolicyAuditProvider provider = new PolicyAuditProvider();
Instant date = Instant.now().truncatedTo(ChronoUnit.SECONDS);
- System.out.println(date);
+ provider.createAuditRecords(pfDao, generatePolicyAudits(date, GROUP_A, MY_POLICY));
+ provider.createAuditRecords(pfDao, generatePolicyAudits(date, GROUP_B, MY_POLICY));
+ provider.createAuditRecords(pfDao, generatePolicyAudits(date, GROUP_B, MY_POLICY2));
+ Awaitility.await().pollDelay(3, TimeUnit.SECONDS).until(() -> {
+ return true;
+ });
+
+ List<PolicyAudit> records = provider.getAuditRecords(pfDao,
+ AuditFilter.builder().fromDate(date).toDate(Instant.now()).recordNum(NUMBER_RECORDS).build());
+ assertThat(records).hasSize(6);
+
+ List<PolicyAudit> recordsWithGroupB =
+ provider.getAuditRecords(pfDao,
+ AuditFilter.builder().pdpGroup(GROUP_B).recordNum(NUMBER_RECORDS).build());
+ assertThat(recordsWithGroupB).hasSize(4);
+
+ List<PolicyAudit> recordsWithActionDeploy = provider.getAuditRecords(pfDao,
+ AuditFilter.builder().action(AuditAction.DEPLOYMENT).recordNum(NUMBER_RECORDS).build());
+ assertThat(recordsWithActionDeploy).hasSize(3);
+
+ List<PolicyAudit> recordsWithMyPolicy = provider.getAuditRecords(pfDao,
+ AuditFilter.builder().name(MY_POLICY.getName()).version(MY_POLICY.getVersion())
+ .recordNum(NUMBER_RECORDS).build());
+ assertThat(recordsWithMyPolicy).hasSize(4);
+ }
+
+ @Test
+ public void testFiltersOld() {
+ PolicyAuditProvider provider = new PolicyAuditProvider();
+
+ Instant date = Instant.now().truncatedTo(ChronoUnit.SECONDS);
provider.createAuditRecords(pfDao, generatePolicyAudits(date, GROUP_A, MY_POLICY));
provider.createAuditRecords(pfDao, generatePolicyAudits(date, GROUP_B, MY_POLICY));
provider.createAuditRecords(pfDao, generatePolicyAudits(date, GROUP_B, MY_POLICY2));
@@ -202,7 +232,7 @@ public class PolicyAuditProviderTest {
}).hasMessageMatching(String.format(FIELD_IS_NULL, "dao"));
assertThatThrownBy(() -> {
- provider.getAuditRecords(pfDao, null);
+ provider.getAuditRecords(pfDao, (Integer) null);
}).hasMessageMatching(String.format(FIELD_IS_NULL, "numRecords"));
assertThatThrownBy(() -> {
diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpFilterParameters.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpFilterParameters.java
new file mode 100644
index 000000000..6cede2c39
--- /dev/null
+++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpFilterParameters.java
@@ -0,0 +1,64 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 AT&T Intellectual Property. 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.pdp.persistence.provider;
+
+import java.time.Instant;
+import java.util.Map;
+import lombok.Builder;
+import lombok.Getter;
+import org.onap.policy.models.dao.PfFilterParametersIntfc;
+
+@Getter
+@Builder
+public class PdpFilterParameters implements PfFilterParametersIntfc {
+ private String name;
+ private String version;
+ private Instant startTime;
+ private Instant endTime;
+
+ private int recordNum;
+ @Builder.Default
+ private String sortOrder = "DESC";
+
+ private String group;
+ private String subGroup;
+
+ // initialized lazily, if not set via the builder
+ private Map<String, Object> filterMap;
+
+ @Override
+ public Map<String, Object> getFilterMap() {
+ if (filterMap != null) {
+ return filterMap;
+
+ } else if (group == null) {
+ return null;
+
+ } else if (subGroup == null) {
+ filterMap = Map.of("pdpGroupName", group);
+ return filterMap;
+
+ } else {
+ filterMap = Map.of("pdpGroupName", group, "pdpSubGroupName", subGroup);
+ return filterMap;
+ }
+ }
+}
diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpProvider.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpProvider.java
index 4ffb1e40e..fec8ec572 100644
--- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpProvider.java
+++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpProvider.java
@@ -36,6 +36,7 @@ import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.base.PfReferenceKey;
import org.onap.policy.models.dao.PfDao;
+import org.onap.policy.models.dao.PfFilterParameters;
import org.onap.policy.models.pdp.concepts.Pdp;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
@@ -300,10 +301,10 @@ public class PdpProvider {
public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull final PfDao dao, @NonNull final String groupName)
throws PfModelException {
- Map<String, Object> filter = Map.of("pdpGroup", groupName);
+ PfFilterParameters params = PfFilterParameters.builder().filterMap(Map.of("pdpGroup", groupName)).build();
- return dao.getFiltered(JpaPdpPolicyStatus.class, null, null, null, null, filter, null, 0).stream()
- .map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
+ return dao.getFiltered(JpaPdpPolicyStatus.class, params)
+ .stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
}
/**
diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProvider.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProvider.java
index 205761bf5..ea118f362 100644
--- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProvider.java
+++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProvider.java
@@ -37,6 +37,7 @@ import org.onap.policy.models.base.PfKey;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.dao.PfDao;
+import org.onap.policy.models.dao.PfFilterParameters;
import org.onap.policy.models.pdp.concepts.PdpStatistics;
import org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics;
@@ -47,8 +48,6 @@ import org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics;
* @author Ning Xi (ning.xi@est.tech)
*/
public class PdpStatisticsProvider {
- // Recurring string constants
- private static final String DESC_ORDER = "DESC";
/**
* Get PDP statistics.
@@ -113,9 +112,32 @@ public class PdpStatisticsProvider {
filterMap.put("pdpSubGroupName", pdpSubGroup);
}
- return asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, name,
- PfKey.NULL_KEY_VERSION, startTimeStamp,
- endTimeStamp, filterMap, sortOrder, getRecordNum));
+ // @formatter:off
+ return asPdpStatisticsList(
+ dao.getFiltered(JpaPdpStatistics.class,
+ PdpFilterParameters.builder()
+ .name(name)
+ .startTime(startTimeStamp)
+ .endTime(endTimeStamp)
+ .group(pdpGroupName)
+ .subGroup(pdpSubGroup)
+ .sortOrder(sortOrder)
+ .recordNum(getRecordNum)
+ .build()));
+ // @formatter:on
+ }
+
+ /**
+ * Get filtered PDP statistics.
+ *
+ * @param dao the DAO to use to access the database
+ * @param filterParams filter parameters
+ * @return the PDP statistics found
+ * @throws PfModelException on errors getting policies
+ */
+ public List<PdpStatistics> getFilteredPdpStatistics(@NonNull final PfDao dao,
+ PdpFilterParameters filterParams) {
+ return asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, filterParams));
}
/**
@@ -198,10 +220,9 @@ public class PdpStatisticsProvider {
* @throws PfModelException on errors deleting PDP statistics
*/
public List<PdpStatistics> deletePdpStatistics(@NonNull final PfDao dao, @NonNull final String name,
- final Instant timestamp) {
- List<PdpStatistics> pdpStatisticsListToDel = asPdpStatisticsList(
- dao.getFiltered(JpaPdpStatistics.class, name,
- PfKey.NULL_KEY_VERSION, timestamp, timestamp, null, DESC_ORDER, 0));
+ final Instant timestamp) {
+ List<PdpStatistics> pdpStatisticsListToDel = asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class,
+ PfFilterParameters.builder().name(name).startTime(timestamp).endTime(timestamp).build()));
pdpStatisticsListToDel.stream().forEach(s -> dao.delete(JpaPdpStatistics.class,
new PfGeneratedIdKey(s.getPdpInstanceId(), PfKey.NULL_KEY_VERSION, s.getGeneratedId())));
diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpFilterParametersTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpFilterParametersTest.java
new file mode 100644
index 000000000..58ff7f171
--- /dev/null
+++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpFilterParametersTest.java
@@ -0,0 +1,53 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 AT&T Intellectual Property. 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.pdp.persistence.provider;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Map;
+import org.junit.Test;
+
+public class PdpFilterParametersTest {
+
+ private static final String GROUP = "my-group";
+ private static final String SUBGROUP = "my-subgroup";
+
+ @Test
+ public void testGetFilterMap() {
+ assertThat(PdpFilterParameters.builder().build().getFilterMap()).isNull();
+
+ assertThat(PdpFilterParameters.builder().subGroup(SUBGROUP).build().getFilterMap()).isNull();
+
+ PdpFilterParameters params = PdpFilterParameters.builder().group(GROUP).build();
+ Map<String, Object> map = params.getFilterMap();
+ assertThat(map).isEqualTo(Map.of("pdpGroupName", GROUP));
+
+ // should not re-create the map
+ assertThat(params.getFilterMap()).isSameAs(map);
+
+ params = PdpFilterParameters.builder().group(GROUP).subGroup(SUBGROUP).build();
+ map = params.getFilterMap();
+ assertThat(map).isEqualTo(Map.of("pdpGroupName", GROUP, "pdpSubGroupName", SUBGROUP));
+
+ // should not re-create the map
+ assertThat(params.getFilterMap()).isSameAs(map);
+ }
+}
diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProviderTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProviderTest.java
index 8259dc0c6..91eb54e8b 100644
--- a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProviderTest.java
+++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProviderTest.java
@@ -181,7 +181,7 @@ public class PdpStatisticsProviderTest {
}
@Test
- public void testGetFilteredPdpStatistics() throws Exception {
+ public void testGetFilteredPdpStatisticsOld() throws Exception {
assertThatThrownBy(() -> {
new PdpStatisticsProvider().getFilteredPdpStatistics(null, NAME, GROUP, SUBGROUP, TIMESTAMP1, TIMESTAMP2,
ORDER, 1);
@@ -211,6 +211,32 @@ public class PdpStatisticsProviderTest {
}
@Test
+ public void testGetFilteredPdpStatistics() throws Exception {
+
+ assertThatThrownBy(() -> {
+ new PdpStatisticsProvider().getFilteredPdpStatistics(null, PdpFilterParameters.builder().build());
+ }).hasMessageMatching(DAO_IS_NULL);
+
+
+ List<PdpStatistics> createdPdpStatisticsList;
+ createdPdpStatisticsList = new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsTestList);
+ createdListStr = createdPdpStatisticsList.toString();
+ assertEquals(createdListStr.replaceAll("\\s+", ""), testListStr.replaceAll("\\s+", ""));
+
+ List<PdpStatistics> getPdpStatisticsList;
+ getPdpStatisticsList = new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, PdpFilterParameters
+ .builder().name(NAME).group(GROUP).startTime(TIMESTAMP1).endTime(TIMESTAMP2).build());
+ assertThat(getPdpStatisticsList).hasSize(1);
+ getPdpStatisticsList = new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, PdpFilterParameters
+ .builder().name("name2").group(GROUP).startTime(TIMESTAMP1).endTime(TIMESTAMP2).build());
+ assertThat(getPdpStatisticsList).hasSize(1);
+ getPdpStatisticsList = new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao,
+ PdpFilterParameters.builder().name("name2").group(GROUP).subGroup(SUBGROUP)
+ .startTime(TIMESTAMP1).endTime(TIMESTAMP2).build());
+ assertThat(getPdpStatisticsList).hasSize(1);
+ }
+
+ @Test
public void testUpdatePdpStatistics() throws Exception {
assertThatThrownBy(() -> {
new PdpStatisticsProvider().updatePdpStatistics(null, null);