ab762e821b87dbaf701688947d6a9992e63489c7
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2019 Bell Canada
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
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.core
18
19 import io.mockk.every
20 import io.mockk.mockk
21 import io.mockk.verifyAll
22 import org.junit.Before
23 import org.junit.Test
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.DeviceInfo
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.NetconfReceivedEvent
26
27 class NetconfSessionListenerImplTest {
28     // Note: mockk's verifyAll is akin to verify with verifyNoMoreInteractions in Mockito
29     private val netconSession = mockk<NetconfSessionImpl>()
30
31     @Before
32     fun init() {
33         every { netconSession.disconnect() } returns Unit
34         every { netconSession.addDeviceErrorReply(any()) } returns Unit
35         every { netconSession.addDeviceReply(any(), any()) } returns Unit
36     }
37
38     @Test
39     // NetconfReceivedEvent wth DEVICE_UNREGISTERED TYPE should call disconnect() on the NetconfSession
40     fun deviceUnregisteredMessageShouldCallSessionDisconnect() {
41         val netconfSessionListener = NetconfSessionListenerImpl(netconSession)
42         val event: NetconfReceivedEvent = genEventByType(NetconfReceivedEvent.Type.DEVICE_UNREGISTERED)
43         netconfSessionListener.accept(event)
44         verifyAll { netconSession.disconnect() }
45     }
46
47     @Test
48     // NetconfReceivedEvent wth SESSION_CLOSED TYPE should ALSO call disconnect() on the NetconfSession
49     fun sessionClosedMessageShouldCallSesionDisconnect() {
50         val netconfSessionListener = NetconfSessionListenerImpl(netconSession)
51         val event: NetconfReceivedEvent = genEventByType(NetconfReceivedEvent.Type.SESSION_CLOSED)
52         netconfSessionListener.accept(event)
53         verifyAll { netconSession.disconnect() }
54     }
55
56     @Test
57     // NetconfReceivedEvent wth DEVICE_ERROR TYPE should call addDeviceErrorReply() on the NetconfSession
58     // with the event message payload
59     fun deviceErrorMessageShouldCallAddDeviceErrorReply() {
60         val netconfSessionListener = NetconfSessionListenerImpl(netconSession)
61         val event: NetconfReceivedEvent = genEventByType(NetconfReceivedEvent.Type.DEVICE_ERROR)
62         netconfSessionListener.accept(event)
63         verifyAll { netconSession.addDeviceErrorReply(event.messagePayload) }
64     }
65
66     @Test
67     // NetconfReceivedEvent wth DEVICE_REPLY TYPE should call addDeviceReply(messageId, payload) on the NetconfSession
68     fun deviceReplyMessageShouldCallAddDeviceReply() {
69         val netconfSessionListener = NetconfSessionListenerImpl(netconSession)
70         val event: NetconfReceivedEvent = genEventByType(NetconfReceivedEvent.Type.DEVICE_REPLY)
71         netconfSessionListener.accept(event)
72         verifyAll { netconSession.addDeviceReply(event.messageId, event.messagePayload) }
73     }
74
75     /**
76      * Helper to generate {@link NetconfReceivedEvent} object based on the {@link NetconfReceivedEvent.Type}
77      * @param type {@link NetconfReceivedEvent.Type} of event
78      */
79     private fun genEventByType(type: NetconfReceivedEvent.Type): NetconfReceivedEvent {
80         return NetconfReceivedEvent(
81             type,
82             "messagePayload",
83             "messageId",
84             DeviceInfo()
85         )
86     }
87 }