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