d00d98ae0e5cd37861028e82a865849e58c47d86
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 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.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotEquals;
28 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30
31 import java.time.Instant;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.models.acm.concepts.AcElementStatistics;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
36 import org.onap.policy.clamp.models.acm.concepts.Participant;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfReferenceTimestampKey;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40
41 /**
42  * Test the {@link JpaAcElementStatistics} class.
43  */
44 class JpaAcElementStatisticsTest {
45
46     private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
47
48     @Test
49     void testJpaAcElementStatisticsConstructor() {
50         assertThatThrownBy(() -> {
51             new JpaAcElementStatistics((JpaAcElementStatistics) null);
52         }).hasMessageMatching("copyConcept is marked .*ull but is null");
53
54         assertThatThrownBy(() -> {
55             new JpaAcElementStatistics((PfReferenceTimestampKey) null);
56         }).hasMessageMatching(NULL_KEY_ERROR);
57
58         assertThatThrownBy(() -> {
59             new JpaAcElementStatistics(null, null);
60         }).hasMessageMatching(NULL_KEY_ERROR);
61
62         assertThatThrownBy(() -> {
63             new JpaAcElementStatistics(null, new PfConceptKey());
64         }).hasMessageMatching(NULL_KEY_ERROR);
65
66         assertThatThrownBy(() -> {
67             new JpaAcElementStatistics(new PfReferenceTimestampKey(), null);
68         }).hasMessageMatching("participantId is marked .*ull but is null");
69
70         assertNotNull(new JpaAcElementStatistics());
71         assertNotNull(new JpaAcElementStatistics((new PfReferenceTimestampKey())));
72         assertNotNull(new JpaAcElementStatistics(new PfReferenceTimestampKey(), new PfConceptKey()));
73     }
74
75     @Test
76     void testJpaAcElementStatistics() {
77         JpaAcElementStatistics testJpaAcElementStatistics = createJpaAcElementStatisticsInstance();
78
79         AcElementStatistics aces = createAcElementStatisticsInstance();
80         assertEquals(aces, testJpaAcElementStatistics.toAuthorative());
81
82         assertThatThrownBy(() -> {
83             testJpaAcElementStatistics.fromAuthorative(null);
84         }).hasMessageMatching("acElementStatistics is marked .*ull but is null");
85
86         assertThatThrownBy(() -> new JpaAcElementStatistics((JpaAcElementStatistics) null))
87                 .isInstanceOf(NullPointerException.class);
88
89         JpaAcElementStatistics testJpaAcElementStatisticsFa = new JpaAcElementStatistics();
90         testJpaAcElementStatisticsFa.setKey(null);
91         testJpaAcElementStatisticsFa.fromAuthorative(aces);
92         assertEquals(testJpaAcElementStatistics, testJpaAcElementStatisticsFa);
93         testJpaAcElementStatisticsFa.setKey(PfReferenceTimestampKey.getNullKey());
94         testJpaAcElementStatisticsFa.fromAuthorative(aces);
95         assertEquals(testJpaAcElementStatistics, testJpaAcElementStatisticsFa);
96         testJpaAcElementStatisticsFa.setKey(new PfReferenceTimestampKey("elementName", "0.0.1",
97             "a95757ba-b34a-4049-a2a8-46773abcbe5e", Instant.ofEpochSecond(123456L)));
98         testJpaAcElementStatisticsFa.fromAuthorative(aces);
99         assertEquals(testJpaAcElementStatistics, testJpaAcElementStatisticsFa);
100
101         testJpaAcElementStatisticsFa = new JpaAcElementStatistics(aces);
102         assertEquals(testJpaAcElementStatistics, testJpaAcElementStatisticsFa);
103
104         assertEquals(1, testJpaAcElementStatistics.getKeys().size());
105
106         assertEquals("elementName", testJpaAcElementStatistics.getKey().getReferenceKey().getParentKeyName());
107
108         testJpaAcElementStatistics.clean();
109         assertEquals("elementName", testJpaAcElementStatistics.getKey().getReferenceKey().getParentKeyName());
110
111         JpaAcElementStatistics testJpaAcElementStatistics2 = new JpaAcElementStatistics(testJpaAcElementStatistics);
112         assertEquals(testJpaAcElementStatistics, testJpaAcElementStatistics2);
113     }
114
115     @Test
116     void testJpaAcElementStatisticsValidation() {
117         JpaAcElementStatistics testJpaAcElementStatistics = createJpaAcElementStatisticsInstance();
118
119         assertThatThrownBy(() -> {
120             testJpaAcElementStatistics.validate(null);
121         }).hasMessageMatching("fieldName is marked .*ull but is null");
122
123         assertTrue(testJpaAcElementStatistics.validate("").isValid());
124     }
125
126     @Test
127     void testJpaAcElementStatisticsCompareTo() {
128         JpaAcElementStatistics testJpaAcElementStatistics = createJpaAcElementStatisticsInstance();
129
130         JpaAcElementStatistics otherJpaAcElementStatistics = new JpaAcElementStatistics(testJpaAcElementStatistics);
131         assertEquals(0, testJpaAcElementStatistics.compareTo(otherJpaAcElementStatistics));
132         assertEquals(-1, testJpaAcElementStatistics.compareTo(null));
133         assertEquals(0, testJpaAcElementStatistics.compareTo(testJpaAcElementStatistics));
134         assertNotEquals(0, testJpaAcElementStatistics.compareTo(new DummyJpaAcElementStatisticsChild()));
135
136         testJpaAcElementStatistics.setState(AutomationCompositionState.PASSIVE);
137         assertNotEquals(0, testJpaAcElementStatistics.compareTo(otherJpaAcElementStatistics));
138         testJpaAcElementStatistics.setState(AutomationCompositionState.UNINITIALISED);
139         assertEquals(0, testJpaAcElementStatistics.compareTo(otherJpaAcElementStatistics));
140
141         assertEquals(testJpaAcElementStatistics, new JpaAcElementStatistics(testJpaAcElementStatistics));
142     }
143
144     @Test
145     void testJpaAcElementStatisticsLombok() {
146         assertNotNull(new Participant());
147         JpaAcElementStatistics aces0 = new JpaAcElementStatistics();
148
149         assertThat(aces0.toString()).contains("JpaAcElementStatistics(");
150         assertThat(aces0.hashCode()).isNotZero();
151         assertEquals(aces0, aces0);
152         assertNotEquals(null, aces0);
153
154
155         JpaAcElementStatistics aces11 = new JpaAcElementStatistics();
156
157         aces11.setState(AutomationCompositionState.UNINITIALISED);
158
159         assertThat(aces11.toString()).contains("JpaAcElementStatistics(");
160         assertNotEquals(0, aces11.hashCode());
161         assertNotEquals(aces11, aces0);
162         assertNotEquals(null, aces11);
163
164         assertNotEquals(aces11, aces0);
165
166         JpaAcElementStatistics aces2 = new JpaAcElementStatistics();
167         assertEquals(aces2, aces0);
168     }
169
170     private JpaAcElementStatistics createJpaAcElementStatisticsInstance() {
171         AcElementStatistics testAces = createAcElementStatisticsInstance();
172         JpaAcElementStatistics testJpaAcElementStatistics = new JpaAcElementStatistics();
173         testJpaAcElementStatistics.setKey(null);
174         testJpaAcElementStatistics.fromAuthorative(testAces);
175         testJpaAcElementStatistics.setKey(PfReferenceTimestampKey.getNullKey());
176         testJpaAcElementStatistics.fromAuthorative(testAces);
177
178         return testJpaAcElementStatistics;
179     }
180
181     private AcElementStatistics createAcElementStatisticsInstance() {
182         AcElementStatistics acElementStatistics = new AcElementStatistics();
183         acElementStatistics.setParticipantId(new ToscaConceptIdentifier("elementName", "0.0.1"));
184         acElementStatistics.setId(UUID.fromString("a95757ba-b34a-4049-a2a8-46773abcbe5e"));
185         acElementStatistics.setTimeStamp(Instant.ofEpochSecond(123456L));
186         acElementStatistics.setState(AutomationCompositionState.UNINITIALISED);
187
188         return acElementStatistics;
189     }
190 }