afeeb90c7493d0057fd626eb7f93755280a86197
[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.Mockito.verify;
12 import static org.mockito.Mockito.when;
13
14 import java.net.InetSocketAddress;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.binding.runtime.spi.BindingRuntimeHelpers;
22 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
23 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
24 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
25 import org.opendaylight.mdsal.dom.api.DOMNotification;
26 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
27 import org.opendaylight.mdsal.dom.api.DOMRpcService;
28 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.IetfNetconfService;
30 import org.opendaylight.yangtools.concepts.ObjectRegistration;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class MountInstanceTest {
37     private static final Logger LOG = LoggerFactory.getLogger(MountInstanceTest.class);
38
39     private static EffectiveModelContext SCHEMA_CONTEXT;
40
41     @Mock
42     private DOMMountPointService service;
43     @Mock
44     private DOMDataBroker broker;
45     @Mock
46     private DOMRpcService rpcService;
47     @Mock
48     private NetconfDeviceNotificationService notificationService;
49     @Mock
50     private DOMMountPointService.DOMMountPointBuilder mountPointBuilder;
51     @Mock
52     private ObjectRegistration<DOMMountPoint> registration;
53     @Mock
54     private DOMNotification notification;
55
56     private NetconfDeviceSalProvider.MountInstance mountInstance;
57
58     @BeforeClass
59     public static void suiteSetUp() throws Exception {
60         SCHEMA_CONTEXT = BindingRuntimeHelpers.createEffectiveModel(IetfNetconfService.class);
61     }
62
63     @Before
64     public void setUp() throws Exception {
65         MockitoAnnotations.initMocks(this);
66         when(service.createMountPoint(any(YangInstanceIdentifier.class))).thenReturn(mountPointBuilder);
67
68         when(mountPointBuilder.register()).thenReturn(registration);
69         mountInstance = new NetconfDeviceSalProvider.MountInstance(
70                 service, new RemoteDeviceId("device-1", InetSocketAddress.createUnresolved("localhost", 17830)));
71     }
72
73
74     @Test
75     public void testOnTopologyDeviceConnected() throws Exception {
76         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
77         verify(mountPointBuilder).addInitialSchemaContext(SCHEMA_CONTEXT);
78         verify(mountPointBuilder).addService(DOMDataBroker.class, broker);
79         verify(mountPointBuilder).addService(DOMRpcService.class, rpcService);
80         verify(mountPointBuilder).addService(DOMNotificationService.class, notificationService);
81     }
82
83     @Test
84     public void testOnTopologyDeviceDisconnected() throws Exception {
85         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
86         mountInstance.onTopologyDeviceDisconnected();
87         verify(registration).close();
88         try {
89             mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
90         } catch (final IllegalStateException e) {
91             LOG.warn("Operation failed.", e);
92             Assert.fail("Topology registration still present after disconnect ");
93         }
94     }
95
96     @Test
97     public void testClose() throws Exception {
98         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
99         mountInstance.close();
100         verify(registration).close();
101     }
102
103     @Test
104     public void testPublishNotification() throws Exception {
105         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
106         verify(mountPointBuilder).addInitialSchemaContext(SCHEMA_CONTEXT);
107         verify(mountPointBuilder).addService(DOMNotificationService.class, notificationService);
108         mountInstance.publish(notification);
109         verify(notificationService).publishNotification(notification);
110     }
111
112
113 }