0872923547595da81476aa3fa59b6f00338b0146
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts;
22
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;
29
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;
36
37 /**
38  * Test the {@link JpaParticiant} class.
39  */
40 class JpaParticipantTest {
41
42     private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
43
44     @Test
45     void testJpaParticipantConstructor() {
46         assertThatThrownBy(() -> {
47             new JpaParticipant((JpaParticipant) null);
48         }).hasMessageMatching("copyConcept is marked .*ull but is null");
49
50         assertThatThrownBy(() -> {
51             new JpaParticipant((PfConceptKey) null);
52         }).hasMessageMatching(NULL_KEY_ERROR);
53
54         assertThatThrownBy(() -> {
55             new JpaParticipant(null, null, null, null);
56         }).hasMessageMatching(NULL_KEY_ERROR);
57
58         assertThatThrownBy(() -> {
59             new JpaParticipant(null, null, null, ParticipantHealthStatus.HEALTHY);
60         }).hasMessageMatching(NULL_KEY_ERROR);
61
62         assertThatThrownBy(() -> {
63             new JpaParticipant(null, null, ParticipantState.ACTIVE, null);
64         }).hasMessageMatching(NULL_KEY_ERROR);
65
66         assertThatThrownBy(() -> {
67             new JpaParticipant(null, null, ParticipantState.ACTIVE, ParticipantHealthStatus.HEALTHY);
68         }).hasMessageMatching(NULL_KEY_ERROR);
69
70         assertThatThrownBy(() -> {
71             new JpaParticipant(null, new PfConceptKey(), null, null);
72         }).hasMessageMatching(NULL_KEY_ERROR);
73
74         assertThatThrownBy(() -> {
75             new JpaParticipant(null, new PfConceptKey(), null, ParticipantHealthStatus.HEALTHY);
76         }).hasMessageMatching(NULL_KEY_ERROR);
77
78         assertThatThrownBy(() -> {
79             new JpaParticipant(null, new PfConceptKey(), ParticipantState.ACTIVE, null);
80         }).hasMessageMatching(NULL_KEY_ERROR);
81
82         assertThatThrownBy(() -> {
83             new JpaParticipant(null, new PfConceptKey(), ParticipantState.ACTIVE, ParticipantHealthStatus.HEALTHY);
84         }).hasMessageMatching(NULL_KEY_ERROR);
85
86         assertThatThrownBy(() -> {
87             new JpaParticipant(new PfConceptKey(), null, null, null);
88         }).hasMessageMatching("definition is marked .*ull but is null");
89
90         assertThatThrownBy(() -> {
91             new JpaParticipant(new PfConceptKey(), null, null, ParticipantHealthStatus.HEALTHY);
92         }).hasMessageMatching("definition is marked .*ull but is null");
93
94         assertThatThrownBy(() -> {
95             new JpaParticipant(new PfConceptKey(), null, ParticipantState.ACTIVE, null);
96         }).hasMessageMatching("definition is marked .*ull but is null");
97
98         assertThatThrownBy(() -> {
99             new JpaParticipant(new PfConceptKey(), null, ParticipantState.ACTIVE, ParticipantHealthStatus.HEALTHY);
100         }).hasMessageMatching("definition is marked .*ull but is null");
101
102         assertThatThrownBy(() -> {
103             new JpaParticipant(new PfConceptKey(), new PfConceptKey(), null, null);
104         }).hasMessageMatching("participantState is marked .*ull but is null");
105
106         assertThatThrownBy(() -> {
107             new JpaParticipant(new PfConceptKey(), new PfConceptKey(), null, ParticipantHealthStatus.HEALTHY);
108         }).hasMessageMatching("participantState is marked .*ull but is null");
109
110         assertThatThrownBy(() -> {
111             new JpaParticipant(new PfConceptKey(), new PfConceptKey(), ParticipantState.ACTIVE, null);
112         }).hasMessageMatching("healthStatus is marked .*ull but is null");
113
114         assertNotNull(new JpaParticipant());
115         assertNotNull(new JpaParticipant((new PfConceptKey())));
116         assertNotNull(new JpaParticipant(new PfConceptKey(), new PfConceptKey(), ParticipantState.ACTIVE,
117                 ParticipantHealthStatus.HEALTHY));
118     }
119
120     @Test
121     void testJpaParticipant() {
122         JpaParticipant testJpaParticipant = createJpaParticipantInstance();
123
124         Participant participant = createParticipantInstance();
125         assertEquals(participant, testJpaParticipant.toAuthorative());
126
127         assertThatThrownBy(() -> {
128             testJpaParticipant.fromAuthorative(null);
129         }).hasMessageMatching("participant is marked .*ull but is null");
130
131         assertThatThrownBy(() -> new JpaParticipant((JpaParticipant) null)).isInstanceOf(NullPointerException.class);
132
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);
143
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());
148
149         testJpaParticipant.clean();
150         assertEquals("participant", testJpaParticipant.getKey().getName());
151
152         testJpaParticipant.setDescription("   A Message   ");
153         testJpaParticipant.clean();
154         assertEquals("A Message", testJpaParticipant.getDescription());
155
156         JpaParticipant testJpaParticipant2 = new JpaParticipant(testJpaParticipant);
157         assertEquals(testJpaParticipant, testJpaParticipant2);
158     }
159
160     @Test
161     void testJpaParticipantValidation() {
162         JpaParticipant testJpaParticipant = createJpaParticipantInstance();
163
164         assertThatThrownBy(() -> {
165             testJpaParticipant.validate(null);
166         }).hasMessageMatching("fieldName is marked .*ull but is null");
167
168         assertTrue(testJpaParticipant.validate("").isValid());
169     }
170
171     @Test
172     void testJpaParticipantCompareTo() {
173         JpaParticipant testJpaParticipant = createJpaParticipantInstance();
174
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()));
180
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));
185
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));
190
191         testJpaParticipant.setParticipantState(ParticipantState.PASSIVE);
192         assertNotEquals(0, testJpaParticipant.compareTo(otherJpaParticipant));
193         testJpaParticipant.setParticipantState(ParticipantState.UNKNOWN);
194         assertEquals(0, testJpaParticipant.compareTo(otherJpaParticipant));
195
196         testJpaParticipant.setHealthStatus(ParticipantHealthStatus.NOT_HEALTHY);
197         assertNotEquals(0, testJpaParticipant.compareTo(otherJpaParticipant));
198         testJpaParticipant.setHealthStatus(ParticipantHealthStatus.UNKNOWN);
199         assertEquals(0, testJpaParticipant.compareTo(otherJpaParticipant));
200
201         assertEquals(testJpaParticipant, new JpaParticipant(testJpaParticipant));
202     }
203
204     @Test
205     void testJpaParticipantLombok() {
206         assertNotNull(new Participant());
207         JpaParticipant p0 = new JpaParticipant();
208
209         assertThat(p0.toString()).contains("JpaParticipant(");
210         assertThat(p0.hashCode()).isNotZero();
211         assertEquals(true, p0.equals(p0));
212         assertEquals(false, p0.equals(null));
213
214
215         JpaParticipant p1 = new JpaParticipant();
216
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);
222
223         assertThat(p1.toString()).contains("Participant(");
224         assertEquals(false, p1.hashCode() == 0);
225         assertEquals(false, p1.equals(p0));
226         assertEquals(false, p1.equals(null));
227
228         assertNotEquals(p1, p0);
229
230         JpaParticipant p2 = new JpaParticipant();
231         assertEquals(p2, p0);
232     }
233
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);
241
242         return testJpaParticipant;
243     }
244
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"));
251
252         return testParticipant;
253     }
254 }