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.legacy;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.verify;
16 import org.junit.AfterClass;
17 import org.junit.Before;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
24 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
31 * Tests for handling {@link SchemaContext}.
33 @RunWith(MockitoJUnitRunner.StrictStubs.class)
34 public class SchemaContextHandlerTest {
35 private static EffectiveModelContext SCHEMA_CONTEXT;
37 private SchemaContextHandler schemaContextHandler;
40 private DOMDataBroker mockDOMDataBroker;
42 private DOMSchemaService mockDOMSchemaService;
44 private ListenerRegistration<?> mockListenerReg;
47 public static void beforeClass() {
48 SCHEMA_CONTEXT = YangParserTestUtils.parseYangResourceDirectory("/modules");
52 public static void afterClass() {
53 SCHEMA_CONTEXT = null;
57 public void setup() throws Exception {
58 doReturn(mockListenerReg).when(mockDOMSchemaService).registerSchemaContextListener(any());
60 schemaContextHandler = new SchemaContextHandler(mockDOMDataBroker, mockDOMSchemaService);
61 verify(mockDOMSchemaService).registerSchemaContextListener(schemaContextHandler);
63 schemaContextHandler.onModelContextUpdated(SCHEMA_CONTEXT);
67 * Testing init and close.
70 public void testInitAndClose() {
71 schemaContextHandler.close();
72 verify(mockListenerReg).close();
76 * Test getting actual {@link SchemaContext}.
79 * Get <code>SchemaContext</code> from <code>SchemaContextHandler</code> and compare it to actual
80 * <code>SchemaContext</code>.
83 public void getSchemaContextTest() {
84 assertEquals("SchemaContextHandler should has reference to actual SchemaContext",
85 SCHEMA_CONTEXT, schemaContextHandler.get());
89 * Test updating of {@link SchemaContext}.
92 * Create new <code>SchemaContext</code>, set it to <code>SchemaContextHandler</code> and check if
93 * <code>SchemaContextHandler</code> reference to new <code>SchemaContext</code> instead of old one.
96 public void onGlobalContextUpdateTest() {
97 // create new SchemaContext and update SchemaContextHandler
98 final EffectiveModelContext newSchemaContext =
99 YangParserTestUtils.parseYangResourceDirectory("/modules/modules-behind-mount-point");
100 schemaContextHandler.onModelContextUpdated(newSchemaContext);
102 assertNotEquals("SchemaContextHandler should not has reference to old SchemaContext",
103 SCHEMA_CONTEXT, schemaContextHandler.get());
104 assertEquals("SchemaContextHandler should has reference to new SchemaContext",
105 newSchemaContext, schemaContextHandler.get());