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;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.mockito.Mockito;
17 import org.opendaylight.mdsal.common.api.CommitInfo;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
19 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
20 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
21 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28 * Tests for handling {@link SchemaContext}.
30 public class SchemaContextHandlerTest {
32 private static final String PATH_FOR_ACTUAL_SCHEMA_CONTEXT = "/modules";
33 private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/modules/modules-behind-mount-point";
35 private SchemaContextHandler schemaContextHandler;
36 private EffectiveModelContext schemaContext;
37 private final DOMSchemaService mockDOMSchemaService = Mockito.mock(DOMSchemaService.class);
40 public void setup() throws Exception {
41 final TransactionChainHandler txHandler = Mockito.mock(TransactionChainHandler.class);
42 final DOMTransactionChain domTx = Mockito.mock(DOMTransactionChain.class);
43 Mockito.when(txHandler.get()).thenReturn(domTx);
44 final DOMDataTreeWriteTransaction wTx = Mockito.mock(DOMDataTreeWriteTransaction.class);
45 Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
46 Mockito.doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
48 this.schemaContextHandler = SchemaContextHandler.newInstance(txHandler, mockDOMSchemaService);
51 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_ACTUAL_SCHEMA_CONTEXT));
52 this.schemaContextHandler.onModelContextUpdated(this.schemaContext);
56 * Testing init and close.
59 public void testInitAndClose() {
60 ListenerRegistration<?> mockListenerReg = Mockito.mock(ListenerRegistration.class);
61 Mockito.doReturn(mockListenerReg).when(mockDOMSchemaService)
62 .registerSchemaContextListener(schemaContextHandler);
64 schemaContextHandler.init();
66 Mockito.verify(mockDOMSchemaService).registerSchemaContextListener(schemaContextHandler);
68 schemaContextHandler.close();
70 Mockito.verify(mockListenerReg).close();
74 * Test getting actual {@link SchemaContext}.
77 * Get <code>SchemaContext</code> from <code>SchemaContextHandler</code> and compare it to actual
78 * <code>SchemaContext</code>.
81 public void getSchemaContextTest() {
82 assertEquals("SchemaContextHandler should has reference to actual SchemaContext",
83 this.schemaContext, this.schemaContextHandler.get());
87 * Test updating of {@link SchemaContext}.
90 * Create new <code>SchemaContext</code>, set it to <code>SchemaContextHandler</code> and check if
91 * <code>SchemaContextHandler</code> reference to new <code>SchemaContext</code> instead of old one.
94 public void onGlobalContextUpdateTest() throws Exception {
95 // create new SchemaContext and update SchemaContextHandler
96 final EffectiveModelContext newSchemaContext =
97 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
98 this.schemaContextHandler.onModelContextUpdated(newSchemaContext);
100 assertNotEquals("SchemaContextHandler should not has reference to old SchemaContext",
101 this.schemaContext, this.schemaContextHandler.get());
102 assertEquals("SchemaContextHandler should has reference to new SchemaContext",
103 newSchemaContext, this.schemaContextHandler.get());