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 java.util.UUID;
32 import org.junit.Test;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfReferenceTimestampKey;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 * Test the {@link JpaClElementStatistics} class.
43 public class JpaClElementStatisticsTest {
45 private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
48 public void testJpaClElementStatisticsConstructor() {
49 assertThatThrownBy(() -> {
50 new JpaClElementStatistics((JpaClElementStatistics) null);
51 }).hasMessageMatching("copyConcept is marked .*ull but is null");
53 assertThatThrownBy(() -> {
54 new JpaClElementStatistics((PfReferenceTimestampKey) null);
55 }).hasMessageMatching(NULL_KEY_ERROR);
57 assertThatThrownBy(() -> {
58 new JpaClElementStatistics(null, null);
59 }).hasMessageMatching(NULL_KEY_ERROR);
61 assertThatThrownBy(() -> {
62 new JpaClElementStatistics(null, new PfConceptKey());
63 }).hasMessageMatching(NULL_KEY_ERROR);
65 assertThatThrownBy(() -> {
66 new JpaClElementStatistics(new PfReferenceTimestampKey(), null);
67 }).hasMessageMatching("participantId is marked .*ull but is null");
69 assertNotNull(new JpaClElementStatistics());
70 assertNotNull(new JpaClElementStatistics((new PfReferenceTimestampKey())));
71 assertNotNull(new JpaClElementStatistics(new PfReferenceTimestampKey(), new PfConceptKey()));
75 public void testJpaClElementStatistics() {
76 JpaClElementStatistics testJpaClElementStatistics = createJpaClElementStatisticsInstance();
78 ClElementStatistics cles = createClElementStatisticsInstance();
79 assertEquals(cles, testJpaClElementStatistics.toAuthorative());
81 assertThatThrownBy(() -> {
82 testJpaClElementStatistics.fromAuthorative(null);
83 }).hasMessageMatching("clElementStatistics is marked .*ull but is null");
85 assertThatThrownBy(() -> new JpaClElementStatistics((JpaClElementStatistics) null))
86 .isInstanceOf(NullPointerException.class);
88 JpaClElementStatistics testJpaClElementStatisticsFa = new JpaClElementStatistics();
89 testJpaClElementStatisticsFa.setKey(null);
90 testJpaClElementStatisticsFa.fromAuthorative(cles);
91 assertEquals(testJpaClElementStatistics, testJpaClElementStatisticsFa);
92 testJpaClElementStatisticsFa.setKey(PfReferenceTimestampKey.getNullKey());
93 testJpaClElementStatisticsFa.fromAuthorative(cles);
94 assertEquals(testJpaClElementStatistics, testJpaClElementStatisticsFa);
95 testJpaClElementStatisticsFa.setKey(new PfReferenceTimestampKey("elementName", "0.0.1",
96 "a95757ba-b34a-4049-a2a8-46773abcbe5e", Instant.ofEpochSecond(123456L)));
97 testJpaClElementStatisticsFa.fromAuthorative(cles);
98 assertEquals(testJpaClElementStatistics, testJpaClElementStatisticsFa);
100 testJpaClElementStatisticsFa = new JpaClElementStatistics(cles);
101 assertEquals(testJpaClElementStatistics, testJpaClElementStatisticsFa);
103 assertEquals(1, testJpaClElementStatistics.getKeys().size());
105 assertEquals("elementName", testJpaClElementStatistics.getKey().getReferenceKey().getParentKeyName());
107 testJpaClElementStatistics.clean();
108 assertEquals("elementName", testJpaClElementStatistics.getKey().getReferenceKey().getParentKeyName());
110 JpaClElementStatistics testJpaClElementStatistics2 = new JpaClElementStatistics(testJpaClElementStatistics);
111 assertEquals(testJpaClElementStatistics, testJpaClElementStatistics2);
115 public void testJpaClElementStatisticsValidation() {
116 JpaClElementStatistics testJpaClElementStatistics = createJpaClElementStatisticsInstance();
118 assertThatThrownBy(() -> {
119 testJpaClElementStatistics.validate(null);
120 }).hasMessageMatching("fieldName is marked .*ull but is null");
122 assertTrue(testJpaClElementStatistics.validate("").isValid());
126 public void testJpaClElementStatisticsCompareTo() {
127 JpaClElementStatistics testJpaClElementStatistics = createJpaClElementStatisticsInstance();
129 JpaClElementStatistics otherJpaClElementStatistics = new JpaClElementStatistics(testJpaClElementStatistics);
130 assertEquals(0, testJpaClElementStatistics.compareTo(otherJpaClElementStatistics));
131 assertEquals(-1, testJpaClElementStatistics.compareTo(null));
132 assertEquals(0, testJpaClElementStatistics.compareTo(testJpaClElementStatistics));
133 assertNotEquals(0, testJpaClElementStatistics.compareTo(new DummyJpaClElementStatisticsChild()));
135 testJpaClElementStatistics.setState(ControlLoopState.PASSIVE);
136 assertNotEquals(0, testJpaClElementStatistics.compareTo(otherJpaClElementStatistics));
137 testJpaClElementStatistics.setState(ControlLoopState.UNINITIALISED);
138 assertEquals(0, testJpaClElementStatistics.compareTo(otherJpaClElementStatistics));
140 assertEquals(testJpaClElementStatistics, new JpaClElementStatistics(testJpaClElementStatistics));
144 public void testJpaClElementStatisticsLombok() {
145 assertNotNull(new Participant());
146 JpaClElementStatistics cles0 = new JpaClElementStatistics();
148 assertThat(cles0.toString()).contains("JpaClElementStatistics(");
149 assertThat(cles0.hashCode()).isNotZero();
150 assertEquals(true, cles0.equals(cles0));
151 assertEquals(false, cles0.equals(null));
154 JpaClElementStatistics cles11 = new JpaClElementStatistics();
156 cles11.setState(ControlLoopState.UNINITIALISED);
158 assertThat(cles11.toString()).contains("JpaClElementStatistics(");
159 assertEquals(false, cles11.hashCode() == 0);
160 assertEquals(false, cles11.equals(cles0));
161 assertEquals(false, cles11.equals(null));
163 assertNotEquals(cles11, cles0);
165 JpaClElementStatistics cles2 = new JpaClElementStatistics();
166 assertEquals(cles2, cles0);
169 private JpaClElementStatistics createJpaClElementStatisticsInstance() {
170 ClElementStatistics testCles = createClElementStatisticsInstance();
171 JpaClElementStatistics testJpaClElementStatistics = new JpaClElementStatistics();
172 testJpaClElementStatistics.setKey(null);
173 testJpaClElementStatistics.fromAuthorative(testCles);
174 testJpaClElementStatistics.setKey(PfReferenceTimestampKey.getNullKey());
175 testJpaClElementStatistics.fromAuthorative(testCles);
177 return testJpaClElementStatistics;
180 private ClElementStatistics createClElementStatisticsInstance() {
181 ClElementStatistics clElementStatistics = new ClElementStatistics();
182 clElementStatistics.setParticipantId(new ToscaConceptIdentifier("elementName", "0.0.1"));
183 clElementStatistics.setId(UUID.fromString("a95757ba-b34a-4049-a2a8-46773abcbe5e"));
184 clElementStatistics.setTimeStamp(Instant.ofEpochSecond(123456L));
185 clElementStatistics.setControlLoopState(ControlLoopState.UNINITIALISED);
187 return clElementStatistics;