Move NetconfDeviceTopologyAdapter
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceSalFacadeTest.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.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17
18 import java.net.InetSocketAddress;
19 import java.util.List;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
26 import org.opendaylight.mdsal.dom.api.DOMNotification;
27 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
28 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
29 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
30 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceSchema;
31 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
32 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
33 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
34 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
35 import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37
38 @RunWith(MockitoJUnitRunner.StrictStubs.class)
39 public class NetconfDeviceSalFacadeTest {
40     private final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("test", new InetSocketAddress("127.0.0.1", 8000));
41
42     @Mock
43     private NetconfDeviceSalProvider.MountInstance mountInstance;
44     @Mock
45     private NetconfDeviceSalProvider salProvider;
46
47     private NetconfDeviceSalFacade deviceFacade;
48
49     @Before
50     public void setUp() throws Exception {
51         deviceFacade = new NetconfDeviceSalFacade(remoteDeviceId, salProvider, true);
52
53         doReturn(mountInstance).when(salProvider).getMountInstance();
54         doNothing().when(mountInstance).onTopologyDeviceDisconnected();
55     }
56
57     @Test
58     public void testOnDeviceDisconnected() {
59         deviceFacade.onDeviceDisconnected();
60
61         verify(mountInstance, times(1)).onTopologyDeviceDisconnected();
62     }
63
64     @Test
65     public void testOnDeviceFailed() {
66         final Throwable throwable = new Throwable();
67         deviceFacade.onDeviceFailed(throwable);
68
69         verify(mountInstance, times(1)).onTopologyDeviceDisconnected();
70     }
71
72     @Test
73     public void testOnDeviceClose() throws Exception {
74         deviceFacade.close();
75         verify(salProvider).close();
76     }
77
78     @Test
79     public void testOnDeviceConnected() {
80         final EffectiveModelContext schemaContext = mock(EffectiveModelContext.class);
81
82         final var netconfSessionPreferences = NetconfSessionPreferences.fromStrings(
83             List.of(NetconfMessageTransformUtil.NETCONF_CANDIDATE_URI.toString()));
84
85         final var deviceServices = new RemoteDeviceServices(mock(Rpcs.Normalized.class), null);
86         deviceFacade.onDeviceConnected(
87             new NetconfDeviceSchema(NetconfDeviceCapabilities.empty(), new EmptyMountPointContext(schemaContext)),
88             netconfSessionPreferences, deviceServices);
89
90         verify(mountInstance, times(1)).onTopologyDeviceConnected(eq(schemaContext), eq(deviceServices),
91             any(DOMDataBroker.class), any(NetconfDataTreeService.class));
92     }
93
94     @Test
95     public void testOnDeviceNotification() throws Exception {
96         final DOMNotification domNotification = mock(DOMNotification.class);
97         deviceFacade.onNotification(domNotification);
98         verify(mountInstance).publish(domNotification);
99     }
100 }