ecffec877b86c4f0c20f159388762582b538b61c
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaEventFilterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 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
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.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 import org.junit.Test;
32 import org.onap.policy.models.base.PfConceptKey;
33 import org.onap.policy.models.base.PfReferenceKey;
34
35 /**
36  * DAO test for ToscaEventFilter.
37  *
38  * @author Liam Fallon (liam.fallon@est.tech)
39  */
40 public class JpaToscaEventFilterTest {
41
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";
46
47     @Test
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()));
53
54         assertThatThrownBy(() -> new JpaToscaEventFilter((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL);
55
56         assertThatThrownBy(() -> new JpaToscaEventFilter(null, null)).hasMessageMatching(KEY_IS_NULL);
57
58         assertThatThrownBy(() -> new JpaToscaEventFilter(null, new PfConceptKey())).hasMessageMatching(KEY_IS_NULL);
59
60         assertThatThrownBy(() -> new JpaToscaEventFilter(new PfReferenceKey(), null))
61                 .hasMessageMatching("node is marked .*on.*ull but is null");
62
63         assertThatThrownBy(() -> new JpaToscaEventFilter((JpaToscaEventFilter) null))
64                 .isInstanceOf(NullPointerException.class);
65
66         PfConceptKey efParentKey = new PfConceptKey("tParentKey", VERSION_001);
67         PfReferenceKey efKey = new PfReferenceKey(efParentKey, "trigger0");
68         PfConceptKey nodeKey = new PfConceptKey("tParentKey", VERSION_001);
69         JpaToscaEventFilter tef = new JpaToscaEventFilter(efKey, nodeKey);
70
71         tef.setRequirement(A_REQUREMENT);
72         assertEquals(A_REQUREMENT, tef.getRequirement());
73
74         tef.setCapability(A_CAPABILITY);
75         assertEquals(A_CAPABILITY, tef.getCapability());
76
77         JpaToscaEventFilter tdtClone0 = new JpaToscaEventFilter(tef);
78         assertEquals(tef, tdtClone0);
79         assertEquals(0, tef.compareTo(tdtClone0));
80
81         JpaToscaEventFilter tdtClone1 = new JpaToscaEventFilter(tef);
82         assertEquals(tef, tdtClone1);
83         assertEquals(0, tef.compareTo(tdtClone1));
84
85         assertEquals(-1, tef.compareTo(null));
86         assertEquals(0, tef.compareTo(tef));
87         assertNotEquals(0, tef.compareTo(tef.getKey()));
88
89         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherEventFilter");
90         JpaToscaEventFilter otherDt = new JpaToscaEventFilter(otherDtKey);
91
92         assertNotEquals(0, tef.compareTo(otherDt));
93         otherDt.setKey(efKey);
94         assertNotEquals(0, tef.compareTo(otherDt));
95         otherDt.setNode(nodeKey);
96         assertNotEquals(0, tef.compareTo(otherDt));
97         otherDt.setRequirement(A_REQUREMENT);
98         assertNotEquals(0, tef.compareTo(otherDt));
99         otherDt.setCapability(A_CAPABILITY);
100         assertEquals(0, tef.compareTo(otherDt));
101
102         assertEquals(2, tef.getKeys().size());
103         assertEquals(2, new JpaToscaEventFilter().getKeys().size());
104
105         new JpaToscaEventFilter().clean();
106         tef.clean();
107         assertEquals(tdtClone0, tef);
108
109         assertFalse(new JpaToscaEventFilter().validate("").isValid());
110         assertTrue(tef.validate("").isValid());
111
112         tef.setRequirement(null);
113         assertTrue(tef.validate("").isValid());
114         tef.setRequirement("");
115         assertFalse(tef.validate("").isValid());
116         tef.setRequirement(A_REQUREMENT);
117         assertTrue(tef.validate("").isValid());
118
119         tef.setCapability(null);
120         assertTrue(tef.validate("").isValid());
121         tef.setCapability("");
122         assertFalse(tef.validate("").isValid());
123         tef.setCapability(A_CAPABILITY);
124         assertTrue(tef.validate("").isValid());
125
126         tef.setNode(null);
127         assertFalse(tef.validate("").isValid());
128         tef.setNode(PfConceptKey.getNullKey());
129         assertFalse(tef.validate("").isValid());
130         tef.setNode(nodeKey);
131         assertTrue(tef.validate("").isValid());
132
133         assertThatThrownBy(() -> tef.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
134     }
135 }