Fix netconf-connector-config groupId
[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.Matchers.any;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.when;
13
14 import com.google.common.collect.Lists;
15 import java.net.InetSocketAddress;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
23 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
24 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
25 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
26 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
27 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
28 import org.opendaylight.yangtools.concepts.ObjectRegistration;
29 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32
33 public class MountInstanceTest {
34
35     private static SchemaContext SCHEMA_CONTEXT;
36
37     @Mock
38     private DOMMountPointService service;
39     @Mock
40     private DOMDataBroker broker;
41     @Mock
42     private DOMRpcService rpcService;
43     @Mock
44     private NetconfDeviceNotificationService notificationService;
45     @Mock
46     private DOMMountPointService.DOMMountPointBuilder mountPointBuilder;
47     @Mock
48     private ObjectRegistration<DOMMountPoint> registration;
49
50     private NetconfDeviceSalProvider.MountInstance mountInstance;
51
52     @BeforeClass
53     public static void suiteSetUp() throws Exception{
54         final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
55         moduleInfoBackedContext.addModuleInfos(
56                 Lists.newArrayList(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl.getInstance()));
57         SCHEMA_CONTEXT = moduleInfoBackedContext.tryToCreateSchemaContext().get();
58
59     }
60
61     @Before
62     public void setUp() throws Exception {
63         MockitoAnnotations.initMocks(this);
64         when(service.createMountPoint(any(YangInstanceIdentifier.class))).thenReturn(mountPointBuilder);
65
66         when(mountPointBuilder.register()).thenReturn(registration);
67         mountInstance = new NetconfDeviceSalProvider.MountInstance(service, new RemoteDeviceId("device-1", InetSocketAddress.createUnresolved("localhost", 17830)));
68     }
69
70     @Test
71     public void testOnDeviceConnected() throws Exception {
72         mountInstance.onDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
73         verify(mountPointBuilder).addInitialSchemaContext(SCHEMA_CONTEXT);
74         verify(mountPointBuilder).addService(DOMDataBroker.class, broker);
75         verify(mountPointBuilder).addService(DOMRpcService.class, rpcService);
76         verify(mountPointBuilder).addService(DOMNotificationService.class, notificationService);
77     }
78
79     @Test
80     public void testOnDeviceDisconnected() throws Exception {
81         mountInstance.onDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
82         mountInstance.onDeviceDisconnected();
83         verify(registration).close();
84         try {
85             mountInstance.onDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
86         } catch (IllegalStateException e) {
87             e.printStackTrace();
88             Assert.fail("Registration still present after disconnect ");
89         }
90     }
91
92     @Test
93     public void testOnTopologyDeviceConnected() throws Exception {
94         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
95         verify(mountPointBuilder).addInitialSchemaContext(SCHEMA_CONTEXT);
96         verify(mountPointBuilder).addService(DOMDataBroker.class, broker);
97         verify(mountPointBuilder).addService(DOMRpcService.class, rpcService);
98         verify(mountPointBuilder).addService(DOMNotificationService.class, notificationService);
99     }
100
101     @Test
102     public void testOnTopologyDeviceDisconnected() throws Exception {
103         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
104         mountInstance.onTopologyDeviceDisconnected();
105         verify(registration).close();
106         try {
107             mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
108         } catch (IllegalStateException e) {
109             e.printStackTrace();
110             Assert.fail("Topology registration still present after disconnect ");
111         }
112     }
113
114     @Test
115     public void testClose() throws Exception {
116         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
117         mountInstance.close();
118         verify(registration).close();
119     }
120
121 }