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