Use SchemaContextHandler non-statically
[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 com.google.common.util.concurrent.CheckedFuture;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mockito;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
21 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
22 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
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 SchemaContext 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 DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
45         Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
46         final CheckedFuture<Void,TransactionCommitFailedException> checked = Mockito.mock(CheckedFuture.class);
47         Mockito.when(wTx.submit()).thenReturn(checked);
48         Mockito.when(checked.checkedGet()).thenReturn(null);
49
50
51         this.schemaContextHandler = SchemaContextHandler.newInstance(txHandler, mockDOMSchemaService);
52
53         this.schemaContext =
54                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_ACTUAL_SCHEMA_CONTEXT));
55         this.schemaContextHandler.onGlobalContextUpdated(this.schemaContext);
56     }
57
58     /**
59      * Testing init and close.
60      */
61     @Test
62     public void testInitAndClose() {
63         ListenerRegistration<?> mockListenerReg = Mockito.mock(ListenerRegistration.class);
64         Mockito.doReturn(mockListenerReg).when(mockDOMSchemaService)
65             .registerSchemaContextListener(schemaContextHandler);
66
67         schemaContextHandler.init();
68
69         Mockito.verify(mockDOMSchemaService).registerSchemaContextListener(schemaContextHandler);
70
71         schemaContextHandler.close();
72
73         Mockito.verify(mockListenerReg).close();
74     }
75
76     /**
77      * Test getting actual {@link SchemaContext}.
78      *
79      * <p>
80      * Get <code>SchemaContext</code> from <code>SchemaContextHandler</code> and compare it to actual
81      * <code>SchemaContext</code>.
82      */
83     @Test
84     public void getSchemaContextTest() {
85         assertEquals("SchemaContextHandler should has reference to actual SchemaContext",
86                 this.schemaContext, this.schemaContextHandler.get());
87     }
88
89     /**
90      * Test updating of {@link SchemaContext}.
91      *
92      * <p>
93      * Create new <code>SchemaContext</code>, set it to <code>SchemaContextHandler</code> and check if
94      * <code>SchemaContextHandler</code> reference to new <code>SchemaContext</code> instead of old one.
95      */
96     @Test
97     public void onGlobalContextUpdateTest() throws Exception {
98         // create new SchemaContext and update SchemaContextHandler
99         final SchemaContext newSchemaContext =
100                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
101         this.schemaContextHandler.onGlobalContextUpdated(newSchemaContext);
102
103         assertNotEquals("SchemaContextHandler should not has reference to old SchemaContext",
104                 this.schemaContext, this.schemaContextHandler.get());
105         assertEquals("SchemaContextHandler should has reference to new SchemaContext",
106                 newSchemaContext, this.schemaContextHandler.get());
107     }
108 }