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; |
| 21 | |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 22 | import static org.junit.Assert.assertFalse; |
| 23 | import static org.junit.Assert.assertTrue; |
| 24 | import static org.junit.Assert.assertNull; |
| 25 | import static org.junit.Assert.assertNotNull; |
| 26 | import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*; |
| 27 | |
| 28 | /** |
| 29 | * Unit tests for methods of FujitsuVoltPonLinkConfig. |
| 30 | */ |
| 31 | public class FujitsuVoltPonLinkConfigTest { |
| 32 | |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 33 | 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"; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 38 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 39 | 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", |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 46 | }; |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 47 | private static final String[] VALID_GET_TCS = { |
| 48 | "1", |
| 49 | null, |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 50 | }; |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 51 | 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", |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 66 | }; |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 67 | 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", |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 76 | }; |
| 77 | private Integer currentKey; |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 78 | private FujitsuNetconfControllerMock controller; |
| 79 | private FujitsuDriverHandlerAdapter driverHandler; |
| 80 | private FujitsuVoltPonLinkConfig voltConfig; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 81 | |
| 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 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 98 | for (int i = ZERO; i < INVALID_GET_TCS.length; i++) { |
| 99 | target = INVALID_GET_TCS[i]; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 100 | reply = voltConfig.getPonLinks(target); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 101 | assertNull("Incorrect response for INVALID_GET_TCS", reply); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 102 | } |
| 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 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 113 | for (int i = ZERO; i < VALID_GET_TCS.length; i++) { |
| 114 | target = VALID_GET_TCS[i]; |
| 115 | currentKey = i; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 116 | reply = voltConfig.getPonLinks(target); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 117 | assertNotNull("Incorrect response for VALID_GET_TCS", reply); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 118 | } |
| 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 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 129 | for (int i = ZERO; i < INVALID_SET_TCS.length; i++) { |
| 130 | target = INVALID_SET_TCS[i]; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 131 | result = voltConfig.setPonLink(target); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 132 | assertFalse("Incorrect response for INVALID_SET_TCS", result); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 133 | } |
| 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 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 144 | for (int i = ZERO; i < VALID_SET_TCS.length; i++) { |
| 145 | target = VALID_SET_TCS[i]; |
| 146 | currentKey = i; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 147 | result = voltConfig.setPonLink(target); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 148 | assertTrue("Incorrect response for VALID_SET_TCS", result); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 149 | } |
| 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(); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 160 | String target = VALID_GET_TCS[currentKey]; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 161 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 162 | rpc.append(TEST_VOLT_NE_OPEN + TEST_VOLT_NE_NAMESPACE); |
| 163 | rpc.append(TEST_ANGLE_RIGHT + TEST_NEW_LINE); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 164 | rpc.append(startTag(TEST_VOLT_PORTS)); |
| 165 | if (target != null) { |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 166 | 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)); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 173 | } else { |
| 174 | rpc.append(emptyTag(TEST_GPON_PONLINK_PORTS)); |
| 175 | } |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 176 | rpc.append(endTag(TEST_VOLT_PORTS)) |
| 177 | .append(TEST_VOLT_NE_CLOSE); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 178 | |
| 179 | String testRequest = rpc.toString(); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 180 | 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(); |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 193 | String target = VALID_SET_TCS[currentKey]; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 194 | String[] data = target.split(TEST_COLON); |
| 195 | |
xueliang | c6e47e2 | 2016-10-20 16:37:24 +0900 | [diff] [blame] | 196 | 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); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 211 | |
| 212 | String testRequest = rpc.toString(); |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 213 | 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 | } |