Merge "Fix simple sonar issues in models: errors to sim-pdp"
[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 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 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.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.time.Duration;
31 import java.util.Date;
32 import org.junit.Test;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfReferenceKey;
35 import org.onap.policy.models.base.PfValidationResult;
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 @NonNull 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)).hasMessage(KEY_IS_NULL);
59
60         assertThatThrownBy(() -> new JpaToscaTrigger(null, null, null)).hasMessage(KEY_IS_NULL);
61
62         assertThatThrownBy(() -> new JpaToscaTrigger(null, EVENT_TYPE, null)).hasMessage(KEY_IS_NULL);
63
64         assertThatThrownBy(() -> new JpaToscaTrigger(null, EVENT_TYPE, ACTION)).hasMessage(KEY_IS_NULL);
65
66         assertThatThrownBy(() -> new JpaToscaTrigger(null, null, ACTION)).hasMessage(KEY_IS_NULL);
67
68         assertThatThrownBy(() -> new JpaToscaTrigger(new PfReferenceKey(), null, null))
69                         .hasMessage("eventType is marked @NonNull but is null");
70
71         assertThatThrownBy(() -> new JpaToscaTrigger(new PfReferenceKey(), EVENT_TYPE, null))
72                         .hasMessage("action is marked @NonNull but is null");
73
74         assertThatThrownBy(() -> new JpaToscaTrigger(new PfReferenceKey(), null, ACTION))
75                         .hasMessage("eventType is marked @NonNull but is null");
76
77         assertThatThrownBy(() -> new JpaToscaTrigger((JpaToscaTrigger) null))
78                         .hasMessage("copyConcept is marked @NonNull but is null");
79
80         PfConceptKey tparentKey = new PfConceptKey("tParentKey", VERSION_001);
81         PfReferenceKey tkey = new PfReferenceKey(tparentKey, "trigger0");
82         JpaToscaTrigger tdt = new JpaToscaTrigger(tkey, EVENT_TYPE, ACTION);
83
84         JpaToscaTimeInterval schedule =
85                 new JpaToscaTimeInterval(new PfReferenceKey(tkey, "sched"), new Date(), new Date());
86         tdt.setSchedule(schedule);
87
88         JpaToscaEventFilter targetFilter =
89                 new JpaToscaEventFilter(new PfReferenceKey(tkey, "filter"), new PfConceptKey("NodeName", VERSION_001));
90         tdt.setTargetFilter(targetFilter);
91
92         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
93         tdt.setCondition(lsc);
94         assertEquals(lsc, tdt.getCondition());
95         tdt.setConstraint(lsc);
96         assertEquals(lsc, tdt.getConstraint());
97
98         tdt.setPeriod(Duration.ZERO);
99         assertEquals(Duration.ZERO, tdt.getPeriod());
100
101         tdt.setDescription(A_DESCRIPTION);
102         assertEquals(A_DESCRIPTION, tdt.getDescription());
103
104         tdt.setMethod(A_METHOD);
105         assertEquals(A_METHOD, tdt.getMethod());
106
107         JpaToscaTrigger tdtClone0 = new JpaToscaTrigger(tdt);
108         assertEquals(tdt, tdtClone0);
109         assertEquals(0, tdt.compareTo(tdtClone0));
110
111         JpaToscaTrigger tdtClone1 = new JpaToscaTrigger();
112         tdt.copyTo(tdtClone1);
113         assertEquals(tdt, tdtClone1);
114         assertEquals(0, tdt.compareTo(tdtClone1));
115
116         assertEquals(-1, tdt.compareTo(null));
117         assertEquals(0, tdt.compareTo(tdt));
118         assertFalse(tdt.compareTo(tdt.getKey()) == 0);
119
120         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherTrigger");
121         JpaToscaTrigger otherDt = new JpaToscaTrigger(otherDtKey);
122
123         assertFalse(tdt.compareTo(otherDt) == 0);
124         otherDt.setKey(tkey);
125         assertFalse(tdt.compareTo(otherDt) == 0);
126         otherDt.setDescription(A_DESCRIPTION);
127         assertFalse(tdt.compareTo(otherDt) == 0);
128         otherDt.setEventType(EVENT_TYPE);
129         assertFalse(tdt.compareTo(otherDt) == 0);
130         otherDt.setSchedule(schedule);
131         assertFalse(tdt.compareTo(otherDt) == 0);
132         otherDt.setTargetFilter(targetFilter);
133         assertFalse(tdt.compareTo(otherDt) == 0);
134         otherDt.setCondition(lsc);
135         assertFalse(tdt.compareTo(otherDt) == 0);
136         otherDt.setConstraint(lsc);
137         assertFalse(tdt.compareTo(otherDt) == 0);
138         otherDt.setPeriod(Duration.ZERO);
139         assertFalse(tdt.compareTo(otherDt) == 0);
140         otherDt.setMethod(A_METHOD);
141         assertFalse(tdt.compareTo(otherDt) == 0);
142         otherDt.setAction(ACTION);
143         assertEquals(0, tdt.compareTo(otherDt));
144
145         otherDt.setEvaluations(100);
146         assertFalse(tdt.compareTo(otherDt) == 0);
147         otherDt.setEvaluations(0);
148         assertEquals(0, tdt.compareTo(otherDt));
149
150         assertThatThrownBy(() -> tdt.copyTo(null)).hasMessage("target is marked @NonNull but is null");
151
152         assertEquals(4, tdt.getKeys().size());
153         assertEquals(1, new JpaToscaTrigger().getKeys().size());
154
155         new JpaToscaTrigger().clean();
156         tdt.clean();
157         assertEquals(tdtClone0, tdt);
158
159         assertFalse(new JpaToscaTrigger().validate(new PfValidationResult()).isValid());
160         assertTrue(tdt.validate(new PfValidationResult()).isValid());
161
162         tdt.setDescription(null);
163         assertTrue(tdt.validate(new PfValidationResult()).isValid());
164         tdt.setDescription("");
165         assertFalse(tdt.validate(new PfValidationResult()).isValid());
166         tdt.setDescription(A_DESCRIPTION);
167         assertTrue(tdt.validate(new PfValidationResult()).isValid());
168
169         tdt.setEvaluations(-1);
170         assertFalse(tdt.validate(new PfValidationResult()).isValid());
171         tdt.setEvaluations(100);
172         assertTrue(tdt.validate(new PfValidationResult()).isValid());
173
174         tdt.setMethod(null);
175         assertTrue(tdt.validate(new PfValidationResult()).isValid());
176         tdt.setMethod("");
177         assertFalse(tdt.validate(new PfValidationResult()).isValid());
178         tdt.setMethod(A_METHOD);
179         assertTrue(tdt.validate(new PfValidationResult()).isValid());
180
181         assertThatThrownBy(() -> tdt.validate(null)).hasMessage("resultIn is marked @NonNull but is null");
182     }
183 }