diff options
author | ktimoney <kevin.timoney@est.tech> | 2021-04-13 14:40:06 +0100 |
---|---|---|
committer | ktimoney <kevin.timoney@est.tech> | 2021-04-22 20:09:36 +0100 |
commit | a3241bec1cf1ae5fd514923ba5f923abc5427762 (patch) | |
tree | 0c06bc0d1d026ab411391c7cf9aac293207ad231 /models-dao/src/test/java | |
parent | 0b7b94b17340406b34817d6088daa97d965d5c2c (diff) |
Add a new key class which uses @GeneratedValue to base classes
This will provide an alternative unique key.
It will be used in place of PfTimestampKey in the PdpStatistics table.
We make use of the @GeneratedValue annotation to provide this feature.
Added DummyGeneratedIdEntity and unit tests
Added PfFilterFactory class
Added new filter classes
Issue-ID: POLICY-3189
Change-Id: I097eab318d2cb958e1157e35825fd6d35d429a25
Signed-off-by: ktimoney <kevin.timoney@est.tech>
Diffstat (limited to 'models-dao/src/test/java')
-rw-r--r-- | models-dao/src/test/java/org/onap/policy/models/dao/DummyGeneratedIdEntity.java | 132 | ||||
-rw-r--r-- | models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java | 155 |
2 files changed, 287 insertions, 0 deletions
diff --git a/models-dao/src/test/java/org/onap/policy/models/dao/DummyGeneratedIdEntity.java b/models-dao/src/test/java/org/onap/policy/models/dao/DummyGeneratedIdEntity.java new file mode 100644 index 000000000..6ba045c54 --- /dev/null +++ b/models-dao/src/test/java/org/onap/policy/models/dao/DummyGeneratedIdEntity.java @@ -0,0 +1,132 @@ +/*- + * ============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.Date; +import java.util.List; +import javax.persistence.Column; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +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.PfGeneratedIdKey; +import org.onap.policy.models.base.PfKey; + + +@Entity +@Table(name = "DummyGeneratedIdEntity") +@Data +@EqualsAndHashCode(callSuper = false) +public class DummyGeneratedIdEntity extends PfConcept { + private static final long serialVersionUID = -2962570563281067896L; + + @EmbeddedId() + @NonNull + private PfGeneratedIdKey key; + + @Column(precision = 3) + @Temporal(TemporalType.TIMESTAMP) + private Date timeStamp; + + @Column + private double doubleValue; + + /** + * Default constructor. + */ + public DummyGeneratedIdEntity() { + this.key = new PfGeneratedIdKey(); + this.timeStamp = new Date(); + } + + public DummyGeneratedIdEntity(DummyGeneratedIdEntity source) { + this.key = source.key; + this.timeStamp = source.timeStamp; + } + + /** + * Constructor. + * + * @param key the key + * @param timeStamp the date value + */ + public DummyGeneratedIdEntity(final PfGeneratedIdKey key, final Date timeStamp) { + this.key = key; + this.timeStamp = timeStamp; + } + + /** + * Constructor. + * + * @param key the key + * @param timeStamp the date value + * @param doubleValue the double value * + */ + public DummyGeneratedIdEntity(final PfGeneratedIdKey key, final Date timeStamp, final double doubleValue) { + this.key = key; + this.timeStamp = timeStamp; + this.doubleValue = doubleValue; + } + + @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 DummyGeneratedIdEntity other = (DummyGeneratedIdEntity) otherObj; + if (this.timeStamp != other.timeStamp) { + return timeStamp.compareTo(other.timeStamp); + } + return Double.compare(doubleValue, other.doubleValue); + } + +} 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 bc022be0a..80730743b 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 @@ -29,6 +29,7 @@ import static org.junit.Assert.assertNull; import java.time.Instant; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -40,6 +41,7 @@ import org.eclipse.persistence.config.PersistenceUnitProperties; import org.junit.After; import org.junit.Test; 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.PfTimestampKey; @@ -62,6 +64,10 @@ public class EntityTest { private static final Instant TIMESTAMP0 = Instant.ofEpochSecond(1613494293); private static final Instant TIMESTAMP1 = Instant.ofEpochSecond(1613494293).plusSeconds(55); private static final Instant TIMESTAMP2 = Instant.ofEpochSecond(1613494293).plusSeconds(90); + private static final Long GENERATEDID0 = 10000L; + private static final Long GENERATEDID1 = 10001L; + private static final Long GENERATEDID2 = 10002L; + private PfDao pfDao; /** @@ -126,9 +132,13 @@ public class EntityTest { testAllOps(); + testGeneratedId(); + testVersionOps(); testgetFilteredOps(); + + testgetFilteredOps2(); } @Test @@ -367,6 +377,111 @@ public class EntityTest { assertEquals(0, pfDao.size(DummyTimestampEntity.class)); } + private void testGeneratedId() { + final PfGeneratedIdKey agKey0 = new PfGeneratedIdKey("AT-KEY0", VERSION001, GENERATEDID0); + final PfGeneratedIdKey agKey1 = new PfGeneratedIdKey("AT-KEY1", VERSION001, GENERATEDID1); + final PfGeneratedIdKey agKey2 = new PfGeneratedIdKey("AT-KEY2", VERSION001, GENERATEDID2); + final DummyGeneratedIdEntity gkeyInfo0 = new DummyGeneratedIdEntity(agKey0, Date.from(TIMESTAMP0)); + final DummyGeneratedIdEntity gkeyInfo1 = new DummyGeneratedIdEntity(agKey1, Date.from(TIMESTAMP1)); + final DummyGeneratedIdEntity gkeyInfo2 = new DummyGeneratedIdEntity(agKey2, Date.from(TIMESTAMP2)); + + pfDao.create(gkeyInfo0); + + final DummyGeneratedIdEntity gkeyInfoBack0 = pfDao.get(DummyGeneratedIdEntity.class, agKey0); + assertEquals(gkeyInfo0, gkeyInfoBack0); + + assertEquals(1, pfDao.getByTimestamp(DummyGeneratedIdEntity.class, agKey0, TIMESTAMP0).size()); + + final DummyGeneratedIdEntity gkeyInfoBackNull = + pfDao.get(DummyGeneratedIdEntity.class, PfGeneratedIdKey.getNullKey()); + assertNull(gkeyInfoBackNull); + + final Set<DummyGeneratedIdEntity> gkeyInfoSetIn = new TreeSet<>(); + gkeyInfoSetIn.add(gkeyInfo1); + gkeyInfoSetIn.add(gkeyInfo2); + + pfDao.createCollection(gkeyInfoSetIn); + + Set<DummyGeneratedIdEntity> gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); + + gkeyInfoSetIn.add(gkeyInfo0); + assertEquals(gkeyInfoSetIn, gkeyInfoSetOut); + + pfDao.delete(gkeyInfo1); + gkeyInfoSetIn.remove(gkeyInfo1); + gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); + assertEquals(gkeyInfoSetIn, gkeyInfoSetOut); + + pfDao.deleteCollection(gkeyInfoSetIn); + gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); + assertEquals(0, gkeyInfoSetOut.size()); + + gkeyInfoSetIn.add(gkeyInfo2); + pfDao.createCollection(gkeyInfoSetIn); + gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); + assertEquals(gkeyInfoSetIn, gkeyInfoSetOut); + + pfDao.delete(DummyGeneratedIdEntity.class, agKey2); + gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); + assertEquals(gkeyInfoSetOut.size(), pfDao.size(DummyGeneratedIdEntity.class)); + + pfDao.deleteAll(DummyGeneratedIdEntity.class); + assertEquals(0, pfDao.size(DummyGeneratedIdEntity.class)); + + final PfGeneratedIdKey agKey3 = new PfGeneratedIdKey("AT-KEY0", VERSION001); + final PfGeneratedIdKey agKey4 = new PfGeneratedIdKey("AT-KEY1", VERSION001); + final PfGeneratedIdKey agKey5 = new PfGeneratedIdKey("AT-KEY2", VERSION001); + final DummyGeneratedIdEntity gkeyInfo3 = new DummyGeneratedIdEntity(agKey3, Date.from(TIMESTAMP0)); + final DummyGeneratedIdEntity gkeyInfo4 = new DummyGeneratedIdEntity(agKey4, Date.from(TIMESTAMP1)); + final DummyGeneratedIdEntity gkeyInfo5 = new DummyGeneratedIdEntity(agKey5, Date.from(TIMESTAMP2)); + + pfDao.create(gkeyInfo3); + + final DummyGeneratedIdEntity gkeyInfoBack3 = pfDao.get(DummyGeneratedIdEntity.class, agKey3); + assertEquals(gkeyInfo3, gkeyInfoBack3); + + assertEquals(1, pfDao.getByTimestamp(DummyGeneratedIdEntity.class, agKey3, TIMESTAMP0).size()); + + assertEquals(1, gkeyInfo3.getKeys().size()); + + assertEquals(1, gkeyInfo4.compareTo(gkeyInfo3)); + + assertNull(gkeyInfo4.validate(VERSION002).getResult()); + + + gkeyInfoSetIn.clear(); + gkeyInfoSetIn.add(gkeyInfo4); + gkeyInfoSetIn.add(gkeyInfo5); + + pfDao.createCollection(gkeyInfoSetIn); + + gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); + + gkeyInfoSetIn.add(gkeyInfo3); + assertEquals(gkeyInfoSetIn, gkeyInfoSetOut); + + pfDao.delete(gkeyInfo4); + gkeyInfoSetIn.remove(gkeyInfo4); + gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); + assertEquals(gkeyInfoSetIn, gkeyInfoSetOut); + + pfDao.deleteCollection(gkeyInfoSetIn); + gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); + assertEquals(0, gkeyInfoSetOut.size()); + + gkeyInfoSetIn.add(gkeyInfo5); + pfDao.createCollection(gkeyInfoSetIn); + gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); + assertEquals(gkeyInfoSetIn, gkeyInfoSetOut); + + pfDao.delete(DummyGeneratedIdEntity.class, agKey5); + gkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyGeneratedIdEntity.class)); + assertEquals(gkeyInfoSetOut.size(), pfDao.size(DummyGeneratedIdEntity.class)); + + pfDao.deleteAll(DummyGeneratedIdEntity.class); + assertEquals(0, pfDao.size(DummyGeneratedIdEntity.class)); + } + private void testVersionOps() { final PfConceptKey aKey0 = new PfConceptKey("AAA0", VERSION001); final PfConceptKey aKey1 = new PfConceptKey("AAA0", VERSION002); @@ -466,6 +581,46 @@ public class EntityTest { filterMap.put("doubleValue", 200.1); assertEquals(1, pfDao.getFiltered(DummyTimestampEntity.class, null, null, null, null, filterMap, "DESC", 0).size()); + } + + private void testgetFilteredOps2() { + Map<String, Object> filterMap = new HashMap<>(); + filterMap.put("doubleValue", 200.1); + + final PfGeneratedIdKey agKey0 = new PfGeneratedIdKey("AT-KEY0", VERSION001); + final PfGeneratedIdKey agKey1 = new PfGeneratedIdKey("AT-KEY1", VERSION001); + final PfGeneratedIdKey agKey2 = new PfGeneratedIdKey("AT-KEY2", VERSION001); + final DummyGeneratedIdEntity gkeyInfo0 = new DummyGeneratedIdEntity(agKey0, Date.from(TIMESTAMP0), 200.0); + final DummyGeneratedIdEntity gkeyInfo1 = new DummyGeneratedIdEntity(agKey1, Date.from(TIMESTAMP1), 200.1); + final DummyGeneratedIdEntity gkeyInfo2 = new DummyGeneratedIdEntity(agKey2, Date.from(TIMESTAMP2), 200.2); + + pfDao.create(gkeyInfo0); + pfDao.create(gkeyInfo1); + 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()); } } |