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.rest;
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;
15 import org.junit.Before;
16 import org.junit.Rule;
17 import org.junit.Test;
18 import org.junit.rules.ExpectedException;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
23 import org.opendaylight.controller.sal.core.api.Broker;
24 import org.opendaylight.controller.sal.core.api.model.SchemaService;
25 import org.opendaylight.restconf.RestConnectorProvider;
26 import org.opendaylight.restconf.common.handlers.api.SchemaContextHandler;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
31 * Unit tests for {@link RestConnectorProvider}
33 public class RestConnectorProviderTest {
35 private RestConnectorProvider connectorProvider;
37 @Mock private Broker.ProviderSession mockSession;
38 @Mock private SchemaService mockSchemaService;
39 @Mock private DOMMountPointService mockMountPointService;
40 @Mock private ListenerRegistration<SchemaContextListener> mockRegistration;
43 public ExpectedException thrown = ExpectedException.none();
47 MockitoAnnotations.initMocks(this);
48 this.connectorProvider = new RestConnectorProvider();
52 * Test of successful initialization of {@link RestConnectorProvider}.
55 public void restConnectorProviderInitTest() {
56 assertNotNull("Connector provider should be initialized and not null", this.connectorProvider);
60 * Test for successful registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)}
61 * when all conditions are satisfied.
63 * Condition 1: <code>Broker.ProviderSession</code> contains <code>SchemaService</code>
64 * Condition 2: <code>Broker.ProviderSession</code> contains <code>DOMMountPointService</code>
67 public void successfulRegistrationTest() {
69 when(this.mockSession.getService(SchemaService.class)).thenReturn(this.mockSchemaService);
70 when(this.mockSession.getService(DOMMountPointService.class)).thenReturn(this.mockMountPointService);
73 this.connectorProvider.onSessionInitiated(this.mockSession);
75 // verify interactions
76 verify(this.mockSession, times(1)).getService(SchemaService.class);
77 verify(this.mockSession, times(1)).getService(DOMMountPointService.class);
78 verify(this.mockSchemaService, times(1)).registerSchemaContextListener(Mockito.any(SchemaContextHandler.class));
82 * Test for successful registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)}
83 * without <code>DOMMountPointService</code>.
85 * Condition 1: <code>Broker.ProviderSession</code> contains <code>SchemaService</code>
86 * Condition 2: <code>Broker.ProviderSession</code> does not contain <code>DOMMountPointService</code>
89 public void successfulRegistrationWithoutMountPointTest() {
91 when(this.mockSession.getService(SchemaService.class)).thenReturn(this.mockSchemaService);
92 when(this.mockSession.getService(DOMMountPointService.class)).thenReturn(null);
95 this.connectorProvider.onSessionInitiated(this.mockSession);
97 // verify interactions
98 verify(this.mockSession, times(1)).getService(SchemaService.class);
99 verify(this.mockSession, times(1)).getService(DOMMountPointService.class);
100 verify(this.mockSchemaService, times(1)).registerSchemaContextListener(Mockito.any(SchemaContextHandler.class));
104 * Negative test of registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)} with
105 * null input. Test is expected to fail with <code>NullPointerException</code>.
108 public void nullSessionRegistrationNegativeTest() {
109 this.thrown.expect(NullPointerException.class);
110 this.connectorProvider.onSessionInitiated(null);
114 * Negative test of registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)} when
115 * <code>Broker.ProviderSession</code> does not contain <code>SchemaService</code>. Test is expected to fail with
116 * <code>NullPointerException</code>.
119 public void withoutSchemaServiceRegistrationNegativeTest() {
120 // prepare conditions
121 when(this.mockSession.getService(SchemaService.class)).thenReturn(null);
124 this.thrown.expect(NullPointerException.class);
125 this.connectorProvider.onSessionInitiated(this.mockSession);
127 // verify interaction
128 verify(this.mockSession, times(1)).getService(SchemaService.class);
132 * Test of closing <code>null</code> registration.
135 public void closeNotOpenTest() throws Exception {
136 this.connectorProvider.close();
140 * Test of creating and closing not <code>null</code> registration.
143 public void closeOpenTest() throws Exception {
144 // prepare conditions
145 when(this.mockSession.getService(SchemaService.class)).thenReturn(this.mockSchemaService);
146 when(this.mockSession.getService(DOMMountPointService.class)).thenReturn(this.mockMountPointService);
147 when(this.mockSchemaService.registerSchemaContextListener(Mockito.any(SchemaContextHandler.class)))
148 .thenReturn(this.mockRegistration);
151 this.connectorProvider.onSessionInitiated(this.mockSession);
153 // close registration
154 this.connectorProvider.close();
156 // verify interaction
157 verify(this.mockRegistration, times(1)).close();