Bug 8153: Enforce check-style rules for netconf - sal-netconf-connector
[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.DOMNotification;
26 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
27 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
28 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
29 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
30 import org.opendaylight.yangtools.concepts.ObjectRegistration;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class MountInstanceTest {
37
38     private static final Logger LOG = LoggerFactory.getLogger(MountInstanceTest.class);
39
40     private static SchemaContext SCHEMA_CONTEXT;
41
42     @Mock
43     private DOMMountPointService service;
44     @Mock
45     private DOMDataBroker broker;
46     @Mock
47     private DOMRpcService rpcService;
48     @Mock
49     private NetconfDeviceNotificationService notificationService;
50     @Mock
51     private DOMMountPointService.DOMMountPointBuilder mountPointBuilder;
52     @Mock
53     private ObjectRegistration<DOMMountPoint> registration;
54     @Mock
55     private DOMNotification notification;
56
57     private NetconfDeviceSalProvider.MountInstance mountInstance;
58
59     @BeforeClass
60     public static void suiteSetUp() throws Exception {
61         final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
62         moduleInfoBackedContext.addModuleInfos(Lists.newArrayList(org.opendaylight.yang.gen
63                 .v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl.getInstance()));
64         SCHEMA_CONTEXT = moduleInfoBackedContext.tryToCreateSchemaContext().get();
65
66     }
67
68     @Before
69     public void setUp() throws Exception {
70         MockitoAnnotations.initMocks(this);
71         when(service.createMountPoint(any(YangInstanceIdentifier.class))).thenReturn(mountPointBuilder);
72
73         when(mountPointBuilder.register()).thenReturn(registration);
74         mountInstance = new NetconfDeviceSalProvider.MountInstance(
75                 service, new RemoteDeviceId("device-1", InetSocketAddress.createUnresolved("localhost", 17830)));
76     }
77
78
79     @Test
80     public void testOnTopologyDeviceConnected() throws Exception {
81         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
82         verify(mountPointBuilder).addInitialSchemaContext(SCHEMA_CONTEXT);
83         verify(mountPointBuilder).addService(DOMDataBroker.class, broker);
84         verify(mountPointBuilder).addService(DOMRpcService.class, rpcService);
85         verify(mountPointBuilder).addService(DOMNotificationService.class, notificationService);
86     }
87
88     @Test
89     public void testOnTopologyDeviceDisconnected() throws Exception {
90         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
91         mountInstance.onTopologyDeviceDisconnected();
92         verify(registration).close();
93         try {
94             mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
95         } catch (final IllegalStateException e) {
96             LOG.warn("Operation failed.", e);
97             Assert.fail("Topology registration still present after disconnect ");
98         }
99     }
100
101     @Test
102     public void testClose() throws Exception {
103         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
104         mountInstance.close();
105         verify(registration).close();
106     }
107
108     @Test
109     public void testPublishNotification() throws Exception {
110         mountInstance.onTopologyDeviceConnected(SCHEMA_CONTEXT, broker, rpcService, notificationService);
111         verify(mountPointBuilder).addInitialSchemaContext(SCHEMA_CONTEXT);
112         verify(mountPointBuilder).addService(DOMNotificationService.class, notificationService);
113         mountInstance.publish(notification);
114         verify(notificationService).publishNotification(notification);
115     }
116
117
118 }