Add support for legacy guard policies
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / ToscaEventFilterTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
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;
28
29 import org.junit.Test;
30 import org.onap.policy.models.base.PfConceptKey;
31 import org.onap.policy.models.base.PfReferenceKey;
32 import org.onap.policy.models.base.PfValidationResult;
33 import org.onap.policy.models.tosca.simple.concepts.ToscaEventFilter;
34
35 /**
36  * DAO test for ToscaEventFilter.
37  *
38  * @author Liam Fallon (liam.fallon@est.tech)
39  */
40 public class ToscaEventFilterTest {
41
42     @Test
43     public void testEventFilterPojo() {
44         assertNotNull(new ToscaEventFilter());
45         assertNotNull(new ToscaEventFilter(new PfReferenceKey()));
46         assertNotNull(new ToscaEventFilter(new PfReferenceKey(), new PfConceptKey()));
47         assertNotNull(new ToscaEventFilter(new ToscaEventFilter()));
48
49         try {
50             new ToscaEventFilter((PfReferenceKey) null);
51             fail("test should throw an exception");
52         } catch (Exception exc) {
53             assertEquals("key is marked @NonNull but is null", exc.getMessage());
54         }
55
56         try {
57             new ToscaEventFilter(null, null);
58             fail("test should throw an exception");
59         } catch (Exception exc) {
60             assertEquals("key is marked @NonNull but is null", exc.getMessage());
61         }
62
63         try {
64             new ToscaEventFilter(null, new PfConceptKey());
65             fail("test should throw an exception");
66         } catch (Exception exc) {
67             assertEquals("key is marked @NonNull but is null", exc.getMessage());
68         }
69
70         try {
71             new ToscaEventFilter(new PfReferenceKey(), null);
72             fail("test should throw an exception");
73         } catch (Exception exc) {
74             assertEquals("node is marked @NonNull but is null", exc.getMessage());
75         }
76
77         try {
78             new ToscaEventFilter((ToscaEventFilter) null);
79             fail("test should throw an exception");
80         } catch (Exception exc) {
81             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
82         }
83
84         PfConceptKey efParentKey = new PfConceptKey("tParentKey", "0.0.1");
85         PfReferenceKey efKey = new PfReferenceKey(efParentKey, "trigger0");
86         PfConceptKey nodeKey = new PfConceptKey("tParentKey", "0.0.1");
87         ToscaEventFilter tef = new ToscaEventFilter(efKey, nodeKey);
88
89         tef.setRequirement("A Requrement");
90         assertEquals("A Requrement", tef.getRequirement());
91
92         tef.setCapability("A Capability");
93         assertEquals("A Capability", tef.getCapability());
94
95         ToscaEventFilter tdtClone0 = new ToscaEventFilter(tef);
96         assertEquals(tef, tdtClone0);
97         assertEquals(0, tef.compareTo(tdtClone0));
98
99         ToscaEventFilter tdtClone1 = new ToscaEventFilter();
100         tef.copyTo(tdtClone1);
101         assertEquals(tef, tdtClone1);
102         assertEquals(0, tef.compareTo(tdtClone1));
103
104         assertEquals(-1, tef.compareTo(null));
105         assertEquals(0, tef.compareTo(tef));
106         assertFalse(tef.compareTo(tef.getKey()) == 0);
107
108         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherEventFilter");
109         ToscaEventFilter otherDt = new ToscaEventFilter(otherDtKey);
110
111         assertFalse(tef.compareTo(otherDt) == 0);
112         otherDt.setKey(efKey);
113         assertFalse(tef.compareTo(otherDt) == 0);
114         otherDt.setNode(nodeKey);
115         assertFalse(tef.compareTo(otherDt) == 0);
116         otherDt.setRequirement("A Requrement");
117         assertFalse(tef.compareTo(otherDt) == 0);
118         otherDt.setCapability("A Capability");
119         assertEquals(0, tef.compareTo(otherDt));
120
121         try {
122             tef.copyTo(null);
123             fail("test should throw an exception");
124         } catch (Exception exc) {
125             assertEquals("target is marked @NonNull but is null", exc.getMessage());
126         }
127
128         assertEquals(2, tef.getKeys().size());
129         assertEquals(2, new ToscaEventFilter().getKeys().size());
130
131         new ToscaEventFilter().clean();
132         tef.clean();
133         assertEquals(tdtClone0, tef);
134
135         assertFalse(new ToscaEventFilter().validate(new PfValidationResult()).isValid());
136         assertTrue(tef.validate(new PfValidationResult()).isValid());
137
138         tef.setRequirement(null);
139         assertTrue(tef.validate(new PfValidationResult()).isValid());
140         tef.setRequirement("");
141         assertFalse(tef.validate(new PfValidationResult()).isValid());
142         tef.setRequirement("A Requrement");
143         assertTrue(tef.validate(new PfValidationResult()).isValid());
144
145         tef.setCapability(null);
146         assertTrue(tef.validate(new PfValidationResult()).isValid());
147         tef.setCapability("");
148         assertFalse(tef.validate(new PfValidationResult()).isValid());
149         tef.setCapability("A Capability");
150         assertTrue(tef.validate(new PfValidationResult()).isValid());
151
152         tef.setNode(null);
153         assertFalse(tef.validate(new PfValidationResult()).isValid());
154         tef.setNode(PfConceptKey.getNullKey());
155         assertFalse(tef.validate(new PfValidationResult()).isValid());
156         tef.setNode(nodeKey);
157         assertTrue(tef.validate(new PfValidationResult()).isValid());
158
159         try {
160             tef.validate(null);
161             fail("test should throw an exception");
162         } catch (Exception exc) {
163             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
164         }
165     }
166 }