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;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.MockitoAnnotations;
20 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
21 import org.opendaylight.controller.netconf.impl.NetconfServerSession;
22 import org.opendaylight.controller.netconf.impl.NetconfServerSessionListener;
23 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
24 import org.opendaylight.controller.netconf.mapping.api.Capability;
25 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
26 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
27 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
28 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertNotNull;
32 import static org.mockito.Matchers.anyString;
33 import static org.mockito.Mockito.*;
35 public class NetconfMonitoringServiceImplTest {
37 private NetconfMonitoringServiceImpl service;
40 private NetconfOperationProvider operationProvider;
42 private NetconfManagementSession managementSession;
44 private NetconfOperationServiceSnapshot snapshot;
46 private NetconfOperationService operationService;
49 public void setUp() throws Exception {
50 MockitoAnnotations.initMocks(this);
51 service = new NetconfMonitoringServiceImpl(operationProvider);
55 public void testSessions() throws Exception {
56 doReturn("sessToStr").when(managementSession).toString();
57 service.onSessionUp(managementSession);
60 @Test(expected = RuntimeException.class)
61 public void testGetSchemas() throws Exception {
62 doThrow(RuntimeException.class).when(operationProvider).openSnapshot(anyString());
66 @Test(expected = IllegalStateException.class)
67 public void testGetSchemas2() throws Exception {
68 doThrow(Exception.class).when(operationProvider).openSnapshot(anyString());
73 public void testGetSchemas3() throws Exception {
74 doReturn("").when(managementSession).toString();
75 Capability cap = mock(Capability.class);
76 Set<Capability> caps = Sets.newHashSet(cap);
77 Set<NetconfOperationService> services = Sets.newHashSet(operationService);
78 doReturn(snapshot).when(operationProvider).openSnapshot(anyString());
79 doReturn(services).when(snapshot).getServices();
80 doReturn(caps).when(operationService).getCapabilities();
81 Optional<String> opt = mock(Optional.class);
82 doReturn(opt).when(cap).getCapabilitySchema();
83 doReturn(true).when(opt).isPresent();
84 doReturn(opt).when(cap).getModuleNamespace();
85 doReturn("namespace").when(opt).get();
86 Optional<String> optRev = Optional.of("rev");
87 doReturn(optRev).when(cap).getRevision();
88 doReturn(Optional.of("modName")).when(cap).getModuleName();
89 doReturn(Optional.of(Lists.newArrayList("loc"))).when(cap).getLocation();
90 doNothing().when(snapshot).close();
92 assertNotNull(service.getSchemas());
93 verify(snapshot, times(1)).close();
95 NetconfServerSessionListener sessionListener = mock(NetconfServerSessionListener.class);
96 Channel channel = mock(Channel.class);
97 NetconfHelloMessageAdditionalHeader header = new NetconfHelloMessageAdditionalHeader("name", "addr", "2", "tcp", "id");
98 NetconfServerSession sm = new NetconfServerSession(sessionListener, channel, 10, header);
99 doNothing().when(sessionListener).onSessionUp(any(NetconfServerSession.class));
101 service.onSessionUp(sm);
102 assertEquals(1, service.getSessions().getSession().size());
104 assertEquals(Long.valueOf(10), service.getSessions().getSession().get(0).getSessionId());
106 service.onSessionDown(sm);
107 assertEquals(0, service.getSessions().getSession().size());