5c41d9647f78b47c69840a45ca14f0e4b55d4acb
[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 org.junit.Test;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfTimestampKey;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
38
39 /**
40  * Test the {@link JpaClElementStatistics} class.
41  */
42 public class JpaClElementStatisticsTest {
43
44     private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
45
46     @Test
47     public void testJpaClElementStatisticsConstructor() {
48         assertThatThrownBy(() -> {
49             new JpaClElementStatistics((JpaClElementStatistics) null);
50         }).hasMessageMatching("copyConcept is marked .*ull but is null");
51
52         assertThatThrownBy(() -> {
53             new JpaClElementStatistics((PfTimestampKey) null);
54         }).hasMessageMatching(NULL_KEY_ERROR);
55
56         assertThatThrownBy(() -> {
57             new JpaClElementStatistics(null, null);
58         }).hasMessageMatching(NULL_KEY_ERROR);
59
60         assertThatThrownBy(() -> {
61             new JpaClElementStatistics(null, new PfConceptKey());
62         }).hasMessageMatching(NULL_KEY_ERROR);
63
64         assertThatThrownBy(() -> {
65             new JpaClElementStatistics(new PfTimestampKey(), null);
66         }).hasMessageMatching("clElementId is marked .*ull but is null");
67
68         assertNotNull(new JpaClElementStatistics());
69         assertNotNull(new JpaClElementStatistics((new PfTimestampKey())));
70         assertNotNull(new JpaClElementStatistics(new PfTimestampKey(), new PfConceptKey()));
71     }
72
73     @Test
74     public void testJpaClElementStatistics() {
75         JpaClElementStatistics testJpaClElementStatistics = createJpaClElementStatisticsInstance();
76
77         ClElementStatistics cles = createClElementStatisticsInstance();
78         assertEquals(cles, testJpaClElementStatistics.toAuthorative());
79
80         assertThatThrownBy(() -> {
81             testJpaClElementStatistics.fromAuthorative(null);
82         }).hasMessageMatching("clElementStatistics is marked .*ull but is null");
83
84         assertThatThrownBy(() -> new JpaClElementStatistics((JpaClElementStatistics) null))
85                 .isInstanceOf(NullPointerException.class);
86
87         JpaClElementStatistics testJpaClElementStatisticsFa = new JpaClElementStatistics();
88         testJpaClElementStatisticsFa.setKey(null);
89         testJpaClElementStatisticsFa.fromAuthorative(cles);
90         assertEquals(testJpaClElementStatistics, testJpaClElementStatisticsFa);
91         testJpaClElementStatisticsFa.setKey(PfTimestampKey.getNullKey());
92         testJpaClElementStatisticsFa.fromAuthorative(cles);
93         assertEquals(testJpaClElementStatistics, testJpaClElementStatisticsFa);
94         testJpaClElementStatisticsFa.setKey(new PfTimestampKey("elementName", "0.0.1", Instant.ofEpochMilli(123456L)));
95         testJpaClElementStatisticsFa.fromAuthorative(cles);
96         assertEquals(testJpaClElementStatistics, testJpaClElementStatisticsFa);
97
98         testJpaClElementStatisticsFa = new JpaClElementStatistics(cles);
99         assertEquals(testJpaClElementStatistics, testJpaClElementStatisticsFa);
100
101         assertEquals(1, testJpaClElementStatistics.getKeys().size());
102
103         assertEquals("elementName", testJpaClElementStatistics.getKey().getName());
104
105         testJpaClElementStatistics.clean();
106         assertEquals("elementName", testJpaClElementStatistics.getKey().getName());
107
108         JpaClElementStatistics testJpaClElementStatistics2 = new JpaClElementStatistics(testJpaClElementStatistics);
109         assertEquals(testJpaClElementStatistics, testJpaClElementStatistics2);
110     }
111
112     @Test
113     public void testJpaClElementStatisticsValidation() {
114         JpaClElementStatistics testJpaClElementStatistics = createJpaClElementStatisticsInstance();
115
116         assertThatThrownBy(() -> {
117             testJpaClElementStatistics.validate(null);
118         }).hasMessageMatching("fieldName is marked .*ull but is null");
119
120         assertTrue(testJpaClElementStatistics.validate("").isValid());
121     }
122
123     @Test
124     public void testJpaClElementStatisticsConmpareTo() {
125         JpaClElementStatistics testJpaClElementStatistics = createJpaClElementStatisticsInstance();
126
127         JpaClElementStatistics otherJpaClElementStatistics = new JpaClElementStatistics(testJpaClElementStatistics);
128         assertEquals(0, testJpaClElementStatistics.compareTo(otherJpaClElementStatistics));
129         assertEquals(-1, testJpaClElementStatistics.compareTo(null));
130         assertEquals(0, testJpaClElementStatistics.compareTo(testJpaClElementStatistics));
131         assertNotEquals(0, testJpaClElementStatistics.compareTo(new DummyJpaClElementStatisticsChild()));
132
133         testJpaClElementStatistics.setState(ControlLoopState.PASSIVE);
134         assertNotEquals(0, testJpaClElementStatistics.compareTo(otherJpaClElementStatistics));
135         testJpaClElementStatistics.setState(ControlLoopState.UNINITIALISED);
136         assertEquals(0, testJpaClElementStatistics.compareTo(otherJpaClElementStatistics));
137
138         assertEquals(testJpaClElementStatistics, new JpaClElementStatistics(testJpaClElementStatistics));
139     }
140
141     @Test
142     public void testJpaClElementStatisticsLombok() {
143         assertNotNull(new Participant());
144         JpaClElementStatistics cles0 = new JpaClElementStatistics();
145
146         assertThat(cles0.toString()).contains("JpaClElementStatistics(");
147         assertThat(cles0.hashCode()).isNotZero();
148         assertEquals(true, cles0.equals(cles0));
149         assertEquals(false, cles0.equals(null));
150
151
152         JpaClElementStatistics cles11 = new JpaClElementStatistics();
153
154         cles11.setState(ControlLoopState.UNINITIALISED);
155
156         assertThat(cles11.toString()).contains("JpaClElementStatistics(");
157         assertEquals(false, cles11.hashCode() == 0);
158         assertEquals(false, cles11.equals(cles0));
159         assertEquals(false, cles11.equals(null));
160
161         assertNotEquals(cles11, cles0);
162
163         JpaClElementStatistics cles2 = new JpaClElementStatistics();
164         assertEquals(cles2, cles0);
165     }
166
167     private JpaClElementStatistics createJpaClElementStatisticsInstance() {
168         ClElementStatistics testCles = createClElementStatisticsInstance();
169         JpaClElementStatistics testJpaClElementStatistics = new JpaClElementStatistics();
170         testJpaClElementStatistics.setKey(null);
171         testJpaClElementStatistics.fromAuthorative(testCles);
172         testJpaClElementStatistics.setKey(PfTimestampKey.getNullKey());
173         testJpaClElementStatistics.fromAuthorative(testCles);
174
175         return testJpaClElementStatistics;
176     }
177
178     private ClElementStatistics createClElementStatisticsInstance() {
179         ClElementStatistics clElementStatistics = new ClElementStatistics();
180         clElementStatistics.setControlLoopElementId(new ToscaConceptIdentifier("elementName", "0.0.1"));
181         clElementStatistics.setTimeStamp(Instant.ofEpochMilli(123456L));
182         clElementStatistics.setControlLoopState(ControlLoopState.UNINITIALISED);
183
184         return clElementStatistics;
185     }
186 }