981d744f60c2793d0277171c59e512c1b9fa1cf0
[netconf.git] / plugins / netconf-client-mdsal / src / test / java / org / opendaylight / netconf / client / mdsal / SchemalessNetconfDeviceTest.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;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.isNull;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
15
16 import com.google.common.collect.Lists;
17 import java.net.InetSocketAddress;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import org.junit.Test;
21 import org.mockito.Mockito;
22 import org.opendaylight.mdsal.dom.api.DOMNotification;
23 import org.opendaylight.netconf.api.NetconfMessage;
24 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
25 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
26 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceHandler;
27 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
28 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices;
29 import org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil;
30 import org.opendaylight.netconf.client.mdsal.impl.SchemalessMessageTransformer;
31
32 public class SchemalessNetconfDeviceTest extends AbstractBaseSchemasTest {
33
34     private static final String TEST_NAMESPACE = "test:namespace";
35     private static final String TEST_MODULE = "test-module";
36     private static final String TEST_REVISION = "2013-07-22";
37
38     @Test
39     public void testSessionOnMethods() throws Exception {
40         final RemoteDeviceHandler facade = getFacade();
41         final NetconfDeviceCommunicator listener = mockCloseableClass(NetconfDeviceCommunicator.class);
42         final SchemalessMessageTransformer messageTransformer = mock(SchemalessMessageTransformer.class);
43         final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("test-D",
44                 InetSocketAddress.createUnresolved("localhost", 22));
45         final Throwable throwable = new Throwable();
46
47         final SchemalessNetconfDevice device = new SchemalessNetconfDevice(BASE_SCHEMAS, remoteDeviceId, facade,
48             messageTransformer);
49
50         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
51                 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION));
52
53         final NetconfMessage netconfMessage = mock(NetconfMessage.class);
54
55         device.onRemoteSessionUp(sessionCaps, listener);
56         verify(facade).onDeviceConnected(
57                 any(NetconfDeviceSchema.class), any(NetconfSessionPreferences.class), any(RemoteDeviceServices.class));
58
59         device.onNotification(netconfMessage);
60         verify(facade).onNotification(isNull());
61
62         device.onRemoteSessionDown();
63         verify(facade).onDeviceDisconnected();
64
65         device.onRemoteSessionFailed(throwable);
66         verify(facade).onDeviceFailed(throwable);
67     }
68
69     private static RemoteDeviceHandler getFacade() throws Exception {
70         final RemoteDeviceHandler remoteDeviceHandler = mockCloseableClass(RemoteDeviceHandler.class);
71         doNothing().when(remoteDeviceHandler).onDeviceConnected(
72                 any(NetconfDeviceSchema.class), any(NetconfSessionPreferences.class), any(RemoteDeviceServices.class));
73         doNothing().when(remoteDeviceHandler).onDeviceDisconnected();
74         doNothing().when(remoteDeviceHandler).onNotification(any(DOMNotification.class));
75         return remoteDeviceHandler;
76     }
77
78     private static <T extends AutoCloseable> T mockCloseableClass(
79             final Class<T> remoteDeviceHandlerClass) throws Exception {
80         final T mock = mockClass(remoteDeviceHandlerClass);
81         doNothing().when(mock).close();
82         return mock;
83     }
84
85     private static <T> T mockClass(final Class<T> remoteDeviceHandlerClass) {
86         final T mock = mock(remoteDeviceHandlerClass);
87         Mockito.doReturn(remoteDeviceHandlerClass.getSimpleName()).when(mock).toString();
88         return mock;
89     }
90
91     private static NetconfSessionPreferences getSessionCaps(final boolean addMonitor,
92                                                             final Collection<String> additionalCapabilities) {
93         final ArrayList<String> capabilities = Lists.newArrayList(
94                 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
95                 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
96
97         if (addMonitor) {
98             capabilities.add(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING.getNamespace().toString());
99         }
100
101         capabilities.addAll(additionalCapabilities);
102
103         return NetconfSessionPreferences.fromStrings(capabilities);
104     }
105 }