diff options
author | huaxing <huaxing.jin@est.tech> | 2020-02-13 11:40:35 +0800 |
---|---|---|
committer | huaxing <huaxing.jin@est.tech> | 2020-02-17 09:54:02 +0800 |
commit | 9105d83bbfe5635e2a571efbc534fbce6d17d7f1 (patch) | |
tree | 4fb40d04f2343dec4d9f964cabc703a2a8a8ace3 /models-pdp/src/test/java/org/onap | |
parent | ad1cd2013f45da5764fc9610db1f679d3c3762cb (diff) |
Add/Update test cases for PdpStatistics entity
Add test cases for PdpStatistics / PdpEngineWorkerStatistics / JpaPdpStatistics.
Add lombok.Data annotation to PdpStatistics and
PdpEngineWorkerStatistics to facilitate testing.
Issue-ID: POLICY-1628
Signed-off-by: huaxing <huaxing.jin@est.tech>
Change-Id: I4269678641716a56db260ad67a30c97b904123cd
Diffstat (limited to 'models-pdp/src/test/java/org/onap')
3 files changed, 194 insertions, 27 deletions
diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpEngineWorkerStatisticsTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpEngineWorkerStatisticsTest.java new file mode 100644 index 000000000..c0d2ba6bf --- /dev/null +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpEngineWorkerStatisticsTest.java @@ -0,0 +1,62 @@ +/*- + * ============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.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; + +import java.util.Date; +import org.junit.Test; +import org.onap.policy.models.pdp.enums.PdpEngineWorkerState; + +public class PdpEngineWorkerStatisticsTest { + + @Test + public void testCopyConstructor() { + assertThatThrownBy(() -> new PdpEngineWorkerStatistics(null)).hasMessageContaining("source"); + + PdpEngineWorkerStatistics stat = createPdpEngineWorkerStatistics(); + PdpEngineWorkerStatistics stat2 = new PdpEngineWorkerStatistics(stat); + assertEquals(stat, stat2); + } + + @Test + public void testClean() { + PdpEngineWorkerStatistics stat = createPdpEngineWorkerStatistics(); + stat.setEngineId(" Engine0 "); + stat.clean(); + assertEquals("Engine0", stat.getEngineId()); + } + + private PdpEngineWorkerStatistics createPdpEngineWorkerStatistics() { + PdpEngineWorkerStatistics stat = new PdpEngineWorkerStatistics(); + stat.setEngineId("Engine0"); + stat.setEngineWorkerState(PdpEngineWorkerState.READY); + stat.setEngineTimeStamp(new Date().getTime()); + stat.setEventCount(1); + stat.setLastExecutionTime(100); + stat.setAverageExecutionTime(99); + stat.setUpTime(1000); + stat.setLastEnterTime(2000); + stat.setLastStart(3000); + return stat; + } +}
\ No newline at end of file diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpStatisticsTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpStatisticsTest.java index 08098cc28..adf9b9f7c 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpStatisticsTest.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpStatisticsTest.java @@ -3,6 +3,7 @@ * ONAP Policy Models * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications 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. @@ -22,32 +23,35 @@ package org.onap.policy.models.pdp.concepts; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; -import static org.onap.policy.models.pdp.concepts.PdpMessageUtils.removeVariableFields; +import java.util.ArrayList; +import java.util.Date; import org.junit.Test; public class PdpStatisticsTest { @Test public void testCopyConstructor() { - assertThatThrownBy(() -> new PdpStatistics(null)).isInstanceOf(NullPointerException.class); + assertThatThrownBy(() -> new PdpStatistics(null)).hasMessageContaining("source"); - PdpStatistics orig = new PdpStatistics(); - - // verify with null values - assertEquals(removeVariableFields(orig.toString()), removeVariableFields(new PdpStatistics(orig).toString())); - - // verify with all values - orig.setPdpInstanceId("my-instance"); - - int count = 1; - orig.setPolicyDeployCount(count++); - orig.setPolicyDeployFailCount(count++); - orig.setPolicyDeploySuccessCount(count++); - orig.setPolicyExecutedCount(count++); - orig.setPolicyExecutedFailCount(count++); - orig.setPolicyExecutedSuccessCount(count++); + PdpStatistics orig = createPdpStatistics(); + PdpStatistics copied = new PdpStatistics(orig); + assertEquals(orig, copied); + } - assertEquals(removeVariableFields(orig.toString()), removeVariableFields(new PdpStatistics(orig).toString())); + private PdpStatistics createPdpStatistics() { + PdpStatistics pdpStat = new PdpStatistics(); + pdpStat.setPdpInstanceId("PDP0"); + pdpStat.setPdpGroupName("PDPGroup0"); + pdpStat.setPdpSubGroupName("PDPSubGroup0"); + pdpStat.setTimeStamp(new Date()); + pdpStat.setPolicyDeployCount(3); + pdpStat.setPolicyDeploySuccessCount(1); + pdpStat.setPolicyDeployFailCount(2); + pdpStat.setPolicyExecutedCount(9); + pdpStat.setPolicyExecutedSuccessCount(4); + pdpStat.setPolicyExecutedFailCount(5); + pdpStat.setEngineStats(new ArrayList<>()); + return pdpStat; } } diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatisticsTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatisticsTest.java index 62f0c5be0..0b22e1b3e 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatisticsTest.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatisticsTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-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. @@ -21,22 +21,123 @@ package org.onap.policy.models.pdp.persistence.concepts; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import java.util.ArrayList; +import java.util.Date; import org.junit.Test; +import org.onap.policy.models.base.PfTimestampKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.pdp.concepts.PdpStatistics; /** * Test the {@link JpaPdpStatistics} class. - * */ public class JpaPdpStatisticsTest { - private static final String NULL_KEY_ERROR = "key is marked @NonNull but is null"; - private static final String PDP1 = "ThePDP"; - // TODO More unit test cases will be added later. @Test - public void testJpaPdpStatistics() { - assertThatThrownBy(() -> { - new JpaPdpStatistics((JpaPdpStatistics) null); - }).hasMessage("copyConcept is marked @NonNull but is null"); + public void testConstructor() { + assertThatThrownBy(() -> new JpaPdpStatistics((PfTimestampKey) null)).hasMessageContaining("key"); + + assertThatThrownBy(() -> new JpaPdpStatistics((JpaPdpStatistics) null)) + .hasMessageContaining("copyConcept"); + + assertThatThrownBy(() -> new JpaPdpStatistics((PdpStatistics) null)) + .hasMessageContaining("authorativeConcept"); + + assertNotNull(new JpaPdpStatistics()); + assertNotNull(new JpaPdpStatistics(new PfTimestampKey())); + + PdpStatistics pdpStat = createPdpStatistics(); + JpaPdpStatistics jpaPdpStat = new JpaPdpStatistics(createPdpStatistics()); + checkEquals(pdpStat, jpaPdpStat); + + JpaPdpStatistics jpaPdpStat2 = new JpaPdpStatistics(jpaPdpStat); + assertEquals(0, jpaPdpStat2.compareTo(jpaPdpStat)); + } + + @Test + public void testFromAuthorative() { + PdpStatistics pdpStat = createPdpStatistics(); + JpaPdpStatistics jpaPdpStat = new JpaPdpStatistics(); + jpaPdpStat.fromAuthorative(pdpStat); + checkEquals(pdpStat, jpaPdpStat); + } + + @Test + public void testToAuthorative() { + PdpStatistics pdpStat = createPdpStatistics(); + JpaPdpStatistics jpaPdpStat = new JpaPdpStatistics(pdpStat); + PdpStatistics toPdpStat = jpaPdpStat.toAuthorative(); + assertEquals(pdpStat, toPdpStat); + } + + @Test + public void testCompareTo() { + PdpStatistics pdpStat = createPdpStatistics(); + JpaPdpStatistics jpaPdpStat1 = new JpaPdpStatistics(pdpStat); + assertEquals(-1, jpaPdpStat1.compareTo(null)); + + JpaPdpStatistics jpaPdpStat2 = new JpaPdpStatistics(pdpStat); + assertEquals(0, jpaPdpStat1.compareTo(jpaPdpStat2)); + + PdpStatistics pdpStat3 = createPdpStatistics(); + pdpStat3.setPdpInstanceId("PDP3"); + JpaPdpStatistics jpaPdpStat3 = new JpaPdpStatistics(pdpStat3); + assertNotEquals(0, jpaPdpStat1.compareTo(jpaPdpStat3)); + } + + @Test + public void testValidate() { + JpaPdpStatistics nullKeyJpaPdpStat = new JpaPdpStatistics(); + assertFalse(nullKeyJpaPdpStat.validate(new PfValidationResult()).isOk()); + + PdpStatistics pdpStat = createPdpStatistics(); + JpaPdpStatistics jpaPdpStat2 = new JpaPdpStatistics(pdpStat); + assertTrue(jpaPdpStat2.validate(new PfValidationResult()).isOk()); + } + + @Test + public void testClean() { + PdpStatistics pdpStat = createPdpStatistics(); + JpaPdpStatistics jpaPdpStat = new JpaPdpStatistics(pdpStat); + jpaPdpStat.setPdpGroupName(" PDPGroup0 "); + jpaPdpStat.setPdpSubGroupName(" PDPSubGroup0 "); + jpaPdpStat.clean(); + assertEquals("PDPGroup0", jpaPdpStat.getPdpGroupName()); + assertEquals("PDPSubGroup0", jpaPdpStat.getPdpSubGroupName()); + } + + private void checkEquals(PdpStatistics pdpStat, JpaPdpStatistics jpaPdpStat) { + assertEquals(pdpStat.getPdpInstanceId(), jpaPdpStat.getKey().getName()); + assertEquals(pdpStat.getPdpGroupName(), jpaPdpStat.getPdpGroupName()); + assertEquals(pdpStat.getPdpSubGroupName(), jpaPdpStat.getPdpSubGroupName()); + assertEquals(pdpStat.getTimeStamp(), jpaPdpStat.getKey().getTimeStamp()); + assertEquals(pdpStat.getPolicyDeployCount(), jpaPdpStat.getPolicyDeployCount()); + assertEquals(pdpStat.getPolicyDeploySuccessCount(), jpaPdpStat.getPolicyDeploySuccessCount()); + assertEquals(pdpStat.getPolicyDeployFailCount(), jpaPdpStat.getPolicyDeployFailCount()); + assertEquals(pdpStat.getPolicyExecutedCount(), jpaPdpStat.getPolicyExecutedCount()); + assertEquals(pdpStat.getPolicyExecutedSuccessCount(), jpaPdpStat.getPolicyExecutedSuccessCount()); + assertEquals(pdpStat.getPolicyExecutedFailCount(), jpaPdpStat.getPolicyExecutedFailCount()); + } + + private PdpStatistics createPdpStatistics() { + PdpStatistics pdpStat = new PdpStatistics(); + pdpStat.setPdpInstanceId("PDP0"); + pdpStat.setPdpGroupName("PDPGroup0"); + pdpStat.setPdpSubGroupName("PDPSubGroup0"); + pdpStat.setTimeStamp(new Date()); + pdpStat.setPolicyDeployCount(3); + pdpStat.setPolicyDeploySuccessCount(1); + pdpStat.setPolicyDeployFailCount(2); + pdpStat.setPolicyExecutedCount(9); + pdpStat.setPolicyExecutedSuccessCount(4); + pdpStat.setPolicyExecutedFailCount(5); + pdpStat.setEngineStats(new ArrayList<>()); + return pdpStat; } } |