blob: b6bf0de54e7a2d68b2dbd8c63c46c7166e9f0f1d [file] [log] [blame]
xueliangffe73e42016-09-27 10:38:56 +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.junit.Before;
20import org.junit.Test;
21
22import static org.junit.Assert.assertTrue;
23import static org.junit.Assert.assertNull;
24import static org.junit.Assert.assertNotNull;
25import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*;
26
27/**
28 * Unit tests for methods of FujitsuVoltOnuOperConfig.
29 */
30public class FujitsuVoltOnuOperConfigTest {
31
32 private final FujitsuNetconfSessionListenerTest listener = new InternalSessionListener();
33
34 private static final String TEST_ONU_REBOOT = "onu-reboot";
35 private static final String TEST_ONU_ETHPORT_LOOPBACK = "onu-ethport-loopback";
36 private static final String TEST_ETHPORT_ID = "ethport-id";
37 private static final String TEST_LOOPBACK_MODE = "mode";
38
39 private static final String TEST_ONU_REBOOT_WITH_NAMESPACE = TEST_ANGLE_LEFT +
40 TEST_ONU_REBOOT + TEST_SPACE + TEST_VOLT_NE_NAMESPACE;
41 private static final String TEST_ONU_ETHPORT_LOOPBACK_WITH_NAMESPACE =
42 TEST_ANGLE_LEFT + TEST_ONU_ETHPORT_LOOPBACK + TEST_SPACE +
43 TEST_VOLT_NE_NAMESPACE;
44
45 private static final String[] INVALID_REBOOT_TCS = {
46 "xy1-b",
47 "--1",
48 "s-1",
49 "16-1-1",
50 "&AA-1",
51 "-1-1",
52 };
53 private static final String[] VALID_REBOOT_TCS = {
54 "1-2",
55 "16-11",
56 };
57 private static final String[] INVALID_ETHPORT_LOOPBACK_TCS = {
58 "-11-3--11",
59 "1-CCa",
60 "abc-1",
61 "^1-1-3",
62 "1-2:23-1",
63 "1:33:2",
64 "2-2-2:false",
65 };
66 private static final String[] VALID_ETHPORT_LOOPBACK_TCS = {
67 "8-1-1",
68 "1-11-3:release",
69 "2-2-2:operate",
70 };
71 private Integer currentKey;
72 private FujitsuNetconfControllerMock controller;
73 private FujitsuDriverHandlerAdapter driverHandler;
74 private FujitsuVoltOnuOperConfig voltConfig;
75
76 @Before
77 public void setUp() throws Exception {
78 controller = new FujitsuNetconfControllerMock();
79 driverHandler = controller.setUp(listener);
80 voltConfig = new FujitsuVoltOnuOperConfig();
81 voltConfig.setHandler(driverHandler);
82 }
83
84 /**
85 * Run to verify handling of invalid input for rpc operation.
86 */
87 @Test
88 public void testInvalidRebootOnuInput() throws Exception {
89 String reply;
90 String target;
91
92 for (int i = ZERO; i < INVALID_REBOOT_TCS.length; i++) {
93 target = INVALID_REBOOT_TCS[i];
94 reply = voltConfig.rebootOnu(target);
95 assertNull("Incorrect response for INVALID_REBOOT_TCS", reply);
96 }
97 }
98
99 /**
100 * Run to verify handling of valid input for rpc operation.
101 */
102 @Test
103 public void testValidRebootOnu() throws Exception {
104 String reply;
105 String target;
106
107 for (int i = ZERO; i < VALID_REBOOT_TCS.length; i++) {
108 target = VALID_REBOOT_TCS[i];
109 currentKey = i;
110 reply = voltConfig.rebootOnu(target);
111 assertNotNull("Incorrect response for VALID_REBOOT_TCS", reply);
112 }
113 }
114
115 /**
116 * Run to verify handling of invalid input for rpc operation.
117 */
118 @Test
119 public void testInvalidEthLoopbackOnuInput() throws Exception {
120 String target;
121 String reply;
122
123 for (int i = ZERO; i < INVALID_ETHPORT_LOOPBACK_TCS.length; i++) {
124 target = INVALID_ETHPORT_LOOPBACK_TCS[i];
125 reply = voltConfig.loopbackEthOnu(target);
126 assertNull("Incorrect response for INVALID_ETHPORT_LOOPBACK_TCS", reply);
127 }
128 }
129
130 /**
131 * Run to verify handling of valid input for rpc operation.
132 */
133 @Test
134 public void testValidLoopbackEthOnu() throws Exception {
135 String target;
136 String reply;
137
138 for (int i = ZERO; i < VALID_ETHPORT_LOOPBACK_TCS.length; i++) {
139 target = VALID_ETHPORT_LOOPBACK_TCS[i];
140 currentKey = i;
141 reply = voltConfig.loopbackEthOnu(target);
142 assertNotNull("Incorrect response for VALID_ETHPORT_LOOPBACK_TCS", reply);
143 }
144 }
145
146 /**
147 * Verifies XML request string by comparing with generated string.
148 *
149 * @param request XML string for rpc operation
150 * @return true or false
151 */
152 private boolean verifyWrappedRpcRequestForReboot(String request) {
153 StringBuilder rpc = new StringBuilder();
154 String target = VALID_REBOOT_TCS[currentKey];
155 String[] data = target.split(TEST_COLON);
156 String[] onuId = data[FIRST_PART].split(TEST_HYPHEN);
157
158 rpc.append(TEST_ANGLE_LEFT + TEST_ONU_REBOOT + TEST_SPACE);
159 rpc.append(TEST_VOLT_NE_NAMESPACE + TEST_ANGLE_RIGHT + TEST_NEW_LINE);
160
161 rpc.append(startTag(TEST_PONLINK_ID, false))
162 .append(onuId[FIRST_PART])
163 .append(endTag(TEST_PONLINK_ID))
164 .append(startTag(TEST_ONU_ID, false))
165 .append(onuId[SECOND_PART])
166 .append(endTag(TEST_ONU_ID))
167 .append(endTag(TEST_ONU_REBOOT));
168
169 String testRequest = rpc.toString();
170 String regex = TEST_WHITESPACES_REGEX;
171 int index = rpc.indexOf(regex);
172 while (index >= ZERO) {
173 testRequest = rpc.replace(index, index + regex.length(), TEST_EMPTY_STRING).toString();
174 request = request.replaceAll(regex, TEST_EMPTY_STRING);
175 }
176 boolean result = request.equals(testRequest);
177 assertTrue("Does not match with generated string", result);
178 return result;
179 }
180
181 /**
182 * Verifies XML request string by comparing with generated string.
183 *
184 * @param request XML string for rpc operation
185 * @return true or false
186 */
187 private boolean verifyWrappedRpcRequestForEthLoopback(String request) {
188 StringBuilder rpc = new StringBuilder();
189 String target = VALID_ETHPORT_LOOPBACK_TCS[currentKey];
190 String[] data = target.split(TEST_COLON);
191 String[] ethId = data[FIRST_PART].split(TEST_HYPHEN);
192
193 rpc.append(TEST_ANGLE_LEFT + TEST_ONU_ETHPORT_LOOPBACK + TEST_SPACE);
194 rpc.append(TEST_VOLT_NE_NAMESPACE + TEST_ANGLE_RIGHT + TEST_NEW_LINE);
195
196 rpc.append(startTag(TEST_PONLINK_ID, false))
197 .append(ethId[FIRST_PART])
198 .append(endTag(TEST_PONLINK_ID))
199 .append(startTag(TEST_ONU_ID, false))
200 .append(ethId[SECOND_PART])
201 .append(endTag(TEST_ONU_ID))
202 .append(startTag(TEST_ETHPORT_ID, false))
203 .append(ethId[THIRD_PART])
204 .append(endTag(TEST_ETHPORT_ID));
205 if (data.length > SECOND_PART) {
206 rpc.append(startTag(TEST_LOOPBACK_MODE, false))
207 .append(data[SECOND_PART])
208 .append(endTag(TEST_LOOPBACK_MODE));
209 }
210 rpc.append(endTag(TEST_ONU_ETHPORT_LOOPBACK));
211
212 String testRequest = rpc.toString();
213 String regex = TEST_WHITESPACES_REGEX;
214 int index = rpc.indexOf(regex);
215 while (index >= ZERO) {
216 testRequest = rpc.replace(index, index + regex.length(), TEST_EMPTY_STRING).toString();
217 request = request.replaceAll(regex, TEST_EMPTY_STRING);
218 }
219 boolean result = request.equals(testRequest);
220 assertTrue("Does not match with generated string", result);
221 return result;
222 }
223
224 /**
225 * Internal listener for device service events.
226 */
227 private class InternalSessionListener implements FujitsuNetconfSessionListenerTest {
228 @Override
229 public boolean verifyEditConfig(String request) {
230 return false;
231 }
232
233 @Override
234 public boolean verifyEditConfig(String target, String mode, String request) {
235 return false;
236 }
237
238 @Override
239 public boolean verifyGet(String filterSchema, String withDefaultsMode) {
240 return false;
241 }
242
243 @Override
244 public String buildGetReply() {
245 return null;
246 }
247
248 @Override
249 public boolean verifyWrappedRpc(String request) {
250 boolean result;
251 boolean reboot = false;
252
253 if (request.contains(TEST_ONU_REBOOT)) {
254 request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
255 assertTrue("Does not contain:" + TEST_ONU_REBOOT_WITH_NAMESPACE,
256 request.contains(TEST_ONU_REBOOT_WITH_NAMESPACE));
257 reboot = true;
258 } else {
259 request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
260 assertTrue("Does not contain:" + TEST_ONU_ETHPORT_LOOPBACK_WITH_NAMESPACE,
261 request.contains(TEST_ONU_ETHPORT_LOOPBACK_WITH_NAMESPACE));
262 }
263
264 if (reboot) {
265 result = verifyWrappedRpcRequestForReboot(request);
266 } else {
267 result = verifyWrappedRpcRequestForEthLoopback(request);
268 }
269
270 assertTrue("XML verification failure", result);
271 return result;
272 }
273
274 @Override
275 public void verifyStartSubscription(String filterSchema) {
276 }
277 }
278
279}