Merge "Remove logged unstubbed exception in test for netconf-monitoring"
[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 static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.any;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.doThrow;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21
22 import com.google.common.base.Optional;
23 import com.google.common.collect.Lists;
24 import com.google.common.collect.Sets;
25 import io.netty.channel.Channel;
26 import java.util.Set;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
32 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
33 import org.opendaylight.controller.netconf.mapping.api.Capability;
34 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
35 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
36 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
37 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
38
39 public class NetconfMonitoringServiceImplTest {
40
41     private NetconfMonitoringServiceImpl service;
42
43     @Mock
44     private NetconfOperationProvider operationProvider;
45     @Mock
46     private NetconfManagementSession managementSession;
47     @Mock
48     private NetconfOperationServiceSnapshot snapshot;
49     @Mock
50     private NetconfOperationService operationService;
51
52     @Before
53     public void setUp() throws Exception {
54         MockitoAnnotations.initMocks(this);
55         service = new NetconfMonitoringServiceImpl(operationProvider);
56     }
57
58     @Test
59     public void testSessions() throws Exception {
60         doReturn("sessToStr").when(managementSession).toString();
61         service.onSessionUp(managementSession);
62     }
63
64     @Test(expected = RuntimeException.class)
65     public void testGetSchemas() throws Exception {
66         doThrow(RuntimeException.class).when(operationProvider).openSnapshot(anyString());
67         service.getSchemas();
68     }
69
70     @Test(expected = IllegalStateException.class)
71     public void testGetSchemas2() throws Exception {
72         doThrow(Exception.class).when(operationProvider).openSnapshot(anyString());
73         service.getSchemas();
74     }
75
76     @Test
77     public void testGetSchemas3() throws Exception {
78         doReturn("").when(managementSession).toString();
79         Capability cap = mock(Capability.class);
80         Set<Capability> caps = Sets.newHashSet(cap);
81         Set<NetconfOperationService> services = Sets.newHashSet(operationService);
82         doReturn(snapshot).when(operationProvider).openSnapshot(anyString());
83         doReturn(services).when(snapshot).getServices();
84         doReturn(caps).when(operationService).getCapabilities();
85         Optional<String> opt = mock(Optional.class);
86         doReturn(opt).when(cap).getCapabilitySchema();
87         doReturn(true).when(opt).isPresent();
88         doReturn(opt).when(cap).getModuleNamespace();
89         doReturn("namespace").when(opt).get();
90         Optional<String> optRev = Optional.of("rev");
91         doReturn(optRev).when(cap).getRevision();
92         doReturn(Optional.of("modName")).when(cap).getModuleName();
93         doReturn(Optional.of(Lists.newArrayList("loc"))).when(cap).getLocation();
94         doNothing().when(snapshot).close();
95
96         assertNotNull(service.getSchemas());
97         verify(snapshot, times(1)).close();
98
99         NetconfServerSessionListener sessionListener = mock(NetconfServerSessionListener.class);
100         Channel channel = mock(Channel.class);
101         NetconfHelloMessageAdditionalHeader header = new NetconfHelloMessageAdditionalHeader("name", "addr", "2", "tcp", "id");
102         NetconfServerSession sm = new NetconfServerSession(sessionListener, channel, 10, header);
103         doNothing().when(sessionListener).onSessionUp(any(NetconfServerSession.class));
104         sm.sessionUp();
105         service.onSessionUp(sm);
106         assertEquals(1, service.getSessions().getSession().size());
107
108         assertEquals(Long.valueOf(10), service.getSessions().getSession().get(0).getSessionId());
109
110         service.onSessionDown(sm);
111         assertEquals(0, service.getSessions().getSession().size());
112     }
113 }

©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.