blob: 156aacc6b3660159b80d0e6763c3be8ae942b52f [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();
xueliang54525f52016-09-29 17:28:35 +0900164 boolean result = request.equals(testRequest);
165 assertTrue("Does not match with generated string", result);
166 return result;
167 }
168
169 /**
170 * Verifies XML request string by comparing with generated string.
171 *
172 * @param request XML string for set operation
173 * @return true or false
174 */
175 private boolean verifyEditConfigRequest(String request) {
176 StringBuilder rpc = new StringBuilder();
177 String target = VALID_SET_TCS[currentKey];
178
179 rpc.append(TEST_VOLT_NE_OPEN + TEST_VOLT_NE_NAMESPACE);
180 rpc.append(TEST_ANGLE_RIGHT + TEST_NEW_LINE);
181 rpc.append(startTag(TEST_VOLT_ALERTS))
182 .append(startTag(TEST_ALERT_FILTER, false))
183 .append(target)
184 .append(endTag(TEST_ALERT_FILTER))
185 .append(endTag(TEST_VOLT_ALERTS))
186 .append(TEST_VOLT_NE_CLOSE);
187
188 String testRequest = rpc.toString();
xueliang54525f52016-09-29 17:28:35 +0900189 boolean result = request.equals(testRequest);
190 assertTrue("Does not match with generated string", result);
191 return result;
192 }
193
194 /**
195 * Verifies notify-alert XML.
196 */
197 private boolean verifyNotifyAlert() {
198 String testRequest;
199 String target;
200
201 try {
202 InputStream fileStream = getClass().getResourceAsStream(
203 NOTIFY_ALERT_FILE);
204 testRequest = IOUtils.toString(fileStream, StandardCharsets.UTF_8);
205 testRequest = testRequest.substring(testRequest.indexOf(
206 NOTIFY_ALERT_FILE) + NOTIFY_ALERT_FILE.length());
207 } catch (IOException e) {
208 fail("IOException while reading: " + NOTIFY_ALERT_FILE);
209 return false;
210 }
211
212 for (int i = ZERO; i < VERIFY_NOTIFY_ALERT_FILE_TCS.length; i++) {
213 target = VERIFY_NOTIFY_ALERT_FILE_TCS[i];
214 int index = testRequest.indexOf(target);
215 if (index < ZERO) {
216 return false;
217 }
218 }
219 return true;
220 }
221
222 /**
223 * Internal listener for device service events.
224 */
225 private class InternalSessionListener implements FujitsuNetconfSessionListenerTest {
226 @Override
227 public boolean verifyEditConfig(String request) {
228 return false;
229 }
230
231 @Override
232 public boolean verifyEditConfig(String target, String mode, String request) {
233 boolean result;
234
235 assertTrue("Incorrect target", target.equals(TEST_RUNNING));
236 assertNull("Incorrect mode", mode);
237
238 request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
239 assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
240 request.contains(TEST_VOLT_NAMESPACE));
241
242 result = verifyEditConfigRequest(request);
243 assertTrue("XML verification failure", result);
244 return result;
245 }
246
247 @Override
248 public boolean verifyGet(String filterSchema, String withDefaultsMode) {
249 boolean result;
250
251 assertTrue("Incorrect withDefaultsMode", withDefaultsMode.equals(TEST_REPORT_ALL));
252 filterSchema = filterSchema.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
253 assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
254 filterSchema.contains(TEST_VOLT_NAMESPACE));
255
256 result = verifyGetRequest(filterSchema);
257 assertTrue("XML verification failure", result);
258 return result;
259 }
260
261 @Override
262 public String buildGetReply() {
263 return null;
264 }
265
266 @Override
267 public boolean verifyWrappedRpc(String request) {
268 return false;
269 }
270
271 @Override
272 public void verifyStartSubscription(String filterSchema) {
273
274 filterSchema = filterSchema.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
275 assertTrue("Does not contain:" + TEST_NOTIFY_ALERT_WITH_NAMESPACE,
276 filterSchema.contains(TEST_NOTIFY_ALERT_WITH_NAMESPACE));
277
278 StringBuilder rpc = new StringBuilder();
279 rpc.append(TEST_ANGLE_LEFT + TEST_NOTIFY_ALERT + TEST_SPACE);
280 rpc.append(TEST_VOLT_NE_NAMESPACE + TEST_SLASH + TEST_ANGLE_RIGHT);
281
282 String testRequest = rpc.toString();
xueliangc6e47e22016-10-20 16:37:24 +0900283 boolean result = filterSchema.equals(testRequest);
284 assertTrue("Does not match with generated string", result);
xueliang54525f52016-09-29 17:28:35 +0900285 }
286 }
287
288}