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