xueliang | adec4dc | 2016-10-03 16:57:04 +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.assertTrue; |
| 23 | import static org.junit.Assert.assertNull; |
| 24 | import static org.junit.Assert.assertNotNull; |
| 25 | import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*; |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * Unit tests for methods of FujitsuVoltFwdlConfig. |
| 30 | */ |
| 31 | public class FujitsuVoltFwdlConfigTest { |
| 32 | |
| 33 | private final FujitsuNetconfSessionListenerTest listener = new InternalSessionListener(); |
| 34 | |
| 35 | private static final String TEST_ONDEMAND_FIRMWARE_UPGRADE = "ondemand-firmware-upgrade"; |
| 36 | private static final String TEST_PARTICIPANT_LIST = "participant-list"; |
| 37 | private static final String TEST_MEMBER = "member"; |
| 38 | private static final String TEST_IMAGE_NAME = "image-name"; |
| 39 | private static final String TEST_REBOOT_MODE = "reboot-mode"; |
| 40 | private static final String TEST_COMMA = ","; |
| 41 | |
| 42 | private static final String TEST_ONDEMAND_FWDL_WITH_NAMESPACE = |
| 43 | TEST_ANGLE_LEFT + TEST_ONDEMAND_FIRMWARE_UPGRADE + |
| 44 | TEST_SPACE + TEST_VOLT_NE_NAMESPACE; |
| 45 | |
| 46 | private static final String[] INVALID_ONDEMAND_FWDL_TCS = { |
| 47 | "xy1-b:a1-1", |
| 48 | "AAA:1-2,--1", |
| 49 | "CcC:s-1,2-2,3-2:auto", |
| 50 | "xYZam:1-1,2-2,3-3:false", |
| 51 | "JKml901:16-1-1,2-16:a-2", |
| 52 | "abc:&AA-1,11-2:auto", |
| 53 | "abc:xyz:-1-1", |
| 54 | "@bcf11:xyz:auto", |
| 55 | "FJ123:1-1&5-2:auto", |
| 56 | }; |
| 57 | private static final String[] VALID_ONDEMAND_FWDL_TCS = { |
| 58 | "Fujitsu123:1-2", |
| 59 | "abcDE90f:16-11,1-1,17-3:auto", |
| 60 | "fujitsuONU12:1-1,2-2,3-3,4-4,5-5,6-6,7-7", |
| 61 | }; |
| 62 | private Integer currentKey; |
| 63 | private FujitsuNetconfControllerMock controller; |
| 64 | private FujitsuDriverHandlerAdapter driverHandler; |
| 65 | private FujitsuVoltFwdlConfig voltConfig; |
| 66 | |
| 67 | @Before |
| 68 | public void setUp() throws Exception { |
| 69 | controller = new FujitsuNetconfControllerMock(); |
| 70 | driverHandler = controller.setUp(listener); |
| 71 | voltConfig = new FujitsuVoltFwdlConfig(); |
| 72 | voltConfig.setHandler(driverHandler); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Run to verify handling of invalid input for rpc operation. |
| 77 | */ |
| 78 | @Test |
| 79 | public void testInvalidOndemandFirmwareUpgradeInput() throws Exception { |
| 80 | String reply; |
| 81 | String target; |
| 82 | |
| 83 | for (int i = ZERO; i < INVALID_ONDEMAND_FWDL_TCS.length; i++) { |
| 84 | target = INVALID_ONDEMAND_FWDL_TCS[i]; |
| 85 | reply = voltConfig.upgradeFirmwareOndemand(target); |
| 86 | assertNull("Incorrect response for INVALID_ONDEMAND_FWDL_TCS", reply); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Run to verify handling of valid input for rpc operation. |
| 92 | */ |
| 93 | @Test |
| 94 | public void testValidOndemandFirmwareUpgrade() throws Exception { |
| 95 | String reply; |
| 96 | String target; |
| 97 | |
| 98 | for (int i = ZERO; i < VALID_ONDEMAND_FWDL_TCS.length; i++) { |
| 99 | target = VALID_ONDEMAND_FWDL_TCS[i]; |
| 100 | currentKey = i; |
| 101 | reply = voltConfig.upgradeFirmwareOndemand(target); |
| 102 | assertNotNull("Incorrect response for VALID_ONDEMAND_FWDL_TCS", reply); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Verifies XML request string by comparing with generated string. |
| 108 | * |
| 109 | * @param request XML string for rpc operation |
| 110 | * @return true or false |
| 111 | */ |
| 112 | private boolean verifyWrappedRpcRequest(String request) { |
| 113 | StringBuilder rpc = new StringBuilder(); |
| 114 | String target = VALID_ONDEMAND_FWDL_TCS[currentKey]; |
| 115 | String[] data = target.split(TEST_COLON); |
| 116 | String[] onuList = data[SECOND_PART].split(TEST_COMMA); |
| 117 | int count; |
| 118 | |
| 119 | rpc.append(TEST_ANGLE_LEFT + TEST_ONDEMAND_FIRMWARE_UPGRADE + TEST_SPACE); |
| 120 | rpc.append(TEST_VOLT_NE_NAMESPACE + TEST_ANGLE_RIGHT + TEST_NEW_LINE); |
| 121 | |
| 122 | rpc.append(startTag(TEST_PARTICIPANT_LIST)); |
| 123 | for (count = ZERO; count < onuList.length; count++) { |
| 124 | String[] onuId = onuList[count].split(TEST_HYPHEN); |
| 125 | rpc.append(startTag(TEST_MEMBER)) |
| 126 | .append(startTag(TEST_PONLINK_ID)) |
| 127 | .append(onuId[FIRST_PART]) |
| 128 | .append(endTag(TEST_PONLINK_ID)) |
| 129 | .append(startTag(TEST_ONU_ID)) |
| 130 | .append(onuId[SECOND_PART]) |
| 131 | .append(endTag(TEST_ONU_ID)) |
| 132 | .append(endTag(TEST_MEMBER)); |
| 133 | } |
| 134 | rpc.append(endTag(TEST_PARTICIPANT_LIST)) |
| 135 | .append(startTag(TEST_IMAGE_NAME)) |
| 136 | .append(data[FIRST_PART]) |
| 137 | .append(endTag(TEST_IMAGE_NAME)); |
| 138 | if (data.length == THREE) { |
| 139 | rpc.append(startTag(TEST_REBOOT_MODE)) |
| 140 | .append(data[THIRD_PART]) |
| 141 | .append(endTag(TEST_REBOOT_MODE)); |
| 142 | } |
| 143 | rpc.append(endTag(TEST_ONDEMAND_FIRMWARE_UPGRADE)); |
| 144 | |
| 145 | String testRequest = rpc.toString(); |
xueliang | adec4dc | 2016-10-03 16:57:04 +0900 | [diff] [blame] | 146 | boolean result = request.equals(testRequest); |
| 147 | assertTrue("Does not match with generated string", result); |
| 148 | return result; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Internal listener for device service events. |
| 153 | */ |
| 154 | private class InternalSessionListener implements FujitsuNetconfSessionListenerTest { |
| 155 | @Override |
| 156 | public boolean verifyEditConfig(String request) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public boolean verifyEditConfig(String target, String mode, String request) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public boolean verifyGet(String filterSchema, String withDefaultsMode) { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public String buildGetReply() { |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public boolean verifyWrappedRpc(String request) { |
| 177 | boolean result; |
| 178 | |
| 179 | request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE); |
| 180 | assertTrue("Does not contain:" + TEST_ONDEMAND_FWDL_WITH_NAMESPACE, |
| 181 | request.contains(TEST_ONDEMAND_FWDL_WITH_NAMESPACE)); |
| 182 | |
| 183 | result = verifyWrappedRpcRequest(request); |
| 184 | assertTrue("XML verification failure", result); |
| 185 | return result; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void verifyStartSubscription(String filterSchema) { |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | } |