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