9a3652aa142637e1e084ab39e350fc7d874b097c
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / netconf-executor / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / netconf / executor / core / NetconfRpcServiceImplTest.kt
1 /*
2  * Copyright © 2019 Bell Canada
3  * Modifications Copyright (c) 2019 IBM
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.core
19
20 import io.mockk.every
21 import io.mockk.mockk
22 import io.mockk.spyk
23 import org.junit.Before
24 import org.junit.Test
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
34
35 class NetconfRpcServiceImplTest {
36     private lateinit var mockNetconfSession: NetconfSessionImpl
37
38     companion object {
39         private const val someString = "someString"
40         private const val replyStr = "this is a reply"
41         private val failedDeviceResponse = DeviceResponse(
42             status = RpcStatus.FAILURE,
43             requestMessage = "request message", responseMessage = replyStr
44         ) // responseMessage will be null in this POJO
45         private val successfulDeviceResponse = DeviceResponse(
46             status = RpcStatus.SUCCESS,
47             requestMessage = "request message", responseMessage = replyStr
48         ) // responseMessage will be null in this POJO
49         // but will be set later from mockSession
50         private const val msgId = "100"
51         private const val timeout = 5
52         private val deviceInfo: DeviceInfo = DeviceInfo().apply {
53             username = "username"
54             password = "password"
55             ipAddress = "localhost"
56             port = 2224
57             connectTimeout = 5
58         }
59     }
60
61     @Before
62     fun setup() {
63         mockNetconfSession = mockk()
64     }
65
66     @Test
67     fun `invokeRpc completes normally`() {
68         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
69         netconfRpcService.setNetconfSession(mockNetconfSession)
70         val spy = spyk(netconfRpcService)
71         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
72         val invokeRpcrResult = spy.invokeRpc(someString)
73         assertEquals(successfulDeviceResponse, invokeRpcrResult)
74     }
75
76     @Test
77     fun `invokeRpc on error sets DeviceResponse status to FAILURE`() {
78         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
79         netconfRpcService.setNetconfSession(mockNetconfSession)
80         val spy = spyk(netconfRpcService)
81         every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
82         val invokeRpcrResult = spy.invokeRpc(someString)
83         assertEquals(failedDeviceResponse.status, invokeRpcrResult.status)
84         assertTrue { invokeRpcrResult.errorMessage!!.contains("failed in 'invokeRpc' command") }
85     }
86
87     @Test
88     fun `get completes normally`() {
89         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
90         netconfRpcService.setNetconfSession(mockNetconfSession)
91         val spy = spyk(netconfRpcService)
92         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
93         val getRpcrResult = spy.get(someString)
94         assertEquals(successfulDeviceResponse, getRpcrResult)
95     }
96
97     @Test
98     fun `get on error sets DeviceResponse status to FAILURE`() {
99         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
100         netconfRpcService.setNetconfSession(mockNetconfSession)
101         val spy = spyk(netconfRpcService)
102         every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
103         val getRpcResult = spy.get(someString)
104         assertEquals(failedDeviceResponse.status, getRpcResult.status)
105         assertTrue { getRpcResult.errorMessage!!.contains("failed in 'get' command") }
106     }
107
108     @Test
109     fun `getConfig completes normally`() {
110         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
111         netconfRpcService.setNetconfSession(mockNetconfSession)
112         val spy = spyk(netconfRpcService)
113         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
114         val getConfigRpcResult = spy.getConfig(someString)
115         assertEquals(successfulDeviceResponse, getConfigRpcResult)
116     }
117
118     @Test
119     fun `getConfig on error sets DeviceResponse status to FAILURE`() {
120         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
121         netconfRpcService.setNetconfSession(mockNetconfSession)
122         val spy = spyk(netconfRpcService)
123         every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
124         val getConfigRpcResult = spy.getConfig(someString)
125         assertEquals(failedDeviceResponse.status, getConfigRpcResult.status)
126         assertTrue { getConfigRpcResult.errorMessage!!.contains("failed in 'get-config' command") }
127     }
128
129     @Test
130     fun `deleteConfig completes normally`() {
131         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
132         netconfRpcService.setNetconfSession(mockNetconfSession)
133         val spy = spyk(netconfRpcService)
134         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
135         val rpcResult = spy.deleteConfig(someString)
136         assertEquals(successfulDeviceResponse, rpcResult)
137     }
138
139     @Test
140     fun `deleteConfig on error sets DeviceResponse status to FAILURE`() {
141         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
142         netconfRpcService.setNetconfSession(mockNetconfSession)
143         val spy = spyk(netconfRpcService)
144         every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
145         val rpcResult = spy.deleteConfig(someString)
146         assertEquals(failedDeviceResponse.status, rpcResult.status)
147         assertTrue { rpcResult.errorMessage!!.contains("failed in 'delete-config' command") }
148     }
149
150     @Test
151     fun `lock completes normally`() {
152         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
153         netconfRpcService.setNetconfSession(mockNetconfSession)
154         val spy = spyk(netconfRpcService)
155         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
156         val rpcResult = spy.lock(someString)
157         assertEquals(successfulDeviceResponse, rpcResult)
158     }
159
160     @Test
161     fun `lock on error sets DeviceResponse status to FAILURE`() {
162         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
163         netconfRpcService.setNetconfSession(mockNetconfSession)
164         val spy = spyk(netconfRpcService)
165         every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
166         val rpcResult = spy.lock(someString)
167         assertEquals(failedDeviceResponse.status, rpcResult.status)
168         assertTrue { rpcResult.errorMessage!!.contains("failed in 'lock' command") }
169     }
170
171     @Test
172     fun `unLock completes normally`() {
173         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
174         netconfRpcService.setNetconfSession(mockNetconfSession)
175         val spy = spyk(netconfRpcService)
176         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
177         val rpcResult = spy.unLock(someString)
178         assertEquals(successfulDeviceResponse, rpcResult)
179     }
180
181     @Test
182     fun `unLock on error sets DeviceResponse status to FAILURE`() {
183         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
184         netconfRpcService.setNetconfSession(mockNetconfSession)
185         val spy = spyk(netconfRpcService)
186         every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
187         val rpcResult = spy.unLock(someString)
188         assertEquals(failedDeviceResponse.status, rpcResult.status)
189         assertTrue { rpcResult.errorMessage!!.contains("failed in 'unLock' command") }
190     }
191
192     @Test
193     fun `commit completes normally on confirmed flag and only persist but not persistId specified`() {
194         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
195         netconfRpcService.setNetconfSession(mockNetconfSession)
196         val spy = spyk(netconfRpcService)
197         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
198         val rpcResult = spy.commit(true, timeout, persist = "blah", persistId = "")
199         assertEquals(successfulDeviceResponse, rpcResult)
200     }
201
202     @Test
203     fun `commit completes normally on no confirm flag and only persistId but not persist specified`() {
204         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
205         netconfRpcService.setNetconfSession(mockNetconfSession)
206         val spy = spyk(netconfRpcService)
207         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
208         val rpcResult = spy.commit(false, timeout, persistId = "blah")
209         assertEquals(successfulDeviceResponse, rpcResult)
210     }
211
212     @Test
213     fun `commit fails on confirm flag with persistId specified`() {
214         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
215         netconfRpcService.setNetconfSession(mockNetconfSession)
216         val spy = spyk(netconfRpcService)
217         every { spy.asyncRpc(any(), any()) } returns failedDeviceResponse
218         val rpcResult = spy.commit(true, timeout, persistId = "blah")
219         assertTrue { rpcResult.errorMessage!!.contains("failed in 'commit' command") }
220     }
221
222     @Test
223     fun `commit fails on confirm flag with persist and persistId specified`() {
224         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
225         netconfRpcService.setNetconfSession(mockNetconfSession)
226         val spy = spyk(netconfRpcService)
227         every { spy.asyncRpc(any(), any()) } returns failedDeviceResponse
228         val rpcResult = spy.commit(true, timeout, persist = "blah", persistId = "blah")
229         assertTrue { rpcResult.errorMessage!!.contains("failed in 'commit' command") }
230     }
231
232     @Test
233     fun `commit fails on no confirm flag with persist and persistId specified`() {
234         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
235         netconfRpcService.setNetconfSession(mockNetconfSession)
236         val spy = spyk(netconfRpcService)
237         every { spy.asyncRpc(any(), any()) } returns failedDeviceResponse
238         val rpcResult = spy.commit(false, timeout, persist = "blah", persistId = "blah")
239         assertTrue { rpcResult.errorMessage!!.contains("failed in 'commit' command") }
240     }
241
242     @Test
243     fun `cancelCommit completes normally`() {
244         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
245         netconfRpcService.setNetconfSession(mockNetconfSession)
246         val spy = spyk(netconfRpcService)
247         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
248         val rpcResult = spy.cancelCommit(someString)
249         assertEquals(successfulDeviceResponse, rpcResult)
250     }
251
252     @Test
253     fun `cancelCommit on error sets DeviceResponse status to FAILURE`() {
254         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
255         netconfRpcService.setNetconfSession(mockNetconfSession)
256         val spy = spyk(netconfRpcService)
257         every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
258         val rpcResult = spy.cancelCommit(someString)
259         assertEquals(failedDeviceResponse.status, rpcResult.status)
260         assertTrue { rpcResult.errorMessage!!.contains("failed in 'cancelCommit' command") }
261     }
262
263     @Test
264     fun `discardConfig completes normally`() {
265         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
266         netconfRpcService.setNetconfSession(mockNetconfSession)
267         val spy = spyk(netconfRpcService)
268         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
269         val rpcResult = spy.discardConfig()
270         assertEquals(successfulDeviceResponse, rpcResult)
271     }
272
273     @Test
274     fun `discardConfig on error sets DeviceResponse status to FAILURE`() {
275         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
276         netconfRpcService.setNetconfSession(mockNetconfSession)
277         val spy = spyk(netconfRpcService)
278         every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
279         val rpcResult = spy.discardConfig()
280         assertEquals(failedDeviceResponse.status, rpcResult.status)
281         assertTrue { rpcResult.errorMessage!!.contains("failed in 'discard-config' command") }
282     }
283
284     @Test
285     fun `editConfig completes normally`() {
286         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
287         netconfRpcService.setNetconfSession(mockNetconfSession)
288         val spy = spyk(netconfRpcService)
289         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
290         val rpcResult = spy.editConfig("blah1", "blah2", "blah3")
291         assertEquals(successfulDeviceResponse, rpcResult)
292     }
293
294     @Test
295     fun `editConfig on error sets DeviceResponse status to FAILURE`() {
296         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
297         netconfRpcService.setNetconfSession(mockNetconfSession)
298         val spy = spyk(netconfRpcService)
299         every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
300         val rpcResult = spy.editConfig("blah1", "blah2", "blah3")
301         assertEquals(failedDeviceResponse.status, rpcResult.status)
302         assertTrue { rpcResult.errorMessage!!.contains("failed in 'editConfig' command") }
303     }
304
305     @Test
306     fun `validate completes normally`() {
307         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
308         netconfRpcService.setNetconfSession(mockNetconfSession)
309         val spy = spyk(netconfRpcService)
310         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
311         val rpcResult = spy.validate("blah1")
312         assertEquals(successfulDeviceResponse, rpcResult)
313     }
314
315     @Test
316     fun `validate on error sets DeviceResponse status to FAILURE`() {
317         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
318         netconfRpcService.setNetconfSession(mockNetconfSession)
319         val spy = spyk(netconfRpcService)
320         every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
321         val rpcResult = spy.validate("blah1")
322         assertEquals(failedDeviceResponse.status, rpcResult.status)
323         assertTrue { rpcResult.errorMessage!!.contains("failed in 'validate' command") }
324     }
325
326     @Test
327     fun `closeSession completes normally without force`() {
328         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
329         netconfRpcService.setNetconfSession(mockNetconfSession)
330         val spy = spyk(netconfRpcService)
331         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
332         val rpcResult = spy.closeSession(false)
333         assertEquals(successfulDeviceResponse, rpcResult)
334     }
335
336     @Test
337     fun `closeSession completes normally with force`() {
338         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
339         netconfRpcService.setNetconfSession(mockNetconfSession)
340         val spy = spyk(netconfRpcService)
341         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
342         val rpcResult = spy.closeSession(true)
343         assertEquals(successfulDeviceResponse, rpcResult)
344     }
345
346     @Test
347     fun `closeSession on error sets DeviceResponse status to FAILURE`() {
348         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
349         netconfRpcService.setNetconfSession(mockNetconfSession)
350         val spy = spyk(netconfRpcService)
351         every { spy.asyncRpc(any(), any()) } throws IOException("Some IO exception...")
352         val rpcResult = spy.closeSession(true)
353         assertEquals(failedDeviceResponse.status, rpcResult.status)
354         assertTrue { rpcResult.errorMessage!!.contains("failed in 'closeSession' command") }
355     }
356
357     @Test
358     fun `asyncRpc completes normally`() {
359         val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
360         netconfRpcService.setNetconfSession(mockNetconfSession)
361         val spy = spyk(netconfRpcService)
362         every { spy.asyncRpc(any(), any()) } returns successfulDeviceResponse
363         val rpcResult = spy.asyncRpc("blah1", "blah2")
364         assertEquals(successfulDeviceResponse, rpcResult)
365     }
366
367     @Test
368     fun `asyncRpc on error throws NetconfException`() {
369         assertFailsWith(exceptionClass = NetconfException::class) {
370             val netconfRpcService = NetconfRpcServiceImpl(deviceInfo)
371             netconfRpcService.setNetconfSession(mockNetconfSession)
372             val spy = spyk(netconfRpcService)
373             val erroneousFuture = CompletableFuture<String>()
374             erroneousFuture.complete("something something rpc-error>")
375             every { mockNetconfSession.asyncRpc(any(), any()) } returns erroneousFuture
376             val rpcResult = spy.asyncRpc("blah1", "blah2")
377             assertEquals(failedDeviceResponse.status, rpcResult.status)
378             assertTrue { rpcResult.errorMessage!!.contains("failed in 'closeSession' command") }
379         }
380     }
381 }