d80fecf2baa71067b159f5a99c60a326adf149f4
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2018 Ericsson. All rights reserved.
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  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.controlloop.policy.guard;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.LinkedList;
28 import java.util.List;
29
30 import org.junit.Test;
31
32 public class GuardPolicyTest {
33
34     @Test
35     public void testConstructor() {
36         GuardPolicy guardPolicy = new GuardPolicy();
37
38         assertNotNull(guardPolicy.getId());
39         assertNull(guardPolicy.getName());
40         assertNull(guardPolicy.getDescription());
41         assertNull(guardPolicy.getMatch_parameters());
42         assertNull(guardPolicy.getLimit_constraints());
43     }
44
45     @Test
46     public void testConstructorString() {
47         String id = "guard id";
48         GuardPolicy guardPolicy = new GuardPolicy(id);
49
50         assertEquals(id, guardPolicy.getId());
51         assertNull(guardPolicy.getName());
52         assertNull(guardPolicy.getDescription());
53         assertNull(guardPolicy.getMatch_parameters());
54         assertNull(guardPolicy.getLimit_constraints());
55     }
56
57     @Test
58     public void testConstructorStringStringStringMatchParameters() {
59         String id = "guard id";
60         String name = "guard name";
61         String description = "guard description";
62         MatchParameters matchParameters = new MatchParameters();
63         List<Constraint> limitConstraints = new LinkedList<>();
64         limitConstraints.add(new Constraint());
65         GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters);
66
67         assertNotNull(guardPolicy.getId());
68         assertEquals(name, guardPolicy.getName());
69         assertEquals(description, guardPolicy.getDescription());
70         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
71         assertNull(guardPolicy.getLimit_constraints());
72     }
73
74     @Test
75     public void testConstructorStringMatchParametersList() {
76         String name = "guard name";
77         MatchParameters matchParameters = new MatchParameters();
78         List<Constraint> limitConstraints = new LinkedList<>();
79         limitConstraints.add(new Constraint());
80         GuardPolicy guardPolicy = new GuardPolicy(name, matchParameters, limitConstraints);
81
82         assertNotNull(guardPolicy.getId());
83         assertEquals(name, guardPolicy.getName());
84         assertNull(guardPolicy.getDescription());
85         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
86         assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
87     }
88
89     @Test
90     public void testConstructorStringStringMatchParametersList() {
91         String name = "guard name";
92         String description = "guard description";
93         MatchParameters matchParameters = new MatchParameters();
94         List<Constraint> limitConstraints = new LinkedList<>();
95         limitConstraints.add(new Constraint());
96         GuardPolicy guardPolicy = new GuardPolicy(name, description, matchParameters, limitConstraints);
97
98         assertNotNull(guardPolicy.getId());
99         assertEquals(name, guardPolicy.getName());
100         assertEquals(description, guardPolicy.getDescription());
101         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
102         assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
103     }
104
105     @Test
106     public void testConstructorStringStringStringMatchParametersList() {
107         String id = "guard id";
108         String name = "guard name";
109         String description = "guard description";
110         MatchParameters matchParameters = new MatchParameters();
111         List<Constraint> limitConstraints = new LinkedList<>();
112         limitConstraints.add(new Constraint());
113         GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters, limitConstraints);
114
115         assertEquals(id, guardPolicy.getId());
116         assertEquals(name, guardPolicy.getName());
117         assertEquals(description, guardPolicy.getDescription());
118         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
119         assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
120     }
121
122     @Test
123     public void testConstructorGuardPolicy() {
124         String id = "guard id";
125         String name = "guard name";
126         String description = "guard description";
127         MatchParameters matchParameters = new MatchParameters();
128         List<Constraint> limitConstraints = new LinkedList<>();
129         limitConstraints.add(new Constraint());
130         GuardPolicy guardPolicy1 = new GuardPolicy(id, name, description, matchParameters, limitConstraints);
131
132         GuardPolicy guardPolicy2 = new GuardPolicy(guardPolicy1);
133
134
135         assertEquals(id, guardPolicy2.getId());
136         assertEquals(name, guardPolicy2.getName());
137         assertEquals(description, guardPolicy2.getDescription());
138         assertEquals(matchParameters, guardPolicy2.getMatch_parameters());
139         assertEquals(limitConstraints, guardPolicy2.getLimit_constraints());
140     }
141
142     @Test
143     public void testSetAndGetId() {
144         String id = "guard id";
145         GuardPolicy guardPolicy = new GuardPolicy();
146         guardPolicy.setId(id);
147         assertEquals(id, guardPolicy.getId());
148     }
149
150     @Test
151     public void testSetAndGetName() {
152         String name = "guard name";
153         GuardPolicy guardPolicy = new GuardPolicy();
154         guardPolicy.setName(name);
155         assertEquals(name, guardPolicy.getName());
156     }
157
158     @Test
159     public void testSetAndGetDescription() {
160         String description = "guard description";
161         GuardPolicy guardPolicy = new GuardPolicy();
162         guardPolicy.setDescription(description);
163         assertEquals(description, guardPolicy.getDescription());
164     }
165
166     @Test
167     public void testSetAndGetMatchParameters() {
168         MatchParameters matchParameters = new MatchParameters();
169         GuardPolicy guardPolicy = new GuardPolicy();
170         guardPolicy.setMatch_parameters(matchParameters);
171         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
172     }
173
174     @Test
175     public void testSetAndGetLimitConstraints() {
176         LinkedList<Constraint> limitConstraints = new LinkedList<>();
177         limitConstraints.add(new Constraint());
178         GuardPolicy guardPolicy = new GuardPolicy();
179         guardPolicy.setLimit_constraints(limitConstraints);
180         assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
181     }
182
183     @Test
184     public void testIsValid() {
185         GuardPolicy guardPolicy = new GuardPolicy();
186         assertFalse(guardPolicy.isValid());
187
188         guardPolicy.setName("guard name");
189         assertTrue(guardPolicy.isValid());
190
191         guardPolicy.setId(null);
192         assertFalse(guardPolicy.isValid());
193     }
194
195     @Test
196     public void testToString() {
197         String id = "guard id";
198         String name = "guard name";
199         String description = "guard description";
200         MatchParameters matchParameters = new MatchParameters();
201         List<Constraint> limitConstraints = new LinkedList<>();
202         limitConstraints.add(new Constraint());
203         GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters, limitConstraints);
204
205         assertEquals(guardPolicy.toString(), "Policy [id=guard id, name=guard name, description=guard description, "
206                 + "match_parameters=MatchParameters [controlLoopName=null, actor=null, recipe=null, targets=null], "
207                 + "limitConstraints=[Constraint [freq_limit_per_target=null, time_window=null, active_time_range=null,"
208                 + " blacklist=null]]]",
209                 guardPolicy.toString());
210     }
211
212     @Test
213     public void testEquals() {
214         String id = "guard id";
215         String name = "guard name";
216         String description = "guard description";
217         GuardPolicy guardPolicy1 = new GuardPolicy(id);
218         GuardPolicy guardPolicy2 = new GuardPolicy();
219         assertFalse(guardPolicy1.equals(guardPolicy2));
220
221         guardPolicy2.setId(id);
222         assertTrue(guardPolicy1.equals(guardPolicy2));
223         assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
224
225         guardPolicy1.setName(name);
226         assertFalse(guardPolicy1.equals(guardPolicy2));
227         guardPolicy2.setName(name);
228         assertTrue(guardPolicy1.equals(guardPolicy2));
229         assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
230
231         guardPolicy1.setDescription(description);
232         assertFalse(guardPolicy1.equals(guardPolicy2));
233         guardPolicy2.setDescription(description);
234         assertTrue(guardPolicy1.equals(guardPolicy2));
235         assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
236
237         MatchParameters matchParameters = new MatchParameters();
238         guardPolicy1.setMatch_parameters(matchParameters);
239         assertFalse(guardPolicy1.equals(guardPolicy2));
240         guardPolicy2.setMatch_parameters(matchParameters);
241         assertTrue(guardPolicy1.equals(guardPolicy2));
242         assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
243
244         LinkedList<Constraint> limitConstraints = new LinkedList<>();
245         limitConstraints.add(new Constraint());
246         guardPolicy1.setLimit_constraints(limitConstraints);
247         assertFalse(guardPolicy1.equals(guardPolicy2));
248         guardPolicy2.setLimit_constraints(limitConstraints);
249         assertTrue(guardPolicy1.equals(guardPolicy2));
250         assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
251     }
252
253     @Test
254     public void testEqualsSameObject() {
255         GuardPolicy guardPolicy = new GuardPolicy();
256         assertTrue(guardPolicy.equals(guardPolicy));
257     }
258
259     @Test
260     public void testEqualsNull() {
261         GuardPolicy guardPolicy = new GuardPolicy();
262         assertFalse(guardPolicy.equals(null));
263     }
264
265     @Test
266     public void testEqualsInstanceOfDiffClass() {
267         GuardPolicy guardPolicy = new GuardPolicy();
268         assertFalse(guardPolicy.equals(""));
269     }
270 }