Committed by
Gerrit Code Review
Added Constant file, to resolve hardcoded value from NetconfDeviceProviderTest.
Change-Id: I66e9c761b2b40e5a237ceecac4b82d035c9fb7f1
Showing
2 changed files
with
80 additions
and
67 deletions
... | @@ -19,10 +19,10 @@ import static org.easymock.EasyMock.expect; | ... | @@ -19,10 +19,10 @@ import static org.easymock.EasyMock.expect; |
19 | import static org.easymock.EasyMock.replay; | 19 | import static org.easymock.EasyMock.replay; |
20 | import static org.junit.Assert.assertFalse; | 20 | import static org.junit.Assert.assertFalse; |
21 | import static org.onlab.util.Tools.delay; | 21 | import static org.onlab.util.Tools.delay; |
22 | +import static org.onosproject.provider.netconf.device.impl.NetconfDeviceProviderTestConstant.*; | ||
22 | import static org.slf4j.LoggerFactory.getLogger; | 23 | import static org.slf4j.LoggerFactory.getLogger; |
23 | 24 | ||
24 | import java.io.IOException; | 25 | import java.io.IOException; |
25 | -import java.net.SocketTimeoutException; | ||
26 | import java.net.URI; | 26 | import java.net.URI; |
27 | import java.net.URISyntaxException; | 27 | import java.net.URISyntaxException; |
28 | import java.util.Collection; | 28 | import java.util.Collection; |
... | @@ -57,11 +57,8 @@ import com.tailf.jnc.JNCException; | ... | @@ -57,11 +57,8 @@ import com.tailf.jnc.JNCException; |
57 | 57 | ||
58 | /** | 58 | /** |
59 | * Test Case to Validate Netconf Device Provider. | 59 | * Test Case to Validate Netconf Device Provider. |
60 | - * | ||
61 | */ | 60 | */ |
62 | public class NetconfDeviceProviderTest { | 61 | public class NetconfDeviceProviderTest { |
63 | - // private NetconfDevice device; | ||
64 | - | ||
65 | TestDeviceCreator create; | 62 | TestDeviceCreator create; |
66 | 63 | ||
67 | private final Logger log = getLogger(NetconfDeviceProviderTest.class); | 64 | private final Logger log = getLogger(NetconfDeviceProviderTest.class); |
... | @@ -70,12 +67,7 @@ public class NetconfDeviceProviderTest { | ... | @@ -70,12 +67,7 @@ public class NetconfDeviceProviderTest { |
70 | 67 | ||
71 | private DeviceProviderService providerService; | 68 | private DeviceProviderService providerService; |
72 | 69 | ||
73 | - private static final int EVENTINTERVAL = 5; | 70 | + private static final DeviceId DID1 = DeviceId.deviceId(DEVICE_ID); |
74 | - | ||
75 | - private static final String SCHEME = "netconf"; | ||
76 | - | ||
77 | - private static final DeviceId DID1 = DeviceId | ||
78 | - .deviceId("of:0000000000000001"); | ||
79 | 71 | ||
80 | private final NetconfDeviceProvider provider = new NetconfDeviceProvider(); | 72 | private final NetconfDeviceProvider provider = new NetconfDeviceProvider(); |
81 | private final TestDeviceRegistry registry = new TestDeviceRegistry(); | 73 | private final TestDeviceRegistry registry = new TestDeviceRegistry(); |
... | @@ -90,22 +82,10 @@ public class NetconfDeviceProviderTest { | ... | @@ -90,22 +82,10 @@ public class NetconfDeviceProviderTest { |
90 | } | 82 | } |
91 | 83 | ||
92 | @SuppressWarnings("unchecked") | 84 | @SuppressWarnings("unchecked") |
93 | - private Dictionary<String, String> getDictionaryMock(ComponentContext componentContext) { | ||
94 | - Dictionary<String, String> dictionary = EasyMock | ||
95 | - .createMock(Dictionary.class); | ||
96 | - expect(dictionary.get("devConfigs")) | ||
97 | - .andReturn("cisco:cisco@10.18.11.14:22:active," | ||
98 | - + "sanjay:b33rb3lly@10.18.24.122:2022:inactive"); | ||
99 | - replay(dictionary); | ||
100 | - expect(componentContext.getProperties()).andReturn(dictionary); | ||
101 | - return dictionary; | ||
102 | - } | ||
103 | - | ||
104 | - @SuppressWarnings("unchecked") | ||
105 | private Dictionary<String, String> getDictionaryMockWithoutValues(ComponentContext componentContext) { | 85 | private Dictionary<String, String> getDictionaryMockWithoutValues(ComponentContext componentContext) { |
106 | Dictionary<String, String> dictionary = EasyMock | 86 | Dictionary<String, String> dictionary = EasyMock |
107 | .createMock(Dictionary.class); | 87 | .createMock(Dictionary.class); |
108 | - expect(dictionary.get("devConfigs")).andReturn(""); | 88 | + expect(dictionary.get(DEV_CONFIG)).andReturn(NULL); |
109 | replay(dictionary); | 89 | replay(dictionary); |
110 | expect(componentContext.getProperties()).andReturn(dictionary); | 90 | expect(componentContext.getProperties()).andReturn(dictionary); |
111 | return dictionary; | 91 | return dictionary; |
... | @@ -115,7 +95,7 @@ public class NetconfDeviceProviderTest { | ... | @@ -115,7 +95,7 @@ public class NetconfDeviceProviderTest { |
115 | private Dictionary<String, String> getDictionaryMockWithDeviceEntryNull(ComponentContext componentContext) { | 95 | private Dictionary<String, String> getDictionaryMockWithDeviceEntryNull(ComponentContext componentContext) { |
116 | Dictionary<String, String> dictionary = EasyMock | 96 | Dictionary<String, String> dictionary = EasyMock |
117 | .createMock(Dictionary.class); | 97 | .createMock(Dictionary.class); |
118 | - expect(dictionary.get("devConfigs")).andReturn("null,null"); | 98 | + expect(dictionary.get(DEV_CONFIG)).andReturn(NULL_NULL); |
119 | replay(dictionary); | 99 | replay(dictionary); |
120 | expect(componentContext.getProperties()).andReturn(dictionary); | 100 | expect(componentContext.getProperties()).andReturn(dictionary); |
121 | return dictionary; | 101 | return dictionary; |
... | @@ -125,8 +105,8 @@ public class NetconfDeviceProviderTest { | ... | @@ -125,8 +105,8 @@ public class NetconfDeviceProviderTest { |
125 | private Dictionary<String, String> getDictionaryMockDeviceEntryNumberFomatEx(ComponentContext componentContext) { | 105 | private Dictionary<String, String> getDictionaryMockDeviceEntryNumberFomatEx(ComponentContext componentContext) { |
126 | Dictionary<String, String> dictionary = EasyMock | 106 | Dictionary<String, String> dictionary = EasyMock |
127 | .createMock(Dictionary.class); | 107 | .createMock(Dictionary.class); |
128 | - expect(dictionary.get("devConfigs")) | 108 | + expect(dictionary.get(DEV_CONFIG)) |
129 | - .andReturn("cisco:cisco@10.18.11.14:cisco:active") | 109 | + .andReturn(CONFIG_WITH_INVALID_ENTRY_NUMBER) |
130 | .andThrow(new NumberFormatException()); | 110 | .andThrow(new NumberFormatException()); |
131 | replay(dictionary); | 111 | replay(dictionary); |
132 | expect(componentContext.getProperties()).andReturn(dictionary); | 112 | expect(componentContext.getProperties()).andReturn(dictionary); |
... | @@ -137,8 +117,7 @@ public class NetconfDeviceProviderTest { | ... | @@ -137,8 +117,7 @@ public class NetconfDeviceProviderTest { |
137 | private Dictionary<String, String> getDictionaryMockWithoutUsernameAndPassword(ComponentContext componentContext) { | 117 | private Dictionary<String, String> getDictionaryMockWithoutUsernameAndPassword(ComponentContext componentContext) { |
138 | Dictionary<String, String> dictionary = EasyMock | 118 | Dictionary<String, String> dictionary = EasyMock |
139 | .createMock(Dictionary.class); | 119 | .createMock(Dictionary.class); |
140 | - expect(dictionary.get("devConfigs")) | 120 | + expect(dictionary.get(DEV_CONFIG)).andReturn(CONFIG_WITH_NULL_ENTRY); |
141 | - .andReturn("null:null@null:0:active"); | ||
142 | replay(dictionary); | 121 | replay(dictionary); |
143 | expect(componentContext.getProperties()).andReturn(dictionary); | 122 | expect(componentContext.getProperties()).andReturn(dictionary); |
144 | return dictionary; | 123 | return dictionary; |
... | @@ -148,9 +127,8 @@ public class NetconfDeviceProviderTest { | ... | @@ -148,9 +127,8 @@ public class NetconfDeviceProviderTest { |
148 | private Dictionary<String, String> getDictionaryMockWithDifferentDeviceState(ComponentContext componentContext) { | 127 | private Dictionary<String, String> getDictionaryMockWithDifferentDeviceState(ComponentContext componentContext) { |
149 | Dictionary<String, String> dictionary = EasyMock | 128 | Dictionary<String, String> dictionary = EasyMock |
150 | .createMock(Dictionary.class); | 129 | .createMock(Dictionary.class); |
151 | - expect(dictionary.get("devConfigs")) | 130 | + expect(dictionary.get(DEV_CONFIG)) |
152 | - .andReturn("cisco:cisco@10.18.11.14:22:active,cisco:cisco@10.18.11.18:22:inactive," | 131 | + .andReturn(CONFIG_WITH_DIFFERENT_DEVICE_STATE); |
153 | - + "cisco:cisco@10.18.11.14:22:invalid,cisco:cisco@10.18.11.14:22:null"); | ||
154 | replay(dictionary); | 132 | replay(dictionary); |
155 | expect(componentContext.getProperties()).andReturn(dictionary); | 133 | expect(componentContext.getProperties()).andReturn(dictionary); |
156 | return dictionary; | 134 | return dictionary; |
... | @@ -160,8 +138,8 @@ public class NetconfDeviceProviderTest { | ... | @@ -160,8 +138,8 @@ public class NetconfDeviceProviderTest { |
160 | private Dictionary<String, String> getDictionaryMockDeviceWithArrayOutOFBoundEx(ComponentContext componentContext) { | 138 | private Dictionary<String, String> getDictionaryMockDeviceWithArrayOutOFBoundEx(ComponentContext componentContext) { |
161 | Dictionary<String, String> dictionary = EasyMock | 139 | Dictionary<String, String> dictionary = EasyMock |
162 | .createMock(Dictionary.class); | 140 | .createMock(Dictionary.class); |
163 | - expect(dictionary.get("devConfigs")) | 141 | + expect(dictionary.get(DEV_CONFIG)) |
164 | - .andReturn("@10.18.11.14:22:active") | 142 | + .andReturn(CONFIG_WITH_ARRAY_OUT_OF_BOUNDEX) |
165 | .andThrow(new ArrayIndexOutOfBoundsException()); | 143 | .andThrow(new ArrayIndexOutOfBoundsException()); |
166 | replay(dictionary); | 144 | replay(dictionary); |
167 | expect(componentContext.getProperties()).andReturn(dictionary); | 145 | expect(componentContext.getProperties()).andReturn(dictionary); |
... | @@ -172,21 +150,23 @@ public class NetconfDeviceProviderTest { | ... | @@ -172,21 +150,23 @@ public class NetconfDeviceProviderTest { |
172 | private Dictionary<String, String> getDictionaryMockDeviceEntryForDeactivate(ComponentContext componentContext) { | 150 | private Dictionary<String, String> getDictionaryMockDeviceEntryForDeactivate(ComponentContext componentContext) { |
173 | Dictionary<String, String> dictionary = EasyMock | 151 | Dictionary<String, String> dictionary = EasyMock |
174 | .createMock(Dictionary.class); | 152 | .createMock(Dictionary.class); |
175 | - expect(dictionary.get("devConfigs")) | 153 | + expect(dictionary.get(DEV_CONFIG)) |
176 | - .andReturn("netconf:cisco@10.18.11.14:22:active") | 154 | + .andReturn(CONFIG_ENTRY_FOR_DEACTIVATE) |
177 | .andThrow(new ArrayIndexOutOfBoundsException()); | 155 | .andThrow(new ArrayIndexOutOfBoundsException()); |
178 | replay(dictionary); | 156 | replay(dictionary); |
179 | expect(componentContext.getProperties()).andReturn(dictionary); | 157 | expect(componentContext.getProperties()).andReturn(dictionary); |
180 | return dictionary; | 158 | return dictionary; |
181 | } | 159 | } |
182 | 160 | ||
183 | - @Ignore("Test fails if the hard coded host actually exists.") | 161 | + @Ignore |
184 | - @Test(expected = SocketTimeoutException.class) | 162 | + @Test(expected = IOException.class) |
185 | - public void testSSHAuthentication() throws JNCException, IOException { | 163 | + public void testSSHAuthentication() throws IOException, JNCException { |
186 | - NetconfDevice netconfDevice = new NetconfDevice("10.18.14.19", 22, | 164 | + TestDeviceCreator objForTestDev = new TestDeviceCreator( |
187 | - "cisco", "cisco"); | 165 | + new NetconfDevice( |
188 | - netconfDevice.setConnectTimeout(1000); | 166 | + DEVICE_IP, |
189 | - TestDeviceCreator objForTestDev = new TestDeviceCreator(netconfDevice, | 167 | + DEVICE_PORT, |
168 | + DEVICE_USERNAME, | ||
169 | + DEVICE_PASSWORD), | ||
190 | true); | 170 | true); |
191 | objForTestDev.run(); | 171 | objForTestDev.run(); |
192 | } | 172 | } |
... | @@ -197,16 +177,6 @@ public class NetconfDeviceProviderTest { | ... | @@ -197,16 +177,6 @@ public class NetconfDeviceProviderTest { |
197 | provider.cfgService = null; | 177 | provider.cfgService = null; |
198 | } | 178 | } |
199 | 179 | ||
200 | - @Test | ||
201 | - public void testActiveWithComponentContext() { | ||
202 | - | ||
203 | - ComponentContext componentContext = EasyMock | ||
204 | - .createMock(ComponentContext.class); | ||
205 | - getDictionaryMock(componentContext); | ||
206 | - replay(componentContext); | ||
207 | - provider.activate(componentContext); | ||
208 | - } | ||
209 | - | ||
210 | // To check if deviceCfgValue is empty or null | 180 | // To check if deviceCfgValue is empty or null |
211 | @Test | 181 | @Test |
212 | public void testActiveWithcomponentContextIsNull() { | 182 | public void testActiveWithcomponentContextIsNull() { |
... | @@ -273,7 +243,7 @@ public class NetconfDeviceProviderTest { | ... | @@ -273,7 +243,7 @@ public class NetconfDeviceProviderTest { |
273 | public void isReachableWithInvalidDeviceId() { | 243 | public void isReachableWithInvalidDeviceId() { |
274 | assertFalse("Initially the Device ID Should not be reachable", | 244 | assertFalse("Initially the Device ID Should not be reachable", |
275 | provider.isReachable(DID1)); | 245 | provider.isReachable(DID1)); |
276 | - NetconfDevice device = new NetconfDevice("", 0, "", ""); | 246 | + NetconfDevice device = new NetconfDevice(NULL, ZERO, NULL, NULL); |
277 | provider.netconfDeviceMap.put(DID1, device); | 247 | provider.netconfDeviceMap.put(DID1, device); |
278 | assertFalse("Particular Device ID cannot be Reachable", | 248 | assertFalse("Particular Device ID cannot be Reachable", |
279 | provider.isReachable(DID1)); | 249 | provider.isReachable(DID1)); |
... | @@ -286,7 +256,7 @@ public class NetconfDeviceProviderTest { | ... | @@ -286,7 +256,7 @@ public class NetconfDeviceProviderTest { |
286 | .createMock(ComponentContext.class); | 256 | .createMock(ComponentContext.class); |
287 | getDictionaryMockDeviceEntryForDeactivate(componentContext); | 257 | getDictionaryMockDeviceEntryForDeactivate(componentContext); |
288 | replay(componentContext); | 258 | replay(componentContext); |
289 | - testActiveWithComponentContext(); | 259 | + testActiveWithDeviceEntryWithDifferentDeviceState(); |
290 | provider.deactivate(componentContext); | 260 | provider.deactivate(componentContext); |
291 | } | 261 | } |
292 | 262 | ||
... | @@ -312,7 +282,6 @@ public class NetconfDeviceProviderTest { | ... | @@ -312,7 +282,6 @@ public class NetconfDeviceProviderTest { |
312 | 282 | ||
313 | /** | 283 | /** |
314 | * For each Netconf Device, remove the entry from the device store. | 284 | * For each Netconf Device, remove the entry from the device store. |
315 | - * @throws URISyntaxException | ||
316 | */ | 285 | */ |
317 | private void removeDevices() { | 286 | private void removeDevices() { |
318 | if (device == null) { | 287 | if (device == null) { |
... | @@ -343,8 +312,7 @@ public class NetconfDeviceProviderTest { | ... | @@ -343,8 +312,7 @@ public class NetconfDeviceProviderTest { |
343 | * Initialize Netconf Device object, and notify core saying device | 312 | * Initialize Netconf Device object, and notify core saying device |
344 | * connected. | 313 | * connected. |
345 | */ | 314 | */ |
346 | - private void advertiseDevices() | 315 | + private void advertiseDevices() throws JNCException, IOException { |
347 | - throws JNCException, IOException, SocketTimeoutException { | ||
348 | try { | 316 | try { |
349 | if (device == null) { | 317 | if (device == null) { |
350 | log.warn("The Request Netconf Device is null, cannot proceed further"); | 318 | log.warn("The Request Netconf Device is null, cannot proceed further"); |
... | @@ -356,9 +324,10 @@ public class NetconfDeviceProviderTest { | ... | @@ -356,9 +324,10 @@ public class NetconfDeviceProviderTest { |
356 | DeviceDescription desc = new DefaultDeviceDescription( | 324 | DeviceDescription desc = new DefaultDeviceDescription( |
357 | did.uri(), | 325 | did.uri(), |
358 | Device.Type.OTHER, | 326 | Device.Type.OTHER, |
359 | - "", "", | 327 | + NULL, |
360 | - "", "", | 328 | + NULL, |
361 | - cid); | 329 | + NULL, |
330 | + NULL, cid); | ||
362 | log.info("Persisting Device" + did.uri().toString()); | 331 | log.info("Persisting Device" + did.uri().toString()); |
363 | 332 | ||
364 | netconfDeviceMap.put(did, device); | 333 | netconfDeviceMap.put(did, device); |
... | @@ -372,22 +341,20 @@ public class NetconfDeviceProviderTest { | ... | @@ -372,22 +341,20 @@ public class NetconfDeviceProviderTest { |
372 | + " couldn't persist the device onto the store", e); | 341 | + " couldn't persist the device onto the store", e); |
373 | } catch (JNCException e) { | 342 | } catch (JNCException e) { |
374 | throw e; | 343 | throw e; |
375 | - } catch (SocketTimeoutException e) { | ||
376 | - throw e; | ||
377 | } catch (IOException e) { | 344 | } catch (IOException e) { |
378 | throw e; | 345 | throw e; |
379 | } catch (Exception e) { | 346 | } catch (Exception e) { |
380 | log.error("Error while initializing session for the device: " | 347 | log.error("Error while initializing session for the device: " |
381 | - + (device != null ? device.deviceInfo() : null), e); | 348 | + + device.deviceInfo(), e); |
382 | } | 349 | } |
383 | } | 350 | } |
384 | 351 | ||
385 | private DeviceId getDeviceId() throws URISyntaxException { | 352 | private DeviceId getDeviceId() throws URISyntaxException { |
386 | String additionalSSP = new StringBuilder(device.getUsername()) | 353 | String additionalSSP = new StringBuilder(device.getUsername()) |
387 | - .append("@").append(device.getSshHost()).append(":") | 354 | + .append(AT_THE_RATE).append(device.getSshHost()) |
388 | - .append(device.getSshPort()).toString(); | 355 | + .append(COLON).append(device.getSshPort()).toString(); |
389 | - DeviceId did = DeviceId.deviceId(new URI(SCHEME, additionalSSP, | 356 | + DeviceId did = DeviceId.deviceId(new URI(SCHEME_NETCONF, |
390 | - null)); | 357 | + additionalSSP, null)); |
391 | return did; | 358 | return did; |
392 | } | 359 | } |
393 | } | 360 | } | ... | ... |
1 | +/* | ||
2 | + * Copyright 2015 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 | +package org.onosproject.provider.netconf.device.impl; | ||
17 | + | ||
18 | +public final class NetconfDeviceProviderTestConstant { | ||
19 | + | ||
20 | + private NetconfDeviceProviderTestConstant() { | ||
21 | + } | ||
22 | + | ||
23 | + public static final int ZERO = 0; | ||
24 | + public static final int EVENTINTERVAL = 5; | ||
25 | + public static final String DEV_CONFIG = "devConfigs"; | ||
26 | + public static final String CONFIG_WITH_INVALID_ENTRY_NUMBER = "cisco:cisco" | ||
27 | + + "@10.18.11.14:cisco:active"; | ||
28 | + public static final String CONFIG_WITH_NULL_ENTRY = "null:null@null:0:active"; | ||
29 | + public static final String CONFIG_WITH_DIFFERENT_DEVICE_STATE = "cisco:cisco@10.18.11.14:22:active," | ||
30 | + + "cisco:cisco@10.18.11.18:22:inactive,cisco:cisco@10.18.11.14:22:invalid," | ||
31 | + + "cisco:cisco@10.18.11.14:22:null"; | ||
32 | + public static final String CONFIG_WITH_ARRAY_OUT_OF_BOUNDEX = "@10.18.11.14:22:active"; | ||
33 | + public static final String CONFIG_ENTRY_FOR_DEACTIVATE = "netconf:cisco" | ||
34 | + + "@10.18.11.14:22:active"; | ||
35 | + public static final String DEVICE_IP = "10.18.14.19"; | ||
36 | + public static final int DEVICE_PORT = 22; | ||
37 | + public static final String DEVICE_USERNAME = "cisco"; | ||
38 | + public static final String DEVICE_PASSWORD = "cisco"; | ||
39 | + public static final String AT_THE_RATE = "@"; | ||
40 | + public static final String COLON = ":"; | ||
41 | + public static final String NULL = ""; | ||
42 | + public static final String NULL_NULL = "null,null"; | ||
43 | + public static final String SCHEME_NETCONF = "netconf"; | ||
44 | + public static final String DEVICE_ID = "of:0000000000000001"; | ||
45 | + | ||
46 | +} |
-
Please register or login to post a comment