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;
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.DOMMountPointService;
24 import org.opendaylight.controller.sal.core.api.Broker;
25 import org.opendaylight.controller.sal.core.api.model.SchemaService;
26 import org.opendaylight.restconf.rest.api.schema.context.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 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", 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(mockSession.getService(SchemaService.class)).thenReturn(mockSchemaService);
70 when(mockSession.getService(DOMMountPointService.class)).thenReturn(mockMountPointService);
73 connectorProvider.onSessionInitiated(mockSession);
75 // verify interactions
76 verify(mockSession, times(1)).getService(SchemaService.class);
77 verify(mockSession, times(1)).getService(DOMMountPointService.class);
78 verify(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(mockSession.getService(SchemaService.class)).thenReturn(mockSchemaService);
92 when(mockSession.getService(DOMMountPointService.class)).thenReturn(null);
95 connectorProvider.onSessionInitiated(mockSession);
97 // verify interactions
98 verify(mockSession, times(1)).getService(SchemaService.class);
99 verify(mockSession, times(1)).getService(DOMMountPointService.class);
100 verify(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 thrown.expect(NullPointerException.class);
110 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(mockSession.getService(SchemaService.class)).thenReturn(null);
124 thrown.expect(NullPointerException.class);
125 connectorProvider.onSessionInitiated(mockSession);
127 // verify interaction
128 verify(mockSession, times(1)).getService(SchemaService.class);
132 * Test of closing <code>null</code> registration.
135 public void closeNotOpenTest() throws Exception {
136 connectorProvider.close();
140 * Test of creating and closing not <code>null</code> registration.
143 public void closeOpenTest() throws Exception {
144 // prepare conditions
145 when(mockSession.getService(SchemaService.class)).thenReturn(mockSchemaService);
146 when(mockSession.getService(DOMMountPointService.class)).thenReturn(mockMountPointService);
147 when(mockSchemaService.registerSchemaContextListener(Mockito.any(SchemaContextHandler.class)))
148 .thenReturn(mockRegistration);
151 connectorProvider.onSessionInitiated(mockSession);
153 // close registration
154 connectorProvider.close();
156 // verify interaction
157 verify(mockRegistration, times(1)).close();