Standalone yang library data writer
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / legacy / SchemaContextHandlerTest.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 package org.opendaylight.restconf.nb.rfc8040.legacy;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.verify;
15
16 import org.junit.AfterClass;
17 import org.junit.Before;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
24 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29
30 /**
31  * Tests for handling {@link SchemaContext}.
32  */
33 @RunWith(MockitoJUnitRunner.StrictStubs.class)
34 public class SchemaContextHandlerTest {
35     private static EffectiveModelContext SCHEMA_CONTEXT;
36
37     private SchemaContextHandler schemaContextHandler;
38
39     @Mock
40     private DOMDataBroker mockDOMDataBroker;
41     @Mock
42     private DOMSchemaService mockDOMSchemaService;
43     @Mock
44     private ListenerRegistration<?> mockListenerReg;
45
46     @BeforeClass
47     public static void beforeClass() {
48         SCHEMA_CONTEXT = YangParserTestUtils.parseYangResourceDirectory("/modules");
49     }
50
51     @AfterClass
52     public static void afterClass() {
53         SCHEMA_CONTEXT = null;
54     }
55
56     @Before
57     public void setup() throws Exception {
58         doReturn(mockListenerReg).when(mockDOMSchemaService).registerSchemaContextListener(any());
59
60         schemaContextHandler = new SchemaContextHandler(mockDOMDataBroker, mockDOMSchemaService);
61         verify(mockDOMSchemaService).registerSchemaContextListener(schemaContextHandler);
62
63         schemaContextHandler.onModelContextUpdated(SCHEMA_CONTEXT);
64     }
65
66     /**
67      * Testing init and close.
68      */
69     @Test
70     public void testInitAndClose() {
71         schemaContextHandler.close();
72         verify(mockListenerReg).close();
73     }
74
75     /**
76      * Test getting actual {@link SchemaContext}.
77      *
78      * <p>
79      * Get <code>SchemaContext</code> from <code>SchemaContextHandler</code> and compare it to actual
80      * <code>SchemaContext</code>.
81      */
82     @Test
83     public void getSchemaContextTest() {
84         assertEquals("SchemaContextHandler should has reference to actual SchemaContext",
85                 SCHEMA_CONTEXT, schemaContextHandler.get());
86     }
87
88     /**
89      * Test updating of {@link SchemaContext}.
90      *
91      * <p>
92      * Create new <code>SchemaContext</code>, set it to <code>SchemaContextHandler</code> and check if
93      * <code>SchemaContextHandler</code> reference to new <code>SchemaContext</code> instead of old one.
94      */
95     @Test
96     public void onGlobalContextUpdateTest() {
97         // create new SchemaContext and update SchemaContextHandler
98         final EffectiveModelContext newSchemaContext =
99                 YangParserTestUtils.parseYangResourceDirectory("/modules/modules-behind-mount-point");
100         schemaContextHandler.onModelContextUpdated(newSchemaContext);
101
102         assertNotEquals("SchemaContextHandler should not has reference to old SchemaContext",
103                 SCHEMA_CONTEXT, schemaContextHandler.get());
104         assertEquals("SchemaContextHandler should has reference to new SchemaContext",
105                 newSchemaContext, schemaContextHandler.get());
106     }
107 }