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