blob: 9105a813e537bef837b80985d65c99f07c32468c [file] [log] [blame]
xueliang0e946fc2016-12-08 15:00:49 +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;
Andrei Mihaescuac542ca2017-03-26 21:36:25 +030021import org.onosproject.netconf.TargetConfig;
xueliang0e946fc2016-12-08 15:00:49 +090022
23import static org.junit.Assert.assertFalse;
24import static org.junit.Assert.assertTrue;
25import static org.junit.Assert.assertNull;
26import static org.junit.Assert.assertNotNull;
27import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*;
Andrei Mihaescuac542ca2017-03-26 21:36:25 +030028import static org.onosproject.netconf.TargetConfig.RUNNING;
xueliang0e946fc2016-12-08 15:00:49 +090029
30/**
31 * Unit tests for methods of FujitsuVoltPonLinkConfig.
32 */
33public class FujitsuVoltNniLinkConfigTest {
34
35 private final FujitsuNetconfSessionListenerTest listener = new InternalSessionListenerTest();
36
37 private static final String TEST_VOLT_PORTS = "volt-ports";
38 private static final String TEST_ETH_NNILINK_PORTS = "eth-nnilink-ports";
39 private static final String TEST_ETH_NNILINK_PORT = "eth-nnilink-port";
40 private static final String TEST_NNILINK_ID = "nnilink-id";
41
42 private static final String[] INVALID_GET_TCS = {
43 "a-b-c",
44 "--1",
45 "s-1",
46 "1-1",
47 "1 A",
48 "1*A",
49 "-1",
50 };
51 private static final String[] VALID_GET_TCS = {
52 "1",
53 null,
54 };
55 private static final String[] INVALID_SET_TCS = {
56 "1:loopback-enable:true:1",
57 "-11:loopback-enable:false",
58 "2:loopback:true",
59 "3:loopback-enable:invalid",
60 "1:loopback-uni:8",
61 "-1:loopback-nni:nni",
62 ":loopback-nni:nni",
63 };
64 private static final String[] VALID_SET_TCS = {
65 "1:loopback-enable:true",
66 "2:loopback-enable:false",
67 };
68 private Integer currentKey;
69 private FujitsuNetconfControllerMock controller;
70 private FujitsuDriverHandlerAdapter driverHandler;
71 private FujitsuVoltNniLinkConfig voltConfig;
72
73 @Before
74 public void setUp() throws Exception {
75 controller = new FujitsuNetconfControllerMock();
76 driverHandler = controller.setUp(listener);
77 voltConfig = new FujitsuVoltNniLinkConfig();
78 voltConfig.setHandler(driverHandler);
79 }
80
81 /**
82 * Run to verify handling of invalid input for get operation.
83 */
84 @Test
85 public void testInvalidGetNniLinksInput() throws Exception {
86 String reply;
87 String target;
88
89 for (int i = ZERO; i < INVALID_GET_TCS.length; i++) {
90 target = INVALID_GET_TCS[i];
91 reply = voltConfig.getNniLinks(target);
92 assertNull("Incorrect response for INVALID_GET_TCS", reply);
93 }
94 }
95
96 /**
97 * Run to verify handling of valid input for get operation.
98 */
99 @Test
100 public void testValidGetNniLinksInput() throws Exception {
101 String reply;
102 String target;
103
104 for (int i = ZERO; i < VALID_GET_TCS.length; i++) {
105 target = VALID_GET_TCS[i];
106 currentKey = i;
107 reply = voltConfig.getNniLinks(target);
108 assertNotNull("Incorrect response for VALID_GET_TCS", reply);
109 }
110 }
111
112 /**
113 * Run to verify handling of invalid input for set operation.
114 */
115 @Test
116 public void testInvalidSetNniLinkInput() throws Exception {
117 String target;
118 boolean result;
119
120 for (int i = ZERO; i < INVALID_SET_TCS.length; i++) {
121 target = INVALID_SET_TCS[i];
122 result = voltConfig.setNniLink(target);
123 assertFalse("Incorrect response for INVALID_SET_TCS", result);
124 }
125 }
126
127 /**
128 * Run to verify handling of valid input for set operation.
129 */
130 @Test
131 public void testValidSetNniLinkInput() throws Exception {
132 String target;
133 boolean result;
134
135 for (int i = ZERO; i < VALID_SET_TCS.length; i++) {
136 target = VALID_SET_TCS[i];
137 currentKey = i;
138 result = voltConfig.setNniLink(target);
139 assertTrue("Incorrect response for VALID_SET_TCS", result);
140 }
141 }
142
143 /**
144 * Verifies XML request string by comparing with generated string.
145 *
146 * @param request XML string for set operation
147 * @return true if XML string matches with generated
148 */
149 private boolean verifyGetRequest(String request) {
150 StringBuilder rpc = new StringBuilder();
151 String target = VALID_GET_TCS[currentKey];
152
153 rpc.append(TEST_VOLT_NE_OPEN + TEST_VOLT_NE_NAMESPACE);
154 rpc.append(TEST_ANGLE_RIGHT + TEST_NEW_LINE);
155 rpc.append(startTag(TEST_VOLT_PORTS));
156 if (target != null) {
157 rpc.append(startTag(TEST_ETH_NNILINK_PORTS))
158 .append(startTag(TEST_ETH_NNILINK_PORT))
159 .append(startTag(TEST_NNILINK_ID, false))
160 .append(target)
161 .append(endTag(TEST_NNILINK_ID))
162 .append(endTag(TEST_ETH_NNILINK_PORT))
163 .append(endTag(TEST_ETH_NNILINK_PORTS));
164 } else {
165 rpc.append(emptyTag(TEST_ETH_NNILINK_PORTS));
166 }
167 rpc.append(endTag(TEST_VOLT_PORTS))
168 .append(TEST_VOLT_NE_CLOSE);
169
170 String testRequest = rpc.toString();
171 boolean result = request.equals(testRequest);
172 assertTrue("Does not match with generated string", result);
173 return result;
174 }
175
176 /**
177 * Verifies XML request string by comparing with generated string.
178 *
179 * @param request XML string for set operation
180 * @return true or false
181 */
182 private boolean verifyEditConfigRequest(String request) {
183 StringBuilder rpc = new StringBuilder();
184 String target = VALID_SET_TCS[currentKey];
185 String[] data = target.split(TEST_COLON);
186
187 rpc.append(TEST_VOLT_NE_OPEN + TEST_VOLT_NE_NAMESPACE)
188 .append(TEST_ANGLE_RIGHT + TEST_NEW_LINE)
189 .append(startTag(TEST_VOLT_PORTS))
190 .append(startTag(TEST_ETH_NNILINK_PORTS))
191 .append(startTag(TEST_ETH_NNILINK_PORT))
192 .append(startTag(TEST_NNILINK_ID, false))
193 .append(data[FIRST_PART])
194 .append(endTag(TEST_NNILINK_ID))
195 .append(startTag(data[SECOND_PART], false))
196 .append(data[THIRD_PART])
197 .append(endTag(data[SECOND_PART]))
198 .append(endTag(TEST_ETH_NNILINK_PORT))
199 .append(endTag(TEST_ETH_NNILINK_PORTS))
200 .append(endTag(TEST_VOLT_PORTS))
201 .append(TEST_VOLT_NE_CLOSE);
202
203 String testRequest = rpc.toString();
204 boolean result = request.equals(testRequest);
205 assertTrue("Does not match with generated string", result);
206 return result;
207 }
208
209 /**
210 * Internal listener for device service events.
211 */
212 private class InternalSessionListenerTest implements FujitsuNetconfSessionListenerTest {
213 @Override
214 public boolean verifyEditConfig(String request) {
215 return false;
216 }
217
218 @Override
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300219 public boolean verifyEditConfig(TargetConfig target, String mode, String request) {
xueliang0e946fc2016-12-08 15:00:49 +0900220 boolean result;
221
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300222 assertTrue("Incorrect target", target.equals(RUNNING));
xueliang0e946fc2016-12-08 15:00:49 +0900223 assertNull("Incorrect mode", mode);
224
225 request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
226 assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
227 request.contains(TEST_VOLT_NAMESPACE));
228 result = verifyEditConfigRequest(request);
229 assertTrue("XML verification failure", result);
230 return result;
231 }
232
233 @Override
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300234 public boolean verifyEditConfig(String target, String mode, String request) {
235 boolean result;
236
237 assertTrue("Incorrect target", target.equals(TEST_RUNNING));
238 assertNull("Incorrect mode", mode);
239
240 request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
241 assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
242 request.contains(TEST_VOLT_NAMESPACE));
243 result = verifyEditConfigRequest(request);
244 assertTrue("XML verification failure", result);
245 return result;
246 }
247
248 @Override
xueliang0e946fc2016-12-08 15:00:49 +0900249 public boolean verifyGet(String filterSchema, String withDefaultsMode) {
250 boolean result;
251
252 assertTrue("Incorrect withDefaultsMode",
253 withDefaultsMode.equals(TEST_REPORT_ALL));
254
255 filterSchema = filterSchema.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
256 assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE,
257 filterSchema.contains(TEST_VOLT_NAMESPACE));
258 result = verifyGetRequest(filterSchema);
259 assertTrue("XML verification failure", result);
260 return result;
261 }
262
263 @Override
264 public String buildGetReply() {
265 return null;
266 }
267
268 @Override
269 public boolean verifyWrappedRpc(String request) {
270 return false;
271 }
272
273 @Override
274 public void verifyStartSubscription(String filterSchema) {
275 }
276 }
277
278}