2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
30 import java.time.Instant;
31 import org.junit.Test;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfTimestampKey;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 * Test the {@link JpaClElementStatistics} class.
42 public class JpaClElementStatisticsTest {
44 private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
47 public void testJpaClElementStatisticsConstructor() {
48 assertThatThrownBy(() -> {
49 new JpaClElementStatistics((JpaClElementStatistics) null);
50 }).hasMessageMatching("copyConcept is marked .*ull but is null");
52 assertThatThrownBy(() -> {
53 new JpaClElementStatistics((PfTimestampKey) null);
54 }).hasMessageMatching(NULL_KEY_ERROR);
56 assertThatThrownBy(() -> {
57 new JpaClElementStatistics(null, null);
58 }).hasMessageMatching(NULL_KEY_ERROR);
60 assertThatThrownBy(() -> {
61 new JpaClElementStatistics(null, new PfConceptKey());
62 }).hasMessageMatching(NULL_KEY_ERROR);
64 assertThatThrownBy(() -> {
65 new JpaClElementStatistics(new PfTimestampKey(), null);
66 }).hasMessageMatching("clElementId is marked .*ull but is null");
68 assertNotNull(new JpaClElementStatistics());
69 assertNotNull(new JpaClElementStatistics((new PfTimestampKey())));
70 assertNotNull(new JpaClElementStatistics(new PfTimestampKey(), new PfConceptKey()));
74 public void testJpaClElementStatistics() {
75 JpaClElementStatistics testJpaClElementStatistics = createJpaClElementStatisticsInstance();
77 ClElementStatistics cles = createClElementStatisticsInstance();
78 assertEquals(cles, testJpaClElementStatistics.toAuthorative());
80 assertThatThrownBy(() -> {
81 testJpaClElementStatistics.fromAuthorative(null);
82 }).hasMessageMatching("clElementStatistics is marked .*ull but is null");
84 assertThatThrownBy(() -> new JpaClElementStatistics((JpaClElementStatistics) null))
85 .isInstanceOf(NullPointerException.class);
87 JpaClElementStatistics testJpaClElementStatisticsFa = new JpaClElementStatistics();
88 testJpaClElementStatisticsFa.setKey(null);
89 testJpaClElementStatisticsFa.fromAuthorative(cles);
90 assertEquals(testJpaClElementStatistics, testJpaClElementStatisticsFa);
91 testJpaClElementStatisticsFa.setKey(PfTimestampKey.getNullKey());
92 testJpaClElementStatisticsFa.fromAuthorative(cles);
93 assertEquals(testJpaClElementStatistics, testJpaClElementStatisticsFa);
94 testJpaClElementStatisticsFa.setKey(new PfTimestampKey("elementName", "0.0.1", Instant.ofEpochMilli(123456L)));
95 testJpaClElementStatisticsFa.fromAuthorative(cles);
96 assertEquals(testJpaClElementStatistics, testJpaClElementStatisticsFa);
98 testJpaClElementStatisticsFa = new JpaClElementStatistics(cles);
99 assertEquals(testJpaClElementStatistics, testJpaClElementStatisticsFa);
101 assertEquals(1, testJpaClElementStatistics.getKeys().size());
103 assertEquals("elementName", testJpaClElementStatistics.getKey().getName());
105 testJpaClElementStatistics.clean();
106 assertEquals("elementName", testJpaClElementStatistics.getKey().getName());
108 JpaClElementStatistics testJpaClElementStatistics2 = new JpaClElementStatistics(testJpaClElementStatistics);
109 assertEquals(testJpaClElementStatistics, testJpaClElementStatistics2);
113 public void testJpaClElementStatisticsValidation() {
114 JpaClElementStatistics testJpaClElementStatistics = createJpaClElementStatisticsInstance();
116 assertThatThrownBy(() -> {
117 testJpaClElementStatistics.validate(null);
118 }).hasMessageMatching("fieldName is marked .*ull but is null");
120 assertTrue(testJpaClElementStatistics.validate("").isValid());
124 public void testJpaClElementStatisticsConmpareTo() {
125 JpaClElementStatistics testJpaClElementStatistics = createJpaClElementStatisticsInstance();
127 JpaClElementStatistics otherJpaClElementStatistics = new JpaClElementStatistics(testJpaClElementStatistics);
128 assertEquals(0, testJpaClElementStatistics.compareTo(otherJpaClElementStatistics));
129 assertEquals(-1, testJpaClElementStatistics.compareTo(null));
130 assertEquals(0, testJpaClElementStatistics.compareTo(testJpaClElementStatistics));
131 assertNotEquals(0, testJpaClElementStatistics.compareTo(new DummyJpaClElementStatisticsChild()));
133 testJpaClElementStatistics.setState(ControlLoopState.PASSIVE);
134 assertNotEquals(0, testJpaClElementStatistics.compareTo(otherJpaClElementStatistics));
135 testJpaClElementStatistics.setState(ControlLoopState.UNINITIALISED);
136 assertEquals(0, testJpaClElementStatistics.compareTo(otherJpaClElementStatistics));
138 assertEquals(testJpaClElementStatistics, new JpaClElementStatistics(testJpaClElementStatistics));
142 public void testJpaClElementStatisticsLombok() {
143 assertNotNull(new Participant());
144 JpaClElementStatistics cles0 = new JpaClElementStatistics();
146 assertThat(cles0.toString()).contains("JpaClElementStatistics(");
147 assertThat(cles0.hashCode()).isNotZero();
148 assertEquals(true, cles0.equals(cles0));
149 assertEquals(false, cles0.equals(null));
152 JpaClElementStatistics cles11 = new JpaClElementStatistics();
154 cles11.setState(ControlLoopState.UNINITIALISED);
156 assertThat(cles11.toString()).contains("JpaClElementStatistics(");
157 assertEquals(false, cles11.hashCode() == 0);
158 assertEquals(false, cles11.equals(cles0));
159 assertEquals(false, cles11.equals(null));
161 assertNotEquals(cles11, cles0);
163 JpaClElementStatistics cles2 = new JpaClElementStatistics();
164 assertEquals(cles2, cles0);
167 private JpaClElementStatistics createJpaClElementStatisticsInstance() {
168 ClElementStatistics testCles = createClElementStatisticsInstance();
169 JpaClElementStatistics testJpaClElementStatistics = new JpaClElementStatistics();
170 testJpaClElementStatistics.setKey(null);
171 testJpaClElementStatistics.fromAuthorative(testCles);
172 testJpaClElementStatistics.setKey(PfTimestampKey.getNullKey());
173 testJpaClElementStatistics.fromAuthorative(testCles);
175 return testJpaClElementStatistics;
178 private ClElementStatistics createClElementStatisticsInstance() {
179 ClElementStatistics clElementStatistics = new ClElementStatistics();
180 clElementStatistics.setControlLoopElementId(new ToscaConceptIdentifier("elementName", "0.0.1"));
181 clElementStatistics.setTimeStamp(Instant.ofEpochMilli(123456L));
182 clElementStatistics.setControlLoopState(ControlLoopState.UNINITIALISED);
184 return clElementStatistics;