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