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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.policy.guard;
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;
29 import java.util.LinkedList;
30 import java.util.List;
31 import org.junit.Test;
33 public class GuardPolicyTest {
35 private static final String GUARD_DESCRIPTION = "guard description";
36 private static final String GUARD_ID = "guard id";
37 private static final String GUARD_NAME = "guard name";
40 public void testConstructor() {
41 GuardPolicy guardPolicy = new GuardPolicy();
43 assertNotNull(guardPolicy.getId());
44 assertNull(guardPolicy.getName());
45 assertNull(guardPolicy.getDescription());
46 assertNull(guardPolicy.getMatch_parameters());
47 assertNull(guardPolicy.getLimit_constraints());
51 public void testConstructorString() {
53 GuardPolicy guardPolicy = new GuardPolicy(id);
55 assertEquals(id, guardPolicy.getId());
56 assertNull(guardPolicy.getName());
57 assertNull(guardPolicy.getDescription());
58 assertNull(guardPolicy.getMatch_parameters());
59 assertNull(guardPolicy.getLimit_constraints());
63 public void testConstructorStringStringStringMatchParameters() {
65 String name = GUARD_NAME;
66 String description = GUARD_DESCRIPTION;
67 MatchParameters matchParameters = new MatchParameters();
68 List<Constraint> limitConstraints = new LinkedList<>();
69 limitConstraints.add(new Constraint());
70 GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters);
72 assertNotNull(guardPolicy.getId());
73 assertEquals(name, guardPolicy.getName());
74 assertEquals(description, guardPolicy.getDescription());
75 assertEquals(matchParameters, guardPolicy.getMatch_parameters());
76 assertNull(guardPolicy.getLimit_constraints());
80 public void testConstructorStringMatchParametersList() {
81 String name = GUARD_NAME;
82 MatchParameters matchParameters = new MatchParameters();
83 List<Constraint> limitConstraints = new LinkedList<>();
84 limitConstraints.add(new Constraint());
85 GuardPolicy guardPolicy = new GuardPolicy(name, matchParameters, limitConstraints);
87 assertNotNull(guardPolicy.getId());
88 assertEquals(name, guardPolicy.getName());
89 assertNull(guardPolicy.getDescription());
90 assertEquals(matchParameters, guardPolicy.getMatch_parameters());
91 assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
95 public void testConstructorStringStringMatchParametersList() {
96 String name = GUARD_NAME;
97 String description = GUARD_DESCRIPTION;
98 MatchParameters matchParameters = new MatchParameters();
99 List<Constraint> limitConstraints = new LinkedList<>();
100 limitConstraints.add(new Constraint());
101 GuardPolicy guardPolicy = new GuardPolicy(name, description, matchParameters, limitConstraints);
103 assertNotNull(guardPolicy.getId());
104 assertEquals(name, guardPolicy.getName());
105 assertEquals(description, guardPolicy.getDescription());
106 assertEquals(matchParameters, guardPolicy.getMatch_parameters());
107 assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
111 public void testConstructorStringStringStringMatchParametersList() {
112 String id = GUARD_ID;
113 String name = GUARD_NAME;
114 String description = GUARD_DESCRIPTION;
115 MatchParameters matchParameters = new MatchParameters();
116 List<Constraint> limitConstraints = new LinkedList<>();
117 limitConstraints.add(new Constraint());
118 GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters, limitConstraints);
120 assertEquals(id, guardPolicy.getId());
121 assertEquals(name, guardPolicy.getName());
122 assertEquals(description, guardPolicy.getDescription());
123 assertEquals(matchParameters, guardPolicy.getMatch_parameters());
124 assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
128 public void testConstructorGuardPolicy() {
129 String id = GUARD_ID;
130 String name = GUARD_NAME;
131 String description = GUARD_DESCRIPTION;
132 MatchParameters matchParameters = new MatchParameters();
133 List<Constraint> limitConstraints = new LinkedList<>();
134 limitConstraints.add(new Constraint());
135 GuardPolicy guardPolicy1 = new GuardPolicy(id, name, description, matchParameters, limitConstraints);
137 GuardPolicy guardPolicy2 = new GuardPolicy(guardPolicy1);
140 assertEquals(id, guardPolicy2.getId());
141 assertEquals(name, guardPolicy2.getName());
142 assertEquals(description, guardPolicy2.getDescription());
143 assertEquals(matchParameters, guardPolicy2.getMatch_parameters());
144 assertEquals(limitConstraints, guardPolicy2.getLimit_constraints());
148 public void testSetAndGetId() {
149 String id = GUARD_ID;
150 GuardPolicy guardPolicy = new GuardPolicy();
151 guardPolicy.setId(id);
152 assertEquals(id, guardPolicy.getId());
156 public void testSetAndGetName() {
157 String name = GUARD_NAME;
158 GuardPolicy guardPolicy = new GuardPolicy();
159 guardPolicy.setName(name);
160 assertEquals(name, guardPolicy.getName());
164 public void testSetAndGetDescription() {
165 String description = GUARD_DESCRIPTION;
166 GuardPolicy guardPolicy = new GuardPolicy();
167 guardPolicy.setDescription(description);
168 assertEquals(description, guardPolicy.getDescription());
172 public void testSetAndGetMatchParameters() {
173 MatchParameters matchParameters = new MatchParameters();
174 GuardPolicy guardPolicy = new GuardPolicy();
175 guardPolicy.setMatch_parameters(matchParameters);
176 assertEquals(matchParameters, guardPolicy.getMatch_parameters());
180 public void testSetAndGetLimitConstraints() {
181 LinkedList<Constraint> limitConstraints = new LinkedList<>();
182 limitConstraints.add(new Constraint());
183 GuardPolicy guardPolicy = new GuardPolicy();
184 guardPolicy.setLimit_constraints(limitConstraints);
185 assertEquals(limitConstraints, guardPolicy.getLimit_constraints());
189 public void testIsValid() {
190 GuardPolicy guardPolicy = new GuardPolicy();
191 assertFalse(guardPolicy.isValid());
193 guardPolicy.setName(GUARD_NAME);
194 assertTrue(guardPolicy.isValid());
196 guardPolicy.setId(null);
197 assertFalse(guardPolicy.isValid());
201 public void testToString() {
202 String id = GUARD_ID;
203 String name = GUARD_NAME;
204 String description = GUARD_DESCRIPTION;
205 MatchParameters matchParameters = new MatchParameters();
206 List<Constraint> limitConstraints = new LinkedList<>();
207 limitConstraints.add(new Constraint());
208 GuardPolicy guardPolicy = new GuardPolicy(id, name, description, matchParameters, limitConstraints);
210 assertEquals(guardPolicy.toString(), "Policy [id=guard id, name=guard name, description=guard description, "
211 + "match_parameters=MatchParameters [controlLoopName=null, actor=null, recipe=null, targets=null], "
212 + "limitConstraints=[Constraint [freq_limit_per_target=null, time_window=null, active_time_range=null,"
213 + " blacklist=null]]]", guardPolicy.toString());
217 public void testEquals() {
218 final String id = GUARD_ID;
219 final String name = GUARD_NAME;
220 final String description = GUARD_DESCRIPTION;
221 GuardPolicy guardPolicy1 = new GuardPolicy(id);
222 GuardPolicy guardPolicy2 = new GuardPolicy();
223 assertFalse(guardPolicy1.equals(guardPolicy2));
225 guardPolicy2.setId(id);
226 assertTrue(guardPolicy1.equals(guardPolicy2));
227 assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
229 guardPolicy1.setName(name);
230 assertFalse(guardPolicy1.equals(guardPolicy2));
231 guardPolicy2.setName(name);
232 assertTrue(guardPolicy1.equals(guardPolicy2));
233 assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
235 guardPolicy1.setDescription(description);
236 assertFalse(guardPolicy1.equals(guardPolicy2));
237 guardPolicy2.setDescription(description);
238 assertTrue(guardPolicy1.equals(guardPolicy2));
239 assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
241 MatchParameters matchParameters = new MatchParameters();
242 guardPolicy1.setMatch_parameters(matchParameters);
243 assertFalse(guardPolicy1.equals(guardPolicy2));
244 guardPolicy2.setMatch_parameters(matchParameters);
245 assertTrue(guardPolicy1.equals(guardPolicy2));
246 assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
248 LinkedList<Constraint> limitConstraints = new LinkedList<>();
249 limitConstraints.add(new Constraint());
250 guardPolicy1.setLimit_constraints(limitConstraints);
251 assertFalse(guardPolicy1.equals(guardPolicy2));
252 guardPolicy2.setLimit_constraints(limitConstraints);
253 assertTrue(guardPolicy1.equals(guardPolicy2));
254 assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
258 public void testEqualsSameObject() {
259 GuardPolicy guardPolicy = new GuardPolicy();
260 assertTrue(guardPolicy.equals(guardPolicy));
264 public void testEqualsNull() {
265 GuardPolicy guardPolicy = new GuardPolicy();
266 assertFalse(guardPolicy.equals(null));
270 public void testEqualsInstanceOfDiffClass() {
271 GuardPolicy guardPolicy = new GuardPolicy();
272 assertFalse(guardPolicy.equals(""));