blob: 0377070e6d3bb6265d227ef195cd867bdb61f4e0 [file] [log] [blame]
xueliangadec4dc2016-10-03 16:57:04 +09001/*
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
17package org.onosproject.drivers.fujitsu;
18
19import org.junit.Before;
20import org.junit.Test;
21
22import static org.junit.Assert.assertTrue;
23import static org.junit.Assert.assertNull;
24import static org.junit.Assert.assertNotNull;
25import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtilityMock.*;
26
27
28/**
29 * Unit tests for methods of FujitsuVoltFwdlConfig.
30 */
31public 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();
146 String regex = TEST_WHITESPACES_REGEX;
147 int index = rpc.indexOf(regex);
148 while (index >= ZERO) {
149 testRequest = rpc.replace(index, index + regex.length(), TEST_EMPTY_STRING).toString();
150 request = request.replaceAll(regex, TEST_EMPTY_STRING);
151 }
152 boolean result = request.equals(testRequest);
153 assertTrue("Does not match with generated string", result);
154 return result;
155 }
156
157 /**
158 * Internal listener for device service events.
159 */
160 private class InternalSessionListener implements FujitsuNetconfSessionListenerTest {
161 @Override
162 public boolean verifyEditConfig(String request) {
163 return false;
164 }
165
166 @Override
167 public boolean verifyEditConfig(String target, String mode, String request) {
168 return false;
169 }
170
171 @Override
172 public boolean verifyGet(String filterSchema, String withDefaultsMode) {
173 return false;
174 }
175
176 @Override
177 public String buildGetReply() {
178 return null;
179 }
180
181 @Override
182 public boolean verifyWrappedRpc(String request) {
183 boolean result;
184
185 request = request.replaceAll(TEST_DUPLICATE_SPACES_REGEX, TEST_SPACE);
186 assertTrue("Does not contain:" + TEST_ONDEMAND_FWDL_WITH_NAMESPACE,
187 request.contains(TEST_ONDEMAND_FWDL_WITH_NAMESPACE));
188
189 result = verifyWrappedRpcRequest(request);
190 assertTrue("XML verification failure", result);
191 return result;
192 }
193
194 @Override
195 public void verifyStartSubscription(String filterSchema) {
196 }
197 }
198
199}