Fix Sonar Issues on policy-models-tosca
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaTriggerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.time.Duration;
32 import java.time.Instant;
33 import org.junit.Test;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfReferenceKey;
36
37 /**
38  * DAO test for ToscaTrigger.
39  *
40  * @author Liam Fallon (liam.fallon@est.tech)
41  */
42 public class JpaToscaTriggerTest {
43
44     private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
45     private static final String EVENT_TYPE = "EventType";
46     private static final String ACTION = "Action";
47     private static final String A_METHOD = "A Method";
48     private static final String A_DESCRIPTION = "A Description";
49     private static final String VERSION_001 = "0.0.1";
50
51     @Test
52     public void testTriggerPojo() {
53         assertNotNull(new JpaToscaTrigger());
54         assertNotNull(new JpaToscaTrigger(new PfReferenceKey()));
55         assertNotNull(new JpaToscaTrigger(new PfReferenceKey(), EVENT_TYPE, ACTION));
56         assertNotNull(new JpaToscaTrigger(new JpaToscaTrigger()));
57
58         assertThatThrownBy(() -> new JpaToscaTrigger((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL);
59
60         assertThatThrownBy(() -> new JpaToscaTrigger(null, null, null)).hasMessageMatching(KEY_IS_NULL);
61
62         assertThatThrownBy(() -> new JpaToscaTrigger(null, EVENT_TYPE, null)).hasMessageMatching(KEY_IS_NULL);
63
64         assertThatThrownBy(() -> new JpaToscaTrigger(null, EVENT_TYPE, ACTION)).hasMessageMatching(KEY_IS_NULL);
65
66         assertThatThrownBy(() -> new JpaToscaTrigger(null, null, ACTION)).hasMessageMatching(KEY_IS_NULL);
67
68         assertThatThrownBy(() -> new JpaToscaTrigger(new PfReferenceKey(), null, null))
69                 .hasMessageMatching("eventType is marked .*on.*ull but is null");
70
71         assertThatThrownBy(() -> new JpaToscaTrigger(new PfReferenceKey(), EVENT_TYPE, null))
72                 .hasMessageMatching("action is marked .*on.*ull but is null");
73
74         assertThatThrownBy(() -> new JpaToscaTrigger(new PfReferenceKey(), null, ACTION))
75                 .hasMessageMatching("eventType is marked .*on.*ull but is null");
76
77         assertThatThrownBy(() -> new JpaToscaTrigger((JpaToscaTrigger) null)).isInstanceOf(NullPointerException.class);
78     }
79
80     @Test
81     public void testTriggerConstraints() {
82         PfConceptKey tparentKey = new PfConceptKey("tParentKey", VERSION_001);
83         PfReferenceKey tkey = new PfReferenceKey(tparentKey, "trigger0");
84         JpaToscaTrigger tdt = new JpaToscaTrigger(tkey, EVENT_TYPE, ACTION);
85
86         JpaToscaTimeInterval schedule =
87                 new JpaToscaTimeInterval(new PfReferenceKey(tkey, "sched"), Instant.now(), Instant.now());
88         tdt.setSchedule(schedule);
89
90         JpaToscaEventFilter targetFilter =
91                 new JpaToscaEventFilter(new PfReferenceKey(tkey, "filter"), new PfConceptKey("NodeName", VERSION_001));
92         tdt.setTargetFilter(targetFilter);
93
94         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
95         tdt.setCondition(lsc);
96         assertEquals(lsc, tdt.getCondition());
97         tdt.setConstraint(lsc);
98         assertEquals(lsc, tdt.getConstraint());
99
100         tdt.setPeriod(Duration.ZERO);
101         assertEquals(Duration.ZERO, tdt.getPeriod());
102
103         tdt.setDescription(A_DESCRIPTION);
104         assertEquals(A_DESCRIPTION, tdt.getDescription());
105
106         tdt.setMethod(A_METHOD);
107         assertEquals(A_METHOD, tdt.getMethod());
108
109         JpaToscaTrigger tdtClone0 = new JpaToscaTrigger(tdt);
110         checkEqualsToscaTriggers(tdt, tdtClone0);
111
112         JpaToscaTrigger tdtClone1 = new JpaToscaTrigger(tdt);
113         checkEqualsToscaTriggers(tdt, tdtClone1);
114
115         assertEquals(-1, tdt.compareTo(null));
116         assertEquals(0, tdt.compareTo(tdt));
117         assertNotEquals(0, tdt.compareTo(tdt.getKey()));
118
119         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherTrigger");
120         JpaToscaTrigger otherDt = new JpaToscaTrigger(otherDtKey);
121
122         assertNotEquals(0, tdt.compareTo(otherDt));
123         otherDt.setKey(tkey);
124         assertNotEquals(0, tdt.compareTo(otherDt));
125         otherDt.setDescription(A_DESCRIPTION);
126         assertNotEquals(0, tdt.compareTo(otherDt));
127         otherDt.setEventType(EVENT_TYPE);
128         assertNotEquals(0, tdt.compareTo(otherDt));
129         otherDt.setSchedule(schedule);
130         assertNotEquals(0, tdt.compareTo(otherDt));
131         otherDt.setTargetFilter(targetFilter);
132         assertNotEquals(0, tdt.compareTo(otherDt));
133         otherDt.setCondition(lsc);
134         assertNotEquals(0, tdt.compareTo(otherDt));
135         otherDt.setConstraint(lsc);
136         assertNotEquals(0, tdt.compareTo(otherDt));
137         otherDt.setPeriod(Duration.ZERO);
138         assertNotEquals(0, tdt.compareTo(otherDt));
139         otherDt.setMethod(A_METHOD);
140         assertNotEquals(0, tdt.compareTo(otherDt));
141         otherDt.setAction(ACTION);
142         assertEquals(0, tdt.compareTo(otherDt));
143
144         otherDt.setEvaluations(100);
145         assertNotEquals(0, tdt.compareTo(otherDt));
146         otherDt.setEvaluations(0);
147         assertEquals(0, tdt.compareTo(otherDt));
148
149         assertEquals(4, tdt.getKeys().size());
150         assertEquals(1, new JpaToscaTrigger().getKeys().size());
151     }
152
153     @Test
154     public void testCloneToscaTrigger() {
155         PfConceptKey tparentKey = new PfConceptKey("tParentKey", VERSION_001);
156         PfReferenceKey tkey = new PfReferenceKey(tparentKey, "trigger0");
157         JpaToscaTrigger tdt = new JpaToscaTrigger(tkey, EVENT_TYPE, ACTION);
158
159         JpaToscaTrigger tdtClone0 = new JpaToscaTrigger(tdt);
160
161         new JpaToscaTrigger().clean();
162         tdt.clean();
163         assertEquals(tdtClone0, tdt);
164
165         assertFalse(new JpaToscaTrigger().validate("").isValid());
166         assertTrue(tdt.validate("").isValid());
167
168         tdt.setDescription(null);
169         assertTrue(tdt.validate("").isValid());
170         tdt.setDescription("");
171         assertFalse(tdt.validate("").isValid());
172         tdt.setDescription(A_DESCRIPTION);
173         assertTrue(tdt.validate("").isValid());
174
175         tdt.setEvaluations(-1);
176         assertFalse(tdt.validate("").isValid());
177         tdt.setEvaluations(100);
178         assertTrue(tdt.validate("").isValid());
179
180         tdt.setMethod(null);
181         assertTrue(tdt.validate("").isValid());
182         tdt.setMethod("");
183         assertFalse(tdt.validate("").isValid());
184         tdt.setMethod(A_METHOD);
185         assertTrue(tdt.validate("").isValid());
186
187         assertThatThrownBy(() -> tdt.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
188     }
189
190     private void checkEqualsToscaTriggers(JpaToscaTrigger tdt1, JpaToscaTrigger tdt2) {
191         assertEquals(tdt1, tdt2);
192         assertEquals(0, tdt1.compareTo(tdt2));
193     }
194 }