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 com.google.common.util.concurrent.CheckedFuture;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mockito;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
21 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
22 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
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 SchemaContext 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 DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
45 Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
46 final CheckedFuture<Void,TransactionCommitFailedException> checked = Mockito.mock(CheckedFuture.class);
47 Mockito.when(wTx.submit()).thenReturn(checked);
48 Mockito.when(checked.checkedGet()).thenReturn(null);
51 this.schemaContextHandler = SchemaContextHandler.newInstance(txHandler, mockDOMSchemaService);
54 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_ACTUAL_SCHEMA_CONTEXT));
55 this.schemaContextHandler.onGlobalContextUpdated(this.schemaContext);
59 * Testing init and close.
62 public void testInitAndClose() {
63 ListenerRegistration<?> mockListenerReg = Mockito.mock(ListenerRegistration.class);
64 Mockito.doReturn(mockListenerReg).when(mockDOMSchemaService)
65 .registerSchemaContextListener(schemaContextHandler);
67 schemaContextHandler.init();
69 Mockito.verify(mockDOMSchemaService).registerSchemaContextListener(schemaContextHandler);
71 schemaContextHandler.close();
73 Mockito.verify(mockListenerReg).close();
77 * Test getting actual {@link SchemaContext}.
80 * Get <code>SchemaContext</code> from <code>SchemaContextHandler</code> and compare it to actual
81 * <code>SchemaContext</code>.
84 public void getSchemaContextTest() {
85 assertEquals("SchemaContextHandler should has reference to actual SchemaContext",
86 this.schemaContext, this.schemaContextHandler.get());
90 * Test updating of {@link SchemaContext}.
93 * Create new <code>SchemaContext</code>, set it to <code>SchemaContextHandler</code> and check if
94 * <code>SchemaContextHandler</code> reference to new <code>SchemaContext</code> instead of old one.
97 public void onGlobalContextUpdateTest() throws Exception {
98 // create new SchemaContext and update SchemaContextHandler
99 final SchemaContext newSchemaContext =
100 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
101 this.schemaContextHandler.onGlobalContextUpdated(newSchemaContext);
103 assertNotEquals("SchemaContextHandler should not has reference to old SchemaContext",
104 this.schemaContext, this.schemaContextHandler.get());
105 assertEquals("SchemaContextHandler should has reference to new SchemaContext",
106 newSchemaContext, this.schemaContextHandler.get());