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.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;
43 * DAO test for ToscaTrigger.
45 * @author Liam Fallon (liam.fallon@est.tech)
47 public class JpaToscaTriggerTest {
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()));
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());
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());
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());
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());
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());
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());
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());
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());
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());
119 PfConceptKey tparentKey = new PfConceptKey("tParentKey", "0.0.1");
120 PfReferenceKey tkey = new PfReferenceKey(tparentKey, "trigger0");
121 JpaToscaTrigger tdt = new JpaToscaTrigger(tkey, "EventType", "Action");
123 JpaToscaTimeInterval schedule =
124 new JpaToscaTimeInterval(new PfReferenceKey(tkey, "sched"), new Date(), new Date());
125 tdt.setSchedule(schedule);
127 JpaToscaEventFilter targetFilter =
128 new JpaToscaEventFilter(new PfReferenceKey(tkey, "filter"), new PfConceptKey("NodeName", "0.0.1"));
129 tdt.setTargetFilter(targetFilter);
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());
138 tdt.setPeriod(Duration.ZERO);
139 assertEquals(Duration.ZERO, tdt.getPeriod());
141 tdt.setDescription("A Description");
142 assertEquals("A Description", tdt.getDescription());
144 tdt.setMethod("A Method");
145 assertEquals("A Method", tdt.getMethod());
147 JpaToscaTrigger tdtClone0 = new JpaToscaTrigger(tdt);
148 assertEquals(tdt, tdtClone0);
149 assertEquals(0, tdt.compareTo(tdtClone0));
151 JpaToscaTrigger tdtClone1 = new JpaToscaTrigger();
152 tdt.copyTo(tdtClone1);
153 assertEquals(tdt, tdtClone1);
154 assertEquals(0, tdt.compareTo(tdtClone1));
156 assertEquals(-1, tdt.compareTo(null));
157 assertEquals(0, tdt.compareTo(tdt));
158 assertFalse(tdt.compareTo(tdt.getKey()) == 0);
160 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherTrigger");
161 JpaToscaTrigger otherDt = new JpaToscaTrigger(otherDtKey);
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));
185 otherDt.setEvaluations(100);
186 assertFalse(tdt.compareTo(otherDt) == 0);
187 otherDt.setEvaluations(0);
188 assertEquals(0, tdt.compareTo(otherDt));
192 fail("test should throw an exception");
193 } catch (Exception exc) {
194 assertEquals("target is marked @NonNull but is null", exc.getMessage());
197 assertEquals(6, tdt.getKeys().size());
198 assertEquals(1, new JpaToscaTrigger().getKeys().size());
200 new JpaToscaTrigger().clean();
202 assertEquals(tdtClone0, tdt);
204 assertFalse(new JpaToscaTrigger().validate(new PfValidationResult()).isValid());
205 assertTrue(tdt.validate(new PfValidationResult()).isValid());
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());
214 tdt.setEvaluations(-1);
215 assertFalse(tdt.validate(new PfValidationResult()).isValid());
216 tdt.setEvaluations(100);
217 assertTrue(tdt.validate(new PfValidationResult()).isValid());
220 assertTrue(tdt.validate(new PfValidationResult()).isValid());
222 assertFalse(tdt.validate(new PfValidationResult()).isValid());
223 tdt.setMethod("A Method");
224 assertTrue(tdt.validate(new PfValidationResult()).isValid());
228 fail("test should throw an exception");
229 } catch (Exception exc) {
230 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());