ef4c63b985053c1123c290b761b72e8eb7194b38
[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 import static org.junit.Assert.assertNotNull;
14
15 import com.google.common.util.concurrent.CheckedFuture;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mockito;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
21 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
22 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25
26 /**
27  * Tests for handling {@link SchemaContext}.
28  */
29 public class SchemaContextHandlerTest {
30
31     private static final String PATH_FOR_ACTUAL_SCHEMA_CONTEXT = "/modules";
32     private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/modules/modules-behind-mount-point";
33
34     private SchemaContextHandler schemaContextHandler;
35     private SchemaContext schemaContext;
36
37     @Before
38     public void setup() throws Exception {
39         final TransactionChainHandler txHandler = Mockito.mock(TransactionChainHandler.class);
40         final DOMTransactionChain domTx = Mockito.mock(DOMTransactionChain.class);
41         Mockito.when(txHandler.get()).thenReturn(domTx);
42         final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
43         Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
44         final CheckedFuture<Void,TransactionCommitFailedException> checked = Mockito.mock(CheckedFuture.class);
45         Mockito.when(wTx.submit()).thenReturn(checked);
46         Mockito.when(checked.checkedGet()).thenReturn(null);
47         this.schemaContextHandler = new SchemaContextHandler(txHandler);
48
49         this.schemaContext =
50                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_ACTUAL_SCHEMA_CONTEXT));
51         this.schemaContextHandler.onGlobalContextUpdated(this.schemaContext);
52     }
53
54     /**
55      * Testing init of {@link SchemaContextHandler}.
56      */
57     @Test
58     public void schemaContextHandlerImplInitTest() {
59         assertNotNull("Handler should be created and not null", this.schemaContextHandler);
60     }
61
62     /**
63      * Test getting actual {@link SchemaContext}.
64      *
65      * <p>
66      * Get <code>SchemaContext</code> from <code>SchemaContextHandler</code> and compare it to actual
67      * <code>SchemaContext</code>.
68      */
69     @Test
70     public void getSchemaContextTest() {
71         assertEquals("SchemaContextHandler should has reference to actual SchemaContext",
72                 this.schemaContext, this.schemaContextHandler.get());
73     }
74
75     /**
76      * Test updating of {@link SchemaContext}.
77      *
78      * <p>
79      * Create new <code>SchemaContext</code>, set it to <code>SchemaContextHandler</code> and check if
80      * <code>SchemaContextHandler</code> reference to new <code>SchemaContext</code> instead of old one.
81      */
82     @Test
83     public void onGlobalContextUpdateTest() throws Exception {
84         // create new SchemaContext and update SchemaContextHandler
85         final SchemaContext newSchemaContext =
86                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
87         this.schemaContextHandler.onGlobalContextUpdated(newSchemaContext);
88
89         assertNotEquals("SchemaContextHandler should not has reference to old SchemaContext",
90                 this.schemaContext, this.schemaContextHandler.get());
91         assertEquals("SchemaContextHandler should has reference to new SchemaContext",
92                 newSchemaContext, this.schemaContextHandler.get());
93     }
94 }