Sonar Fixes policy/models, removing model-yaml
[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 import org.onap.policy.models.base.PfValidationResult;
35
36 /**
37  * DAO test for ToscaEventFilter.
38  *
39  * @author Liam Fallon (liam.fallon@est.tech)
40  */
41 public class JpaToscaEventFilterTest {
42
43     private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
44     private static final String A_REQUREMENT = "A Requrement";
45     private static final String A_CAPABILITY = "A Capability";
46     private static final String VERSION_001 = "0.0.1";
47
48     @Test
49     public void testEventFilterPojo() {
50         assertNotNull(new JpaToscaEventFilter());
51         assertNotNull(new JpaToscaEventFilter(new PfReferenceKey()));
52         assertNotNull(new JpaToscaEventFilter(new PfReferenceKey(), new PfConceptKey()));
53         assertNotNull(new JpaToscaEventFilter(new JpaToscaEventFilter()));
54
55         assertThatThrownBy(() -> new JpaToscaEventFilter((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL);
56
57         assertThatThrownBy(() -> new JpaToscaEventFilter(null, null)).hasMessageMatching(KEY_IS_NULL);
58
59         assertThatThrownBy(() -> new JpaToscaEventFilter(null, new PfConceptKey())).hasMessageMatching(KEY_IS_NULL);
60
61         assertThatThrownBy(() -> new JpaToscaEventFilter(new PfReferenceKey(), null))
62                 .hasMessageMatching("node is marked .*on.*ull but is null");
63
64         assertThatThrownBy(() -> new JpaToscaEventFilter((JpaToscaEventFilter) null))
65                 .isInstanceOf(NullPointerException.class);
66
67         PfConceptKey efParentKey = new PfConceptKey("tParentKey", VERSION_001);
68         PfReferenceKey efKey = new PfReferenceKey(efParentKey, "trigger0");
69         PfConceptKey nodeKey = new PfConceptKey("tParentKey", VERSION_001);
70         JpaToscaEventFilter tef = new JpaToscaEventFilter(efKey, nodeKey);
71
72         tef.setRequirement(A_REQUREMENT);
73         assertEquals(A_REQUREMENT, tef.getRequirement());
74
75         tef.setCapability(A_CAPABILITY);
76         assertEquals(A_CAPABILITY, tef.getCapability());
77
78         JpaToscaEventFilter tdtClone0 = new JpaToscaEventFilter(tef);
79         assertEquals(tef, tdtClone0);
80         assertEquals(0, tef.compareTo(tdtClone0));
81
82         JpaToscaEventFilter tdtClone1 = new JpaToscaEventFilter(tef);
83         assertEquals(tef, tdtClone1);
84         assertEquals(0, tef.compareTo(tdtClone1));
85
86         assertEquals(-1, tef.compareTo(null));
87         assertEquals(0, tef.compareTo(tef));
88         assertNotEquals(0, tef.compareTo(tef.getKey()));
89
90         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherEventFilter");
91         JpaToscaEventFilter otherDt = new JpaToscaEventFilter(otherDtKey);
92
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));
102
103         assertEquals(2, tef.getKeys().size());
104         assertEquals(2, new JpaToscaEventFilter().getKeys().size());
105
106         new JpaToscaEventFilter().clean();
107         tef.clean();
108         assertEquals(tdtClone0, tef);
109
110         assertFalse(new JpaToscaEventFilter().validate(new PfValidationResult()).isValid());
111         assertTrue(tef.validate(new PfValidationResult()).isValid());
112
113         tef.setRequirement(null);
114         assertTrue(tef.validate(new PfValidationResult()).isValid());
115         tef.setRequirement("");
116         assertFalse(tef.validate(new PfValidationResult()).isValid());
117         tef.setRequirement(A_REQUREMENT);
118         assertTrue(tef.validate(new PfValidationResult()).isValid());
119
120         tef.setCapability(null);
121         assertTrue(tef.validate(new PfValidationResult()).isValid());
122         tef.setCapability("");
123         assertFalse(tef.validate(new PfValidationResult()).isValid());
124         tef.setCapability(A_CAPABILITY);
125         assertTrue(tef.validate(new PfValidationResult()).isValid());
126
127         tef.setNode(null);
128         assertFalse(tef.validate(new PfValidationResult()).isValid());
129         tef.setNode(PfConceptKey.getNullKey());
130         assertFalse(tef.validate(new PfValidationResult()).isValid());
131         tef.setNode(nodeKey);
132         assertTrue(tef.validate(new PfValidationResult()).isValid());
133
134         assertThatThrownBy(() -> tef.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
135     }
136 }