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.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.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.jupiter.api.Test;
33 import org.onap.policy.clamp.models.acm.concepts.AcElementStatistics;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
35 import org.onap.policy.clamp.models.acm.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 JpaAcElementStatistics} class.
43 class JpaAcElementStatisticsTest {
45 private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
48 void testJpaAcElementStatisticsConstructor() {
49 assertThatThrownBy(() -> {
50 new JpaAcElementStatistics((JpaAcElementStatistics) null);
51 }).hasMessageMatching("copyConcept is marked .*ull but is null");
53 assertThatThrownBy(() -> {
54 new JpaAcElementStatistics((PfReferenceTimestampKey) null);
55 }).hasMessageMatching(NULL_KEY_ERROR);
57 assertThatThrownBy(() -> {
58 new JpaAcElementStatistics(null, null);
59 }).hasMessageMatching(NULL_KEY_ERROR);
61 assertThatThrownBy(() -> {
62 new JpaAcElementStatistics(null, new PfConceptKey());
63 }).hasMessageMatching(NULL_KEY_ERROR);
65 assertThatThrownBy(() -> {
66 new JpaAcElementStatistics(new PfReferenceTimestampKey(), null);
67 }).hasMessageMatching("participantId is marked .*ull but is null");
69 assertNotNull(new JpaAcElementStatistics());
70 assertNotNull(new JpaAcElementStatistics((new PfReferenceTimestampKey())));
71 assertNotNull(new JpaAcElementStatistics(new PfReferenceTimestampKey(), new PfConceptKey()));
75 void testJpaAcElementStatistics() {
76 JpaAcElementStatistics testJpaAcElementStatistics = createJpaAcElementStatisticsInstance();
78 AcElementStatistics aces = createAcElementStatisticsInstance();
79 assertEquals(aces, testJpaAcElementStatistics.toAuthorative());
81 assertThatThrownBy(() -> {
82 testJpaAcElementStatistics.fromAuthorative(null);
83 }).hasMessageMatching("acElementStatistics is marked .*ull but is null");
85 assertThatThrownBy(() -> new JpaAcElementStatistics((JpaAcElementStatistics) null))
86 .isInstanceOf(NullPointerException.class);
88 JpaAcElementStatistics testJpaAcElementStatisticsFa = new JpaAcElementStatistics();
89 testJpaAcElementStatisticsFa.setKey(null);
90 testJpaAcElementStatisticsFa.fromAuthorative(aces);
91 assertEquals(testJpaAcElementStatistics, testJpaAcElementStatisticsFa);
92 testJpaAcElementStatisticsFa.setKey(PfReferenceTimestampKey.getNullKey());
93 testJpaAcElementStatisticsFa.fromAuthorative(aces);
94 assertEquals(testJpaAcElementStatistics, testJpaAcElementStatisticsFa);
95 testJpaAcElementStatisticsFa.setKey(new PfReferenceTimestampKey("elementName", "0.0.1",
96 "a95757ba-b34a-4049-a2a8-46773abcbe5e", Instant.ofEpochSecond(123456L)));
97 testJpaAcElementStatisticsFa.fromAuthorative(aces);
98 assertEquals(testJpaAcElementStatistics, testJpaAcElementStatisticsFa);
100 testJpaAcElementStatisticsFa = new JpaAcElementStatistics(aces);
101 assertEquals(testJpaAcElementStatistics, testJpaAcElementStatisticsFa);
103 assertEquals(1, testJpaAcElementStatistics.getKeys().size());
105 assertEquals("elementName", testJpaAcElementStatistics.getKey().getReferenceKey().getParentKeyName());
107 testJpaAcElementStatistics.clean();
108 assertEquals("elementName", testJpaAcElementStatistics.getKey().getReferenceKey().getParentKeyName());
110 JpaAcElementStatistics testJpaAcElementStatistics2 = new JpaAcElementStatistics(testJpaAcElementStatistics);
111 assertEquals(testJpaAcElementStatistics, testJpaAcElementStatistics2);
115 void testJpaAcElementStatisticsValidation() {
116 JpaAcElementStatistics testJpaAcElementStatistics = createJpaAcElementStatisticsInstance();
118 assertThatThrownBy(() -> {
119 testJpaAcElementStatistics.validate(null);
120 }).hasMessageMatching("fieldName is marked .*ull but is null");
122 assertTrue(testJpaAcElementStatistics.validate("").isValid());
126 void testJpaAcElementStatisticsCompareTo() {
127 JpaAcElementStatistics testJpaAcElementStatistics = createJpaAcElementStatisticsInstance();
129 JpaAcElementStatistics otherJpaAcElementStatistics = new JpaAcElementStatistics(testJpaAcElementStatistics);
130 assertEquals(0, testJpaAcElementStatistics.compareTo(otherJpaAcElementStatistics));
131 assertEquals(-1, testJpaAcElementStatistics.compareTo(null));
132 assertEquals(0, testJpaAcElementStatistics.compareTo(testJpaAcElementStatistics));
133 assertNotEquals(0, testJpaAcElementStatistics.compareTo(new DummyJpaAcElementStatisticsChild()));
135 testJpaAcElementStatistics.setState(AutomationCompositionState.PASSIVE);
136 assertNotEquals(0, testJpaAcElementStatistics.compareTo(otherJpaAcElementStatistics));
137 testJpaAcElementStatistics.setState(AutomationCompositionState.UNINITIALISED);
138 assertEquals(0, testJpaAcElementStatistics.compareTo(otherJpaAcElementStatistics));
140 assertEquals(testJpaAcElementStatistics, new JpaAcElementStatistics(testJpaAcElementStatistics));
144 void testJpaAcElementStatisticsLombok() {
145 assertNotNull(new Participant());
146 JpaAcElementStatistics aces0 = new JpaAcElementStatistics();
148 assertThat(aces0.toString()).contains("JpaAcElementStatistics(");
149 assertThat(aces0.hashCode()).isNotZero();
150 assertEquals(true, aces0.equals(aces0));
151 assertEquals(false, aces0.equals(null));
154 JpaAcElementStatistics aces11 = new JpaAcElementStatistics();
156 aces11.setState(AutomationCompositionState.UNINITIALISED);
158 assertThat(aces11.toString()).contains("JpaAcElementStatistics(");
159 assertEquals(false, aces11.hashCode() == 0);
160 assertEquals(false, aces11.equals(aces0));
161 assertEquals(false, aces11.equals(null));
163 assertNotEquals(aces11, aces0);
165 JpaAcElementStatistics aces2 = new JpaAcElementStatistics();
166 assertEquals(aces2, aces0);
169 private JpaAcElementStatistics createJpaAcElementStatisticsInstance() {
170 AcElementStatistics testAces = createAcElementStatisticsInstance();
171 JpaAcElementStatistics testJpaAcElementStatistics = new JpaAcElementStatistics();
172 testJpaAcElementStatistics.setKey(null);
173 testJpaAcElementStatistics.fromAuthorative(testAces);
174 testJpaAcElementStatistics.setKey(PfReferenceTimestampKey.getNullKey());
175 testJpaAcElementStatistics.fromAuthorative(testAces);
177 return testJpaAcElementStatistics;
180 private AcElementStatistics createAcElementStatisticsInstance() {
181 AcElementStatistics acElementStatistics = new AcElementStatistics();
182 acElementStatistics.setParticipantId(new ToscaConceptIdentifier("elementName", "0.0.1"));
183 acElementStatistics.setId(UUID.fromString("a95757ba-b34a-4049-a2a8-46773abcbe5e"));
184 acElementStatistics.setTimeStamp(Instant.ofEpochSecond(123456L));
185 acElementStatistics.setState(AutomationCompositionState.UNINITIALISED);
187 return acElementStatistics;