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"; |
xueliang | 714dd2b | 2016-09-13 16:43:32 +0900 | [diff] [blame] | 45 | |
| 46 | private static final Map<Integer, String> INVALID_GET_TCS = new HashMap<Integer, String>() { |
| 47 | { |
| 48 | put(1, "a-b-c"); |
| 49 | put(2, "--1"); |
| 50 | put(3, "s-1"); |
| 51 | put(4, "1-1"); |
| 52 | put(5, "1 A"); |
| 53 | put(6, "1*A"); |
| 54 | } |
| 55 | }; |
| 56 | private static final Map<Integer, String> VALID_GET_TCS = new HashMap<Integer, String>() { |
| 57 | { |
| 58 | put(1, "1"); |
| 59 | put(2, null); |
| 60 | } |
| 61 | }; |
| 62 | private static final Map<Integer, String> INVALID_SET_TCS = new HashMap<Integer, String>() { |
| 63 | { |
| 64 | put(1, "-11:admin-state:enable"); |
| 65 | put(2, "1:admin-state:false"); |
| 66 | put(3, "2-1:onu-discovery-mode:manual"); |
| 67 | put(4, "2:onu-discovery-mode:abcdef"); |
| 68 | put(5, "3:a:onu-discovery-interval:8"); |
| 69 | put(6, "3:onu-discovery-interval:-1"); |
| 70 | put(7, "3:onu-discovery-interval:s1"); |
| 71 | put(8, "4:dba-cycle-time:41"); |
| 72 | put(9, "5*8:mac-age-time:30000"); |
| 73 | put(10, "8:mac-age-time:3699999"); |
| 74 | put(11, "1:lof-threshold:111"); |
| 75 | put(12, "2:los-threshold:22"); |
| 76 | put(13, "3:pm-enable:xyz"); |
| 77 | put(14, "3:abc-enable:xyz"); |
| 78 | } |
| 79 | }; |
| 80 | private static final Map<Integer, String> VALID_SET_TCS = new HashMap<Integer, String>() { |
| 81 | { |
| 82 | put(1, "1:admin-state:disable"); |
| 83 | put(2, "2:onu-discovery-mode:manual"); |
| 84 | put(3, "3:onu-discovery-interval:8"); |
| 85 | put(4, "4:dba-cycle-time:8"); |
| 86 | put(5, "5:mac-age-time:33333"); |
| 87 | put(6, "6:lof-threshold:7"); |
| 88 | put(7, "7:los-threshold:5"); |
| 89 | put(8, "8:pm-enable:true"); |
| 90 | } |
| 91 | }; |
| 92 | private Integer currentKey; |
| 93 | |
| 94 | |
| 95 | @Before |
| 96 | public void setUp() throws Exception { |
| 97 | controller = new FujitsuNetconfControllerMock(); |
| 98 | driverHandler = controller.setUp(listener); |
| 99 | voltConfig = new FujitsuVoltPonLinkConfig(); |
| 100 | voltConfig.setHandler(driverHandler); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Run to verify handling of invalid input for get operation. |
| 105 | */ |
| 106 | @Test |
| 107 | public void testInvalidGetPonLinksInput() throws Exception { |
| 108 | String reply; |
| 109 | String target; |
| 110 | |
| 111 | for (Integer key : INVALID_GET_TCS.keySet()) { |
| 112 | target = INVALID_GET_TCS.get(key); |
| 113 | reply = voltConfig.getPonLinks(target); |
| 114 | assertNull("Incorrect response for " + target, reply); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Run to verify handling of valid input for get operation. |
| 120 | */ |
| 121 | @Test |
| 122 | public void testValidGetPonLinks() throws Exception { |
| 123 | String reply; |
| 124 | String target; |
| 125 | |
| 126 | for (Integer key : VALID_GET_TCS.keySet()) { |
| 127 | target = VALID_GET_TCS.get(key); |
| 128 | currentKey = key; |
| 129 | reply = voltConfig.getPonLinks(target); |
| 130 | assertNotNull("Incorrect response for " + target, reply); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Run to verify handling of invalid input for set operation. |
| 136 | */ |
| 137 | @Test |
| 138 | public void testInvalidSetPonLinkInput() throws Exception { |
| 139 | String target; |
| 140 | boolean result; |
| 141 | |
| 142 | for (Integer key : INVALID_SET_TCS.keySet()) { |
| 143 | target = INVALID_SET_TCS.get(key); |
| 144 | result = voltConfig.setPonLink(target); |
| 145 | assertFalse("Incorrect response for ", result); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Run to verify handling of valid input for set operation. |
| 151 | */ |
| 152 | @Test |
| 153 | public void testValidSetPonLink() throws Exception { |
| 154 | String target; |
| 155 | boolean result; |
| 156 | |
| 157 | for (Integer key : VALID_SET_TCS.keySet()) { |
| 158 | target = VALID_SET_TCS.get(key); |
| 159 | currentKey = key; |
| 160 | result = voltConfig.setPonLink(target); |
| 161 | assertTrue("Incorrect response for ", result); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Verifies XML request string by comparing with generated string. |
| 167 | * |
| 168 | * @param request XML string for set operation |
| 169 | * @return true if XML string matches with generated |
| 170 | */ |
| 171 | private boolean verifyGetRequest(String request) { |
| 172 | StringBuilder rpc = new StringBuilder(); |
| 173 | String target = VALID_GET_TCS.get(currentKey); |
| 174 | |
| 175 | rpc.append(TEST_VOLT_NE_OPEN).append(TEST_VOLT_NE_NAMESPACE); |
| 176 | rpc.append(TEST_ANGLE_RIGHT).append(TEST_NEW_LINE); |
| 177 | rpc.append(startTag(TEST_VOLT_PORTS)); |
| 178 | if (target != null) { |
| 179 | rpc.append(startTag(TEST_GPON_PONLINK_PORTS)); |
| 180 | rpc.append(startTag(TEST_GPON_PONLINK_PORT)); |
| 181 | rpc.append(startTag(TEST_PONLINK_ID, false)); |
| 182 | rpc.append(target); |
| 183 | rpc.append(endTag(TEST_PONLINK_ID)); |
| 184 | rpc.append(endTag(TEST_GPON_PONLINK_PORT)); |
| 185 | rpc.append(endTag(TEST_GPON_PONLINK_PORTS)); |
| 186 | } else { |
| 187 | rpc.append(emptyTag(TEST_GPON_PONLINK_PORTS)); |
| 188 | } |
| 189 | rpc.append(endTag(TEST_VOLT_PORTS)); |
| 190 | rpc.append(TEST_VOLT_NE_CLOSE); |
| 191 | |
| 192 | String testRequest = rpc.toString(); |
| 193 | testRequest = testRequest.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING); |
| 194 | request = request.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING); |
| 195 | boolean result = request.equals(testRequest); |
| 196 | assertTrue("Does not match with generated string", result); |
| 197 | return result; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Verifies XML request string by comparing with generated string. |
| 202 | * |
| 203 | * @param request XML string for set operation |
| 204 | * @return true or false |
| 205 | */ |
| 206 | private boolean verifyEditConfigRequest(String request) { |
| 207 | StringBuilder rpc = new StringBuilder(); |
| 208 | String target = VALID_SET_TCS.get(currentKey); |
| 209 | String[] data = target.split(TEST_COLON); |
| 210 | |
| 211 | rpc.append(TEST_VOLT_NE_OPEN).append(TEST_VOLT_NE_NAMESPACE); |
| 212 | rpc.append(TEST_ANGLE_RIGHT).append(TEST_NEW_LINE); |
| 213 | rpc.append(startTag(TEST_VOLT_PORTS)); |
| 214 | rpc.append(startTag(TEST_GPON_PONLINK_PORTS)); |
| 215 | rpc.append(startTag(TEST_GPON_PONLINK_PORT)); |
| 216 | rpc.append(startTag(TEST_PONLINK_ID, false)); |
| 217 | rpc.append(data[FIRST_PART]); |
| 218 | rpc.append(endTag(TEST_PONLINK_ID)); |
| 219 | rpc.append(startTag(data[SECOND_PART], false)); |
| 220 | rpc.append(data[THIRD_PART]); |
| 221 | rpc.append(endTag(data[SECOND_PART])); |
| 222 | rpc.append(endTag(TEST_GPON_PONLINK_PORT)); |
| 223 | rpc.append(endTag(TEST_GPON_PONLINK_PORTS)); |
| 224 | rpc.append(endTag(TEST_VOLT_PORTS)); |
| 225 | rpc.append(TEST_VOLT_NE_CLOSE); |
| 226 | |
| 227 | String testRequest = rpc.toString(); |
| 228 | testRequest = testRequest.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING); |
| 229 | request = request.replaceAll(TEST_WHITESPACES_REGEX, TEST_EMPTY_STRING); |
| 230 | boolean result = request.equals(testRequest); |
| 231 | assertTrue("Does not match with generated string", result); |
| 232 | return result; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Internal listener for device service events. |
| 237 | */ |
| 238 | private class InternalSessionListenerTest implements FujitsuNetconfSessionListenerTest { |
| 239 | @Override |
| 240 | public boolean verifyEditConfig(String request) { |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 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, |
| 253 | request.contains(TEST_VOLT_NAMESPACE)); |
| 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 | } |