diff options
Diffstat (limited to 'models-dao/src/test')
3 files changed, 209 insertions, 0 deletions
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" /> |