From e2fe6126d0221ca55bed2f1f0de6a6b9346bd220 Mon Sep 17 00:00:00 2001 From: "ning.xi" Date: Thu, 13 Feb 2020 18:00:36 +0800 Subject: add unit test for pdpstatistics provider Issue-ID: POLICY-2314 Signed-off-by: ning.xi Change-Id: Ifb23ab301a326cc30950ecc2f115979fa261bc68 --- .../policy/models/dao/DummyTimestampEntity.java | 110 +++++++++++++++++++++ .../org/onap/policy/models/dao/EntityTest.java | 99 ++++++++++++++++++- .../src/test/resources/META-INF/persistence.xml | 12 +-- 3 files changed, 214 insertions(+), 7 deletions(-) create mode 100644 models-dao/src/test/java/org/onap/policy/models/dao/DummyTimestampEntity.java (limited to 'models-dao') diff --git a/models-dao/src/test/java/org/onap/policy/models/dao/DummyTimestampEntity.java b/models-dao/src/test/java/org/onap/policy/models/dao/DummyTimestampEntity.java new file mode 100644 index 000000000..d18a91573 --- /dev/null +++ b/models-dao/src/test/java/org/onap/policy/models/dao/DummyTimestampEntity.java @@ -0,0 +1,110 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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.utils.validation.Assertions; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfTimestampKey; +import org.onap.policy.models.base.PfValidationResult; + +@Entity +@Table(name = "DummyTimestampEntity") +@Data +@EqualsAndHashCode(callSuper = false) +public class DummyTimestampEntity extends PfConcept { + private static final long serialVersionUID = -2962570563281067895L; + + @EmbeddedId() + @NonNull + private PfTimestampKey key; + + @Column + private double doubleValue; + + /** + * Default constructor. + */ + public DummyTimestampEntity() { + this.key = new PfTimestampKey(); + this.doubleValue = 123.45; + } + + public DummyTimestampEntity(DummyTimestampEntity source) { + this.key = source.key; + this.doubleValue = source.doubleValue; + } + + /** + * Constructor. + * + * @param key the key + * @param doubleValue the double value + */ + public DummyTimestampEntity(final PfTimestampKey key, final double doubleValue) { + this.key = key; + this.doubleValue = doubleValue; + } + + @Override + public List getKeys() { + final List keyList = new ArrayList<>(); + keyList.add(getKey()); + return keyList; + } + + @Override + public PfValidationResult validate(final PfValidationResult result) { + return key.validate(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 DummyTimestampEntity other = (DummyTimestampEntity) otherObj; + + 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 70505aa2d..f5702e60a 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -28,7 +28,10 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.TreeSet; @@ -38,6 +41,7 @@ import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfTimestampKey; import org.onap.policy.models.dao.impl.DefaultPfDao; /** @@ -54,6 +58,9 @@ public class EntityTest { private static final String VERSION003 = "0.0.3"; private static final String VERSION002 = "0.0.2"; private static final String VERSION001 = "0.0.1"; + private static final Date TIMESTAMP0 = new Date(); + private static final Date TIMESTAMP1 = new Date(); + private static final Date TIMESTAMP2 = new Date(); private PfDao pfDao; @Test @@ -127,6 +134,7 @@ public class EntityTest { final PfConceptKey nullKey = null; final PfReferenceKey nullRefKey = null; + final PfTimestampKey nullTimeKey = null; final List nullKeyList = null; final List emptyKeyList = new ArrayList<>(); final List nullRKeyList = null; @@ -141,6 +149,7 @@ public class EntityTest { pfDao.deleteCollection(emptyKeyList); pfDao.delete(PfConceptKey.class, nullKey); pfDao.delete(PfReferenceKey.class, nullRefKey); + pfDao.delete(PfTimestampKey.class, nullTimeKey); pfDao.deleteByConceptKey(PfConceptKey.class, nullKeyList); pfDao.deleteByConceptKey(PfConceptKey.class, emptyKeyList); pfDao.deleteByReferenceKey(PfReferenceKey.class, nullRKeyList); @@ -148,6 +157,7 @@ public class EntityTest { pfDao.get(null, nullKey); pfDao.get(null, nullRefKey); + pfDao.get(null, nullTimeKey); pfDao.getAll(null); pfDao.getAll(null, nullKey); pfDao.getConcept(null, nullKey); @@ -297,6 +307,57 @@ public class EntityTest { assertEquals(2, deletedRCount); pfDao.update(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 120.0)); + + final PfTimestampKey atKey0 = new PfTimestampKey("AT-KEY0", VERSION001, TIMESTAMP0); + final PfTimestampKey atKey1 = new PfTimestampKey("AT-KEY1", VERSION001, TIMESTAMP1); + final PfTimestampKey atKey2 = new PfTimestampKey("AT-KEY2", VERSION001, TIMESTAMP2); + final DummyTimestampEntity tkeyInfo0 = new DummyTimestampEntity(atKey0, 200.0); + final DummyTimestampEntity tkeyInfo1 = new DummyTimestampEntity(atKey1, 200.1); + final DummyTimestampEntity tkeyInfo2 = new DummyTimestampEntity(atKey2, 200.2); + + pfDao.create(tkeyInfo0); + + final DummyTimestampEntity tkeyInfoBack0 = pfDao.get(DummyTimestampEntity.class, atKey0); + assertEquals(tkeyInfo0, tkeyInfoBack0); + + final DummyTimestampEntity tkeyInfoBackNull = + pfDao.get(DummyTimestampEntity.class, PfTimestampKey.getNullKey()); + assertNull(tkeyInfoBackNull); + + + + final Set tkeyInfoSetIn = new TreeSet<>(); + tkeyInfoSetIn.add(tkeyInfo1); + tkeyInfoSetIn.add(tkeyInfo2); + + pfDao.createCollection(tkeyInfoSetIn); + + Set tkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyTimestampEntity.class)); + + tkeyInfoSetIn.add(tkeyInfo0); + assertEquals(tkeyInfoSetIn, tkeyInfoSetOut); + + pfDao.delete(tkeyInfo1); + tkeyInfoSetIn.remove(tkeyInfo1); + tkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyTimestampEntity.class)); + assertEquals(tkeyInfoSetIn, tkeyInfoSetOut); + + pfDao.deleteCollection(tkeyInfoSetIn); + tkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyTimestampEntity.class)); + assertEquals(0, tkeyInfoSetOut.size()); + + tkeyInfoSetIn.add(tkeyInfo2); + pfDao.createCollection(tkeyInfoSetIn); + tkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyTimestampEntity.class)); + assertEquals(keyInfoSetIn, keyInfoSetOut); + + pfDao.delete(DummyTimestampEntity.class, atKey2); + tkeyInfoSetOut = new TreeSet<>(pfDao.getAll(DummyTimestampEntity.class)); + assertEquals(3, keyInfoSetOut.size()); + assertEquals(1, pfDao.size(DummyTimestampEntity.class)); + + pfDao.deleteAll(DummyTimestampEntity.class); + assertEquals(0, pfDao.size(DummyTimestampEntity.class)); } private void testVersionOps() { @@ -363,5 +424,41 @@ public class EntityTest { 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()); + + final PfTimestampKey atKey0 = new PfTimestampKey("AT-KEY0", VERSION001, TIMESTAMP0); + final PfTimestampKey atKey1 = new PfTimestampKey("AT-KEY1", VERSION001, TIMESTAMP1); + final PfTimestampKey atKey2 = new PfTimestampKey("AT-KEY2", VERSION001, TIMESTAMP2); + final DummyTimestampEntity tkeyInfo0 = new DummyTimestampEntity(atKey0, 200.0); + final DummyTimestampEntity tkeyInfo1 = new DummyTimestampEntity(atKey1, 200.1); + final DummyTimestampEntity tkeyInfo2 = new DummyTimestampEntity(atKey2, 200.2); + + pfDao.create(tkeyInfo0); + pfDao.create(tkeyInfo1); + 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()); + + Map filterMap = new HashMap<>(); + filterMap.put("doubleValue", 200.1); + assertEquals(1, + pfDao.getFiltered(DummyTimestampEntity.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 5314ebbdb..04b2c5b44 100644 --- a/models-dao/src/test/resources/META-INF/persistence.xml +++ b/models-dao/src/test/resources/META-INF/persistence.xml @@ -1,20 +1,20 @@ @@ -28,12 +28,13 @@ org.onap.policy.models.base.PfConceptKey org.onap.policy.models.dao.DummyConceptEntity org.onap.policy.models.dao.DummyReferenceEntity + org.onap.policy.models.dao.DummyTimestampEntity - + @@ -44,7 +45,6 @@ - -- cgit 1.2.3-korg