2 * Copyright © 2019 Bell Canada
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.core
22 import org.junit.Before
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.DeviceInfo
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.DeviceResponse
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.NetconfException
28 import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.utils.RpcStatus
29 import java.io.IOException
30 import java.util.concurrent.CompletableFuture
31 import kotlin.test.assertEquals
32 import kotlin.test.assertFailsWith
33 import kotlin.test.assertTrue
35 class NetconfRpcServiceImplTest {
36 private lateinit var mockNetconfSession: NetconfSessionImpl
39 private const val someString = "someString"
40 private const val replyStr = "this is a reply"
41 private val failedDeviceResponse = DeviceResponse(status = RpcStatus.FAILURE,
42 requestMessage = "request message", responseMessage = replyStr) //responseMessage will be null in this POJO
43 private val successfulDeviceResponse = DeviceResponse(status = RpcStatus.SUCCESS,
44 requestMessage = "request message", responseMessage = replyStr) //responseMessage will be null in this POJO
45 //but will be set later from mockSession
46 private const val msgId = "100"
47 private const val timeout = 5
48 private val deviceInfo: DeviceInfo = DeviceInfo().apply {
51 ipAddress = "localhost"
59 mockNetconfSession = mockk()
63 fun `invokeRpc completes normally`() {
64 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
65 netconfRpcService.setNetconfSession(mockNetconfSession)
66 val spy = spyk(netconfRpcService)
67 every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
68 val invokeRpcrResult = spy.invokeRpc(someString)
69 assertEquals(successfulDeviceResponse, invokeRpcrResult)
73 fun `invokeRpc on error sets DeviceResponse status to FAILURE`() {
74 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
75 netconfRpcService.setNetconfSession(mockNetconfSession)
76 val spy = spyk(netconfRpcService)
77 every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
78 val invokeRpcrResult = spy.invokeRpc(someString)
79 assertEquals(failedDeviceResponse.status, invokeRpcrResult.status)
80 assertTrue { invokeRpcrResult.errorMessage!!.contains("failed in 'invokeRpc' command") }
84 fun `getConfig completes normally`() {
85 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
86 netconfRpcService.setNetconfSession(mockNetconfSession)
87 val spy = spyk(netconfRpcService)
88 every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
89 val invokeRpcrResult = spy.getConfig(someString)
90 assertEquals(successfulDeviceResponse, invokeRpcrResult)
94 fun `getConfig on error sets DeviceResponse status to FAILURE`() {
95 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
96 netconfRpcService.setNetconfSession(mockNetconfSession)
97 val spy = spyk(netconfRpcService)
98 every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
99 val getConfigRpcResult = spy.getConfig(someString)
100 assertEquals(failedDeviceResponse.status, getConfigRpcResult.status)
101 assertTrue { getConfigRpcResult.errorMessage!!.contains("failed in 'get-config' command") }
105 fun `deleteConfig completes normally`() {
106 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
107 netconfRpcService.setNetconfSession(mockNetconfSession)
108 val spy = spyk(netconfRpcService)
109 every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
110 val rpcResult = spy.deleteConfig(someString)
111 assertEquals(successfulDeviceResponse, rpcResult)
115 fun `deleteConfig on error sets DeviceResponse status to FAILURE`() {
116 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
117 netconfRpcService.setNetconfSession(mockNetconfSession)
118 val spy = spyk(netconfRpcService)
119 every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
120 val rpcResult = spy.deleteConfig(someString)
121 assertEquals(failedDeviceResponse.status, rpcResult.status)
122 assertTrue { rpcResult.errorMessage!!.contains("failed in 'delete-config' command") }
126 fun `lock completes normally`() {
127 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
128 netconfRpcService.setNetconfSession(mockNetconfSession)
129 val spy = spyk(netconfRpcService)
130 every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
131 val rpcResult = spy.lock(someString)
132 assertEquals(successfulDeviceResponse, rpcResult)
136 fun `lock on error sets DeviceResponse status to FAILURE`() {
137 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
138 netconfRpcService.setNetconfSession(mockNetconfSession)
139 val spy = spyk(netconfRpcService)
140 every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
141 val rpcResult = spy.lock(someString)
142 assertEquals(failedDeviceResponse.status, rpcResult.status)
143 assertTrue { rpcResult.errorMessage!!.contains("failed in 'lock' command") }
147 fun `unLock completes normally`() {
148 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
149 netconfRpcService.setNetconfSession(mockNetconfSession)
150 val spy = spyk(netconfRpcService)
151 every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
152 val rpcResult = spy.unLock(someString)
153 assertEquals(successfulDeviceResponse, rpcResult)
157 fun `unLock on error sets DeviceResponse status to FAILURE`() {
158 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
159 netconfRpcService.setNetconfSession(mockNetconfSession)
160 val spy = spyk(netconfRpcService)
161 every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
162 val rpcResult = spy.unLock(someString)
163 assertEquals(failedDeviceResponse.status, rpcResult.status)
164 assertTrue { rpcResult.errorMessage!!.contains("failed in 'unLock' command") }
168 fun `commit completes normally on confirmed flag and only persist but not persistId specified`() {
169 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
170 netconfRpcService.setNetconfSession(mockNetconfSession)
171 val spy = spyk(netconfRpcService)
172 every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
173 val rpcResult = spy.commit(true, timeout, persist = "blah", persistId = "")
174 assertEquals(successfulDeviceResponse, rpcResult)
178 fun `commit completes normally on no confirm flag and only persistId but not persist specified`() {
179 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
180 netconfRpcService.setNetconfSession(mockNetconfSession)
181 val spy = spyk(netconfRpcService)
182 every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
183 val rpcResult = spy.commit(false, timeout, persistId = "blah")
184 assertEquals(successfulDeviceResponse, rpcResult)
188 fun `commit fails on confirm flag with persistId specified`() {
189 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
190 netconfRpcService.setNetconfSession(mockNetconfSession)
191 val spy = spyk(netconfRpcService)
192 every { spy.asyncRpc(any(), any()) } returns failedDeviceResponse
193 val rpcResult = spy.commit(true, timeout, persistId = "blah")
194 assertTrue { rpcResult.errorMessage!!.contains("failed in 'commit' command") }
198 fun `commit fails on confirm flag with persist and persistId specified`() {
199 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
200 netconfRpcService.setNetconfSession(mockNetconfSession)
201 val spy = spyk(netconfRpcService)
202 every { spy.asyncRpc(any(), any()) } returns failedDeviceResponse
203 val rpcResult = spy.commit(true, timeout, persist = "blah", persistId = "blah")
204 assertTrue { rpcResult.errorMessage!!.contains("failed in 'commit' command") }
208 fun `commit fails on no confirm flag with persist and persistId specified`() {
209 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
210 netconfRpcService.setNetconfSession(mockNetconfSession)
211 val spy = spyk(netconfRpcService)
212 every { spy.asyncRpc(any(), any()) } returns failedDeviceResponse
213 val rpcResult = spy.commit(false, timeout, persist = "blah", persistId = "blah")
214 assertTrue { rpcResult.errorMessage!!.contains("failed in 'commit' command") }
218 fun `cancelCommit completes normally`() {
219 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
220 netconfRpcService.setNetconfSession(mockNetconfSession)
221 val spy = spyk(netconfRpcService)
222 every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
223 val rpcResult = spy.cancelCommit(someString)
224 assertEquals(successfulDeviceResponse, rpcResult)
228 fun `cancelCommit on error sets DeviceResponse status to FAILURE`() {
229 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
230 netconfRpcService.setNetconfSession(mockNetconfSession)
231 val spy = spyk(netconfRpcService)
232 every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
233 val rpcResult = spy.cancelCommit(someString)
234 assertEquals(failedDeviceResponse.status, rpcResult.status)
235 assertTrue { rpcResult.errorMessage!!.contains("failed in 'cancelCommit' command") }
239 fun `discardConfig completes normally`() {
240 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
241 netconfRpcService.setNetconfSession(mockNetconfSession)
242 val spy = spyk(netconfRpcService)
243 every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
244 val rpcResult = spy.discardConfig()
245 assertEquals(successfulDeviceResponse, rpcResult)
249 fun `discardConfig on error sets DeviceResponse status to FAILURE`() {
250 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
251 netconfRpcService.setNetconfSession(mockNetconfSession)
252 val spy = spyk(netconfRpcService)
253 every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
254 val rpcResult = spy.discardConfig()
255 assertEquals(failedDeviceResponse.status, rpcResult.status)
256 assertTrue { rpcResult.errorMessage!!.contains("failed in 'discard-config' command") }
260 fun `editConfig completes normally`() {
261 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
262 netconfRpcService.setNetconfSession(mockNetconfSession)
263 val spy = spyk(netconfRpcService)
264 every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
265 val rpcResult = spy.editConfig("blah1", "blah2", "blah3")
266 assertEquals(successfulDeviceResponse, rpcResult)
270 fun `editConfig on error sets DeviceResponse status to FAILURE`() {
271 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
272 netconfRpcService.setNetconfSession(mockNetconfSession)
273 val spy = spyk(netconfRpcService)
274 every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
275 val rpcResult = spy.editConfig("blah1", "blah2", "blah3")
276 assertEquals(failedDeviceResponse.status, rpcResult.status)
277 assertTrue { rpcResult.errorMessage!!.contains("failed in 'editConfig' command") }
281 fun `validate completes normally`() {
282 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
283 netconfRpcService.setNetconfSession(mockNetconfSession)
284 val spy = spyk(netconfRpcService)
285 every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
286 val rpcResult = spy.validate("blah1")
287 assertEquals(successfulDeviceResponse, rpcResult)
291 fun `validate on error sets DeviceResponse status to FAILURE`() {
292 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
293 netconfRpcService.setNetconfSession(mockNetconfSession)
294 val spy = spyk(netconfRpcService)
295 every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
296 val rpcResult = spy.validate("blah1")
297 assertEquals(failedDeviceResponse.status, rpcResult.status)
298 assertTrue { rpcResult.errorMessage!!.contains("failed in 'validate' command") }
302 fun `closeSession completes normally without force`() {
303 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
304 netconfRpcService.setNetconfSession(mockNetconfSession)
305 val spy = spyk(netconfRpcService)
306 every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
307 val rpcResult = spy.closeSession(false)
308 assertEquals(successfulDeviceResponse, rpcResult)
312 fun `closeSession completes normally with force`() {
313 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
314 netconfRpcService.setNetconfSession(mockNetconfSession)
315 val spy = spyk(netconfRpcService)
316 every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
317 val rpcResult = spy.closeSession(true)
318 assertEquals(successfulDeviceResponse, rpcResult)
322 fun `closeSession on error sets DeviceResponse status to FAILURE`() {
323 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
324 netconfRpcService.setNetconfSession(mockNetconfSession)
325 val spy = spyk(netconfRpcService)
326 every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
327 val rpcResult = spy.closeSession(true)
328 assertEquals(failedDeviceResponse.status, rpcResult.status)
329 assertTrue { rpcResult.errorMessage!!.contains("failed in 'closeSession' command") }
333 fun `asyncRpc completes normally`() {
334 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
335 netconfRpcService.setNetconfSession(mockNetconfSession)
336 val spy = spyk(netconfRpcService)
337 every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
338 val rpcResult = spy.asyncRpc("blah1", "blah2")
339 assertEquals(successfulDeviceResponse, rpcResult)
343 fun `asyncRpc on error throws NetconfException`() {
344 assertFailsWith(exceptionClass = NetconfException::class) {
345 val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
346 netconfRpcService.setNetconfSession(mockNetconfSession)
347 val spy = spyk(netconfRpcService)
348 val erroneousFuture = CompletableFuture<String>()
349 erroneousFuture.complete("something something rpc-error>")
350 every { mockNetconfSession.asyncRpc(any(), any()) } returns erroneousFuture
351 val rpcResult = spy.asyncRpc("blah1", "blah2")
352 assertEquals(failedDeviceResponse.status, rpcResult.status)
353 assertTrue { rpcResult.errorMessage!!.contains("failed in 'closeSession' command") }