2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.impl;
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Lists;
13 import com.google.common.collect.Sets;
14 import io.netty.channel.Channel;
15 import java.util.List;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
22 import org.opendaylight.controller.netconf.impl.NetconfServerSession;
23 import org.opendaylight.controller.netconf.impl.NetconfServerSessionListener;
24 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
25 import org.opendaylight.controller.netconf.mapping.api.Capability;
26 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
27 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
28 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
29 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertNotNull;
33 import static org.mockito.Matchers.anyString;
34 import static org.mockito.Mockito.*;
36 public class NetconfMonitoringServiceImplTest {
38 private NetconfMonitoringServiceImpl service;
41 private NetconfOperationProvider operationProvider;
43 private NetconfManagementSession managementSession;
45 private NetconfOperationServiceSnapshot snapshot;
47 private NetconfOperationService operationService;
50 public void setUp() throws Exception {
51 MockitoAnnotations.initMocks(this);
52 service = new NetconfMonitoringServiceImpl(operationProvider);
56 public void testSessions() throws Exception {
57 doReturn("sessToStr").when(managementSession).toString();
58 service.onSessionUp(managementSession);
59 List list = Lists.newArrayList(managementSession);
62 @Test(expected = RuntimeException.class)
63 public void testGetSchemas() throws Exception {
64 doThrow(RuntimeException.class).when(operationProvider).openSnapshot(anyString());
68 @Test(expected = IllegalStateException.class)
69 public void testGetSchemas2() throws Exception {
70 doThrow(Exception.class).when(operationProvider).openSnapshot(anyString());
75 public void testGetSchemas3() throws Exception {
76 doReturn("").when(managementSession).toString();
77 Capability cap = mock(Capability.class);
78 Set<Capability> caps = Sets.newHashSet(cap);
79 Set<NetconfOperationService> services = Sets.newHashSet(operationService);
80 doReturn(snapshot).when(operationProvider).openSnapshot(anyString());
81 doReturn(services).when(snapshot).getServices();
82 doReturn(caps).when(operationService).getCapabilities();
83 Optional opt = mock(Optional.class);
84 doReturn(opt).when(cap).getCapabilitySchema();
85 doReturn(true).when(opt).isPresent();
86 doReturn(opt).when(cap).getModuleNamespace();
87 doReturn("namespace").when(opt).get();
88 Optional optRev = Optional.of("rev");
89 doReturn(optRev).when(cap).getRevision();
90 doReturn(Optional.of("modName")).when(cap).getModuleName();
91 doReturn(Optional.of(Lists.newArrayList("loc"))).when(cap).getLocation();
92 doNothing().when(snapshot).close();
94 assertNotNull(service.getSchemas());
95 verify(snapshot, times(1)).close();
97 NetconfServerSessionListener sessionListener = mock(NetconfServerSessionListener.class);
98 Channel channel = mock(Channel.class);
99 NetconfHelloMessageAdditionalHeader header = new NetconfHelloMessageAdditionalHeader("name", "addr", "2", "tcp", "id");
100 NetconfServerSession sm = new NetconfServerSession(sessionListener, channel, 10, header);
101 doNothing().when(sessionListener).onSessionUp(any(NetconfServerSession.class));
103 service.onSessionUp(sm);
104 assertEquals(1, service.getSessions().getSession().size());
106 assertEquals(Long.valueOf(10), service.getSessions().getSession().get(0).getSessionId());
108 service.onSessionDown(sm);
109 assertEquals(0, service.getSessions().getSession().size());