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 org.junit.jupiter.api.Test;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
38 * Test the {@link JpaParticiant} class.
40 class JpaParticipantTest {
42 private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
45 void testJpaParticipantConstructor() {
46 assertThatThrownBy(() -> {
47 new JpaParticipant((JpaParticipant) null);
48 }).hasMessageMatching("copyConcept is marked .*ull but is null");
50 assertThatThrownBy(() -> {
51 new JpaParticipant((PfConceptKey) null);
52 }).hasMessageMatching(NULL_KEY_ERROR);
54 assertThatThrownBy(() -> {
55 new JpaParticipant(null, null, null, null);
56 }).hasMessageMatching(NULL_KEY_ERROR);
58 assertThatThrownBy(() -> {
59 new JpaParticipant(null, null, null, ParticipantHealthStatus.HEALTHY);
60 }).hasMessageMatching(NULL_KEY_ERROR);
62 assertThatThrownBy(() -> {
63 new JpaParticipant(null, null, ParticipantState.ACTIVE, null);
64 }).hasMessageMatching(NULL_KEY_ERROR);
66 assertThatThrownBy(() -> {
67 new JpaParticipant(null, null, ParticipantState.ACTIVE, ParticipantHealthStatus.HEALTHY);
68 }).hasMessageMatching(NULL_KEY_ERROR);
70 assertThatThrownBy(() -> {
71 new JpaParticipant(null, new PfConceptKey(), null, null);
72 }).hasMessageMatching(NULL_KEY_ERROR);
74 assertThatThrownBy(() -> {
75 new JpaParticipant(null, new PfConceptKey(), null, ParticipantHealthStatus.HEALTHY);
76 }).hasMessageMatching(NULL_KEY_ERROR);
78 assertThatThrownBy(() -> {
79 new JpaParticipant(null, new PfConceptKey(), ParticipantState.ACTIVE, null);
80 }).hasMessageMatching(NULL_KEY_ERROR);
82 assertThatThrownBy(() -> {
83 new JpaParticipant(null, new PfConceptKey(), ParticipantState.ACTIVE, ParticipantHealthStatus.HEALTHY);
84 }).hasMessageMatching(NULL_KEY_ERROR);
86 assertThatThrownBy(() -> {
87 new JpaParticipant(new PfConceptKey(), null, null, null);
88 }).hasMessageMatching("definition is marked .*ull but is null");
90 assertThatThrownBy(() -> {
91 new JpaParticipant(new PfConceptKey(), null, null, ParticipantHealthStatus.HEALTHY);
92 }).hasMessageMatching("definition is marked .*ull but is null");
94 assertThatThrownBy(() -> {
95 new JpaParticipant(new PfConceptKey(), null, ParticipantState.ACTIVE, null);
96 }).hasMessageMatching("definition is marked .*ull but is null");
98 assertThatThrownBy(() -> {
99 new JpaParticipant(new PfConceptKey(), null, ParticipantState.ACTIVE, ParticipantHealthStatus.HEALTHY);
100 }).hasMessageMatching("definition is marked .*ull but is null");
102 assertThatThrownBy(() -> {
103 new JpaParticipant(new PfConceptKey(), new PfConceptKey(), null, null);
104 }).hasMessageMatching("participantState is marked .*ull but is null");
106 assertThatThrownBy(() -> {
107 new JpaParticipant(new PfConceptKey(), new PfConceptKey(), null, ParticipantHealthStatus.HEALTHY);
108 }).hasMessageMatching("participantState is marked .*ull but is null");
110 assertThatThrownBy(() -> {
111 new JpaParticipant(new PfConceptKey(), new PfConceptKey(), ParticipantState.ACTIVE, null);
112 }).hasMessageMatching("healthStatus is marked .*ull but is null");
114 assertNotNull(new JpaParticipant());
115 assertNotNull(new JpaParticipant((new PfConceptKey())));
116 assertNotNull(new JpaParticipant(new PfConceptKey(), new PfConceptKey(), ParticipantState.ACTIVE,
117 ParticipantHealthStatus.HEALTHY));
121 void testJpaParticipant() {
122 JpaParticipant testJpaParticipant = createJpaParticipantInstance();
124 Participant participant = createParticipantInstance();
125 assertEquals(participant, testJpaParticipant.toAuthorative());
127 assertThatThrownBy(() -> {
128 testJpaParticipant.fromAuthorative(null);
129 }).hasMessageMatching("participant is marked .*ull but is null");
131 assertThatThrownBy(() -> new JpaParticipant((JpaParticipant) null)).isInstanceOf(NullPointerException.class);
133 JpaParticipant testJpaParticipantFa = new JpaParticipant();
134 testJpaParticipantFa.setKey(null);
135 testJpaParticipantFa.fromAuthorative(participant);
136 assertEquals(testJpaParticipant, testJpaParticipantFa);
137 testJpaParticipantFa.setKey(PfConceptKey.getNullKey());
138 testJpaParticipantFa.fromAuthorative(participant);
139 assertEquals(testJpaParticipant, testJpaParticipantFa);
140 testJpaParticipantFa.setKey(new PfConceptKey("participant", "0.0.1"));
141 testJpaParticipantFa.fromAuthorative(participant);
142 assertEquals(testJpaParticipant, testJpaParticipantFa);
144 assertEquals("participant", testJpaParticipant.getKey().getName());
145 assertEquals("participant", new JpaParticipant(createParticipantInstance()).getKey().getName());
146 assertEquals("participant",
147 ((PfConceptKey) new JpaParticipant(createParticipantInstance()).getKeys().get(0)).getName());
149 testJpaParticipant.clean();
150 assertEquals("participant", testJpaParticipant.getKey().getName());
152 testJpaParticipant.setDescription(" A Message ");
153 testJpaParticipant.clean();
154 assertEquals("A Message", testJpaParticipant.getDescription());
156 JpaParticipant testJpaParticipant2 = new JpaParticipant(testJpaParticipant);
157 assertEquals(testJpaParticipant, testJpaParticipant2);
161 void testJpaParticipantValidation() {
162 JpaParticipant testJpaParticipant = createJpaParticipantInstance();
164 assertThatThrownBy(() -> {
165 testJpaParticipant.validate(null);
166 }).hasMessageMatching("fieldName is marked .*ull but is null");
168 assertTrue(testJpaParticipant.validate("").isValid());
172 void testJpaParticipantCompareTo() {
173 JpaParticipant testJpaParticipant = createJpaParticipantInstance();
175 JpaParticipant otherJpaParticipant = new JpaParticipant(testJpaParticipant);
176 assertEquals(0, testJpaParticipant.compareTo(otherJpaParticipant));
177 assertEquals(-1, testJpaParticipant.compareTo(null));
178 assertEquals(0, testJpaParticipant.compareTo(testJpaParticipant));
179 assertNotEquals(0, testJpaParticipant.compareTo(new DummyJpaParticipantChild()));
181 testJpaParticipant.setKey(new PfConceptKey("BadValue", "0.0.1"));
182 assertNotEquals(0, testJpaParticipant.compareTo(otherJpaParticipant));
183 testJpaParticipant.setKey(new PfConceptKey("participant", "0.0.1"));
184 assertEquals(0, testJpaParticipant.compareTo(otherJpaParticipant));
186 testJpaParticipant.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
187 assertNotEquals(0, testJpaParticipant.compareTo(otherJpaParticipant));
188 testJpaParticipant.setDefinition(new PfConceptKey("participantDefinitionName", "0.0.1"));
189 assertEquals(0, testJpaParticipant.compareTo(otherJpaParticipant));
191 testJpaParticipant.setParticipantState(ParticipantState.PASSIVE);
192 assertNotEquals(0, testJpaParticipant.compareTo(otherJpaParticipant));
193 testJpaParticipant.setParticipantState(ParticipantState.UNKNOWN);
194 assertEquals(0, testJpaParticipant.compareTo(otherJpaParticipant));
196 testJpaParticipant.setHealthStatus(ParticipantHealthStatus.NOT_HEALTHY);
197 assertNotEquals(0, testJpaParticipant.compareTo(otherJpaParticipant));
198 testJpaParticipant.setHealthStatus(ParticipantHealthStatus.UNKNOWN);
199 assertEquals(0, testJpaParticipant.compareTo(otherJpaParticipant));
201 assertEquals(testJpaParticipant, new JpaParticipant(testJpaParticipant));
205 void testJpaParticipantLombok() {
206 assertNotNull(new Participant());
207 JpaParticipant p0 = new JpaParticipant();
209 assertThat(p0.toString()).contains("JpaParticipant(");
210 assertThat(p0.hashCode()).isNotZero();
211 assertEquals(true, p0.equals(p0));
212 assertEquals(false, p0.equals(null));
215 JpaParticipant p1 = new JpaParticipant();
217 p1.setDefinition(new PfConceptKey("defName", "0.0.1"));
218 p1.setDescription("Description");
219 p1.setHealthStatus(ParticipantHealthStatus.HEALTHY);
220 p1.setKey(new PfConceptKey("participant", "0.0.1"));
221 p1.setParticipantState(ParticipantState.ACTIVE);
223 assertThat(p1.toString()).contains("Participant(");
224 assertEquals(false, p1.hashCode() == 0);
225 assertEquals(false, p1.equals(p0));
226 assertEquals(false, p1.equals(null));
228 assertNotEquals(p1, p0);
230 JpaParticipant p2 = new JpaParticipant();
231 assertEquals(p2, p0);
234 private JpaParticipant createJpaParticipantInstance() {
235 Participant testParticipant = createParticipantInstance();
236 JpaParticipant testJpaParticipant = new JpaParticipant();
237 testJpaParticipant.setKey(null);
238 testJpaParticipant.fromAuthorative(testParticipant);
239 testJpaParticipant.setKey(PfConceptKey.getNullKey());
240 testJpaParticipant.fromAuthorative(testParticipant);
242 return testJpaParticipant;
245 private Participant createParticipantInstance() {
246 Participant testParticipant = new Participant();
247 testParticipant.setName("participant");
248 testParticipant.setVersion("0.0.1");
249 testParticipant.setDefinition(new ToscaConceptIdentifier("participantDefinitionName", "0.0.1"));
250 testParticipant.setParticipantType(new ToscaConceptIdentifier("participantTypeName", "0.0.1"));
252 return testParticipant;