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
21 import io.mockk.verifyAll
22 import org.junit.Before
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.DeviceInfo
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.NetconfReceivedEvent
27 class NetconfSessionListenerImplTest {
28 // Note: mockk's verifyAll is akin to verify with verifyNoMoreInteractions in Mockito
29 private val netconSession = mockk<NetconfSessionImpl>()
33 every { netconSession.disconnect() } returns Unit
34 every { netconSession.addDeviceErrorReply(any()) } returns Unit
35 every { netconSession.addDeviceReply(any(), any()) } returns Unit
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() }
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() }
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) }
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) }
76 * Helper to generate {@link NetconfReceivedEvent} object based on the {@link NetconfReceivedEvent.Type}
77 * @param type {@link NetconfReceivedEvent.Type} of event
79 private fun genEventByType(type: NetconfReceivedEvent.Type): NetconfReceivedEvent {
80 return NetconfReceivedEvent(