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
8 package org.opendaylight.restconf.nb.rfc8040.handlers;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
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.common.api.CommitInfo;
25 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
27 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
28 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
35 * Tests for handling {@link SchemaContext}.
37 @RunWith(MockitoJUnitRunner.StrictStubs.class)
38 public class SchemaContextHandlerTest {
40 private static final String PATH_FOR_ACTUAL_SCHEMA_CONTEXT = "/modules";
41 private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/modules/modules-behind-mount-point";
43 private static EffectiveModelContext SCHEMA_CONTEXT;
45 private SchemaContextHandler schemaContextHandler;
48 private DOMSchemaService mockDOMSchemaService;
51 public static void beforeClass() throws FileNotFoundException {
52 SCHEMA_CONTEXT = YangParserTestUtils.parseYangFiles(
53 TestRestconfUtils.loadFiles(PATH_FOR_ACTUAL_SCHEMA_CONTEXT));
57 public static void afterClass() {
58 SCHEMA_CONTEXT = null;
62 public void setup() throws Exception {
63 final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
64 final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
65 doReturn(wTx).when(dataBroker).newWriteOnlyTransaction();
66 doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
68 this.schemaContextHandler = new SchemaContextHandler(dataBroker, mockDOMSchemaService);
70 this.schemaContextHandler.onModelContextUpdated(SchemaContextHandlerTest.SCHEMA_CONTEXT);
74 * Testing init and close.
77 public void testInitAndClose() {
78 ListenerRegistration<?> mockListenerReg = mock(ListenerRegistration.class);
79 doReturn(mockListenerReg).when(mockDOMSchemaService)
80 .registerSchemaContextListener(schemaContextHandler);
82 schemaContextHandler.init();
84 verify(mockDOMSchemaService).registerSchemaContextListener(schemaContextHandler);
86 schemaContextHandler.close();
88 verify(mockListenerReg).close();
92 * Test getting actual {@link SchemaContext}.
95 * Get <code>SchemaContext</code> from <code>SchemaContextHandler</code> and compare it to actual
96 * <code>SchemaContext</code>.
99 public void getSchemaContextTest() {
100 assertEquals("SchemaContextHandler should has reference to actual SchemaContext",
101 SCHEMA_CONTEXT, this.schemaContextHandler.get());
105 * Test updating of {@link SchemaContext}.
108 * Create new <code>SchemaContext</code>, set it to <code>SchemaContextHandler</code> and check if
109 * <code>SchemaContextHandler</code> reference to new <code>SchemaContext</code> instead of old one.
112 public void onGlobalContextUpdateTest() throws Exception {
113 // create new SchemaContext and update SchemaContextHandler
114 final EffectiveModelContext newSchemaContext =
115 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
116 this.schemaContextHandler.onModelContextUpdated(newSchemaContext);
118 assertNotEquals("SchemaContextHandler should not has reference to old SchemaContext",
119 SCHEMA_CONTEXT, this.schemaContextHandler.get());
120 assertEquals("SchemaContextHandler should has reference to new SchemaContext",
121 newSchemaContext, this.schemaContextHandler.get());