Disconnect NetconfDeviceRpc from DOMRpcService
[netconf.git] / plugins / netconf-client-mdsal / src / test / java / org / opendaylight / netconf / client / mdsal / spi / 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.client.mdsal.spi;
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.client.mdsal.api.RemoteDeviceId;
31 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices;
32 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices.Rpcs;
33 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.IetfNetconfService;
35 import org.opendaylight.yangtools.concepts.ObjectRegistration;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
38
39 @RunWith(MockitoJUnitRunner.StrictStubs.class)
40 public class MountInstanceTest {
41     private static EffectiveModelContext SCHEMA_CONTEXT;
42
43     @Mock
44     private DOMMountPointService service;
45     @Mock
46     private DOMDataBroker broker;
47     @Mock
48     private NetconfDataTreeService netconfService;
49     @Mock
50     private Rpcs.Normalized rpcService;
51     @Mock
52     private NetconfDeviceNotificationService notificationService;
53     @Mock
54     private DOMMountPointService.DOMMountPointBuilder mountPointBuilder;
55     @Mock
56     private ObjectRegistration<DOMMountPoint> registration;
57     @Mock
58     private DOMNotification notification;
59
60     private NetconfDeviceMount mountInstance;
61
62     @BeforeClass
63     public static void suiteSetUp() throws Exception {
64         SCHEMA_CONTEXT = BindingRuntimeHelpers.createEffectiveModel(IetfNetconfService.class);
65     }
66
67     @Before
68     public void setUp() throws Exception {
69         when(service.createMountPoint(any(YangInstanceIdentifier.class))).thenReturn(mountPointBuilder);
70         when(mountPointBuilder.register()).thenReturn(registration);
71
72         mountInstance = new NetconfDeviceMount(
73             new RemoteDeviceId("device-1", InetSocketAddress.createUnresolved("localhost", 17830)),
74             service, YangInstanceIdentifier.of());
75     }
76
77     @Test
78     public void testOnTopologyDeviceConnected() {
79         mountInstance.onDeviceConnected(SCHEMA_CONTEXT, new RemoteDeviceServices(rpcService, null),
80             notificationService, broker, null);
81         verify(mountPointBuilder).addService(eq(DOMSchemaService.class), any());
82         verify(mountPointBuilder).addService(DOMDataBroker.class, broker);
83         verify(mountPointBuilder).addService(DOMRpcService.class, rpcService.domRpcService());
84         verify(mountPointBuilder).addService(DOMNotificationService.class, notificationService);
85     }
86
87     @Test
88     public void testOnTopologyDeviceConnectedWithNetconfService() {
89         mountInstance.onDeviceConnected(SCHEMA_CONTEXT, new RemoteDeviceServices(rpcService, null),
90             notificationService, null, netconfService);
91         verify(mountPointBuilder).addService(eq(DOMSchemaService.class), any());
92         verify(mountPointBuilder).addService(NetconfDataTreeService.class, netconfService);
93         verify(mountPointBuilder).addService(DOMRpcService.class, rpcService.domRpcService());
94         verify(mountPointBuilder).addService(DOMNotificationService.class, notificationService);
95     }
96
97     @Test
98     public void testOnTopologyDeviceDisconnected() {
99         mountInstance.onDeviceConnected(SCHEMA_CONTEXT, new RemoteDeviceServices(rpcService, null),
100             notificationService, broker, null);
101         mountInstance.onDeviceDisconnected();
102         verify(registration).close();
103         mountInstance.onDeviceConnected(SCHEMA_CONTEXT, new RemoteDeviceServices(rpcService, null),
104             notificationService, broker, null);
105     }
106
107     @Test
108     public void testClose() {
109         mountInstance.onDeviceConnected(SCHEMA_CONTEXT, new RemoteDeviceServices(rpcService, null),
110             notificationService, broker, null);
111         mountInstance.close();
112         verify(registration).close();
113     }
114
115     @Test
116     public void testPublishNotification() {
117         mountInstance.onDeviceConnected(SCHEMA_CONTEXT, new RemoteDeviceServices(rpcService, null),
118             notificationService, broker, null);
119         verify(mountPointBuilder).addService(eq(DOMSchemaService.class), any());
120         verify(mountPointBuilder).addService(DOMNotificationService.class, notificationService);
121         mountInstance.publish(notification);
122         verify(notificationService).publishNotification(notification);
123     }
124 }