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