2 * Copyright (c) 2016 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.restconf;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
16 import org.junit.Before;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.ExpectedException;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
24 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
25 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
26 import org.opendaylight.controller.sal.core.api.Broker;
27 import org.opendaylight.controller.sal.core.api.model.SchemaService;
28 import org.opendaylight.restconf.handlers.SchemaContextHandler;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
33 * Unit tests for {@link RestConnectorProvider}
35 public class RestConnectorProviderTest {
37 private RestConnectorProvider connectorProvider;
39 @Mock private Broker.ProviderSession mockSession;
40 @Mock private SchemaService mockSchemaService;
41 @Mock private DOMMountPointService mockMountPointService;
42 @Mock private ListenerRegistration<SchemaContextListener> mockRegistration;
45 public ExpectedException thrown = ExpectedException.none();
49 MockitoAnnotations.initMocks(this);
50 this.connectorProvider = new RestConnectorProvider();
54 * Test of successful initialization of {@link RestConnectorProvider}.
57 public void restConnectorProviderInitTest() {
58 assertNotNull("Connector provider should be initialized and not null", this.connectorProvider);
62 * Test for successful registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)}
63 * when all conditions are satisfied.
65 * Condition 1: <code>Broker.ProviderSession</code> contains <code>SchemaService</code>
66 * Condition 2: <code>Broker.ProviderSession</code> contains <code>DOMMountPointService</code>
69 public void successfulRegistrationTest() {
71 when(this.mockSession.getService(SchemaService.class)).thenReturn(this.mockSchemaService);
72 when(this.mockSession.getService(DOMMountPointService.class)).thenReturn(this.mockMountPointService);
73 final DOMDataBroker mockDataBroker = Mockito.mock(DOMDataBroker.class);
74 when(this.mockSession.getService(DOMDataBroker.class)).thenReturn(mockDataBroker);
75 final DOMTransactionChain mockTransactionChain = Mockito.mock(DOMTransactionChain.class);
76 when(mockDataBroker.createTransactionChain(Mockito.any())).thenReturn(mockTransactionChain);
79 this.connectorProvider.onSessionInitiated(this.mockSession);
81 // verify interactions
82 verify(this.mockSession, times(1)).getService(SchemaService.class);
83 verify(this.mockSession, times(1)).getService(DOMMountPointService.class);
84 verify(this.mockSchemaService, times(1)).registerSchemaContextListener(Mockito.any(SchemaContextHandler.class));
88 * Test for successful registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)}
89 * without <code>DOMMountPointService</code>.
91 * Condition 1: <code>Broker.ProviderSession</code> contains <code>SchemaService</code>
92 * Condition 2: <code>Broker.ProviderSession</code> does not contain <code>DOMMountPointService</code>
95 public void successfulRegistrationWithoutMountPointTest() {
97 when(this.mockSession.getService(SchemaService.class)).thenReturn(this.mockSchemaService);
98 when(this.mockSession.getService(DOMMountPointService.class)).thenReturn(null);
99 final DOMDataBroker mockDataBroker = Mockito.mock(DOMDataBroker.class);
100 when(this.mockSession.getService(DOMDataBroker.class)).thenReturn(mockDataBroker);
101 final DOMTransactionChain mockTransactionChain = Mockito.mock(DOMTransactionChain.class);
102 when(mockDataBroker.createTransactionChain(Mockito.any())).thenReturn(mockTransactionChain);
103 final DOMMountPointService mockDomMountPoint = Mockito.mock(DOMMountPointService.class);
104 when(this.mockSession.getService(DOMMountPointService.class)).thenReturn(mockDomMountPoint);
107 this.connectorProvider.onSessionInitiated(this.mockSession);
109 // verify interactions
110 verify(this.mockSession, times(1)).getService(SchemaService.class);
111 verify(this.mockSession, times(1)).getService(DOMMountPointService.class);
112 verify(this.mockSchemaService, times(1)).registerSchemaContextListener(Mockito.any(SchemaContextHandler.class));
116 * Negative test of registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)} with
117 * null input. Test is expected to fail with <code>NullPointerException</code>.
120 public void nullSessionRegistrationNegativeTest() {
121 this.thrown.expect(NullPointerException.class);
122 this.connectorProvider.onSessionInitiated(null);
126 * Negative test of registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)} when
127 * <code>Broker.ProviderSession</code> does not contain <code>SchemaService</code>. Test is expected to fail with
128 * <code>NullPointerException</code>.
131 public void withoutSchemaServiceRegistrationNegativeTest() {
132 // prepare conditions
133 when(this.mockSession.getService(SchemaService.class)).thenReturn(null);
136 this.thrown.expect(NullPointerException.class);
137 this.connectorProvider.onSessionInitiated(this.mockSession);
139 // verify interaction
140 verify(this.mockSession, times(1)).getService(SchemaService.class);
144 * Test of closing <code>null</code> registration.
147 public void closeNotOpenTest() throws Exception {
148 this.connectorProvider.close();
152 * Test of creating and closing not <code>null</code> registration.
155 public void closeOpenTest() throws Exception {
156 // prepare conditions
157 when(this.mockSession.getService(SchemaService.class)).thenReturn(this.mockSchemaService);
158 when(this.mockSession.getService(DOMMountPointService.class)).thenReturn(this.mockMountPointService);
159 when(this.mockSchemaService.registerSchemaContextListener(Mockito.any(SchemaContextHandler.class)))
160 .thenReturn(this.mockRegistration);
161 final DOMDataBroker mockDataBroker = Mockito.mock(DOMDataBroker.class);
162 when(this.mockSession.getService(DOMDataBroker.class)).thenReturn(mockDataBroker);
163 final DOMTransactionChain mockTransactionChain = Mockito.mock(DOMTransactionChain.class);
164 when(mockDataBroker.createTransactionChain(Mockito.any())).thenReturn(mockTransactionChain);
167 this.connectorProvider.onSessionInitiated(this.mockSession);
169 // close registration
170 this.connectorProvider.close();
172 // verify interaction
173 verify(this.mockRegistration, times(1)).close();
174 verify(mockTransactionChain, times(1)).close();