blob: 2cef69c672983a9a84f0689ff7e0077e9d310ada [file] [log] [blame]
xueliang54525f52016-09-29 17:28:35 +09001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.drivers.fujitsu;
18
19import org.apache.commons.io.IOUtils;
20import org.junit.Before;
21import org.junit.Test;
22
23import java.io.IOException;
24import java.nio.charset.StandardCharsets;
25
26import java.io.InputStream;
27
28import static org.junit.Assert.assertTrue;
29import static org.junit.Assert.assertFalse;
30import static org.junit.Assert.assertNull;
31import static org.junit.Assert.fail;
32import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*;
33
34
35/**
36 * Unit tests for methods of FujitsuVoltAlertConfig.
37 */
38public class FujitsuVoltAlertConfigTest {
39
40 private final FujitsuNetconfSessionListenerTest listener = new InternalSessionListener();
41
42 private static final String TEST_VOLT_ALERTS = "volt-alerts";
43 private static final String TEST_ALERT_FILTER = "alert-filter";
44 private static final String TEST_NOTIFY_ALERT = "notify-alert";
45
46 private static final String TEST_NOTIFY_ALERT_WITH_NAMESPACE =
47 TEST_ANGLE_LEFT + TEST_NOTIFY_ALERT + TEST_SPACE +
48 TEST_VOLT_NE_NAMESPACE;
49
50 private static final String NOTIFY_ALERT_FILE = "/notifyalert.xml";
51
52 private static final String[] INVALID_SET_TCS = {
53 ":abc",
54 "@critical",
55 "1234",
56 };
57 private static final String[] VALID_SET_TCS = {
58 "minor",
59 "critical",
60 "none",
61 "info",
62 "major",
63 };
64 private static final String[] VERIFY_NOTIFY_ALERT_FILE_TCS = {
65 "notify-alert",
66 "alert-seqnum",
67 "alert-type",
68 "alert-clear",
69 "severity",
70 "resource-id",
71 "ponlink-id",
72 "alert-time",
73 "date",
74 "time",
75 };
76 private Integer currentKey;
77 private FujitsuNetconfControllerMock controller;
78 private FujitsuDriverHandlerAdapter driverHandler;
79 private FujitsuVoltAlertConfig voltConfig;
80
81 @Before
82 public void setUp() throws Exception {
83 controller = new FujitsuNetconfControllerMock();
84 driverHandler = controller.setUp(listener);
85 voltConfig = new FujitsuVoltAlertConfig();
86 voltConfig.setHandler(driverHandler);
87 }
88
89 /**
90 * Run to verify handling of subscription.
91 */
92 @Test
93 public void testSubscribe() throws Exception {
94 assertTrue("Incorrect response", voltConfig.subscribe(null));
95 assertFalse("Incorrect response", voltConfig.subscribe("false"));
96 assertTrue("Incorrect response", voltConfig.subscribe("disable"));
97 }
98
99 /**
100 * Run to verify handling of subscription.
101 */
102 @Test
103 public void testGetAlertFilter() throws Exception {
104 voltConfig.getAlertFilter();
105 }
106
107 /**
108 * Run to verify handling of invalid input for set operation.
109 */
110 @Test
111 public void testInvalidSetAlertFilterInput() throws Exception {
112 String target;
113 boolean result;
114
115 for (int i = ZERO; i < INVALID_SET_TCS.length; i++) {
116 target = INVALID_SET_TCS[i];
117 result = voltConfig.setAlertFilter(target);
118 assertFalse("Incorrect response for ", result);
119 }
120 }
121
122 /**
123 * Run to verify handling of valid input for set operation.
124 */
125 @Test
126 public void testValidSetAlertFilter() throws Exception {
127 String target;
128 boolean result;
129
130 for (int i = ZERO; i < VALID_SET_TCS.length; i++) {
131 target = VALID_SET_TCS[i];
132 currentKey = i;
133 result = voltConfig.setAlertFilter(target);
134 assertTrue("Incorrect response for ", result);
135 }
136 }
137
138 /**
139 * Run to verify sample notify-alert components.
140 */
141 @Test
142 public void testNotifyAlert() throws Exception {
143 boolean result;
144 result = verifyNotifyAlert();
145 assertTrue("Incorrect response for ", result);
146 }
147
148 /**
149 * Verifies XML request string by comparing with generated string.
150 *
151 * @param request XML string for set operation
152 * @return true if XML string matches with generated
153 */
154 private boolean verifyGetRequest(String request) {
155 StringBuilder rpc = new StringBuilder();
156 rpc.append(TEST_VOLT_NE_OPEN + TEST_VOLT_NE_NAMESPACE);
157 rpc.append(TEST_ANGLE_RIGHT + TEST_NEW_LINE);
158 rpc.append(startTag(TEST_VOLT_ALERTS))
159 .append(emptyTag(TEST_ALERT_FILTER))
160 .append(endTag(TEST_VOLT_ALERTS))
161 .append(TEST_VOLT_NE_CLOSE);
162
163 String testRequest = rpc.toString();
164 String regex = TEST_WHITESPACES_REGEX;
165 int index = rpc.indexOf(regex);
166 while (index >= ZERO) {
167 testRequest = rpc.replace(index, index + regex.length(), TEST_EMPTY_STRING).toString();
168 request = request.replaceAll(regex, TEST_EMPTY_STRING);
169 }
170 boolean result = request.equals(testRequest);
171 assertTrue("Does not match with generated string", result);
172 return result;
173 }
174
175 /**
176 * Verifies XML request string by comparing with generated string.
177 *
178 * @param request XML string for set operation
179 * @return true or false
180 */
181 private boolean verifyEditConfigRequest(String request) {
182 StringBuilder rpc = new StringBuilder();
183 String target = VALID_SET_TCS[currentKey];
184
185 rpc.append(TEST_VOLT_NE_OPEN + TEST_VOLT_NE_NAMESPACE);
186 rpc.append(TEST_ANGLE_RIGHT + TEST_NEW_LINE);
187 rpc.append(startTag(TEST_VOLT_ALERTS))
188 .append(startTag(TEST_ALERT_FILTER, false))
189 .append(target)
190 .append(endTag(TEST_ALERT_FILTER))
191 .append(endTag(TEST_VOLT_ALERTS))
192 .append(TEST_VOLT_NE_CLOSE);
193
194 String testRequest = rpc.toString();
195 String regex = TEST_WHITESPACES_REGEX;
196 int index = rpc.indexOf(regex);
197 while (index >= ZERO) {
198 testRequest = rpc.replace(index, index + regex.length(), TEST_EMPTY_STRING).toString();
199 request = request.replaceAll(regex, TEST_EMPTY_STRING);
200 }
201 boolean result = request.equals(testRequest);
202 assertTrue("Does not match with generated string", result);
203 return result;
204 }
205
206 /**
207 * Verifies notify-alert XML.
208 */
209 private boolean verifyNotifyAlert() {
210 String testRequest;
211 String target;
212
213 try {
214 InputStream fileStream = getClass().getResourceAsStream(
215 NOTIFY_ALERT_FILE);
216 testRequest = IOUtils.toString(fileStream, StandardCharsets.UTF_8);
217 testRequest = testRequest.substring(testRequest.indexOf(
218 NOTIFY_ALERT_FILE) + NOTIFY_ALERT_FILE.length());
219 } catch (IOException e) {
220 fail("IOException while reading: " + NOTIFY_ALERT_FILE);
221 return false;
222 }
223
224 for (int i = ZERO; i < VERIFY_NOTIFY_ALERT_FILE_TCS.length; i++) {
225 target = VERIFY_NOTIFY_ALERT_FILE_TCS[i];
226 int index = testRequest.indexOf(target);
227 if (index < ZERO) {
228 return false;
229 }
230 }
231 return true;
232 }
233
234 /**
235 * Internal listener for device service events.
236 */
237 private class InternalSessionListener implements FujitsuNetconfSessionListenerTest {
238 @Override
239 public boolean verifyEditConfig(String request) {
240 return false;
241 }
242
243 @Override
244 public boolean verifyEditConfig(String target, String mode, String request) {
245 boolean result;
246
247 assertTrue("Incorrect target", target.equals(TEST_RUNNING));
248 assertNull("Incorrect mode", mode);
249
250 request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
251 assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
252 request.contains(TEST_VOLT_NAMESPACE));
253
254 result = verifyEditConfigRequest(request);
255 assertTrue("XML verification failure", result);
256 return result;
257 }
258
259 @Override
260 public boolean verifyGet(String filterSchema, String withDefaultsMode) {
261 boolean result;
262
263 assertTrue("Incorrect withDefaultsMode", withDefaultsMode.equals(TEST_REPORT_ALL));
264 filterSchema = filterSchema.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
265 assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
266 filterSchema.contains(TEST_VOLT_NAMESPACE));
267
268 result = verifyGetRequest(filterSchema);
269 assertTrue("XML verification failure", result);
270 return result;
271 }
272
273 @Override
274 public String buildGetReply() {
275 return null;
276 }
277
278 @Override
279 public boolean verifyWrappedRpc(String request) {
280 return false;
281 }
282
283 @Override
284 public void verifyStartSubscription(String filterSchema) {
285
286 filterSchema = filterSchema.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
287 assertTrue("Does not contain:" + TEST_NOTIFY_ALERT_WITH_NAMESPACE,
288 filterSchema.contains(TEST_NOTIFY_ALERT_WITH_NAMESPACE));
289
290 StringBuilder rpc = new StringBuilder();
291 rpc.append(TEST_ANGLE_LEFT + TEST_NOTIFY_ALERT + TEST_SPACE);
292 rpc.append(TEST_VOLT_NE_NAMESPACE + TEST_SLASH + TEST_ANGLE_RIGHT);
293
294 String testRequest = rpc.toString();
295 String regex = TEST_WHITESPACES_REGEX;
296 int index = rpc.indexOf(regex);
297 while (index >= ZERO) {
298 testRequest = rpc.replace(index, index + regex.length(), TEST_EMPTY_STRING).toString();
299 filterSchema = filterSchema.replaceAll(regex, TEST_EMPTY_STRING);
300 }
301 assertTrue("Does not match with generated string",
302 filterSchema.equals(testRequest));
303 }
304 }
305
306}