diff options
author | ktimoney <kevin.timoney@est.tech> | 2021-04-27 11:26:09 +0100 |
---|---|---|
committer | ktimoney <kevin.timoney@est.tech> | 2021-04-27 16:17:43 +0100 |
commit | eb830bfda2a18618032434aa7ebe3501bca2cca7 (patch) | |
tree | a1222d4b4d2f66e436ee6499b1f842d68982f516 /models-dao/src | |
parent | 6e589fc31f69482d2cf5bf57c733094394439b0f (diff) |
Add correct filter type prefix to query
Added unit tests
Issue-ID: POLICY-3247
Change-Id: I27474948619b7256a1e504611531eecac15c27c1
Signed-off-by: ktimoney <kevin.timoney@est.tech>
Diffstat (limited to 'models-dao/src')
7 files changed, 217 insertions, 1 deletions
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 ef9d4f939..fbe81b3ff 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 @@ -38,6 +38,7 @@ public abstract class PfFilter { private String timeStampEndFilter; private String timeStampFilter; private String nameParameter; + private String keyPrefix; /** * Generates filter string. @@ -59,7 +60,7 @@ public abstract class PfFilter { StringBuilder filterQueryString = new StringBuilder(inputFilterString); if (filterMap != null) { for (String key : filterMap.keySet()) { - filterQueryString.append("c." + key + "= :" + key + AND); + filterQueryString.append(getKeyPrefix() + key + "= :" + key + AND); } } diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/impl/PfNonTimestampKeyFilter.java b/models-dao/src/main/java/org/onap/policy/models/dao/impl/PfNonTimestampKeyFilter.java index 9c9a3e844..ac3224022 100644 --- a/models-dao/src/main/java/org/onap/policy/models/dao/impl/PfNonTimestampKeyFilter.java +++ b/models-dao/src/main/java/org/onap/policy/models/dao/impl/PfNonTimestampKeyFilter.java @@ -32,6 +32,7 @@ public class PfNonTimestampKeyFilter extends PfFilter { private static final String TIMESTAMP_END_FILTER = "c.timeStamp <= :endTime"; private static final String TIMESTAMP_FILTER = " c.timeStamp "; private static final String NAME_PARAMETER = "name"; + private static final String KEY_PREFIX = "c."; /** * The default constructor injects query strings. @@ -42,5 +43,6 @@ public class PfNonTimestampKeyFilter extends PfFilter { setTimeStampEndFilter(TIMESTAMP_END_FILTER); setTimeStampFilter(TIMESTAMP_FILTER); setNameParameter(NAME_PARAMETER); + setKeyPrefix(KEY_PREFIX); } } diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/impl/PfReferenceTimestampKeyFilter.java b/models-dao/src/main/java/org/onap/policy/models/dao/impl/PfReferenceTimestampKeyFilter.java index 53802f306..42b05c19b 100644 --- a/models-dao/src/main/java/org/onap/policy/models/dao/impl/PfReferenceTimestampKeyFilter.java +++ b/models-dao/src/main/java/org/onap/policy/models/dao/impl/PfReferenceTimestampKeyFilter.java @@ -32,6 +32,7 @@ public class PfReferenceTimestampKeyFilter extends PfFilter { private static final String TIMESTAMP_END_FILTER = "c.key.timeStamp <= :endTime"; private static final String TIMESTAMP_FILTER = " c.key.timeStamp "; private static final String NAME_PARAMETER = "parentKeyName"; + private static final String KEY_PREFIX = "c.key.referenceKey."; /** * The default constructor injects query strings. @@ -42,5 +43,6 @@ public class PfReferenceTimestampKeyFilter extends PfFilter { setTimeStampEndFilter(TIMESTAMP_END_FILTER); setTimeStampFilter(TIMESTAMP_FILTER); setNameParameter(NAME_PARAMETER); + setKeyPrefix(KEY_PREFIX); } } diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/impl/PfTimestampKeyFilter.java b/models-dao/src/main/java/org/onap/policy/models/dao/impl/PfTimestampKeyFilter.java index f01f80498..f8d4fa833 100644 --- a/models-dao/src/main/java/org/onap/policy/models/dao/impl/PfTimestampKeyFilter.java +++ b/models-dao/src/main/java/org/onap/policy/models/dao/impl/PfTimestampKeyFilter.java @@ -32,6 +32,7 @@ public class PfTimestampKeyFilter extends PfFilter { private static final String TIMESTAMP_END_FILTER = "c.key.timeStamp <= :endTime"; private static final String TIMESTAMP_FILTER = " c.key.timeStamp "; private static final String NAME_PARAMETER = "name"; + private static final String KEY_PREFIX = "c."; /** * The default constructor injects query strings. @@ -42,5 +43,6 @@ public class PfTimestampKeyFilter extends PfFilter { setTimeStampEndFilter(TIMESTAMP_END_FILTER); setTimeStampFilter(TIMESTAMP_FILTER); setNameParameter(NAME_PARAMETER); + setKeyPrefix(KEY_PREFIX); } } diff --git a/models-dao/src/test/java/org/onap/policy/models/dao/DummyReferenceTimestampEntity.java b/models-dao/src/test/java/org/onap/policy/models/dao/DummyReferenceTimestampEntity.java new file mode 100644 index 000000000..b4983449b --- /dev/null +++ b/models-dao/src/test/java/org/onap/policy/models/dao/DummyReferenceTimestampEntity.java @@ -0,0 +1,103 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.dao; + +import java.util.ArrayList; +import java.util.List; +import javax.persistence.Column; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.Table; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfReferenceTimestampKey; + +@Entity +@Table(name = "DummyReferenceTimestampEntity") +@Data +@EqualsAndHashCode(callSuper = false) +public class DummyReferenceTimestampEntity extends PfConcept { + private static final long serialVersionUID = -2962570563281067895L; + + @EmbeddedId() + @NonNull + private PfReferenceTimestampKey key; + + @Column + private double doubleValue; + + /** + * Default constructor. + */ + public DummyReferenceTimestampEntity() { + this.key = new PfReferenceTimestampKey(); + } + + public DummyReferenceTimestampEntity(DummyReferenceTimestampEntity source) { + this.key = source.key; + } + + /** + * Constructor. + * + * @param key the key + */ + public DummyReferenceTimestampEntity(final PfReferenceTimestampKey key) { + this.key = key; + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = new ArrayList<>(); + keyList.add(getKey()); + return keyList; + } + + @Override + public BeanValidationResult validate(@NonNull String fieldName) { + BeanValidationResult result = new BeanValidationResult(fieldName, this); + result.addResult(key.validate("key")); + return result; + } + + @Override + public void clean() { + key.clean(); + } + + @Override + public int compareTo(@NonNull final PfConcept otherObj) { + if (this == otherObj) { + return 0; + } + if (getClass() != otherObj.getClass()) { + return this.getClass().getName().compareTo(otherObj.getClass().getName()); + } + + final DummyReferenceTimestampEntity other = (DummyReferenceTimestampEntity) otherObj; + + return key.compareTo(other.key); + } +} 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 80730743b..66fc1b502 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 @@ -44,6 +44,7 @@ import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfGeneratedIdKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfReferenceTimestampKey; import org.onap.policy.models.base.PfTimestampKey; import org.onap.policy.models.dao.impl.DefaultPfDao; @@ -134,11 +135,15 @@ public class EntityTest { testGeneratedId(); + testReferenceTimestamp(); + testVersionOps(); testgetFilteredOps(); testgetFilteredOps2(); + + testgetFilteredOps3(); } @Test @@ -482,6 +487,57 @@ public class EntityTest { assertEquals(0, pfDao.size(DummyGeneratedIdEntity.class)); } + private void testReferenceTimestamp() { + final PfConceptKey owner0Key = new PfConceptKey("Owner0", VERSION001); + final PfConceptKey owner1Key = new PfConceptKey("Owner1", VERSION001); + final PfConceptKey owner2Key = new PfConceptKey("Owner2", VERSION001); + final PfReferenceTimestampKey arKey0 = new PfReferenceTimestampKey(owner0Key, "AT-KEY0", TIMESTAMP0); + final PfReferenceTimestampKey arKey1 = new PfReferenceTimestampKey(owner1Key, "AT-KEY1", TIMESTAMP1);; + final PfReferenceTimestampKey arKey2 = new PfReferenceTimestampKey(owner2Key, "AT-KEY2", TIMESTAMP2); + final DummyReferenceTimestampEntity rkeyInfo0 = new DummyReferenceTimestampEntity(arKey0); + final DummyReferenceTimestampEntity rkeyInfo1 = new DummyReferenceTimestampEntity(arKey1); + final DummyReferenceTimestampEntity rkeyInfo2 = new DummyReferenceTimestampEntity(arKey2); + + pfDao.create(rkeyInfo0); + + final DummyReferenceTimestampEntity rkeyInfoBack0 = pfDao.get(DummyReferenceTimestampEntity.class, arKey0); + assertEquals(rkeyInfo0, rkeyInfoBack0); + + + final DummyReferenceTimestampEntity rkeyInfoBackNull = + pfDao.get(DummyReferenceTimestampEntity.class, PfReferenceTimestampKey.getNullKey()); + assertNull(rkeyInfoBackNull); + + final Set<DummyReferenceTimestampEntity> rkeyInfoSetIn = new TreeSet<>(); + rkeyInfoSetIn.add(rkeyInfo1); + rkeyInfoSetIn.add(rkeyInfo2); + + pfDao.createCollection(rkeyInfoSetIn); + + Set<DummyReferenceTimestampEntity> rkeyInfoSetOut = + new TreeSet<>(pfDao.getAll(DummyReferenceTimestampEntity.class)); + + rkeyInfoSetIn.add(rkeyInfo0); + assertEquals(rkeyInfoSetIn, rkeyInfoSetOut); + + pfDao.delete(rkeyInfo1); + rkeyInfoSetIn.remove(rkeyInfo1); + rkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyReferenceTimestampEntity.class)); + assertEquals(rkeyInfoSetIn, rkeyInfoSetOut); + + pfDao.deleteCollection(rkeyInfoSetIn); + rkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyReferenceTimestampEntity.class)); + assertEquals(0, rkeyInfoSetOut.size()); + + rkeyInfoSetIn.add(rkeyInfo2); + pfDao.createCollection(rkeyInfoSetIn); + rkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyReferenceTimestampEntity.class)); + assertEquals(rkeyInfoSetIn, rkeyInfoSetOut); + + pfDao.deleteAll(DummyReferenceTimestampEntity.class); + assertEquals(0, pfDao.size(DummyReferenceTimestampEntity.class)); + } + private void testVersionOps() { final PfConceptKey aKey0 = new PfConceptKey("AAA0", VERSION001); final PfConceptKey aKey1 = new PfConceptKey("AAA0", VERSION002); @@ -623,4 +679,53 @@ public class EntityTest { assertEquals(1, pfDao.getFiltered(DummyGeneratedIdEntity.class, null, null, null, null, filterMap, "DESC", 0).size()); } + + private void testgetFilteredOps3() { + Map<String, Object> filterMap = new HashMap<>(); + filterMap.put("localName", "AT-KEY0"); + + final PfConceptKey owner0Key = new PfConceptKey("Owner0", VERSION001); + final PfConceptKey owner1Key = new PfConceptKey("Owner1", VERSION001); + final PfConceptKey owner2Key = new PfConceptKey("Owner2", VERSION001); + final PfReferenceTimestampKey arKey0 = new PfReferenceTimestampKey(owner0Key, "AT-KEY0", TIMESTAMP0); + final PfReferenceTimestampKey arKey1 = new PfReferenceTimestampKey(owner1Key, "AT-KEY1", TIMESTAMP1);; + final PfReferenceTimestampKey arKey2 = new PfReferenceTimestampKey(owner2Key, "AT-KEY2", TIMESTAMP2); + final DummyReferenceTimestampEntity rkeyInfo0 = new DummyReferenceTimestampEntity(arKey0); + final DummyReferenceTimestampEntity rkeyInfo1 = new DummyReferenceTimestampEntity(arKey1); + final DummyReferenceTimestampEntity rkeyInfo2 = new DummyReferenceTimestampEntity(arKey2); + + pfDao.create(rkeyInfo0); + pfDao.create(rkeyInfo1); + 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()); + } } diff --git a/models-dao/src/test/resources/META-INF/persistence.xml b/models-dao/src/test/resources/META-INF/persistence.xml index fca31e2c6..d324ae0a6 100644 --- a/models-dao/src/test/resources/META-INF/persistence.xml +++ b/models-dao/src/test/resources/META-INF/persistence.xml @@ -30,6 +30,7 @@ <class>org.onap.policy.models.dao.DummyReferenceEntity</class> <class>org.onap.policy.models.dao.DummyTimestampEntity</class> <class>org.onap.policy.models.dao.DummyGeneratedIdEntity</class> + <class>org.onap.policy.models.dao.DummyReferenceTimestampEntity</class> <properties> <property name="eclipselink.target-database" value="MySQL" /> |