2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2020 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
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.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
31 import org.junit.Test;
32 import org.onap.policy.models.base.PfConceptKey;
33 import org.onap.policy.models.base.PfReferenceKey;
36 * DAO test for ToscaEventFilter.
38 * @author Liam Fallon (liam.fallon@est.tech)
40 public class JpaToscaEventFilterTest {
42 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
43 private static final String A_REQUREMENT = "A Requrement";
44 private static final String A_CAPABILITY = "A Capability";
45 private static final String VERSION_001 = "0.0.1";
48 public void testEventFilterPojo() {
49 assertNotNull(new JpaToscaEventFilter());
50 assertNotNull(new JpaToscaEventFilter(new PfReferenceKey()));
51 assertNotNull(new JpaToscaEventFilter(new PfReferenceKey(), new PfConceptKey()));
52 assertNotNull(new JpaToscaEventFilter(new JpaToscaEventFilter()));
54 assertThatThrownBy(() -> new JpaToscaEventFilter((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL);
56 assertThatThrownBy(() -> new JpaToscaEventFilter(null, null)).hasMessageMatching(KEY_IS_NULL);
58 assertThatThrownBy(() -> new JpaToscaEventFilter(null, new PfConceptKey())).hasMessageMatching(KEY_IS_NULL);
60 assertThatThrownBy(() -> new JpaToscaEventFilter(new PfReferenceKey(), null))
61 .hasMessageMatching("node is marked .*on.*ull but is null");
63 assertThatThrownBy(() -> new JpaToscaEventFilter((JpaToscaEventFilter) null))
64 .isInstanceOf(NullPointerException.class);
68 public void testEventFilter() {
69 PfConceptKey efParentKey = new PfConceptKey("tParentKey", VERSION_001);
70 PfReferenceKey efKey = new PfReferenceKey(efParentKey, "trigger0");
71 PfConceptKey nodeKey = new PfConceptKey("tParentKey", VERSION_001);
72 JpaToscaEventFilter tef = new JpaToscaEventFilter(efKey, nodeKey);
74 tef.setRequirement(A_REQUREMENT);
75 assertEquals(A_REQUREMENT, tef.getRequirement());
77 tef.setCapability(A_CAPABILITY);
78 assertEquals(A_CAPABILITY, tef.getCapability());
80 JpaToscaEventFilter tdtClone0 = new JpaToscaEventFilter(tef);
81 checkEqualsEventFilter(tef, tdtClone0);
83 JpaToscaEventFilter tdtClone1 = new JpaToscaEventFilter(tef);
84 checkEqualsEventFilter(tef, tdtClone1);
86 assertEquals(-1, tef.compareTo(null));
87 assertEquals(0, tef.compareTo(tef));
88 assertNotEquals(0, tef.compareTo(tef.getKey()));
90 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherEventFilter");
91 JpaToscaEventFilter otherDt = new JpaToscaEventFilter(otherDtKey);
93 assertNotEquals(0, tef.compareTo(otherDt));
94 otherDt.setKey(efKey);
95 assertNotEquals(0, tef.compareTo(otherDt));
96 otherDt.setNode(nodeKey);
97 assertNotEquals(0, tef.compareTo(otherDt));
98 otherDt.setRequirement(A_REQUREMENT);
99 assertNotEquals(0, tef.compareTo(otherDt));
100 otherDt.setCapability(A_CAPABILITY);
101 assertEquals(0, tef.compareTo(otherDt));
103 assertEquals(2, tef.getKeys().size());
104 assertEquals(2, new JpaToscaEventFilter().getKeys().size());
107 private void checkEqualsEventFilter(JpaToscaEventFilter tef1, JpaToscaEventFilter tef2) {
108 assertEquals(tef1, tef2);
109 assertEquals(0, tef1.compareTo(tef2));
113 public void testValidationEventFilter() {
114 PfConceptKey efParentKey = new PfConceptKey("tParentKey", VERSION_001);
115 PfReferenceKey efKey = new PfReferenceKey(efParentKey, "trigger0");
116 PfConceptKey nodeKey = new PfConceptKey("tParentKey", VERSION_001);
117 JpaToscaEventFilter tef = new JpaToscaEventFilter(efKey, nodeKey);
119 JpaToscaEventFilter tdtClone0 = new JpaToscaEventFilter(tef);
121 new JpaToscaEventFilter().clean();
123 assertEquals(tdtClone0, tef);
125 assertFalse(new JpaToscaEventFilter().validate("").isValid());
126 assertTrue(tef.validate("").isValid());
128 tef.setRequirement(null);
129 assertTrue(tef.validate("").isValid());
130 tef.setRequirement("");
131 assertFalse(tef.validate("").isValid());
132 tef.setRequirement(A_REQUREMENT);
133 assertTrue(tef.validate("").isValid());
135 tef.setCapability(null);
136 assertTrue(tef.validate("").isValid());
137 tef.setCapability("");
138 assertFalse(tef.validate("").isValid());
139 tef.setCapability(A_CAPABILITY);
140 assertTrue(tef.validate("").isValid());
143 assertFalse(tef.validate("").isValid());
144 tef.setNode(PfConceptKey.getNullKey());
145 assertFalse(tef.validate("").isValid());
146 tef.setNode(nodeKey);
147 assertTrue(tef.validate("").isValid());
149 assertThatThrownBy(() -> tef.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");