Merge "Cleanup RpcRoutingStrategy definition"
[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.List;
16 import java.util.Set;
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;
30
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.*;
35
36 public class NetconfMonitoringServiceImplTest {
37
38     private NetconfMonitoringServiceImpl service;
39
40     @Mock
41     private NetconfOperationProvider operationProvider;
42     @Mock
43     private NetconfManagementSession managementSession;
44     @Mock
45     private NetconfOperationServiceSnapshot snapshot;
46     @Mock
47     private NetconfOperationService operationService;
48
49     @Before
50     public void setUp() throws Exception {
51         MockitoAnnotations.initMocks(this);
52         service = new NetconfMonitoringServiceImpl(operationProvider);
53     }
54
55     @Test
56     public void testSessions() throws Exception {
57         doReturn("sessToStr").when(managementSession).toString();
58         service.onSessionUp(managementSession);
59         List list = Lists.newArrayList(managementSession);
60     }
61
62     @Test(expected = RuntimeException.class)
63     public void testGetSchemas() throws Exception {
64         doThrow(RuntimeException.class).when(operationProvider).openSnapshot(anyString());
65         service.getSchemas();
66     }
67
68     @Test(expected = IllegalStateException.class)
69     public void testGetSchemas2() throws Exception {
70         doThrow(Exception.class).when(operationProvider).openSnapshot(anyString());
71         service.getSchemas();
72     }
73
74     @Test
75     public void testGetSchemas3() throws Exception {
76         doReturn("").when(managementSession).toString();
77         Capability cap = mock(Capability.class);
78         Set caps = Sets.newHashSet(cap);
79         Set 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();
93
94         assertNotNull(service.getSchemas());
95         verify(snapshot, times(1)).close();
96
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));
102         sm.sessionUp();
103         service.onSessionUp(sm);
104         assertEquals(1, service.getSessions().getSession().size());
105
106         assertEquals(Long.valueOf(10), service.getSessions().getSession().get(0).getSessionId());
107
108         service.onSessionDown(sm);
109         assertEquals(0, service.getSessions().getSession().size());
110     }
111 }