remove dependency of drool-application/yaml in actors
[policy/models.git] / models-interactions / model-yaml / src / test / java / org / onap / policy / controlloop / policy / guard / GuardPolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2018 Ericsson. All rights reserved.
4  * Modifications Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
5  * Modifications Copyright (C) 2019 Nordix Foundation.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.policy.guard;
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.assertNull;
27 import static org.junit.Assert.assertTrue;
28
29 import java.util.LinkedList;
30 import java.util.List;
31
32 import org.junit.Test;
33
34 public class GuardPolicyTest {
35
36     @Test
37     public void testConstructor() {
38         GuardPolicy guardPolicy = new GuardPolicy();
39
40         assertNotNull(guardPolicy.getId());
41         assertNull(guardPolicy.getName());
42         assertNull(guardPolicy.getDescription());
43         assertNull(guardPolicy.getMatch_parameters());
44         assertNull(guardPolicy.getLimit_constraints());
45     }
46
47     @Test
48     public void testConstructorString() {
49         String id = "guard id";
50         GuardPolicy guardPolicy = new GuardPolicy(id);
51
52         assertEquals(id, guardPolicy.getId());
53         assertNull(guardPolicy.getName());
54         assertNull(guardPolicy.getDescription());
55         assertNull(guardPolicy.getMatch_parameters());
56         assertNull(guardPolicy.getLimit_constraints());
57     }
58
59     @Test
60     public void testConstructorStringStringStringMatchParameters() {
61         String id = "guard id";
62         String name = "guard name";
63         String description = "guard description";
64         MatchParameters matchParameters = new MatchParameters();
65         List<Constraint> limitConstraints = new LinkedList<>();
66         limitConstraints.add(new Constraint());
67         GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters);
68
69         assertNotNull(guardPolicy.getId());
70         assertEquals(name, guardPolicy.getName());
71         assertEquals(description, guardPolicy.getDescription());
72         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
73         assertNull(guardPolicy.getLimit_constraints());
74     }
75
76     @Test
77     public void testConstructorStringMatchParametersList() {
78         String name = "guard name";
79         MatchParameters matchParameters = new MatchParameters();
80         List<Constraint> limitConstraints = new LinkedList<>();
81         limitConstraints.add(new Constraint());
82         GuardPolicy guardPolicy = new GuardPolicy(name, matchParameters, limitConstraints);
83
84         assertNotNull(guardPolicy.getId());
85         assertEquals(name, guardPolicy.getName());
86         assertNull(guardPolicy.getDescription());
87         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
88         assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
89     }
90
91     @Test
92     public void testConstructorStringStringMatchParametersList() {
93         String name = "guard name";
94         String description = "guard description";
95         MatchParameters matchParameters = new MatchParameters();
96         List<Constraint> limitConstraints = new LinkedList<>();
97         limitConstraints.add(new Constraint());
98         GuardPolicy guardPolicy = new GuardPolicy(name, description, matchParameters, limitConstraints);
99
100         assertNotNull(guardPolicy.getId());
101         assertEquals(name, guardPolicy.getName());
102         assertEquals(description, guardPolicy.getDescription());
103         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
104         assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
105     }
106
107     @Test
108     public void testConstructorStringStringStringMatchParametersList() {
109         String id = "guard id";
110         String name = "guard name";
111         String description = "guard description";
112         MatchParameters matchParameters = new MatchParameters();
113         List<Constraint> limitConstraints = new LinkedList<>();
114         limitConstraints.add(new Constraint());
115         GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters, limitConstraints);
116
117         assertEquals(id, guardPolicy.getId());
118         assertEquals(name, guardPolicy.getName());
119         assertEquals(description, guardPolicy.getDescription());
120         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
121         assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
122     }
123
124     @Test
125     public void testConstructorGuardPolicy() {
126         String id = "guard id";
127         String name = "guard name";
128         String description = "guard description";
129         MatchParameters matchParameters = new MatchParameters();
130         List<Constraint> limitConstraints = new LinkedList<>();
131         limitConstraints.add(new Constraint());
132         GuardPolicy guardPolicy1 = new GuardPolicy(id, name, description, matchParameters, limitConstraints);
133
134         GuardPolicy guardPolicy2 = new GuardPolicy(guardPolicy1);
135
136
137         assertEquals(id, guardPolicy2.getId());
138         assertEquals(name, guardPolicy2.getName());
139         assertEquals(description, guardPolicy2.getDescription());
140         assertEquals(matchParameters, guardPolicy2.getMatch_parameters());
141         assertEquals(limitConstraints, guardPolicy2.getLimit_constraints());
142     }
143
144     @Test
145     public void testSetAndGetId() {
146         String id = "guard id";
147         GuardPolicy guardPolicy = new GuardPolicy();
148         guardPolicy.setId(id);
149         assertEquals(id, guardPolicy.getId());
150     }
151
152     @Test
153     public void testSetAndGetName() {
154         String name = "guard name";
155         GuardPolicy guardPolicy = new GuardPolicy();
156         guardPolicy.setName(name);
157         assertEquals(name, guardPolicy.getName());
158     }
159
160     @Test
161     public void testSetAndGetDescription() {
162         String description = "guard description";
163         GuardPolicy guardPolicy = new GuardPolicy();
164         guardPolicy.setDescription(description);
165         assertEquals(description, guardPolicy.getDescription());
166     }
167
168     @Test
169     public void testSetAndGetMatchParameters() {
170         MatchParameters matchParameters = new MatchParameters();
171         GuardPolicy guardPolicy = new GuardPolicy();
172         guardPolicy.setMatch_parameters(matchParameters);
173         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
174     }
175
176     @Test
177     public void testSetAndGetLimitConstraints() {
178         LinkedList<Constraint> limitConstraints = new LinkedList<>();
179         limitConstraints.add(new Constraint());
180         GuardPolicy guardPolicy = new GuardPolicy();
181         guardPolicy.setLimit_constraints(limitConstraints);
182         assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
183     }
184
185     @Test
186     public void testIsValid() {
187         GuardPolicy guardPolicy = new GuardPolicy();
188         assertFalse(guardPolicy.isValid());
189
190         guardPolicy.setName("guard name");
191         assertTrue(guardPolicy.isValid());
192
193         guardPolicy.setId(null);
194         assertFalse(guardPolicy.isValid());
195     }
196
197     @Test
198     public void testToString() {
199         String id = "guard id";
200         String name = "guard name";
201         String description = "guard description";
202         MatchParameters matchParameters = new MatchParameters();
203         List<Constraint> limitConstraints = new LinkedList<>();
204         limitConstraints.add(new Constraint());
205         GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters, limitConstraints);
206
207         assertEquals(guardPolicy.toString(), "Policy [id=guard id, name=guard name, description=guard description, "
208                 + "match_parameters=MatchParameters [controlLoopName=null, actor=null, recipe=null, targets=null], "
209                 + "limitConstraints=[Constraint [freq_limit_per_target=null, time_window=null, active_time_range=null,"
210                 + " blacklist=null]]]", guardPolicy.toString());
211     }
212
213     @Test
214     public void testEquals() {
215         final String id = "guard id";
216         final String name = "guard name";
217         final String description = "guard description";
218         GuardPolicy guardPolicy1 = new GuardPolicy(id);
219         GuardPolicy guardPolicy2 = new GuardPolicy();
220         assertFalse(guardPolicy1.equals(guardPolicy2));
221
222         guardPolicy2.setId(id);
223         assertTrue(guardPolicy1.equals(guardPolicy2));
224         assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
225
226         guardPolicy1.setName(name);
227         assertFalse(guardPolicy1.equals(guardPolicy2));
228         guardPolicy2.setName(name);
229         assertTrue(guardPolicy1.equals(guardPolicy2));
230         assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
231
232         guardPolicy1.setDescription(description);
233         assertFalse(guardPolicy1.equals(guardPolicy2));
234         guardPolicy2.setDescription(description);
235         assertTrue(guardPolicy1.equals(guardPolicy2));
236         assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
237
238         MatchParameters matchParameters = new MatchParameters();
239         guardPolicy1.setMatch_parameters(matchParameters);
240         assertFalse(guardPolicy1.equals(guardPolicy2));
241         guardPolicy2.setMatch_parameters(matchParameters);
242         assertTrue(guardPolicy1.equals(guardPolicy2));
243         assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
244
245         LinkedList<Constraint> limitConstraints = new LinkedList<>();
246         limitConstraints.add(new Constraint());
247         guardPolicy1.setLimit_constraints(limitConstraints);
248         assertFalse(guardPolicy1.equals(guardPolicy2));
249         guardPolicy2.setLimit_constraints(limitConstraints);
250         assertTrue(guardPolicy1.equals(guardPolicy2));
251         assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
252     }
253
254     @Test
255     public void testEqualsSameObject() {
256         GuardPolicy guardPolicy = new GuardPolicy();
257         assertTrue(guardPolicy.equals(guardPolicy));
258     }
259
260     @Test
261     public void testEqualsNull() {
262         GuardPolicy guardPolicy = new GuardPolicy();
263         assertFalse(guardPolicy.equals(null));
264     }
265
266     @Test
267     public void testEqualsInstanceOfDiffClass() {
268         GuardPolicy guardPolicy = new GuardPolicy();
269         assertFalse(guardPolicy.equals(""));
270     }
271 }