Bug 5528 - Handlers
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / rest / RestConnectorProviderTest.java
1 /*
2  * Copyright (c) 2016 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.restconf.rest;
10
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.DOMDataBroker;
23 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
24 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
25 import org.opendaylight.controller.sal.core.api.Broker;
26 import org.opendaylight.controller.sal.core.api.model.SchemaService;
27 import org.opendaylight.restconf.RestConnectorProvider;
28 import org.opendaylight.restconf.handlers.SchemaContextHandler;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
31
32 /**
33  * Unit tests for {@link RestConnectorProvider}
34  */
35 public class RestConnectorProviderTest {
36     // service under test
37     private RestConnectorProvider connectorProvider;
38
39     @Mock private Broker.ProviderSession mockSession;
40     @Mock private SchemaService mockSchemaService;
41     @Mock private DOMMountPointService mockMountPointService;
42     @Mock private ListenerRegistration<SchemaContextListener> mockRegistration;
43
44     @Rule
45     public ExpectedException thrown = ExpectedException.none();
46
47     @Before
48     public void init() {
49         MockitoAnnotations.initMocks(this);
50         this.connectorProvider = new RestConnectorProvider();
51     }
52
53     /**
54      * Test of successful initialization of {@link RestConnectorProvider}.
55      */
56     @Test
57     public void restConnectorProviderInitTest() {
58         assertNotNull("Connector provider should be initialized and not null", this.connectorProvider);
59     }
60
61     /**
62      * Test for successful registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)}
63      * when all conditions are satisfied.
64      * <p>
65      * Condition 1: <code>Broker.ProviderSession</code> contains <code>SchemaService</code>
66      * Condition 2: <code>Broker.ProviderSession</code> contains <code>DOMMountPointService</code>
67      */
68     @Test
69     public void successfulRegistrationTest() {
70         // prepare conditions
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);
77
78         // test
79         this.connectorProvider.onSessionInitiated(this.mockSession);
80
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));
85     }
86
87     /**
88      * Test for successful registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)}
89      * without <code>DOMMountPointService</code>.
90      * <p>
91      * Condition 1: <code>Broker.ProviderSession</code> contains <code>SchemaService</code>
92      * Condition 2: <code>Broker.ProviderSession</code> does not contain <code>DOMMountPointService</code>
93      */
94     @Test
95     public void successfulRegistrationWithoutMountPointTest() {
96         // prepare conditions
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);
105
106         // test
107         this.connectorProvider.onSessionInitiated(this.mockSession);
108
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));
113     }
114
115     /**
116      * Negative test of registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)} with
117      * null input. Test is expected to fail with <code>NullPointerException</code>.
118      */
119     @Test
120     public void nullSessionRegistrationNegativeTest() {
121         this.thrown.expect(NullPointerException.class);
122         this.connectorProvider.onSessionInitiated(null);
123     }
124
125     /**
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>.
129      */
130     @Test
131     public void withoutSchemaServiceRegistrationNegativeTest() {
132         // prepare conditions
133         when(this.mockSession.getService(SchemaService.class)).thenReturn(null);
134
135         // test
136         this.thrown.expect(NullPointerException.class);
137         this.connectorProvider.onSessionInitiated(this.mockSession);
138
139         // verify interaction
140         verify(this.mockSession, times(1)).getService(SchemaService.class);
141     }
142
143     /**
144      * Test of closing <code>null</code> registration.
145      */
146     @Test
147     public void closeNotOpenTest() throws Exception {
148         this.connectorProvider.close();
149     }
150
151     /**
152      * Test of creating and closing not <code>null</code> registration.
153      */
154     @Test
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);
165
166         // register
167         this.connectorProvider.onSessionInitiated(this.mockSession);
168
169         // close registration
170         this.connectorProvider.close();
171
172         // verify interaction
173         verify(this.mockRegistration, times(1)).close();
174     }
175 }