2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.restconf.nb.rfc8040.handlers;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
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;
27 * Tests for handling {@link SchemaContext}.
29 public class SchemaContextHandlerTest {
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";
34 private SchemaContextHandler schemaContextHandler;
35 private SchemaContext schemaContext;
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);
50 YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles(PATH_FOR_ACTUAL_SCHEMA_CONTEXT));
51 this.schemaContextHandler.onGlobalContextUpdated(this.schemaContext);
55 * Testing init of {@link SchemaContextHandler}.
58 public void schemaContextHandlerImplInitTest() {
59 assertNotNull("Handler should be created and not null", this.schemaContextHandler);
63 * Test getting actual {@link SchemaContext}.
66 * Get <code>SchemaContext</code> from <code>SchemaContextHandler</code> and compare it to actual
67 * <code>SchemaContext</code>.
70 public void getSchemaContextTest() {
71 assertEquals("SchemaContextHandler should has reference to actual SchemaContext",
72 this.schemaContext, this.schemaContextHandler.get());
76 * Test updating of {@link SchemaContext}.
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.
83 public void onGlobalContextUpdateTest() throws Exception {
84 // create new SchemaContext and update SchemaContextHandler
85 final SchemaContext newSchemaContext =
86 YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
87 this.schemaContextHandler.onGlobalContextUpdated(newSchemaContext);
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());