blob: a48a75d31faa3cbd824d27fa86a55b597ffab400 [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();
xueliangffe73e42016-09-27 10:38:56 +0900170 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 rpc operation
179 * @return true or false
180 */
181 private boolean verifyWrappedRpcRequestForEthLoopback(String request) {
182 StringBuilder rpc = new StringBuilder();
183 String target = VALID_ETHPORT_LOOPBACK_TCS[currentKey];
184 String[] data = target.split(TEST_COLON);
185 String[] ethId = data[FIRST_PART].split(TEST_HYPHEN);
186
187 rpc.append(TEST_ANGLE_LEFT + TEST_ONU_ETHPORT_LOOPBACK + TEST_SPACE);
188 rpc.append(TEST_VOLT_NE_NAMESPACE + TEST_ANGLE_RIGHT + TEST_NEW_LINE);
189
190 rpc.append(startTag(TEST_PONLINK_ID, false))
191 .append(ethId[FIRST_PART])
192 .append(endTag(TEST_PONLINK_ID))
193 .append(startTag(TEST_ONU_ID, false))
194 .append(ethId[SECOND_PART])
195 .append(endTag(TEST_ONU_ID))
196 .append(startTag(TEST_ETHPORT_ID, false))
197 .append(ethId[THIRD_PART])
198 .append(endTag(TEST_ETHPORT_ID));
199 if (data.length > SECOND_PART) {
200 rpc.append(startTag(TEST_LOOPBACK_MODE, false))
201 .append(data[SECOND_PART])
202 .append(endTag(TEST_LOOPBACK_MODE));
203 }
204 rpc.append(endTag(TEST_ONU_ETHPORT_LOOPBACK));
205
206 String testRequest = rpc.toString();
xueliangffe73e42016-09-27 10:38:56 +0900207 boolean result = request.equals(testRequest);
208 assertTrue("Does not match with generated string", result);
209 return result;
210 }
211
212 /**
213 * Internal listener for device service events.
214 */
215 private class InternalSessionListener implements FujitsuNetconfSessionListenerTest {
216 @Override
217 public boolean verifyEditConfig(String request) {
218 return false;
219 }
220
221 @Override
222 public boolean verifyEditConfig(String target, String mode, String request) {
223 return false;
224 }
225
226 @Override
227 public boolean verifyGet(String filterSchema, String withDefaultsMode) {
228 return false;
229 }
230
231 @Override
232 public String buildGetReply() {
233 return null;
234 }
235
236 @Override
237 public boolean verifyWrappedRpc(String request) {
238 boolean result;
239 boolean reboot = false;
240
241 if (request.contains(TEST_ONU_REBOOT)) {
242 request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
243 assertTrue("Does not contain:" + TEST_ONU_REBOOT_WITH_NAMESPACE,
244 request.contains(TEST_ONU_REBOOT_WITH_NAMESPACE));
245 reboot = true;
246 } else {
247 request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
248 assertTrue("Does not contain:" + TEST_ONU_ETHPORT_LOOPBACK_WITH_NAMESPACE,
249 request.contains(TEST_ONU_ETHPORT_LOOPBACK_WITH_NAMESPACE));
250 }
251
252 if (reboot) {
253 result = verifyWrappedRpcRequestForReboot(request);
254 } else {
255 result = verifyWrappedRpcRequestForEthLoopback(request);
256 }
257
258 assertTrue("XML verification failure", result);
259 return result;
260 }
261
262 @Override
263 public void verifyStartSubscription(String filterSchema) {
264 }
265 }
266
267}