Merge changes I9865d0cd,Ic71d525f,Ib8faba91,Ia10e5ec9,I35591747,If456a131,I9f8709cc
[controller.git] / opendaylight / netconf / netconf-impl / src / test / java / org / opendaylight / controller / netconf / impl / NetconfMonitoringServiceImplTest.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.netconf.impl;
10
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.Set;
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;
29
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.*;
34
35 public class NetconfMonitoringServiceImplTest {
36
37     private NetconfMonitoringServiceImpl service;
38
39     @Mock
40     private NetconfOperationProvider operationProvider;
41     @Mock
42     private NetconfManagementSession managementSession;
43     @Mock
44     private NetconfOperationServiceSnapshot snapshot;
45     @Mock
46     private NetconfOperationService operationService;
47
48     @Before
49     public void setUp() throws Exception {
50         MockitoAnnotations.initMocks(this);
51         service = new NetconfMonitoringServiceImpl(operationProvider);
52     }
53
54     @Test
55     public void testSessions() throws Exception {
56         doReturn("sessToStr").when(managementSession).toString();
57         service.onSessionUp(managementSession);
58     }
59
60     @Test(expected = RuntimeException.class)
61     public void testGetSchemas() throws Exception {
62         doThrow(RuntimeException.class).when(operationProvider).openSnapshot(anyString());
63         service.getSchemas();
64     }
65
66     @Test(expected = IllegalStateException.class)
67     public void testGetSchemas2() throws Exception {
68         doThrow(Exception.class).when(operationProvider).openSnapshot(anyString());
69         service.getSchemas();
70     }
71
72     @Test
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();
91
92         assertNotNull(service.getSchemas());
93         verify(snapshot, times(1)).close();
94
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));
100         sm.sessionUp();
101         service.onSessionUp(sm);
102         assertEquals(1, service.getSessions().getSession().size());
103
104         assertEquals(Long.valueOf(10), service.getSessions().getSession().get(0).getSessionId());
105
106         service.onSessionDown(sm);
107         assertEquals(0, service.getSessions().getSession().size());
108     }
109 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.