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.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;
43 * DAO test for ToscaTrigger.
45 * @author Liam Fallon (liam.fallon@est.tech)
47 public class ToscaTriggerTest {
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()));
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());
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());
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());
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());
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());
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());
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());
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());
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());
119 PfConceptKey tparentKey = new PfConceptKey("tParentKey", "0.0.1");
120 PfReferenceKey tkey = new PfReferenceKey(tparentKey, "trigger0");
121 ToscaTrigger tdt = new ToscaTrigger(tkey, "EventType", "Action");
123 ToscaTimeInterval schedule = new ToscaTimeInterval(new PfReferenceKey(tkey, "sched"), new Date(), new Date());
124 tdt.setSchedule(schedule);
126 ToscaEventFilter targetFilter =
127 new ToscaEventFilter(new PfReferenceKey(tkey, "filter"), new PfConceptKey("NodeName", "0.0.1"));
128 tdt.setTargetFilter(targetFilter);
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());
137 tdt.setPeriod(Duration.ZERO);
138 assertEquals(Duration.ZERO, tdt.getPeriod());
140 tdt.setDescription("A Description");
141 assertEquals("A Description", tdt.getDescription());
143 tdt.setMethod("A Method");
144 assertEquals("A Method", tdt.getMethod());
146 ToscaTrigger tdtClone0 = new ToscaTrigger(tdt);
147 assertEquals(tdt, tdtClone0);
148 assertEquals(0, tdt.compareTo(tdtClone0));
150 ToscaTrigger tdtClone1 = new ToscaTrigger();
151 tdt.copyTo(tdtClone1);
152 assertEquals(tdt, tdtClone1);
153 assertEquals(0, tdt.compareTo(tdtClone1));
155 assertEquals(-1, tdt.compareTo(null));
156 assertEquals(0, tdt.compareTo(tdt));
157 assertFalse(tdt.compareTo(tdt.getKey()) == 0);
159 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherTrigger");
160 ToscaTrigger otherDt = new ToscaTrigger(otherDtKey);
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));
184 otherDt.setEvaluations(100);
185 assertFalse(tdt.compareTo(otherDt) == 0);
186 otherDt.setEvaluations(0);
187 assertEquals(0, tdt.compareTo(otherDt));
191 fail("test should throw an exception");
192 } catch (Exception exc) {
193 assertEquals("target is marked @NonNull but is null", exc.getMessage());
196 assertEquals(6, tdt.getKeys().size());
197 assertEquals(1, new ToscaTrigger().getKeys().size());
199 new ToscaTrigger().clean();
201 assertEquals(tdtClone0, tdt);
203 assertFalse(new ToscaTrigger().validate(new PfValidationResult()).isValid());
204 assertTrue(tdt.validate(new PfValidationResult()).isValid());
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());
213 tdt.setEvaluations(-1);
214 assertFalse(tdt.validate(new PfValidationResult()).isValid());
215 tdt.setEvaluations(100);
216 assertTrue(tdt.validate(new PfValidationResult()).isValid());
219 assertTrue(tdt.validate(new PfValidationResult()).isValid());
221 assertFalse(tdt.validate(new PfValidationResult()).isValid());
222 tdt.setMethod("A Method");
223 assertTrue(tdt.validate(new PfValidationResult()).isValid());
227 fail("test should throw an exception");
228 } catch (Exception exc) {
229 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());