diff options
author | Jim Hahn <jrh3@att.com> | 2021-06-10 17:38:58 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2021-06-14 08:21:04 -0400 |
commit | 8cbad257df2d5f5c585ce37e61cfd16e402be738 (patch) | |
tree | 18027d19e9c6c231e41e4321d5938c8b0a5d5632 /models-dao/src | |
parent | 1b460194bc034443ba57fa690d294131836a3e9e (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>
Diffstat (limited to 'models-dao/src')
6 files changed, 190 insertions, 145 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); } } |