Add support for legacy guard policies
[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  * ================================================================================
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.models.tosca.simple.concepts;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.time.Duration;
30 import java.util.Date;
31
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 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogical.Operation;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogicalString;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaEventFilter;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTimeInterval;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTrigger;
41
42 /**
43  * DAO test for ToscaTrigger.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class JpaToscaTriggerTest {
48
49     @Test
50     public void testTriggerPojo() {
51         assertNotNull(new JpaToscaTrigger());
52         assertNotNull(new JpaToscaTrigger(new PfReferenceKey()));
53         assertNotNull(new JpaToscaTrigger(new PfReferenceKey(), "EventType", "Action"));
54         assertNotNull(new JpaToscaTrigger(new JpaToscaTrigger()));
55
56         try {
57             new JpaToscaTrigger((PfReferenceKey) null);
58             fail("test should throw an exception");
59         } catch (Exception exc) {
60             assertEquals("key is marked @NonNull but is null", exc.getMessage());
61         }
62
63         try {
64             new JpaToscaTrigger(null, null, null);
65             fail("test should throw an exception");
66         } catch (Exception exc) {
67             assertEquals("key is marked @NonNull but is null", exc.getMessage());
68         }
69
70         try {
71             new JpaToscaTrigger(null, "EventType", null);
72             fail("test should throw an exception");
73         } catch (Exception exc) {
74             assertEquals("key is marked @NonNull but is null", exc.getMessage());
75         }
76
77         try {
78             new JpaToscaTrigger(null, "EventType", "Action");
79             fail("test should throw an exception");
80         } catch (Exception exc) {
81             assertEquals("key is marked @NonNull but is null", exc.getMessage());
82         }
83
84         try {
85             new JpaToscaTrigger(null, null, "Action");
86             fail("test should throw an exception");
87         } catch (Exception exc) {
88             assertEquals("key is marked @NonNull but is null", exc.getMessage());
89         }
90
91         try {
92             new JpaToscaTrigger(new PfReferenceKey(), null, null);
93             fail("test should throw an exception");
94         } catch (Exception exc) {
95             assertEquals("eventType is marked @NonNull but is null", exc.getMessage());
96         }
97
98         try {
99             new JpaToscaTrigger(new PfReferenceKey(), "EventType", null);
100             fail("test should throw an exception");
101         } catch (Exception exc) {
102             assertEquals("action is marked @NonNull but is null", exc.getMessage());
103         }
104
105         try {
106             new JpaToscaTrigger(new PfReferenceKey(), null, "Action");
107             fail("test should throw an exception");
108         } catch (Exception exc) {
109             assertEquals("eventType is marked @NonNull but is null", exc.getMessage());
110         }
111
112         try {
113             new JpaToscaTrigger((JpaToscaTrigger) null);
114             fail("test should throw an exception");
115         } catch (Exception exc) {
116             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
117         }
118
119         PfConceptKey tparentKey = new PfConceptKey("tParentKey", "0.0.1");
120         PfReferenceKey tkey = new PfReferenceKey(tparentKey, "trigger0");
121         JpaToscaTrigger tdt = new JpaToscaTrigger(tkey, "EventType", "Action");
122
123         JpaToscaTimeInterval schedule =
124                 new JpaToscaTimeInterval(new PfReferenceKey(tkey, "sched"), new Date(), new Date());
125         tdt.setSchedule(schedule);
126
127         JpaToscaEventFilter targetFilter =
128                 new JpaToscaEventFilter(new PfReferenceKey(tkey, "filter"), new PfConceptKey("NodeName", "0.0.1"));
129         tdt.setTargetFilter(targetFilter);
130
131         JpaToscaConstraintLogicalString lsc =
132                 new JpaToscaConstraintLogicalString(new PfReferenceKey(tkey, "sc"), Operation.EQ, "hello");
133         tdt.setCondition(lsc);
134         assertEquals(lsc, tdt.getCondition());
135         tdt.setConstraint(lsc);
136         assertEquals(lsc, tdt.getConstraint());
137
138         tdt.setPeriod(Duration.ZERO);
139         assertEquals(Duration.ZERO, tdt.getPeriod());
140
141         tdt.setDescription("A Description");
142         assertEquals("A Description", tdt.getDescription());
143
144         tdt.setMethod("A Method");
145         assertEquals("A Method", tdt.getMethod());
146
147         JpaToscaTrigger tdtClone0 = new JpaToscaTrigger(tdt);
148         assertEquals(tdt, tdtClone0);
149         assertEquals(0, tdt.compareTo(tdtClone0));
150
151         JpaToscaTrigger tdtClone1 = new JpaToscaTrigger();
152         tdt.copyTo(tdtClone1);
153         assertEquals(tdt, tdtClone1);
154         assertEquals(0, tdt.compareTo(tdtClone1));
155
156         assertEquals(-1, tdt.compareTo(null));
157         assertEquals(0, tdt.compareTo(tdt));
158         assertFalse(tdt.compareTo(tdt.getKey()) == 0);
159
160         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherTrigger");
161         JpaToscaTrigger otherDt = new JpaToscaTrigger(otherDtKey);
162
163         assertFalse(tdt.compareTo(otherDt) == 0);
164         otherDt.setKey(tkey);
165         assertFalse(tdt.compareTo(otherDt) == 0);
166         otherDt.setDescription("A Description");
167         assertFalse(tdt.compareTo(otherDt) == 0);
168         otherDt.setEventType("EventType");
169         assertFalse(tdt.compareTo(otherDt) == 0);
170         otherDt.setSchedule(schedule);
171         assertFalse(tdt.compareTo(otherDt) == 0);
172         otherDt.setTargetFilter(targetFilter);
173         assertFalse(tdt.compareTo(otherDt) == 0);
174         otherDt.setCondition(lsc);
175         assertFalse(tdt.compareTo(otherDt) == 0);
176         otherDt.setConstraint(lsc);
177         assertFalse(tdt.compareTo(otherDt) == 0);
178         otherDt.setPeriod(Duration.ZERO);
179         assertFalse(tdt.compareTo(otherDt) == 0);
180         otherDt.setMethod("A Method");
181         assertFalse(tdt.compareTo(otherDt) == 0);
182         otherDt.setAction("Action");
183         assertEquals(0, tdt.compareTo(otherDt));
184
185         otherDt.setEvaluations(100);
186         assertFalse(tdt.compareTo(otherDt) == 0);
187         otherDt.setEvaluations(0);
188         assertEquals(0, tdt.compareTo(otherDt));
189
190         try {
191             tdt.copyTo(null);
192             fail("test should throw an exception");
193         } catch (Exception exc) {
194             assertEquals("target is marked @NonNull but is null", exc.getMessage());
195         }
196
197         assertEquals(6, tdt.getKeys().size());
198         assertEquals(1, new JpaToscaTrigger().getKeys().size());
199
200         new JpaToscaTrigger().clean();
201         tdt.clean();
202         assertEquals(tdtClone0, tdt);
203
204         assertFalse(new JpaToscaTrigger().validate(new PfValidationResult()).isValid());
205         assertTrue(tdt.validate(new PfValidationResult()).isValid());
206
207         tdt.setDescription(null);
208         assertTrue(tdt.validate(new PfValidationResult()).isValid());
209         tdt.setDescription("");
210         assertFalse(tdt.validate(new PfValidationResult()).isValid());
211         tdt.setDescription("A Description");
212         assertTrue(tdt.validate(new PfValidationResult()).isValid());
213
214         tdt.setEvaluations(-1);
215         assertFalse(tdt.validate(new PfValidationResult()).isValid());
216         tdt.setEvaluations(100);
217         assertTrue(tdt.validate(new PfValidationResult()).isValid());
218
219         tdt.setMethod(null);
220         assertTrue(tdt.validate(new PfValidationResult()).isValid());
221         tdt.setMethod("");
222         assertFalse(tdt.validate(new PfValidationResult()).isValid());
223         tdt.setMethod("A Method");
224         assertTrue(tdt.validate(new PfValidationResult()).isValid());
225
226         try {
227             tdt.validate(null);
228             fail("test should throw an exception");
229         } catch (Exception exc) {
230             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
231         }
232     }
233 }