blob: 829604d774b55b73240c768b87423938c20aa467 [file] [log] [blame]
xueliang714dd2b2016-09-13 16:43:32 +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
xueliang714dd2b2016-09-13 16:43:32 +090022import static org.junit.Assert.assertFalse;
23import static org.junit.Assert.assertTrue;
24import static org.junit.Assert.assertNull;
25import static org.junit.Assert.assertNotNull;
26import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*;
27
28/**
29 * Unit tests for methods of FujitsuVoltPonLinkConfig.
30 */
31public class FujitsuVoltPonLinkConfigTest {
32
xueliang714dd2b2016-09-13 16:43:32 +090033 private final FujitsuNetconfSessionListenerTest listener = new InternalSessionListenerTest();
34
35 private static final String TEST_VOLT_PORTS = "volt-ports";
36 private static final String TEST_GPON_PONLINK_PORTS = "gpon-ponlink-ports";
37 private static final String TEST_GPON_PONLINK_PORT = "gpon-ponlink-port";
xueliang714dd2b2016-09-13 16:43:32 +090038
xueliangc6e47e22016-10-20 16:37:24 +090039 private static final String[] INVALID_GET_TCS = {
40 "a-b-c",
41 "--1",
42 "s-1",
43 "1-1",
44 "1 A",
45 "1*A",
xueliang714dd2b2016-09-13 16:43:32 +090046 };
xueliangc6e47e22016-10-20 16:37:24 +090047 private static final String[] VALID_GET_TCS = {
48 "1",
49 null,
xueliang714dd2b2016-09-13 16:43:32 +090050 };
xueliangc6e47e22016-10-20 16:37:24 +090051 private static final String[] INVALID_SET_TCS = {
52 "-11:admin-state:enable",
53 "1:admin-state:false",
54 "2-1:onu-discovery-mode:manual",
55 "2:onu-discovery-mode:abcdef",
56 "3:a:onu-discovery-interval:8",
57 "3:onu-discovery-interval:-1",
58 "3:onu-discovery-interval:s1",
59 "4:dba-cycle-time:41",
60 "5*8:mac-age-time:30000",
61 "8:mac-age-time:3699999",
62 "1:lof-threshold:111",
63 "2:los-threshold:22",
64 "3:pm-enable:xyz",
65 "3:abc-enable:xyz",
xueliang714dd2b2016-09-13 16:43:32 +090066 };
xueliangc6e47e22016-10-20 16:37:24 +090067 private static final String[] VALID_SET_TCS = {
68 "1:admin-state:disable",
69 "2:onu-discovery-mode:manual",
70 "3:onu-discovery-interval:8",
71 "4:dba-cycle-time:8",
72 "5:mac-age-time:33333",
73 "6:lof-threshold:7",
74 "7:los-threshold:5",
75 "8:pm-enable:true",
xueliang714dd2b2016-09-13 16:43:32 +090076 };
77 private Integer currentKey;
xueliangc6e47e22016-10-20 16:37:24 +090078 private FujitsuNetconfControllerMock controller;
79 private FujitsuDriverHandlerAdapter driverHandler;
80 private FujitsuVoltPonLinkConfig voltConfig;
xueliang714dd2b2016-09-13 16:43:32 +090081
82 @Before
83 public void setUp() throws Exception {
84 controller = new FujitsuNetconfControllerMock();
85 driverHandler = controller.setUp(listener);
86 voltConfig = new FujitsuVoltPonLinkConfig();
87 voltConfig.setHandler(driverHandler);
88 }
89
90 /**
91 * Run to verify handling of invalid input for get operation.
92 */
93 @Test
94 public void testInvalidGetPonLinksInput() throws Exception {
95 String reply;
96 String target;
97
xueliangc6e47e22016-10-20 16:37:24 +090098 for (int i = ZERO; i < INVALID_GET_TCS.length; i++) {
99 target = INVALID_GET_TCS[i];
xueliang714dd2b2016-09-13 16:43:32 +0900100 reply = voltConfig.getPonLinks(target);
xueliangc6e47e22016-10-20 16:37:24 +0900101 assertNull("Incorrect response for INVALID_GET_TCS", reply);
xueliang714dd2b2016-09-13 16:43:32 +0900102 }
103 }
104
105 /**
106 * Run to verify handling of valid input for get operation.
107 */
108 @Test
109 public void testValidGetPonLinks() throws Exception {
110 String reply;
111 String target;
112
xueliangc6e47e22016-10-20 16:37:24 +0900113 for (int i = ZERO; i < VALID_GET_TCS.length; i++) {
114 target = VALID_GET_TCS[i];
115 currentKey = i;
xueliang714dd2b2016-09-13 16:43:32 +0900116 reply = voltConfig.getPonLinks(target);
xueliangc6e47e22016-10-20 16:37:24 +0900117 assertNotNull("Incorrect response for VALID_GET_TCS", reply);
xueliang714dd2b2016-09-13 16:43:32 +0900118 }
119 }
120
121 /**
122 * Run to verify handling of invalid input for set operation.
123 */
124 @Test
125 public void testInvalidSetPonLinkInput() throws Exception {
126 String target;
127 boolean result;
128
xueliangc6e47e22016-10-20 16:37:24 +0900129 for (int i = ZERO; i < INVALID_SET_TCS.length; i++) {
130 target = INVALID_SET_TCS[i];
xueliang714dd2b2016-09-13 16:43:32 +0900131 result = voltConfig.setPonLink(target);
xueliangc6e47e22016-10-20 16:37:24 +0900132 assertFalse("Incorrect response for INVALID_SET_TCS", result);
xueliang714dd2b2016-09-13 16:43:32 +0900133 }
134 }
135
136 /**
137 * Run to verify handling of valid input for set operation.
138 */
139 @Test
140 public void testValidSetPonLink() throws Exception {
141 String target;
142 boolean result;
143
xueliangc6e47e22016-10-20 16:37:24 +0900144 for (int i = ZERO; i < VALID_SET_TCS.length; i++) {
145 target = VALID_SET_TCS[i];
146 currentKey = i;
xueliang714dd2b2016-09-13 16:43:32 +0900147 result = voltConfig.setPonLink(target);
xueliangc6e47e22016-10-20 16:37:24 +0900148 assertTrue("Incorrect response for VALID_SET_TCS", result);
xueliang714dd2b2016-09-13 16:43:32 +0900149 }
150 }
151
152 /**
153 * Verifies XML request string by comparing with generated string.
154 *
155 * @param request XML string for set operation
156 * @return true if XML string matches with generated
157 */
158 private boolean verifyGetRequest(String request) {
159 StringBuilder rpc = new StringBuilder();
xueliangc6e47e22016-10-20 16:37:24 +0900160 String target = VALID_GET_TCS[currentKey];
xueliang714dd2b2016-09-13 16:43:32 +0900161
xueliangc6e47e22016-10-20 16:37:24 +0900162 rpc.append(TEST_VOLT_NE_OPEN + TEST_VOLT_NE_NAMESPACE);
163 rpc.append(TEST_ANGLE_RIGHT + TEST_NEW_LINE);
xueliang714dd2b2016-09-13 16:43:32 +0900164 rpc.append(startTag(TEST_VOLT_PORTS));
165 if (target != null) {
xueliangc6e47e22016-10-20 16:37:24 +0900166 rpc.append(startTag(TEST_GPON_PONLINK_PORTS))
167 .append(startTag(TEST_GPON_PONLINK_PORT))
168 .append(startTag(TEST_PONLINK_ID, false))
169 .append(target)
170 .append(endTag(TEST_PONLINK_ID))
171 .append(endTag(TEST_GPON_PONLINK_PORT))
172 .append(endTag(TEST_GPON_PONLINK_PORTS));
xueliang714dd2b2016-09-13 16:43:32 +0900173 } else {
174 rpc.append(emptyTag(TEST_GPON_PONLINK_PORTS));
175 }
xueliangc6e47e22016-10-20 16:37:24 +0900176 rpc.append(endTag(TEST_VOLT_PORTS))
177 .append(TEST_VOLT_NE_CLOSE);
xueliang714dd2b2016-09-13 16:43:32 +0900178
179 String testRequest = rpc.toString();
xueliang714dd2b2016-09-13 16:43:32 +0900180 boolean result = request.equals(testRequest);
181 assertTrue("Does not match with generated string", result);
182 return result;
183 }
184
185 /**
186 * Verifies XML request string by comparing with generated string.
187 *
188 * @param request XML string for set operation
189 * @return true or false
190 */
191 private boolean verifyEditConfigRequest(String request) {
192 StringBuilder rpc = new StringBuilder();
xueliangc6e47e22016-10-20 16:37:24 +0900193 String target = VALID_SET_TCS[currentKey];
xueliang714dd2b2016-09-13 16:43:32 +0900194 String[] data = target.split(TEST_COLON);
195
xueliangc6e47e22016-10-20 16:37:24 +0900196 rpc.append(TEST_VOLT_NE_OPEN + TEST_VOLT_NE_NAMESPACE);
197 rpc.append(TEST_ANGLE_RIGHT + TEST_NEW_LINE);
198 rpc.append(startTag(TEST_VOLT_PORTS))
199 .append(startTag(TEST_GPON_PONLINK_PORTS))
200 .append(startTag(TEST_GPON_PONLINK_PORT))
201 .append(startTag(TEST_PONLINK_ID, false))
202 .append(data[FIRST_PART])
203 .append(endTag(TEST_PONLINK_ID))
204 .append(startTag(data[SECOND_PART], false))
205 .append(data[THIRD_PART])
206 .append(endTag(data[SECOND_PART]))
207 .append(endTag(TEST_GPON_PONLINK_PORT))
208 .append(endTag(TEST_GPON_PONLINK_PORTS))
209 .append(endTag(TEST_VOLT_PORTS))
210 .append(TEST_VOLT_NE_CLOSE);
xueliang714dd2b2016-09-13 16:43:32 +0900211
212 String testRequest = rpc.toString();
xueliang714dd2b2016-09-13 16:43:32 +0900213 boolean result = request.equals(testRequest);
214 assertTrue("Does not match with generated string", result);
215 return result;
216 }
217
218 /**
219 * Internal listener for device service events.
220 */
221 private class InternalSessionListenerTest implements FujitsuNetconfSessionListenerTest {
222 @Override
223 public boolean verifyEditConfig(String request) {
224 return false;
225 }
226
227 @Override
228 public boolean verifyEditConfig(String target, String mode, String request) {
229 boolean result;
230
231 assertTrue("Incorrect target", target.equals(TEST_RUNNING));
232 assertNull("Incorrect mode", mode);
233
234 request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
235 assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
236 request.contains(TEST_VOLT_NAMESPACE));
237 result = verifyEditConfigRequest(request);
238 assertTrue("XML verification failure", result);
239 return result;
240 }
241
242 @Override
243 public boolean verifyGet(String filterSchema, String withDefaultsMode) {
244 boolean result;
245
246 assertTrue("Incorrect withDefaultsMode",
247 withDefaultsMode.equals(TEST_REPORT_ALL));
248
249 filterSchema = filterSchema.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
250 assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
251 filterSchema.contains(TEST_VOLT_NAMESPACE));
252 result = verifyGetRequest(filterSchema);
253 assertTrue("XML verification failure", result);
254 return result;
255 }
256
257 @Override
258 public String buildGetReply() {
259 return null;
260 }
261
262 @Override
263 public boolean verifyWrappedRpc(String request) {
264 return false;
265 }
266
267 @Override
268 public void verifyStartSubscription(String filterSchema) {
269 }
270 }
271
272}