2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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.models.acm.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.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotEquals;
28 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import java.time.Instant;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.models.acm.concepts.AcElementStatistics;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
36 import org.onap.policy.clamp.models.acm.concepts.Participant;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfReferenceTimestampKey;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42 * Test the {@link JpaAcElementStatistics} class.
44 class JpaAcElementStatisticsTest {
46 private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
49 void testJpaAcElementStatisticsConstructor() {
50 assertThatThrownBy(() -> {
51 new JpaAcElementStatistics((JpaAcElementStatistics) null);
52 }).hasMessageMatching("copyConcept is marked .*ull but is null");
54 assertThatThrownBy(() -> {
55 new JpaAcElementStatistics((PfReferenceTimestampKey) null);
56 }).hasMessageMatching(NULL_KEY_ERROR);
58 assertThatThrownBy(() -> {
59 new JpaAcElementStatistics(null, null);
60 }).hasMessageMatching(NULL_KEY_ERROR);
62 assertThatThrownBy(() -> {
63 new JpaAcElementStatistics(null, new PfConceptKey());
64 }).hasMessageMatching(NULL_KEY_ERROR);
66 assertThatThrownBy(() -> {
67 new JpaAcElementStatistics(new PfReferenceTimestampKey(), null);
68 }).hasMessageMatching("participantId is marked .*ull but is null");
70 assertNotNull(new JpaAcElementStatistics());
71 assertNotNull(new JpaAcElementStatistics((new PfReferenceTimestampKey())));
72 assertNotNull(new JpaAcElementStatistics(new PfReferenceTimestampKey(), new PfConceptKey()));
76 void testJpaAcElementStatistics() {
77 JpaAcElementStatistics testJpaAcElementStatistics = createJpaAcElementStatisticsInstance();
79 AcElementStatistics aces = createAcElementStatisticsInstance();
80 assertEquals(aces, testJpaAcElementStatistics.toAuthorative());
82 assertThatThrownBy(() -> {
83 testJpaAcElementStatistics.fromAuthorative(null);
84 }).hasMessageMatching("acElementStatistics is marked .*ull but is null");
86 assertThatThrownBy(() -> new JpaAcElementStatistics((JpaAcElementStatistics) null))
87 .isInstanceOf(NullPointerException.class);
89 JpaAcElementStatistics testJpaAcElementStatisticsFa = new JpaAcElementStatistics();
90 testJpaAcElementStatisticsFa.setKey(null);
91 testJpaAcElementStatisticsFa.fromAuthorative(aces);
92 assertEquals(testJpaAcElementStatistics, testJpaAcElementStatisticsFa);
93 testJpaAcElementStatisticsFa.setKey(PfReferenceTimestampKey.getNullKey());
94 testJpaAcElementStatisticsFa.fromAuthorative(aces);
95 assertEquals(testJpaAcElementStatistics, testJpaAcElementStatisticsFa);
96 testJpaAcElementStatisticsFa.setKey(new PfReferenceTimestampKey("elementName", "0.0.1",
97 "a95757ba-b34a-4049-a2a8-46773abcbe5e", Instant.ofEpochSecond(123456L)));
98 testJpaAcElementStatisticsFa.fromAuthorative(aces);
99 assertEquals(testJpaAcElementStatistics, testJpaAcElementStatisticsFa);
101 testJpaAcElementStatisticsFa = new JpaAcElementStatistics(aces);
102 assertEquals(testJpaAcElementStatistics, testJpaAcElementStatisticsFa);
104 assertEquals(1, testJpaAcElementStatistics.getKeys().size());
106 assertEquals("elementName", testJpaAcElementStatistics.getKey().getReferenceKey().getParentKeyName());
108 testJpaAcElementStatistics.clean();
109 assertEquals("elementName", testJpaAcElementStatistics.getKey().getReferenceKey().getParentKeyName());
111 JpaAcElementStatistics testJpaAcElementStatistics2 = new JpaAcElementStatistics(testJpaAcElementStatistics);
112 assertEquals(testJpaAcElementStatistics, testJpaAcElementStatistics2);
116 void testJpaAcElementStatisticsValidation() {
117 JpaAcElementStatistics testJpaAcElementStatistics = createJpaAcElementStatisticsInstance();
119 assertThatThrownBy(() -> {
120 testJpaAcElementStatistics.validate(null);
121 }).hasMessageMatching("fieldName is marked .*ull but is null");
123 assertTrue(testJpaAcElementStatistics.validate("").isValid());
127 void testJpaAcElementStatisticsCompareTo() {
128 JpaAcElementStatistics testJpaAcElementStatistics = createJpaAcElementStatisticsInstance();
130 JpaAcElementStatistics otherJpaAcElementStatistics = new JpaAcElementStatistics(testJpaAcElementStatistics);
131 assertEquals(0, testJpaAcElementStatistics.compareTo(otherJpaAcElementStatistics));
132 assertEquals(-1, testJpaAcElementStatistics.compareTo(null));
133 assertEquals(0, testJpaAcElementStatistics.compareTo(testJpaAcElementStatistics));
134 assertNotEquals(0, testJpaAcElementStatistics.compareTo(new DummyJpaAcElementStatisticsChild()));
136 testJpaAcElementStatistics.setState(AutomationCompositionState.PASSIVE);
137 assertNotEquals(0, testJpaAcElementStatistics.compareTo(otherJpaAcElementStatistics));
138 testJpaAcElementStatistics.setState(AutomationCompositionState.UNINITIALISED);
139 assertEquals(0, testJpaAcElementStatistics.compareTo(otherJpaAcElementStatistics));
141 assertEquals(testJpaAcElementStatistics, new JpaAcElementStatistics(testJpaAcElementStatistics));
145 void testJpaAcElementStatisticsLombok() {
146 assertNotNull(new Participant());
147 JpaAcElementStatistics aces0 = new JpaAcElementStatistics();
149 assertThat(aces0.toString()).contains("JpaAcElementStatistics(");
150 assertThat(aces0.hashCode()).isNotZero();
151 assertEquals(aces0, aces0);
152 assertNotEquals(null, aces0);
155 JpaAcElementStatistics aces11 = new JpaAcElementStatistics();
157 aces11.setState(AutomationCompositionState.UNINITIALISED);
159 assertThat(aces11.toString()).contains("JpaAcElementStatistics(");
160 assertNotEquals(0, aces11.hashCode());
161 assertNotEquals(aces11, aces0);
162 assertNotEquals(null, aces11);
164 assertNotEquals(aces11, aces0);
166 JpaAcElementStatistics aces2 = new JpaAcElementStatistics();
167 assertEquals(aces2, aces0);
170 private JpaAcElementStatistics createJpaAcElementStatisticsInstance() {
171 AcElementStatistics testAces = createAcElementStatisticsInstance();
172 JpaAcElementStatistics testJpaAcElementStatistics = new JpaAcElementStatistics();
173 testJpaAcElementStatistics.setKey(null);
174 testJpaAcElementStatistics.fromAuthorative(testAces);
175 testJpaAcElementStatistics.setKey(PfReferenceTimestampKey.getNullKey());
176 testJpaAcElementStatistics.fromAuthorative(testAces);
178 return testJpaAcElementStatistics;
181 private AcElementStatistics createAcElementStatisticsInstance() {
182 AcElementStatistics acElementStatistics = new AcElementStatistics();
183 acElementStatistics.setParticipantId(new ToscaConceptIdentifier("elementName", "0.0.1"));
184 acElementStatistics.setId(UUID.fromString("a95757ba-b34a-4049-a2a8-46773abcbe5e"));
185 acElementStatistics.setTimeStamp(Instant.ofEpochSecond(123456L));
186 acElementStatistics.setState(AutomationCompositionState.UNINITIALISED);
188 return acElementStatistics;