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.hamcrest.CoreMatchers.equalTo;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.hamcrest.Matchers.containsInAnyOrder;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotEquals;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
20 import java.io.FileNotFoundException;
21 import java.util.stream.Collectors;
22 import org.junit.AfterClass;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.opendaylight.mdsal.common.api.CommitInfo;
30 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
32 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
33 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.restconf.state.Capabilities;
35 import org.opendaylight.yangtools.concepts.ListenerRegistration;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
39 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44 * Tests for handling {@link SchemaContext}.
46 @RunWith(MockitoJUnitRunner.StrictStubs.class)
47 public class SchemaContextHandlerTest {
49 private static final String PATH_FOR_ACTUAL_SCHEMA_CONTEXT = "/modules";
50 private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/modules/modules-behind-mount-point";
52 private static EffectiveModelContext SCHEMA_CONTEXT;
54 private SchemaContextHandler schemaContextHandler;
57 private DOMSchemaService mockDOMSchemaService;
59 private ListenerRegistration<?> mockListenerReg;
62 public static void beforeClass() throws FileNotFoundException {
63 SCHEMA_CONTEXT = YangParserTestUtils.parseYangFiles(
64 TestRestconfUtils.loadFiles(PATH_FOR_ACTUAL_SCHEMA_CONTEXT));
68 public static void afterClass() {
69 SCHEMA_CONTEXT = null;
73 public void setup() throws Exception {
74 final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
75 final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
76 doReturn(wTx).when(dataBroker).newWriteOnlyTransaction();
77 doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
79 doReturn(mockListenerReg).when(mockDOMSchemaService).registerSchemaContextListener(any());
81 schemaContextHandler = new SchemaContextHandler(dataBroker, mockDOMSchemaService);
82 verify(mockDOMSchemaService).registerSchemaContextListener(schemaContextHandler);
84 schemaContextHandler.onModelContextUpdated(SCHEMA_CONTEXT);
88 * Testing init and close.
91 public void testInitAndClose() {
92 schemaContextHandler.close();
93 verify(mockListenerReg).close();
97 * Test getting actual {@link SchemaContext}.
100 * Get <code>SchemaContext</code> from <code>SchemaContextHandler</code> and compare it to actual
101 * <code>SchemaContext</code>.
104 public void getSchemaContextTest() {
105 assertEquals("SchemaContextHandler should has reference to actual SchemaContext",
106 SCHEMA_CONTEXT, schemaContextHandler.get());
110 * Test updating of {@link SchemaContext}.
113 * Create new <code>SchemaContext</code>, set it to <code>SchemaContextHandler</code> and check if
114 * <code>SchemaContextHandler</code> reference to new <code>SchemaContext</code> instead of old one.
117 public void onGlobalContextUpdateTest() throws Exception {
118 // create new SchemaContext and update SchemaContextHandler
119 final EffectiveModelContext newSchemaContext =
120 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
121 schemaContextHandler.onModelContextUpdated(newSchemaContext);
123 assertNotEquals("SchemaContextHandler should not has reference to old SchemaContext",
124 SCHEMA_CONTEXT, schemaContextHandler.get());
125 assertEquals("SchemaContextHandler should has reference to new SchemaContext",
126 newSchemaContext, schemaContextHandler.get());
130 public void restconfStateCapabilitesTest() {
131 final ContainerNode normNode = SchemaContextHandler.mapCapabilites();
133 @SuppressWarnings("unchecked")
134 final LeafSetNode<String> capability = (LeafSetNode<String>) normNode.body().stream()
135 // Find 'capabilities' container
136 .filter(child -> Capabilities.QNAME.equals(child.getIdentifier().getNodeType()))
138 .map(ContainerNode.class::cast)
140 // Find 'capability' leaf-list
142 .filter(child -> SchemaContextHandler.CAPABILITY_QNAME.equals(child.getIdentifier().getNodeType()))
147 capability.body().stream().map(entry -> ((LeafSetEntryNode<?>) entry).body()).collect(Collectors.toList()),
149 equalTo("urn:ietf:params:restconf:capability:depth:1.0"),
150 equalTo("urn:ietf:params:restconf:capability:fields:1.0"),
151 equalTo("urn:ietf:params:restconf:capability:filter:1.0"),
152 equalTo("urn:ietf:params:restconf:capability:replay:1.0"),
153 equalTo("urn:ietf:params:restconf:capability:with-defaults:1.0"),
154 equalTo("urn:opendaylight:params:restconf:capability:pretty-print:1.0"),
155 equalTo("urn:opendaylight:params:restconf:capability:leaf-nodes-only:1.0"),
156 equalTo("urn:opendaylight:params:restconf:capability:skip-notification-data:1.0")));