b50450cd8418e52bff18f076dec90205580f32b4
[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
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;
29
30 /**
31  * Unit tests for {@link RestConnectorProvider}
32  */
33 public class RestConnectorProviderTest {
34     // service under test
35     private RestConnectorProvider connectorProvider;
36
37     @Mock private Broker.ProviderSession mockSession;
38     @Mock private SchemaService mockSchemaService;
39     @Mock private DOMMountPointService mockMountPointService;
40     @Mock private ListenerRegistration<SchemaContextListener> mockRegistration;
41
42     @Rule
43     public ExpectedException thrown = ExpectedException.none();
44
45     @Before
46     public void init() {
47         MockitoAnnotations.initMocks(this);
48         connectorProvider = new RestConnectorProvider();
49     }
50
51     /**
52      * Test of successful initialization of {@link RestConnectorProvider}.
53      */
54     @Test
55     public void restConnectorProviderInitTest() {
56         assertNotNull("Connector provider should be initialized and not null", connectorProvider);
57     }
58
59     /**
60      * Test for successful registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)}
61      * when all conditions are satisfied.
62      * <p>
63      * Condition 1: <code>Broker.ProviderSession</code> contains <code>SchemaService</code>
64      * Condition 2: <code>Broker.ProviderSession</code> contains <code>DOMMountPointService</code>
65      */
66     @Test
67     public void successfulRegistrationTest() {
68         // prepare conditions
69         when(mockSession.getService(SchemaService.class)).thenReturn(mockSchemaService);
70         when(mockSession.getService(DOMMountPointService.class)).thenReturn(mockMountPointService);
71
72         // test
73         connectorProvider.onSessionInitiated(mockSession);
74
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));
79     }
80
81     /**
82      * Test for successful registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)}
83      * without <code>DOMMountPointService</code>.
84      * <p>
85      * Condition 1: <code>Broker.ProviderSession</code> contains <code>SchemaService</code>
86      * Condition 2: <code>Broker.ProviderSession</code> does not contain <code>DOMMountPointService</code>
87      */
88     @Test
89     public void successfulRegistrationWithoutMountPointTest() {
90         // prepare conditions
91         when(mockSession.getService(SchemaService.class)).thenReturn(mockSchemaService);
92         when(mockSession.getService(DOMMountPointService.class)).thenReturn(null);
93
94         // test
95         connectorProvider.onSessionInitiated(mockSession);
96
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));
101     }
102
103     /**
104      * Negative test of registration with {@link RestConnectorProvider#onSessionInitiated(Broker.ProviderSession)} with
105      * null input. Test is expected to fail with <code>NullPointerException</code>.
106      */
107     @Test
108     public void nullSessionRegistrationNegativeTest() {
109         thrown.expect(NullPointerException.class);
110         connectorProvider.onSessionInitiated(null);
111     }
112
113     /**
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>.
117      */
118     @Test
119     public void withoutSchemaServiceRegistrationNegativeTest() {
120         // prepare conditions
121         when(mockSession.getService(SchemaService.class)).thenReturn(null);
122
123         // test
124         thrown.expect(NullPointerException.class);
125         connectorProvider.onSessionInitiated(mockSession);
126
127         // verify interaction
128         verify(mockSession, times(1)).getService(SchemaService.class);
129     }
130
131     /**
132      * Test of closing <code>null</code> registration.
133      */
134     @Test
135     public void closeNotOpenTest() throws Exception {
136         connectorProvider.close();
137     }
138
139     /**
140      * Test of creating and closing not <code>null</code> registration.
141      */
142     @Test
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);
149
150         // register
151         connectorProvider.onSessionInitiated(mockSession);
152
153         // close registration
154         connectorProvider.close();
155
156         // verify interaction
157         verify(mockRegistration, times(1)).close();
158     }
159 }