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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
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;
29 import java.time.Duration;
30 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 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;
41 * DAO test for ToscaTrigger.
43 * @author Liam Fallon (liam.fallon@est.tech)
45 public class JpaToscaTriggerTest {
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()));
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());
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());
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());
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());
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());
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());
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());
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());
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());
117 PfConceptKey tparentKey = new PfConceptKey("tParentKey", "0.0.1");
118 PfReferenceKey tkey = new PfReferenceKey(tparentKey, "trigger0");
119 JpaToscaTrigger tdt = new JpaToscaTrigger(tkey, "EventType", "Action");
121 JpaToscaTimeInterval schedule =
122 new JpaToscaTimeInterval(new PfReferenceKey(tkey, "sched"), new Date(), new Date());
123 tdt.setSchedule(schedule);
125 JpaToscaEventFilter targetFilter =
126 new JpaToscaEventFilter(new PfReferenceKey(tkey, "filter"), new PfConceptKey("NodeName", "0.0.1"));
127 tdt.setTargetFilter(targetFilter);
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());
135 tdt.setPeriod(Duration.ZERO);
136 assertEquals(Duration.ZERO, tdt.getPeriod());
138 tdt.setDescription("A Description");
139 assertEquals("A Description", tdt.getDescription());
141 tdt.setMethod("A Method");
142 assertEquals("A Method", tdt.getMethod());
144 JpaToscaTrigger tdtClone0 = new JpaToscaTrigger(tdt);
145 assertEquals(tdt, tdtClone0);
146 assertEquals(0, tdt.compareTo(tdtClone0));
148 JpaToscaTrigger tdtClone1 = new JpaToscaTrigger();
149 tdt.copyTo(tdtClone1);
150 assertEquals(tdt, tdtClone1);
151 assertEquals(0, tdt.compareTo(tdtClone1));
153 assertEquals(-1, tdt.compareTo(null));
154 assertEquals(0, tdt.compareTo(tdt));
155 assertFalse(tdt.compareTo(tdt.getKey()) == 0);
157 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherTrigger");
158 JpaToscaTrigger otherDt = new JpaToscaTrigger(otherDtKey);
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));
182 otherDt.setEvaluations(100);
183 assertFalse(tdt.compareTo(otherDt) == 0);
184 otherDt.setEvaluations(0);
185 assertEquals(0, tdt.compareTo(otherDt));
189 fail("test should throw an exception");
190 } catch (Exception exc) {
191 assertEquals("target is marked @NonNull but is null", exc.getMessage());
194 assertEquals(4, tdt.getKeys().size());
195 assertEquals(1, new JpaToscaTrigger().getKeys().size());
197 new JpaToscaTrigger().clean();
199 assertEquals(tdtClone0, tdt);
201 assertFalse(new JpaToscaTrigger().validate(new PfValidationResult()).isValid());
202 assertTrue(tdt.validate(new PfValidationResult()).isValid());
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());
211 tdt.setEvaluations(-1);
212 assertFalse(tdt.validate(new PfValidationResult()).isValid());
213 tdt.setEvaluations(100);
214 assertTrue(tdt.validate(new PfValidationResult()).isValid());
217 assertTrue(tdt.validate(new PfValidationResult()).isValid());
219 assertFalse(tdt.validate(new PfValidationResult()).isValid());
220 tdt.setMethod("A Method");
221 assertTrue(tdt.validate(new PfValidationResult()).isValid());
225 fail("test should throw an exception");
226 } catch (Exception exc) {
227 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());