Add RemoteDeviceServices
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / MountInstanceTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.sal.connect.netconf.sal;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.eq;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
14
15 import java.net.InetSocketAddress;
16 import org.junit.Before;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.junit.MockitoJUnitRunner;
22 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
23 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
24 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
25 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
26 import org.opendaylight.mdsal.dom.api.DOMNotification;
27 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
28 import org.opendaylight.mdsal.dom.api.DOMRpcService;
29 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
30 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
31 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
32 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.IetfNetconfService;
34 import org.opendaylight.yangtools.concepts.ObjectRegistration;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 @RunWith(MockitoJUnitRunner.StrictStubs.class)
41 public class MountInstanceTest {
42     private static final Logger LOG = LoggerFactory.getLogger(MountInstanceTest.class);
43
44     private static EffectiveModelContext SCHEMA_CONTEXT;
45
46     @Mock
47     private DOMMountPointService service;
48     @Mock
49     private DOMDataBroker broker;
50     @Mock
51     private NetconfDataTreeService netconfService;
52     @Mock
53     private DOMRpcService rpcService;
54     @Mock
55     private NetconfDeviceNotificationService notificationService;
56     @Mock
57     private DOMMountPointService.DOMMountPointBuilder mountPointBuilder;
58     @Mock
59     private ObjectRegistration<DOMMountPoint> registration;
60     @Mock
61     private DOMNotification notification;
62
63     private NetconfDeviceSalProvider.MountInstance mountInstance;
64
65     @BeforeClass
66     public static void suiteSetUp() throws Exception {
67         SCHEMA_CONTEXT = BindingRuntimeHelpers.createEffectiveModel(IetfNetconfService.class);
68     }
69
70     @Before
71     public void setUp() throws Exception {
72         when(service.createMountPoint(any(YangInstanceIdentifier.class))).thenReturn(mountPointBuilder);
73
74         when(mountPointBuilder.register()).thenReturn(registration);
75         mountInstance = new NetconfDeviceSalProvider.MountInstance(
76                 service, new RemoteDeviceId("device-1", InetSocketAddress.createUnresolved("localhost", 17830)));
77     }
78
79
80     @Test
81     public void testOnTopologyDeviceConnected() {
82         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, new RemoteDeviceServices(rpcService, null),
83             notificationService, broker, null);
84         verify(mountPointBuilder).addService(eq(DOMSchemaService.class), any());
85         verify(mountPointBuilder).addService(DOMDataBroker.class, broker);
86         verify(mountPointBuilder).addService(DOMRpcService.class, rpcService);
87         verify(mountPointBuilder).addService(DOMNotificationService.class, notificationService);
88     }
89
90     @Test
91     public void testOnTopologyDeviceConnectedWithNetconfService() {
92         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, new RemoteDeviceServices(rpcService, null),
93             notificationService, null, netconfService);
94         verify(mountPointBuilder).addService(eq(DOMSchemaService.class), any());
95         verify(mountPointBuilder).addService(NetconfDataTreeService.class, netconfService);
96         verify(mountPointBuilder).addService(DOMRpcService.class, rpcService);
97         verify(mountPointBuilder).addService(DOMNotificationService.class, notificationService);
98     }
99
100     @Test
101     public void testOnTopologyDeviceDisconnected() {
102         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, new RemoteDeviceServices(rpcService, null),
103             notificationService, broker, null);
104         mountInstance.onTopologyDeviceDisconnected();
105         verify(registration).close();
106         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, new RemoteDeviceServices(rpcService, null),
107             notificationService, broker, null);
108     }
109
110     @Test
111     public void testClose() {
112         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, new RemoteDeviceServices(rpcService, null),
113             notificationService, broker, null);
114         mountInstance.close();
115         verify(registration).close();
116     }
117
118     @Test
119     public void testPublishNotification() {
120         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, new RemoteDeviceServices(rpcService, null),
121             notificationService, broker, null);
122         verify(mountPointBuilder).addService(eq(DOMSchemaService.class), any());
123         verify(mountPointBuilder).addService(DOMNotificationService.class, notificationService);
124         mountInstance.publish(notification);
125         verify(notificationService).publishNotification(notification);
126     }
127 }