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 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.dom.api.DOMDataBroker;
25 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
26 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33 * Tests for handling {@link SchemaContext}.
35 @RunWith(MockitoJUnitRunner.StrictStubs.class)
36 public class SchemaContextHandlerTest {
37 private static EffectiveModelContext SCHEMA_CONTEXT;
39 private SchemaContextHandler schemaContextHandler;
42 private DOMDataBroker mockDOMDataBroker;
44 private DOMSchemaService mockDOMSchemaService;
46 private ListenerRegistration<?> mockListenerReg;
49 public static void beforeClass() throws FileNotFoundException {
50 SCHEMA_CONTEXT = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/modules"));
54 public static void afterClass() {
55 SCHEMA_CONTEXT = null;
59 public void setup() throws Exception {
60 doReturn(mockListenerReg).when(mockDOMSchemaService).registerSchemaContextListener(any());
62 schemaContextHandler = new SchemaContextHandler(mockDOMDataBroker, mockDOMSchemaService);
63 verify(mockDOMSchemaService).registerSchemaContextListener(schemaContextHandler);
65 schemaContextHandler.onModelContextUpdated(SCHEMA_CONTEXT);
69 * Testing init and close.
72 public void testInitAndClose() {
73 schemaContextHandler.close();
74 verify(mockListenerReg).close();
78 * Test getting actual {@link SchemaContext}.
81 * Get <code>SchemaContext</code> from <code>SchemaContextHandler</code> and compare it to actual
82 * <code>SchemaContext</code>.
85 public void getSchemaContextTest() {
86 assertEquals("SchemaContextHandler should has reference to actual SchemaContext",
87 SCHEMA_CONTEXT, schemaContextHandler.get());
91 * Test updating of {@link SchemaContext}.
94 * Create new <code>SchemaContext</code>, set it to <code>SchemaContextHandler</code> and check if
95 * <code>SchemaContextHandler</code> reference to new <code>SchemaContext</code> instead of old one.
98 public void onGlobalContextUpdateTest() throws Exception {
99 // create new SchemaContext and update SchemaContextHandler
100 final EffectiveModelContext newSchemaContext =
101 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/modules/modules-behind-mount-point"));
102 schemaContextHandler.onModelContextUpdated(newSchemaContext);
104 assertNotEquals("SchemaContextHandler should not has reference to old SchemaContext",
105 SCHEMA_CONTEXT, schemaContextHandler.get());
106 assertEquals("SchemaContextHandler should has reference to new SchemaContext",
107 newSchemaContext, schemaContextHandler.get());