Add support for legacy guard policies
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / ToscaTriggerTest.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.ToscaConstraintLogical.Operation;
37 import org.onap.policy.models.tosca.simple.concepts.ToscaConstraintLogicalString;
38 import org.onap.policy.models.tosca.simple.concepts.ToscaEventFilter;
39 import org.onap.policy.models.tosca.simple.concepts.ToscaTimeInterval;
40 import org.onap.policy.models.tosca.simple.concepts.ToscaTrigger;
41
42 /**
43  * DAO test for ToscaTrigger.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class ToscaTriggerTest {
48
49     @Test
50     public void testTriggerPojo() {
51         assertNotNull(new ToscaTrigger());
52         assertNotNull(new ToscaTrigger(new PfReferenceKey()));
53         assertNotNull(new ToscaTrigger(new PfReferenceKey(), "EventType", "Action"));
54         assertNotNull(new ToscaTrigger(new ToscaTrigger()));
55
56         try {
57             new ToscaTrigger((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 ToscaTrigger(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 ToscaTrigger(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 ToscaTrigger(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 ToscaTrigger(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 ToscaTrigger(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 ToscaTrigger(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 ToscaTrigger(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 ToscaTrigger((ToscaTrigger) 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         ToscaTrigger tdt = new ToscaTrigger(tkey, "EventType", "Action");
122
123         ToscaTimeInterval schedule = new ToscaTimeInterval(new PfReferenceKey(tkey, "sched"), new Date(), new Date());
124         tdt.setSchedule(schedule);
125
126         ToscaEventFilter targetFilter =
127                 new ToscaEventFilter(new PfReferenceKey(tkey, "filter"), new PfConceptKey("NodeName", "0.0.1"));
128         tdt.setTargetFilter(targetFilter);
129
130         ToscaConstraintLogicalString lsc =
131                 new ToscaConstraintLogicalString(new PfReferenceKey(tkey, "sc"), Operation.EQ, "hello");
132         tdt.setCondition(lsc);
133         assertEquals(lsc, tdt.getCondition());
134         tdt.setConstraint(lsc);
135         assertEquals(lsc, tdt.getConstraint());
136
137         tdt.setPeriod(Duration.ZERO);
138         assertEquals(Duration.ZERO, tdt.getPeriod());
139
140         tdt.setDescription("A Description");
141         assertEquals("A Description", tdt.getDescription());
142
143         tdt.setMethod("A Method");
144         assertEquals("A Method", tdt.getMethod());
145
146         ToscaTrigger tdtClone0 = new ToscaTrigger(tdt);
147         assertEquals(tdt, tdtClone0);
148         assertEquals(0, tdt.compareTo(tdtClone0));
149
150         ToscaTrigger tdtClone1 = new ToscaTrigger();
151         tdt.copyTo(tdtClone1);
152         assertEquals(tdt, tdtClone1);
153         assertEquals(0, tdt.compareTo(tdtClone1));
154
155         assertEquals(-1, tdt.compareTo(null));
156         assertEquals(0, tdt.compareTo(tdt));
157         assertFalse(tdt.compareTo(tdt.getKey()) == 0);
158
159         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherTrigger");
160         ToscaTrigger otherDt = new ToscaTrigger(otherDtKey);
161
162         assertFalse(tdt.compareTo(otherDt) == 0);
163         otherDt.setKey(tkey);
164         assertFalse(tdt.compareTo(otherDt) == 0);
165         otherDt.setDescription("A Description");
166         assertFalse(tdt.compareTo(otherDt) == 0);
167         otherDt.setEventType("EventType");
168         assertFalse(tdt.compareTo(otherDt) == 0);
169         otherDt.setSchedule(schedule);
170         assertFalse(tdt.compareTo(otherDt) == 0);
171         otherDt.setTargetFilter(targetFilter);
172         assertFalse(tdt.compareTo(otherDt) == 0);
173         otherDt.setCondition(lsc);
174         assertFalse(tdt.compareTo(otherDt) == 0);
175         otherDt.setConstraint(lsc);
176         assertFalse(tdt.compareTo(otherDt) == 0);
177         otherDt.setPeriod(Duration.ZERO);
178         assertFalse(tdt.compareTo(otherDt) == 0);
179         otherDt.setMethod("A Method");
180         assertFalse(tdt.compareTo(otherDt) == 0);
181         otherDt.setAction("Action");
182         assertEquals(0, tdt.compareTo(otherDt));
183
184         otherDt.setEvaluations(100);
185         assertFalse(tdt.compareTo(otherDt) == 0);
186         otherDt.setEvaluations(0);
187         assertEquals(0, tdt.compareTo(otherDt));
188
189         try {
190             tdt.copyTo(null);
191             fail("test should throw an exception");
192         } catch (Exception exc) {
193             assertEquals("target is marked @NonNull but is null", exc.getMessage());
194         }
195
196         assertEquals(6, tdt.getKeys().size());
197         assertEquals(1, new ToscaTrigger().getKeys().size());
198
199         new ToscaTrigger().clean();
200         tdt.clean();
201         assertEquals(tdtClone0, tdt);
202
203         assertFalse(new ToscaTrigger().validate(new PfValidationResult()).isValid());
204         assertTrue(tdt.validate(new PfValidationResult()).isValid());
205
206         tdt.setDescription(null);
207         assertTrue(tdt.validate(new PfValidationResult()).isValid());
208         tdt.setDescription("");
209         assertFalse(tdt.validate(new PfValidationResult()).isValid());
210         tdt.setDescription("A Description");
211         assertTrue(tdt.validate(new PfValidationResult()).isValid());
212
213         tdt.setEvaluations(-1);
214         assertFalse(tdt.validate(new PfValidationResult()).isValid());
215         tdt.setEvaluations(100);
216         assertTrue(tdt.validate(new PfValidationResult()).isValid());
217
218         tdt.setMethod(null);
219         assertTrue(tdt.validate(new PfValidationResult()).isValid());
220         tdt.setMethod("");
221         assertFalse(tdt.validate(new PfValidationResult()).isValid());
222         tdt.setMethod("A Method");
223         assertTrue(tdt.validate(new PfValidationResult()).isValid());
224
225         try {
226             tdt.validate(null);
227             fail("test should throw an exception");
228         } catch (Exception exc) {
229             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
230         }
231     }
232 }