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.
66 * Condition 1: <code>Broker.ProviderSession</code> contains <code>SchemaService</code>
67 * Condition 2: <code>Broker.ProviderSession</code> contains <code>DOMMountPointService</code>
70 public void successfulRegistrationTest() {
72 when(this.mockSession.getService(SchemaService.class)).thenReturn(this.mockSchemaService);
73 when(this.mockSession.getService(DOMMountPointService.class)).thenReturn(this.mockMountPointService);
74 final DOMDataBroker mockDataBroker = Mockito.mock(DOMDataBroker.class);
75 when(this.mockSession.getService(DOMDataBroker.class)).thenReturn(mockDataBroker);
76 final DOMTransactionChain mockTransactionChain = Mockito.mock(DOMTransactionChain.class);
77 when(mockDataBroker.createTransactionChain(Mockito.any())).thenReturn(mockTransactionChain);
80 this.connectorProvider.onSessionInitiated(this.mockSession);
82 // verify interactions
83 verify(this.mockSession, times(1)).getService(SchemaService.class);
84 verify(this.mockSession, times(1)).getService(DOMMountPointService.class);
85 verify(this.mockSchemaService, times(1)).registerSchemaContextListener(Mockito.any(SchemaContextHandler.class));
89 * Test for successful registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)}
90 * without <code>DOMMountPointService</code>.
93 * Condition 1: <code>Broker.ProviderSession</code> contains <code>SchemaService</code>
94 * Condition 2: <code>Broker.ProviderSession</code> does not contain <code>DOMMountPointService</code>
97 public void successfulRegistrationWithoutMountPointTest() {
99 when(this.mockSession.getService(SchemaService.class)).thenReturn(this.mockSchemaService);
100 when(this.mockSession.getService(DOMMountPointService.class)).thenReturn(null);
101 final DOMDataBroker mockDataBroker = Mockito.mock(DOMDataBroker.class);
102 when(this.mockSession.getService(DOMDataBroker.class)).thenReturn(mockDataBroker);
103 final DOMTransactionChain mockTransactionChain = Mockito.mock(DOMTransactionChain.class);
104 when(mockDataBroker.createTransactionChain(Mockito.any())).thenReturn(mockTransactionChain);
105 final DOMMountPointService mockDomMountPoint = Mockito.mock(DOMMountPointService.class);
106 when(this.mockSession.getService(DOMMountPointService.class)).thenReturn(mockDomMountPoint);
109 this.connectorProvider.onSessionInitiated(this.mockSession);
111 // verify interactions
112 verify(this.mockSession, times(1)).getService(SchemaService.class);
113 verify(this.mockSession, times(1)).getService(DOMMountPointService.class);
114 verify(this.mockSchemaService, times(1)).registerSchemaContextListener(Mockito.any(SchemaContextHandler.class));
118 * Negative test of registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)} with
119 * null input. Test is expected to fail with <code>NullPointerException</code>.
122 public void nullSessionRegistrationNegativeTest() {
123 this.thrown.expect(NullPointerException.class);
124 this.connectorProvider.onSessionInitiated(null);
128 * Negative test of registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)} when
129 * <code>Broker.ProviderSession</code> does not contain <code>SchemaService</code>. Test is expected to fail with
130 * <code>NullPointerException</code>.
133 public void withoutSchemaServiceRegistrationNegativeTest() {
134 // prepare conditions
135 when(this.mockSession.getService(SchemaService.class)).thenReturn(null);
138 this.thrown.expect(NullPointerException.class);
139 this.connectorProvider.onSessionInitiated(this.mockSession);
141 // verify interaction
142 verify(this.mockSession, times(1)).getService(SchemaService.class);
146 * Test of closing <code>null</code> registration.
149 public void closeNotOpenTest() throws Exception {
150 this.connectorProvider.close();
154 * Test of creating and closing not <code>null</code> registration.
157 public void closeOpenTest() throws Exception {
158 // prepare conditions
159 when(this.mockSession.getService(SchemaService.class)).thenReturn(this.mockSchemaService);
160 when(this.mockSession.getService(DOMMountPointService.class)).thenReturn(this.mockMountPointService);
161 when(this.mockSchemaService.registerSchemaContextListener(Mockito.any(SchemaContextHandler.class)))
162 .thenReturn(this.mockRegistration);
163 final DOMDataBroker mockDataBroker = Mockito.mock(DOMDataBroker.class);
164 when(this.mockSession.getService(DOMDataBroker.class)).thenReturn(mockDataBroker);
165 final DOMTransactionChain mockTransactionChain = Mockito.mock(DOMTransactionChain.class);
166 when(mockDataBroker.createTransactionChain(Mockito.any())).thenReturn(mockTransactionChain);
169 this.connectorProvider.onSessionInitiated(this.mockSession);
171 // close registration
172 this.connectorProvider.close();
174 // verify interaction
175 verify(this.mockRegistration, times(1)).close();
176 verify(mockTransactionChain, times(1)).close();