xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.fujitsu; |
| 18 | |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
Andrei Mihaescu | ac542ca | 2017-03-26 21:36:25 +0300 | [diff] [blame^] | 21 | import org.onosproject.netconf.TargetConfig; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 22 | |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 23 | import static org.junit.Assert.assertFalse; |
| 24 | import static org.junit.Assert.assertTrue; |
| 25 | import static org.junit.Assert.assertNull; |
| 26 | import static org.junit.Assert.assertNotNull; |
| 27 | import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*; |
Andrei Mihaescu | ac542ca | 2017-03-26 21:36:25 +0300 | [diff] [blame^] | 28 | import static org.onosproject.netconf.TargetConfig.RUNNING; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Unit tests for methods of FujitsuVoltPonLinkConfig. |
| 32 | */ |
| 33 | public class FujitsuVoltPonLinkConfigTest { |
| 34 | |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 35 | private final FujitsuNetconfSessionListenerTest listener = new InternalSessionListenerTest(); |
| 36 | |
| 37 | private static final String TEST_VOLT_PORTS = "volt-ports"; |
| 38 | private static final String TEST_GPON_PONLINK_PORTS = "gpon-ponlink-ports"; |
| 39 | private static final String TEST_GPON_PONLINK_PORT = "gpon-ponlink-port"; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 40 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 41 | private static final String[] INVALID_GET_TCS = { |
| 42 | "a-b-c", |
| 43 | "--1", |
| 44 | "s-1", |
| 45 | "1-1", |
| 46 | "1 A", |
| 47 | "1*A", |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 48 | }; |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 49 | private static final String[] VALID_GET_TCS = { |
| 50 | "1", |
| 51 | null, |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 52 | }; |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 53 | private static final String[] INVALID_SET_TCS = { |
| 54 | "-11:admin-state:enable", |
| 55 | "1:admin-state:false", |
| 56 | "2-1:onu-discovery-mode:manual", |
| 57 | "2:onu-discovery-mode:abcdef", |
| 58 | "3:a:onu-discovery-interval:8", |
| 59 | "3:onu-discovery-interval:-1", |
| 60 | "3:onu-discovery-interval:s1", |
| 61 | "4:dba-cycle-time:41", |
| 62 | "5*8:mac-age-time:30000", |
| 63 | "8:mac-age-time:3699999", |
| 64 | "1:lof-threshold:111", |
| 65 | "2:los-threshold:22", |
| 66 | "3:pm-enable:xyz", |
| 67 | "3:abc-enable:xyz", |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 68 | }; |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 69 | private static final String[] VALID_SET_TCS = { |
| 70 | "1:admin-state:disable", |
| 71 | "2:onu-discovery-mode:manual", |
| 72 | "3:onu-discovery-interval:8", |
| 73 | "4:dba-cycle-time:8", |
| 74 | "5:mac-age-time:33333", |
| 75 | "6:lof-threshold:7", |
| 76 | "7:los-threshold:5", |
| 77 | "8:pm-enable:true", |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 78 | }; |
| 79 | private Integer currentKey; |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 80 | private FujitsuNetconfControllerMock controller; |
| 81 | private FujitsuDriverHandlerAdapter driverHandler; |
| 82 | private FujitsuVoltPonLinkConfig voltConfig; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 83 | |
| 84 | @Before |
| 85 | public void setUp() throws Exception { |
| 86 | controller = new FujitsuNetconfControllerMock(); |
| 87 | driverHandler = controller.setUp(listener); |
| 88 | voltConfig = new FujitsuVoltPonLinkConfig(); |
| 89 | voltConfig.setHandler(driverHandler); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Run to verify handling of invalid input for get operation. |
| 94 | */ |
| 95 | @Test |
| 96 | public void testInvalidGetPonLinksInput() throws Exception { |
| 97 | String reply; |
| 98 | String target; |
| 99 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 100 | for (int i = ZERO; i < INVALID_GET_TCS.length; i++) { |
| 101 | target = INVALID_GET_TCS[i]; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 102 | reply = voltConfig.getPonLinks(target); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 103 | assertNull("Incorrect response for INVALID_GET_TCS", reply); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Run to verify handling of valid input for get operation. |
| 109 | */ |
| 110 | @Test |
| 111 | public void testValidGetPonLinks() throws Exception { |
| 112 | String reply; |
| 113 | String target; |
| 114 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 115 | for (int i = ZERO; i < VALID_GET_TCS.length; i++) { |
| 116 | target = VALID_GET_TCS[i]; |
| 117 | currentKey = i; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 118 | reply = voltConfig.getPonLinks(target); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 119 | assertNotNull("Incorrect response for VALID_GET_TCS", reply); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Run to verify handling of invalid input for set operation. |
| 125 | */ |
| 126 | @Test |
| 127 | public void testInvalidSetPonLinkInput() throws Exception { |
| 128 | String target; |
| 129 | boolean result; |
| 130 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 131 | for (int i = ZERO; i < INVALID_SET_TCS.length; i++) { |
| 132 | target = INVALID_SET_TCS[i]; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 133 | result = voltConfig.setPonLink(target); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 134 | assertFalse("Incorrect response for INVALID_SET_TCS", result); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Run to verify handling of valid input for set operation. |
| 140 | */ |
| 141 | @Test |
| 142 | public void testValidSetPonLink() throws Exception { |
| 143 | String target; |
| 144 | boolean result; |
| 145 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 146 | for (int i = ZERO; i < VALID_SET_TCS.length; i++) { |
| 147 | target = VALID_SET_TCS[i]; |
| 148 | currentKey = i; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 149 | result = voltConfig.setPonLink(target); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 150 | assertTrue("Incorrect response for VALID_SET_TCS", result); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Verifies XML request string by comparing with generated string. |
| 156 | * |
| 157 | * @param request XML string for set operation |
| 158 | * @return true if XML string matches with generated |
| 159 | */ |
| 160 | private boolean verifyGetRequest(String request) { |
| 161 | StringBuilder rpc = new StringBuilder(); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 162 | String target = VALID_GET_TCS[currentKey]; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 163 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 164 | rpc.append(TEST_VOLT_NE_OPEN + TEST_VOLT_NE_NAMESPACE); |
| 165 | rpc.append(TEST_ANGLE_RIGHT + TEST_NEW_LINE); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 166 | rpc.append(startTag(TEST_VOLT_PORTS)); |
| 167 | if (target != null) { |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 168 | rpc.append(startTag(TEST_GPON_PONLINK_PORTS)) |
| 169 | .append(startTag(TEST_GPON_PONLINK_PORT)) |
| 170 | .append(startTag(TEST_PONLINK_ID, false)) |
| 171 | .append(target) |
| 172 | .append(endTag(TEST_PONLINK_ID)) |
| 173 | .append(endTag(TEST_GPON_PONLINK_PORT)) |
| 174 | .append(endTag(TEST_GPON_PONLINK_PORTS)); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 175 | } else { |
| 176 | rpc.append(emptyTag(TEST_GPON_PONLINK_PORTS)); |
| 177 | } |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 178 | rpc.append(endTag(TEST_VOLT_PORTS)) |
| 179 | .append(TEST_VOLT_NE_CLOSE); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 180 | |
| 181 | String testRequest = rpc.toString(); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 182 | boolean result = request.equals(testRequest); |
| 183 | assertTrue("Does not match with generated string", result); |
| 184 | return result; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Verifies XML request string by comparing with generated string. |
| 189 | * |
| 190 | * @param request XML string for set operation |
| 191 | * @return true or false |
| 192 | */ |
| 193 | private boolean verifyEditConfigRequest(String request) { |
| 194 | StringBuilder rpc = new StringBuilder(); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 195 | String target = VALID_SET_TCS[currentKey]; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 196 | String[] data = target.split(TEST_COLON); |
| 197 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 198 | rpc.append(TEST_VOLT_NE_OPEN + TEST_VOLT_NE_NAMESPACE); |
| 199 | rpc.append(TEST_ANGLE_RIGHT + TEST_NEW_LINE); |
| 200 | rpc.append(startTag(TEST_VOLT_PORTS)) |
| 201 | .append(startTag(TEST_GPON_PONLINK_PORTS)) |
| 202 | .append(startTag(TEST_GPON_PONLINK_PORT)) |
| 203 | .append(startTag(TEST_PONLINK_ID, false)) |
| 204 | .append(data[FIRST_PART]) |
| 205 | .append(endTag(TEST_PONLINK_ID)) |
| 206 | .append(startTag(data[SECOND_PART], false)) |
| 207 | .append(data[THIRD_PART]) |
| 208 | .append(endTag(data[SECOND_PART])) |
| 209 | .append(endTag(TEST_GPON_PONLINK_PORT)) |
| 210 | .append(endTag(TEST_GPON_PONLINK_PORTS)) |
| 211 | .append(endTag(TEST_VOLT_PORTS)) |
| 212 | .append(TEST_VOLT_NE_CLOSE); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 213 | |
| 214 | String testRequest = rpc.toString(); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 215 | boolean result = request.equals(testRequest); |
| 216 | assertTrue("Does not match with generated string", result); |
| 217 | return result; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Internal listener for device service events. |
| 222 | */ |
| 223 | private class InternalSessionListenerTest implements FujitsuNetconfSessionListenerTest { |
| 224 | @Override |
| 225 | public boolean verifyEditConfig(String request) { |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | @Override |
Andrei Mihaescu | ac542ca | 2017-03-26 21:36:25 +0300 | [diff] [blame^] | 230 | public boolean verifyEditConfig(TargetConfig target, String mode, String request) { |
| 231 | boolean result; |
| 232 | |
| 233 | assertTrue("Incorrect target", target.equals(RUNNING)); |
| 234 | assertNull("Incorrect mode", mode); |
| 235 | |
| 236 | request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE); |
| 237 | assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE, |
| 238 | request.contains(TEST_VOLT_NAMESPACE)); |
| 239 | result = verifyEditConfigRequest(request); |
| 240 | assertTrue("XML verification failure", result); |
| 241 | return result; |
| 242 | } |
| 243 | |
| 244 | @Override |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 245 | public boolean verifyEditConfig(String target, String mode, String request) { |
| 246 | boolean result; |
| 247 | |
| 248 | assertTrue("Incorrect target", target.equals(TEST_RUNNING)); |
| 249 | assertNull("Incorrect mode", mode); |
| 250 | |
| 251 | request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE); |
| 252 | assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE, |
Andrei Mihaescu | ac542ca | 2017-03-26 21:36:25 +0300 | [diff] [blame^] | 253 | request.contains(TEST_VOLT_NAMESPACE)); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 254 | result = verifyEditConfigRequest(request); |
| 255 | assertTrue("XML verification failure", result); |
| 256 | return result; |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public boolean verifyGet(String filterSchema, String withDefaultsMode) { |
| 261 | boolean result; |
| 262 | |
| 263 | assertTrue("Incorrect withDefaultsMode", |
| 264 | withDefaultsMode.equals(TEST_REPORT_ALL)); |
| 265 | |
| 266 | filterSchema = filterSchema.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE); |
| 267 | assertTrue("Does not contain:" + TEST_VOLT_NAMESPACE, |
| 268 | filterSchema.contains(TEST_VOLT_NAMESPACE)); |
| 269 | result = verifyGetRequest(filterSchema); |
| 270 | assertTrue("XML verification failure", result); |
| 271 | return result; |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public String buildGetReply() { |
| 276 | return null; |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public boolean verifyWrappedRpc(String request) { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public void verifyStartSubscription(String filterSchema) { |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | } |