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