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;
32 import org.junit.Test;
34 public class GuardPolicyTest {
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";
41 public void testConstructor() {
42 GuardPolicy guardPolicy = new GuardPolicy();
44 assertNotNull(guardPolicy.getId());
45 assertNull(guardPolicy.getName());
46 assertNull(guardPolicy.getDescription());
47 assertNull(guardPolicy.getMatch_parameters());
48 assertNull(guardPolicy.getLimit_constraints());
52 public void testConstructorString() {
54 GuardPolicy guardPolicy = new GuardPolicy(id);
56 assertEquals(id, guardPolicy.getId());
57 assertNull(guardPolicy.getName());
58 assertNull(guardPolicy.getDescription());
59 assertNull(guardPolicy.getMatch_parameters());
60 assertNull(guardPolicy.getLimit_constraints());
64 public void testConstructorStringStringStringMatchParameters() {
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);
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());
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);
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());
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);
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());
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);
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());
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);
138 GuardPolicy guardPolicy2 = new GuardPolicy(guardPolicy1);
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());
149 public void testSetAndGetId() {
150 String id = GUARD_ID;
151 GuardPolicy guardPolicy = new GuardPolicy();
152 guardPolicy.setId(id);
153 assertEquals(id, guardPolicy.getId());
157 public void testSetAndGetName() {
158 String name = GUARD_NAME;
159 GuardPolicy guardPolicy = new GuardPolicy();
160 guardPolicy.setName(name);
161 assertEquals(name, guardPolicy.getName());
165 public void testSetAndGetDescription() {
166 String description = GUARD_DESCRIPTION;
167 GuardPolicy guardPolicy = new GuardPolicy();
168 guardPolicy.setDescription(description);
169 assertEquals(description, guardPolicy.getDescription());
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());
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());
190 public void testIsValid() {
191 GuardPolicy guardPolicy = new GuardPolicy();
192 assertFalse(guardPolicy.isValid());
194 guardPolicy.setName(GUARD_NAME);
195 assertTrue(guardPolicy.isValid());
197 guardPolicy.setId(null);
198 assertFalse(guardPolicy.isValid());
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);
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());
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));
226 guardPolicy2.setId(id);
227 assertTrue(guardPolicy1.equals(guardPolicy2));
228 assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
230 guardPolicy1.setName(name);
231 assertFalse(guardPolicy1.equals(guardPolicy2));
232 guardPolicy2.setName(name);
233 assertTrue(guardPolicy1.equals(guardPolicy2));
234 assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
236 guardPolicy1.setDescription(description);
237 assertFalse(guardPolicy1.equals(guardPolicy2));
238 guardPolicy2.setDescription(description);
239 assertTrue(guardPolicy1.equals(guardPolicy2));
240 assertEquals(guardPolicy1.hashCode(), guardPolicy2.hashCode());
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());
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());
259 public void testEqualsSameObject() {
260 GuardPolicy guardPolicy = new GuardPolicy();
261 assertTrue(guardPolicy.equals(guardPolicy));
265 public void testEqualsNull() {
266 GuardPolicy guardPolicy = new GuardPolicy();
267 assertFalse(guardPolicy.equals(null));
271 public void testEqualsInstanceOfDiffClass() {
272 GuardPolicy guardPolicy = new GuardPolicy();
273 assertFalse(guardPolicy.equals(""));