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