a7a754f935dc47236e8cf466b03c0a68eaeb463a
[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-2020 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
33 import org.junit.Test;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfReferenceKey;
36 import org.onap.policy.models.base.PfValidationResult;
37
38 /**
39  * DAO test for ToscaTrigger.
40  *
41  * @author Liam Fallon (liam.fallon@est.tech)
42  */
43 public class JpaToscaTriggerTest {
44
45     private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
46     private static final String EVENT_TYPE = "EventType";
47     private static final String ACTION = "Action";
48     private static final String A_METHOD = "A Method";
49     private static final String A_DESCRIPTION = "A Description";
50     private static final String VERSION_001 = "0.0.1";
51
52     @Test
53     public void testTriggerPojo() {
54         assertNotNull(new JpaToscaTrigger());
55         assertNotNull(new JpaToscaTrigger(new PfReferenceKey()));
56         assertNotNull(new JpaToscaTrigger(new PfReferenceKey(), EVENT_TYPE, ACTION));
57         assertNotNull(new JpaToscaTrigger(new JpaToscaTrigger()));
58
59         assertThatThrownBy(() -> new JpaToscaTrigger((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL);
60
61         assertThatThrownBy(() -> new JpaToscaTrigger(null, null, null)).hasMessageMatching(KEY_IS_NULL);
62
63         assertThatThrownBy(() -> new JpaToscaTrigger(null, EVENT_TYPE, null)).hasMessageMatching(KEY_IS_NULL);
64
65         assertThatThrownBy(() -> new JpaToscaTrigger(null, EVENT_TYPE, ACTION)).hasMessageMatching(KEY_IS_NULL);
66
67         assertThatThrownBy(() -> new JpaToscaTrigger(null, null, ACTION)).hasMessageMatching(KEY_IS_NULL);
68
69         assertThatThrownBy(() -> new JpaToscaTrigger(new PfReferenceKey(), null, null))
70                 .hasMessageMatching("eventType is marked .*on.*ull but is null");
71
72         assertThatThrownBy(() -> new JpaToscaTrigger(new PfReferenceKey(), EVENT_TYPE, null))
73                 .hasMessageMatching("action is marked .*on.*ull but is null");
74
75         assertThatThrownBy(() -> new JpaToscaTrigger(new PfReferenceKey(), null, ACTION))
76                 .hasMessageMatching("eventType is marked .*on.*ull but is null");
77
78         assertThatThrownBy(() -> new JpaToscaTrigger((JpaToscaTrigger) null)).isInstanceOf(NullPointerException.class);
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(tdt);
112         assertEquals(tdt, tdtClone1);
113         assertEquals(0, tdt.compareTo(tdtClone1));
114
115         assertEquals(-1, tdt.compareTo(null));
116         assertEquals(0, tdt.compareTo(tdt));
117         assertFalse(tdt.compareTo(tdt.getKey()) == 0);
118
119         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherTrigger");
120         JpaToscaTrigger otherDt = new JpaToscaTrigger(otherDtKey);
121
122         assertFalse(tdt.compareTo(otherDt) == 0);
123         otherDt.setKey(tkey);
124         assertFalse(tdt.compareTo(otherDt) == 0);
125         otherDt.setDescription(A_DESCRIPTION);
126         assertFalse(tdt.compareTo(otherDt) == 0);
127         otherDt.setEventType(EVENT_TYPE);
128         assertFalse(tdt.compareTo(otherDt) == 0);
129         otherDt.setSchedule(schedule);
130         assertFalse(tdt.compareTo(otherDt) == 0);
131         otherDt.setTargetFilter(targetFilter);
132         assertFalse(tdt.compareTo(otherDt) == 0);
133         otherDt.setCondition(lsc);
134         assertFalse(tdt.compareTo(otherDt) == 0);
135         otherDt.setConstraint(lsc);
136         assertFalse(tdt.compareTo(otherDt) == 0);
137         otherDt.setPeriod(Duration.ZERO);
138         assertFalse(tdt.compareTo(otherDt) == 0);
139         otherDt.setMethod(A_METHOD);
140         assertFalse(tdt.compareTo(otherDt) == 0);
141         otherDt.setAction(ACTION);
142         assertEquals(0, tdt.compareTo(otherDt));
143
144         otherDt.setEvaluations(100);
145         assertFalse(tdt.compareTo(otherDt) == 0);
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         new JpaToscaTrigger().clean();
153         tdt.clean();
154         assertEquals(tdtClone0, tdt);
155
156         assertFalse(new JpaToscaTrigger().validate(new PfValidationResult()).isValid());
157         assertTrue(tdt.validate(new PfValidationResult()).isValid());
158
159         tdt.setDescription(null);
160         assertTrue(tdt.validate(new PfValidationResult()).isValid());
161         tdt.setDescription("");
162         assertFalse(tdt.validate(new PfValidationResult()).isValid());
163         tdt.setDescription(A_DESCRIPTION);
164         assertTrue(tdt.validate(new PfValidationResult()).isValid());
165
166         tdt.setEvaluations(-1);
167         assertFalse(tdt.validate(new PfValidationResult()).isValid());
168         tdt.setEvaluations(100);
169         assertTrue(tdt.validate(new PfValidationResult()).isValid());
170
171         tdt.setMethod(null);
172         assertTrue(tdt.validate(new PfValidationResult()).isValid());
173         tdt.setMethod("");
174         assertFalse(tdt.validate(new PfValidationResult()).isValid());
175         tdt.setMethod(A_METHOD);
176         assertTrue(tdt.validate(new PfValidationResult()).isValid());
177
178         assertThatThrownBy(() -> tdt.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
179     }
180 }