7019595b37b24203bac73ec5f23437977f30419d
[policy/drools-applications.git] / controlloop / common / policy-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  * ================================================================================
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  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.controlloop.policy.guard;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.LinkedList;
29 import java.util.List;
30
31 import org.junit.Test;
32
33 public class GuardPolicyTest {
34
35     @Test
36     public void testConstructor() {
37         GuardPolicy guardPolicy = new GuardPolicy();
38
39         assertNotNull(guardPolicy.getId());
40         assertNull(guardPolicy.getName());
41         assertNull(guardPolicy.getDescription());
42         assertNull(guardPolicy.getMatch_parameters());
43         assertNull(guardPolicy.getLimit_constraints());
44     }
45
46     @Test
47     public void testConstructorString() {
48         String id = "guard id";
49         GuardPolicy guardPolicy = new GuardPolicy(id);
50
51         assertEquals(id, guardPolicy.getId());
52         assertNull(guardPolicy.getName());
53         assertNull(guardPolicy.getDescription());
54         assertNull(guardPolicy.getMatch_parameters());
55         assertNull(guardPolicy.getLimit_constraints());
56     }
57
58     @Test
59     public void testConstructorStringStringStringMatchParameters() {
60         String id = "guard id";
61         String name = "guard name";
62         String description = "guard description";
63         MatchParameters matchParameters = new MatchParameters();
64         List<Constraint> limitConstraints = new LinkedList<>();
65         limitConstraints.add(new Constraint());
66         GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters);
67
68         assertNotNull(guardPolicy.getId());
69         assertEquals(name, guardPolicy.getName());
70         assertEquals(description, guardPolicy.getDescription());
71         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
72         assertNull(guardPolicy.getLimit_constraints());
73     }
74
75     @Test
76     public void testConstructorStringMatchParametersList() {
77         String name = "guard name";
78         MatchParameters matchParameters = new MatchParameters();
79         List<Constraint> limitConstraints = new LinkedList<>();
80         limitConstraints.add(new Constraint());
81         GuardPolicy guardPolicy = new GuardPolicy(name, matchParameters, limitConstraints);
82
83         assertNotNull(guardPolicy.getId());
84         assertEquals(name, guardPolicy.getName());
85         assertNull(guardPolicy.getDescription());
86         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
87         assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
88     }
89
90     @Test
91     public void testConstructorStringStringMatchParametersList() {
92         String name = "guard name";
93         String description = "guard description";
94         MatchParameters matchParameters = new MatchParameters();
95         List<Constraint> limitConstraints = new LinkedList<>();
96         limitConstraints.add(new Constraint());
97         GuardPolicy guardPolicy = new GuardPolicy(name, description, matchParameters, limitConstraints);
98
99         assertNotNull(guardPolicy.getId());
100         assertEquals(name, guardPolicy.getName());
101         assertEquals(description, guardPolicy.getDescription());
102         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
103         assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
104     }
105
106     @Test
107     public void testConstructorStringStringStringMatchParametersList() {
108         String id = "guard id";
109         String name = "guard name";
110         String description = "guard description";
111         MatchParameters matchParameters = new MatchParameters();
112         List<Constraint> limitConstraints = new LinkedList<>();
113         limitConstraints.add(new Constraint());
114         GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters, limitConstraints);
115
116         assertEquals(id, guardPolicy.getId());
117         assertEquals(name, guardPolicy.getName());
118         assertEquals(description, guardPolicy.getDescription());
119         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
120         assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
121     }
122
123     @Test
124     public void testConstructorGuardPolicy() {
125         String id = "guard id";
126         String name = "guard name";
127         String description = "guard description";
128         MatchParameters matchParameters = new MatchParameters();
129         List<Constraint> limitConstraints = new LinkedList<>();
130         limitConstraints.add(new Constraint());
131         GuardPolicy guardPolicy1 = new GuardPolicy(id, name, description, matchParameters, limitConstraints);
132
133         GuardPolicy guardPolicy2 = new GuardPolicy(guardPolicy1);
134
135
136         assertEquals(id, guardPolicy2.getId());
137         assertEquals(name, guardPolicy2.getName());
138         assertEquals(description, guardPolicy2.getDescription());
139         assertEquals(matchParameters, guardPolicy2.getMatch_parameters());
140         assertEquals(limitConstraints, guardPolicy2.getLimit_constraints());
141     }
142
143     @Test
144     public void testSetAndGetId() {
145         String id = "guard id";
146         GuardPolicy guardPolicy = new GuardPolicy();
147         guardPolicy.setId(id);
148         assertEquals(id, guardPolicy.getId());
149     }
150
151     @Test
152     public void testSetAndGetName() {
153         String name = "guard name";
154         GuardPolicy guardPolicy = new GuardPolicy();
155         guardPolicy.setName(name);
156         assertEquals(name, guardPolicy.getName());
157     }
158
159     @Test
160     public void testSetAndGetDescription() {
161         String description = "guard description";
162         GuardPolicy guardPolicy = new GuardPolicy();
163         guardPolicy.setDescription(description);
164         assertEquals(description, guardPolicy.getDescription());
165     }
166
167     @Test
168     public void testSetAndGetMatchParameters() {
169         MatchParameters matchParameters = new MatchParameters();
170         GuardPolicy guardPolicy = new GuardPolicy();
171         guardPolicy.setMatch_parameters(matchParameters);
172         assertEquals(matchParameters, guardPolicy.getMatch_parameters());
173     }
174
175     @Test
176     public void testSetAndGetLimitConstraints() {
177         LinkedList<Constraint> limitConstraints = new LinkedList<>();
178         limitConstraints.add(new Constraint());
179         GuardPolicy guardPolicy = new GuardPolicy();
180         guardPolicy.setLimit_constraints(limitConstraints);
181         assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
182     }
183
184     @Test
185     public void testIsValid() {
186         GuardPolicy guardPolicy = new GuardPolicy();
187         assertFalse(guardPolicy.isValid());
188
189         guardPolicy.setName("guard name");
190         assertTrue(guardPolicy.isValid());
191
192         guardPolicy.setId(null);
193         assertFalse(guardPolicy.isValid());
194     }
195
196     @Test
197     public void testToString() {
198         String id = "guard id";
199         String name = "guard name";
200         String description = "guard description";
201         MatchParameters matchParameters = new MatchParameters();
202         List<Constraint> limitConstraints = new LinkedList<>();
203         limitConstraints.add(new Constraint());
204         GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters, limitConstraints);
205
206         assertEquals(guardPolicy.toString(), "Policy [id=guard id, name=guard name, description=guard description, "
207                 + "match_parameters=MatchParameters [controlLoopName=null, actor=null, recipe=null, targets=null], "
208                 + "limitConstraints=[Constraint [freq_limit_per_target=null, time_window=null, active_time_range=null,"
209                 + " blacklist=null]]]",
210                 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 }