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