Fix NPE when trying to download restconf provided yang files
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / 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.nb.rfc8040;
10
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.verify;
13
14 import org.junit.Before;
15 import org.junit.Rule;
16 import org.junit.Test;
17 import org.junit.rules.ExpectedException;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
22 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
23 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
25 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
26 import org.opendaylight.controller.sal.core.api.model.SchemaService;
27 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
28 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
29 import org.opendaylight.restconf.nb.rfc8040.services.wrapper.ServicesWrapperImpl;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
32
33 /**
34  * Unit tests for {@link RestConnectorProvider}.
35  */
36 public class RestConnectorProviderTest {
37     // service under test
38     private RestConnectorProvider connectorProvider;
39
40     @Mock private SchemaService mockSchemaService;
41     @Mock private DOMMountPointService mockMountPointService;
42     @Mock private DOMDataBroker mockDataBroker;
43     @Mock private DOMRpcService mockRpcService;
44     @Mock private DOMNotificationService mockNotificationService;
45     @Mock DOMTransactionChain mockTransactionChain;
46     @Mock private ListenerRegistration<SchemaContextListener> mockRegistration;
47     @Mock
48     private DOMSchemaService domSchemaService;
49
50     @Rule
51     public ExpectedException thrown = ExpectedException.none();
52
53     @Before
54     public void init() {
55         MockitoAnnotations.initMocks(this);
56
57         doReturn(mockTransactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
58         doReturn(mockRegistration).when(mockSchemaService).registerSchemaContextListener(
59                 Mockito.any(SchemaContextHandler.class));
60
61         this.connectorProvider = new RestConnectorProvider(mockDataBroker, mockSchemaService, mockRpcService,
62                 mockNotificationService, mockMountPointService, domSchemaService, ServicesWrapperImpl.getInstance());
63     }
64
65     /**
66      * Test for successful start when all conditions are satisfied.
67      */
68     @Test
69     public void successfulStartTest() {
70         // test
71         this.connectorProvider.start();
72
73         // verify interactions
74         verify(mockDataBroker).createTransactionChain(Mockito.any());
75         verify(mockSchemaService).registerSchemaContextListener(Mockito.any(SchemaContextHandler.class));
76     }
77
78     /**
79      * Test of closing <code>null</code> registration.
80      */
81     @Test
82     public void closeNotOpenTest() throws Exception {
83         this.connectorProvider.close();
84     }
85
86     /**
87      * Test of creating and closing not <code>null</code> registration.
88      */
89     @Test
90     public void closeOpenTest() throws Exception {
91         // start
92         this.connectorProvider.start();
93
94         // close
95         this.connectorProvider.close();
96
97         // verify interaction
98         verify(this.mockRegistration).close();
99         verify(mockTransactionChain).close();
100     }
101 }