a916e1f4683a0e702f46ba828a1bdb335aea64d1
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / 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;
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
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;
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      *
65      * <p>
66      * Condition 1: <code>Broker.ProviderSession</code> contains <code>SchemaService</code>
67      * Condition 2: <code>Broker.ProviderSession</code> contains <code>DOMMountPointService</code>
68      */
69     @Test
70     public void successfulRegistrationTest() {
71         // prepare conditions
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);
78
79         // test
80         this.connectorProvider.onSessionInitiated(this.mockSession);
81
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));
86     }
87
88     /**
89      * Test for successful registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)}
90      * without <code>DOMMountPointService</code>.
91      *
92      * <p>
93      * Condition 1: <code>Broker.ProviderSession</code> contains <code>SchemaService</code>
94      * Condition 2: <code>Broker.ProviderSession</code> does not contain <code>DOMMountPointService</code>
95      */
96     @Test
97     public void successfulRegistrationWithoutMountPointTest() {
98         // prepare conditions
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);
107
108         // test
109         this.connectorProvider.onSessionInitiated(this.mockSession);
110
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));
115     }
116
117     /**
118      * Negative test of registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)} with
119      * null input. Test is expected to fail with <code>NullPointerException</code>.
120      */
121     @Test
122     public void nullSessionRegistrationNegativeTest() {
123         this.thrown.expect(NullPointerException.class);
124         this.connectorProvider.onSessionInitiated(null);
125     }
126
127     /**
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>.
131      */
132     @Test
133     public void withoutSchemaServiceRegistrationNegativeTest() {
134         // prepare conditions
135         when(this.mockSession.getService(SchemaService.class)).thenReturn(null);
136
137         // test
138         this.thrown.expect(NullPointerException.class);
139         this.connectorProvider.onSessionInitiated(this.mockSession);
140
141         // verify interaction
142         verify(this.mockSession, times(1)).getService(SchemaService.class);
143     }
144
145     /**
146      * Test of closing <code>null</code> registration.
147      */
148     @Test
149     public void closeNotOpenTest() throws Exception {
150         this.connectorProvider.close();
151     }
152
153     /**
154      * Test of creating and closing not <code>null</code> registration.
155      */
156     @Test
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);
167
168         // register
169         this.connectorProvider.onSessionInitiated(this.mockSession);
170
171         // close registration
172         this.connectorProvider.close();
173
174         // verify interaction
175         verify(this.mockRegistration, times(1)).close();
176         verify(mockTransactionChain, times(1)).close();
177     }
178 }