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.assertNull;
26 import static org.junit.Assert.assertTrue;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
33 import org.junit.Test;
35 public class ConstraintTest {
37 private static final String TIME_WINDOW_VALUE = "timeWindowValue";
38 private static final String TIME_WINDOW_KEY = "timeWindowKey";
39 private static final String BLACKLIST_ITEM = "blacklist item";
42 public void testConstraint() {
43 Constraint constraint = new Constraint();
45 assertNull(constraint.getFreq_limit_per_target());
46 assertNull(constraint.getTime_window());
47 assertNull(constraint.getActive_time_range());
48 assertNull(constraint.getBlacklist());
52 public void testGetAndSetFreq_limit_per_target() {
53 Integer freqLimitPerTarget = 10;
54 Constraint constraint = new Constraint();
55 constraint.setFreq_limit_per_target(freqLimitPerTarget);
56 assertEquals(freqLimitPerTarget, constraint.getFreq_limit_per_target());
60 public void testGetAndSetTime_window() {
61 Map<String, String> timeWindow = new HashMap<>();
62 timeWindow.put(TIME_WINDOW_KEY, TIME_WINDOW_VALUE);
63 Constraint constraint = new Constraint();
64 constraint.setTime_window(timeWindow);
65 assertEquals(timeWindow, constraint.getTime_window());
69 public void testGetAndSetActive_time_range() {
70 Map<String, String> activeTimeRange = new HashMap<>();
71 activeTimeRange.put(TIME_WINDOW_KEY, TIME_WINDOW_VALUE);
72 Constraint constraint = new Constraint();
73 constraint.setActive_time_range(activeTimeRange);;
74 assertEquals(activeTimeRange, constraint.getActive_time_range());
78 public void testGetAndSetBlacklist() {
79 List<String> blacklist = new ArrayList<>();
80 blacklist.add(BLACKLIST_ITEM);
81 Constraint constraint = new Constraint();
82 constraint.setBlacklist(blacklist);
83 assertEquals(blacklist, constraint.getBlacklist());
87 public void testConstraintIntegerMapOfStringString() {
88 Integer freqLimitPerTarget = 10;
89 Map<String, String> timeWindow = new HashMap<>();
91 Constraint constraint = new Constraint(freqLimitPerTarget, timeWindow);
93 assertEquals(freqLimitPerTarget, constraint.getFreq_limit_per_target());
94 assertEquals(timeWindow, constraint.getTime_window());
95 assertNull(constraint.getActive_time_range());
96 assertNull(constraint.getBlacklist());
100 public void testConstraintListOfString() {
101 List<String> blacklist = new ArrayList<>();
102 blacklist.add(BLACKLIST_ITEM);
103 Constraint constraint = new Constraint(blacklist);
105 assertNull(constraint.getFreq_limit_per_target());
106 assertNull(constraint.getTime_window());
107 assertNull(constraint.getActive_time_range());
108 assertEquals(blacklist, constraint.getBlacklist());
112 public void testConstraintIntegerMapOfStringStringListOfString() {
113 Integer freqLimitPerTarget = 10;
114 Map<String, String> timeWindow = new HashMap<>();
115 List<String> blacklist = new ArrayList<>();
116 blacklist.add(BLACKLIST_ITEM);
117 Constraint constraint = new Constraint(freqLimitPerTarget, timeWindow, blacklist);
119 assertEquals(freqLimitPerTarget, constraint.getFreq_limit_per_target());
120 assertEquals(timeWindow, constraint.getTime_window());
121 assertNull(constraint.getActive_time_range());
122 assertEquals(blacklist, constraint.getBlacklist());
126 public void testConstraintIntegerMapOfStringStringMapOfStringString() {
127 Integer freqLimitPerTarget = 10;
128 Map<String, String> timeWindow = new HashMap<>();
129 Map<String, String> activeTimeRange = new HashMap<>();
130 activeTimeRange.put(TIME_WINDOW_KEY, TIME_WINDOW_VALUE);
131 Constraint constraint = new Constraint(freqLimitPerTarget, timeWindow, activeTimeRange);
133 assertEquals(freqLimitPerTarget, constraint.getFreq_limit_per_target());
134 assertEquals(timeWindow, constraint.getTime_window());
135 assertEquals(activeTimeRange, constraint.getActive_time_range());
136 assertNull(constraint.getBlacklist());
141 public void testConstraintIntegerMapOfStringStringMapOfStringStringListOfString() {
142 Integer freqLimitPerTarget = 10;
143 Map<String, String> timeWindow = new HashMap<>();
144 Map<String, String> activeTimeRange = new HashMap<>();
145 activeTimeRange.put(TIME_WINDOW_KEY, TIME_WINDOW_VALUE);
146 List<String> blacklist = new ArrayList<>();
147 blacklist.add(BLACKLIST_ITEM);
148 Constraint constraint = new Constraint(freqLimitPerTarget, timeWindow, activeTimeRange, blacklist);
150 assertEquals(freqLimitPerTarget, constraint.getFreq_limit_per_target());
151 assertEquals(timeWindow, constraint.getTime_window());
152 assertEquals(activeTimeRange, constraint.getActive_time_range());
153 assertEquals(blacklist, constraint.getBlacklist());
157 public void testConstraintConstraint() {
158 Integer freqLimitPerTarget = 10;
159 Map<String, String> timeWindow = new HashMap<>();
160 Map<String, String> activeTimeRange = new HashMap<>();
161 activeTimeRange.put(TIME_WINDOW_KEY, TIME_WINDOW_VALUE);
162 List<String> blacklist = new ArrayList<>();
163 blacklist.add(BLACKLIST_ITEM);
164 Constraint constraint1 = new Constraint(freqLimitPerTarget, timeWindow, activeTimeRange, blacklist);
165 Constraint constraint2 = new Constraint(constraint1);
167 assertEquals(freqLimitPerTarget, constraint2.getFreq_limit_per_target());
168 assertEquals(timeWindow, constraint2.getTime_window());
169 assertEquals(activeTimeRange, constraint2.getActive_time_range());
170 assertEquals(blacklist, constraint2.getBlacklist());
174 public void testIsValid() {
175 Integer freqLimitPerTarget = 10;
176 final Map<String, String> timeWindow = new HashMap<>();
178 Constraint constraint = new Constraint();
179 assertTrue(constraint.isValid());
181 constraint.setFreq_limit_per_target(freqLimitPerTarget);
182 assertFalse(constraint.isValid());
184 constraint.setTime_window(timeWindow);
185 assertTrue(constraint.isValid());
187 constraint.setFreq_limit_per_target(null);
188 assertFalse(constraint.isValid());
192 public void testToString() {
193 Integer freqLimitPerTarget = 10;
194 Map<String, String> timeWindow = new HashMap<>();
195 Map<String, String> activeTimeRange = new HashMap<>();
196 activeTimeRange.put(TIME_WINDOW_KEY, TIME_WINDOW_VALUE);
197 List<String> blacklist = new ArrayList<>();
198 blacklist.add(BLACKLIST_ITEM);
199 Constraint constraint = new Constraint(freqLimitPerTarget, timeWindow, activeTimeRange, blacklist);
201 assertEquals(constraint.toString(), "Constraint [freq_limit_per_target=" + freqLimitPerTarget + ", time_window="
202 + timeWindow + ", active_time_range=" + activeTimeRange + ", blacklist=" + blacklist + "]");
206 public void testEquals() {
207 Integer freqLimitPerTarget = 10;
208 final Map<String, String> timeWindow = new HashMap<>();
209 final Map<String, String> activeTimeRange = new HashMap<>();
210 List<String> blacklist = new ArrayList<>();
211 blacklist.add(BLACKLIST_ITEM);
213 Constraint constraint1 = new Constraint();
214 Constraint constraint2 = new Constraint();
215 assertTrue(constraint1.equals(constraint2));
217 constraint1.setFreq_limit_per_target(freqLimitPerTarget);
218 assertFalse(constraint1.equals(constraint2));
219 constraint2.setFreq_limit_per_target(freqLimitPerTarget);
220 assertTrue(constraint1.equals(constraint2));
221 assertEquals(constraint1.hashCode(), constraint2.hashCode());
223 constraint1.setTime_window(timeWindow);
224 assertFalse(constraint1.equals(constraint2));
225 constraint2.setTime_window(timeWindow);
226 assertTrue(constraint1.equals(constraint2));
227 assertEquals(constraint1.hashCode(), constraint2.hashCode());
229 constraint1.setActive_time_range(activeTimeRange);
230 assertFalse(constraint1.equals(constraint2));
231 constraint2.setActive_time_range(activeTimeRange);
232 assertTrue(constraint1.equals(constraint2));
233 assertEquals(constraint1.hashCode(), constraint2.hashCode());
235 constraint1.setBlacklist(blacklist);
236 assertFalse(constraint1.equals(constraint2));
237 constraint2.setBlacklist(blacklist);
238 assertTrue(constraint1.equals(constraint2));
239 assertEquals(constraint1.hashCode(), constraint2.hashCode());
243 public void testEqualsSameObject() {
244 Constraint constraint = new Constraint();
245 assertTrue(constraint.equals(constraint));
249 public void testEqualsNull() {
250 Constraint constraint = new Constraint();
251 assertFalse(constraint.equals(null));
255 public void testEqualsInstanceOfDiffClass() {
256 Constraint constraint = new Constraint();
257 assertFalse(constraint.equals(""));