c82dcf060781a57f0a82816d893aa116439601d0
[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 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;
39
40 /**
41  * Test the {@link JpaClElementStatistics} class.
42  */
43 public class JpaClElementStatisticsTest {
44
45     private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
46
47     @Test
48     public void testJpaClElementStatisticsConstructor() {
49         assertThatThrownBy(() -> {
50             new JpaClElementStatistics((JpaClElementStatistics) null);
51         }).hasMessageMatching("copyConcept is marked .*ull but is null");
52
53         assertThatThrownBy(() -> {
54             new JpaClElementStatistics((PfReferenceTimestampKey) null);
55         }).hasMessageMatching(NULL_KEY_ERROR);
56
57         assertThatThrownBy(() -> {
58             new JpaClElementStatistics(null, null);
59         }).hasMessageMatching(NULL_KEY_ERROR);
60
61         assertThatThrownBy(() -> {
62             new JpaClElementStatistics(null, new PfConceptKey());
63         }).hasMessageMatching(NULL_KEY_ERROR);
64
65         assertThatThrownBy(() -> {
66             new JpaClElementStatistics(new PfReferenceTimestampKey(), null);
67         }).hasMessageMatching("participantId is marked .*ull but is null");
68
69         assertNotNull(new JpaClElementStatistics());
70         assertNotNull(new JpaClElementStatistics((new PfReferenceTimestampKey())));
71         assertNotNull(new JpaClElementStatistics(new PfReferenceTimestampKey(), new PfConceptKey()));
72     }
73
74     @Test
75     public void testJpaClElementStatistics() {
76         JpaClElementStatistics testJpaClElementStatistics = createJpaClElementStatisticsInstance();
77
78         ClElementStatistics cles = createClElementStatisticsInstance();
79         assertEquals(cles, testJpaClElementStatistics.toAuthorative());
80
81         assertThatThrownBy(() -> {
82             testJpaClElementStatistics.fromAuthorative(null);
83         }).hasMessageMatching("clElementStatistics is marked .*ull but is null");
84
85         assertThatThrownBy(() -> new JpaClElementStatistics((JpaClElementStatistics) null))
86             .isInstanceOf(NullPointerException.class);
87
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);
99
100         testJpaClElementStatisticsFa = new JpaClElementStatistics(cles);
101         assertEquals(testJpaClElementStatistics, testJpaClElementStatisticsFa);
102
103         assertEquals(1, testJpaClElementStatistics.getKeys().size());
104
105         assertEquals("elementName", testJpaClElementStatistics.getKey().getReferenceKey().getParentKeyName());
106
107         testJpaClElementStatistics.clean();
108         assertEquals("elementName", testJpaClElementStatistics.getKey().getReferenceKey().getParentKeyName());
109
110         JpaClElementStatistics testJpaClElementStatistics2 = new JpaClElementStatistics(testJpaClElementStatistics);
111         assertEquals(testJpaClElementStatistics, testJpaClElementStatistics2);
112     }
113
114     @Test
115     public void testJpaClElementStatisticsValidation() {
116         JpaClElementStatistics testJpaClElementStatistics = createJpaClElementStatisticsInstance();
117
118         assertThatThrownBy(() -> {
119             testJpaClElementStatistics.validate(null);
120         }).hasMessageMatching("fieldName is marked .*ull but is null");
121
122         assertTrue(testJpaClElementStatistics.validate("").isValid());
123     }
124
125     @Test
126     public void testJpaClElementStatisticsCompareTo() {
127         JpaClElementStatistics testJpaClElementStatistics = createJpaClElementStatisticsInstance();
128
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()));
134
135         testJpaClElementStatistics.setState(ControlLoopState.PASSIVE);
136         assertNotEquals(0, testJpaClElementStatistics.compareTo(otherJpaClElementStatistics));
137         testJpaClElementStatistics.setState(ControlLoopState.UNINITIALISED);
138         assertEquals(0, testJpaClElementStatistics.compareTo(otherJpaClElementStatistics));
139
140         assertEquals(testJpaClElementStatistics, new JpaClElementStatistics(testJpaClElementStatistics));
141     }
142
143     @Test
144     public void testJpaClElementStatisticsLombok() {
145         assertNotNull(new Participant());
146         JpaClElementStatistics cles0 = new JpaClElementStatistics();
147
148         assertThat(cles0.toString()).contains("JpaClElementStatistics(");
149         assertThat(cles0.hashCode()).isNotZero();
150         assertEquals(true, cles0.equals(cles0));
151         assertEquals(false, cles0.equals(null));
152
153
154         JpaClElementStatistics cles11 = new JpaClElementStatistics();
155
156         cles11.setState(ControlLoopState.UNINITIALISED);
157
158         assertThat(cles11.toString()).contains("JpaClElementStatistics(");
159         assertEquals(false, cles11.hashCode() == 0);
160         assertEquals(false, cles11.equals(cles0));
161         assertEquals(false, cles11.equals(null));
162
163         assertNotEquals(cles11, cles0);
164
165         JpaClElementStatistics cles2 = new JpaClElementStatistics();
166         assertEquals(cles2, cles0);
167     }
168
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);
176
177         return testJpaClElementStatistics;
178     }
179
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);
186
187         return clElementStatistics;
188     }
189 }