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 +- .../provider/PdpStatisticsProviderTest.java | 233 +++++++++++++++++++++ 4 files changed, 447 insertions(+), 7 deletions(-) create mode 100644 models-dao/src/test/java/org/onap/policy/models/dao/DummyTimestampEntity.java create mode 100644 models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProviderTest.java 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 @@ - diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProviderTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProviderTest.java new file mode 100644 index 000000000..effebe439 --- /dev/null +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProviderTest.java @@ -0,0 +1,233 @@ +/*- + * ============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.pdp.persistence.provider; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Properties; +import org.eclipse.persistence.config.PersistenceUnitProperties; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.models.dao.DaoParameters; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.dao.PfDaoFactory; +import org.onap.policy.models.dao.impl.DefaultPfDao; +import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics; +import org.onap.policy.models.pdp.concepts.PdpStatistics; + +public class PdpStatisticsProviderTest { + private static final String DAO_IS_NULL = "dao is marked @NonNull but is null"; + private static final String LIST_IS_NULL = "pdpStatisticsList is marked @NonNull but is null"; + private static final String GROUP0 = "group0"; + private static final String NAME = "name"; + private static final String GROUP = "group"; + private static final String SUBGROUP = "subgroup"; + private static final Date TIMESTAMP1 = new Date(1078884319); + private static final Date TIMESTAMP2 = new Date(1078884350); + private static final String ORDER = "DESC"; + + private PfDao pfDao; + + private ArrayList pdpStatisticsTestList = new ArrayList<>(); + private List engineStats = new ArrayList<>(); + private String testListStr; + private String name1ListStr; + private String createdListStr; + + /** + * Set up test Dao. + */ + @Before + public void setupDao() throws Exception { + final DaoParameters daoParameters = new DaoParameters(); + daoParameters.setPluginClass(DefaultPfDao.class.getName()); + + daoParameters.setPersistenceUnit("ToscaConceptTest"); + + Properties jdbcProperties = new Properties(); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY"); + + // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver"); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb"); + + daoParameters.setJdbcProperties(jdbcProperties); + + pfDao = new PfDaoFactory().createPfDao(daoParameters); + pfDao.init(daoParameters); + + PdpStatistics pdpStatistics = new PdpStatistics(); + pdpStatistics.setPdpInstanceId(NAME); + pdpStatistics.setTimeStamp(TIMESTAMP1); + pdpStatistics.setPdpGroupName(GROUP); + pdpStatistics.setPdpSubGroupName(SUBGROUP); + pdpStatistics.setPolicyDeployCount(2); + pdpStatistics.setPolicyDeployFailCount(1); + pdpStatistics.setPolicyDeploySuccessCount(1); + pdpStatistics.setPolicyExecutedCount(2); + pdpStatistics.setPolicyExecutedFailCount(1); + pdpStatistics.setPolicyExecutedSuccessCount(1); + pdpStatistics.setEngineStats(engineStats); + pdpStatisticsTestList.add(pdpStatistics); + name1ListStr = pdpStatisticsTestList.toString(); + + PdpStatistics pdpStatistics2 = new PdpStatistics(); + pdpStatistics2.setPdpInstanceId("name2"); + pdpStatistics2.setTimeStamp(TIMESTAMP2); + pdpStatistics2.setPdpGroupName(GROUP); + pdpStatistics2.setPdpSubGroupName(SUBGROUP); + pdpStatistics2.setPolicyDeployCount(2); + pdpStatistics2.setPolicyDeployFailCount(1); + pdpStatistics2.setPolicyDeploySuccessCount(1); + pdpStatistics2.setPolicyExecutedCount(2); + pdpStatistics2.setPolicyExecutedFailCount(1); + pdpStatistics2.setPolicyExecutedSuccessCount(1); + pdpStatistics2.setEngineStats(engineStats); + pdpStatisticsTestList.add(pdpStatistics2); + testListStr = pdpStatisticsTestList.toString(); + + List createdPdpStatisticsList; + createdPdpStatisticsList = new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsTestList); + createdListStr = createdPdpStatisticsList.toString(); + assertEquals(createdListStr.replaceAll("\\s+", ""), testListStr.replaceAll("\\s+", "")); + + } + + @After + public void teardown() { + pfDao.close(); + } + + @Test + public void testNotOkPdpStatistics() { + PdpStatistics pdpStatisticsErr = new PdpStatistics(); + pdpStatisticsErr.setPdpInstanceId("NULL"); + pdpStatisticsErr.setPdpGroupName(GROUP); + ArrayList pdpStatisticsNullList = new ArrayList<>(); + pdpStatisticsNullList.add(pdpStatisticsErr); + + assertThatThrownBy(() -> { + new PdpStatisticsProvider().createPdpStatistics(pfDao, null); + }).hasMessage(LIST_IS_NULL); + + assertThatThrownBy(() -> { + new PdpStatisticsProvider().updatePdpStatistics(pfDao, null); + }).hasMessage(LIST_IS_NULL); + + assertThatThrownBy(() -> { + new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsNullList); + }).hasMessageContaining("pdp statictics \"NULL\" is not valid"); + + assertThatThrownBy(() -> { + new PdpStatisticsProvider().updatePdpStatistics(pfDao, pdpStatisticsNullList); + }).hasMessageContaining("pdp statistics \"NULL:0.0.0:0\" is not valid"); + } + + @Test + public void testGetPdpStatistics() throws Exception { + assertThatThrownBy(() -> { + new PdpStatisticsProvider().createPdpStatistics(null, null); + }).hasMessage(DAO_IS_NULL); + assertThatThrownBy(() -> { + new PdpStatisticsProvider().getPdpStatistics(null, null, null); + }).hasMessage(DAO_IS_NULL); + + List getPdpStatisticsList; + getPdpStatisticsList = new PdpStatisticsProvider().getPdpStatistics(pfDao, NAME, TIMESTAMP1); + assertThat(getPdpStatisticsList).hasSize(1); + String gotListStr = getPdpStatisticsList.toString(); + assertEquals(name1ListStr.replaceAll("\\s+", ""), gotListStr.replaceAll("\\s+", "")); + + // name is null + getPdpStatisticsList = new PdpStatisticsProvider().getPdpStatistics(pfDao, null, TIMESTAMP1); + gotListStr = getPdpStatisticsList.toString(); + assertEquals(testListStr.replaceAll("\\s+", ""), gotListStr.replaceAll("\\s+", "")); + } + + @Test + public void testGetFilteredPdpStatistics() throws Exception { + assertThatThrownBy(() -> { + new PdpStatisticsProvider().getFilteredPdpStatistics(null, NAME, GROUP, SUBGROUP, TIMESTAMP1, TIMESTAMP2, + ORDER, 1); + }).hasMessage(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, NAME, null, null, TIMESTAMP1, TIMESTAMP2, ORDER, + 1); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + + List createdPdpStatisticsList; + createdPdpStatisticsList = new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsTestList); + createdListStr = createdPdpStatisticsList.toString(); + assertEquals(createdListStr.replaceAll("\\s+", ""), testListStr.replaceAll("\\s+", "")); + + List getPdpStatisticsList; + getPdpStatisticsList = new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, NAME, GROUP, null, + TIMESTAMP1, TIMESTAMP2, ORDER, 0); + assertThat(getPdpStatisticsList).hasSize(1); + getPdpStatisticsList = new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, "name2", GROUP, null, + TIMESTAMP1, TIMESTAMP2, ORDER, 0); + assertThat(getPdpStatisticsList).hasSize(1); + getPdpStatisticsList = new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, "name2", GROUP, SUBGROUP, + TIMESTAMP1, TIMESTAMP2, ORDER, 0); + assertThat(getPdpStatisticsList).hasSize(1); + } + + @Test + public void testUpdatePdpStatistics() throws Exception { + assertThatThrownBy(() -> { + new PdpStatisticsProvider().updatePdpStatistics(null, null); + }).hasMessage(DAO_IS_NULL); + + pdpStatisticsTestList.get(0).setPdpGroupName(GROUP0); + testListStr = pdpStatisticsTestList.toString(); + List updatePdpStatisticsList = + new PdpStatisticsProvider().updatePdpStatistics(pfDao, pdpStatisticsTestList); + String gotListStr = updatePdpStatisticsList.toString(); + assertEquals(testListStr.replaceAll("\\s+", ""), gotListStr.replaceAll("\\s+", "")); + + } + + @Test + public void testDeletePdpStatistics() throws Exception { + assertThatThrownBy(() -> { + new PdpStatisticsProvider().deletePdpStatistics(null, null, null); + }).hasMessage(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new PdpStatisticsProvider().deletePdpStatistics(pfDao, null, null); + }).hasMessage("name is marked @NonNull but is null"); + + List deletedPdpStatisticsList = + new PdpStatisticsProvider().deletePdpStatistics(pfDao, NAME, null); + String gotListStr = deletedPdpStatisticsList.toString(); + assertEquals(name1ListStr.replaceAll("\\s+", ""), gotListStr.replaceAll("\\s+", "")); + } + +} -- cgit 1.2.3-korg