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.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.verify;
19 import java.io.FileNotFoundException;
20 import java.util.stream.Collectors;
21 import org.junit.AfterClass;
22 import org.junit.Before;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.mdsal.common.api.CommitInfo;
29 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
31 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
32 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.restconf.state.Capabilities;
34 import org.opendaylight.yangtools.concepts.ListenerRegistration;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
38 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
43 * Tests for handling {@link SchemaContext}.
45 @RunWith(MockitoJUnitRunner.StrictStubs.class)
46 public class SchemaContextHandlerTest {
48 private static final String PATH_FOR_ACTUAL_SCHEMA_CONTEXT = "/modules";
49 private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/modules/modules-behind-mount-point";
51 private static EffectiveModelContext SCHEMA_CONTEXT;
53 private SchemaContextHandler schemaContextHandler;
56 private DOMSchemaService mockDOMSchemaService;
59 public static void beforeClass() throws FileNotFoundException {
60 SCHEMA_CONTEXT = YangParserTestUtils.parseYangFiles(
61 TestRestconfUtils.loadFiles(PATH_FOR_ACTUAL_SCHEMA_CONTEXT));
65 public static void afterClass() {
66 SCHEMA_CONTEXT = null;
70 public void setup() throws Exception {
71 final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
72 final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
73 doReturn(wTx).when(dataBroker).newWriteOnlyTransaction();
74 doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
76 schemaContextHandler = new SchemaContextHandler(dataBroker, mockDOMSchemaService);
78 schemaContextHandler.onModelContextUpdated(SCHEMA_CONTEXT);
82 * Testing init and close.
85 public void testInitAndClose() {
86 ListenerRegistration<?> mockListenerReg = mock(ListenerRegistration.class);
87 doReturn(mockListenerReg).when(mockDOMSchemaService)
88 .registerSchemaContextListener(schemaContextHandler);
90 schemaContextHandler.init();
92 verify(mockDOMSchemaService).registerSchemaContextListener(schemaContextHandler);
94 schemaContextHandler.close();
96 verify(mockListenerReg).close();
100 * Test getting actual {@link SchemaContext}.
103 * Get <code>SchemaContext</code> from <code>SchemaContextHandler</code> and compare it to actual
104 * <code>SchemaContext</code>.
107 public void getSchemaContextTest() {
108 assertEquals("SchemaContextHandler should has reference to actual SchemaContext",
109 SCHEMA_CONTEXT, schemaContextHandler.get());
113 * Test updating of {@link SchemaContext}.
116 * Create new <code>SchemaContext</code>, set it to <code>SchemaContextHandler</code> and check if
117 * <code>SchemaContextHandler</code> reference to new <code>SchemaContext</code> instead of old one.
120 public void onGlobalContextUpdateTest() throws Exception {
121 // create new SchemaContext and update SchemaContextHandler
122 final EffectiveModelContext newSchemaContext =
123 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
124 schemaContextHandler.onModelContextUpdated(newSchemaContext);
126 assertNotEquals("SchemaContextHandler should not has reference to old SchemaContext",
127 SCHEMA_CONTEXT, schemaContextHandler.get());
128 assertEquals("SchemaContextHandler should has reference to new SchemaContext",
129 newSchemaContext, schemaContextHandler.get());
133 public void restconfStateCapabilitesTest() {
134 final ContainerNode normNode = SchemaContextHandler.mapCapabilites();
136 @SuppressWarnings("unchecked")
137 final LeafSetNode<String> capability = (LeafSetNode<String>) normNode.body().stream()
138 // Find 'capabilities' container
139 .filter(child -> Capabilities.QNAME.equals(child.getIdentifier().getNodeType()))
141 .map(ContainerNode.class::cast)
143 // Find 'capability' leaf-list
145 .filter(child -> SchemaContextHandler.CAPABILITY_QNAME.equals(child.getIdentifier().getNodeType()))
150 capability.body().stream().map(entry -> ((LeafSetEntryNode<?>) entry).body()).collect(Collectors.toList()),
152 equalTo("urn:ietf:params:restconf:capability:depth:1.0"),
153 equalTo("urn:ietf:params:restconf:capability:fields:1.0"),
154 equalTo("urn:ietf:params:restconf:capability:filter:1.0"),
155 equalTo("urn:ietf:params:restconf:capability:replay:1.0"),
156 equalTo("urn:ietf:params:restconf:capability:with-defaults:1.0"),
157 equalTo("urn:opendaylight:params:restconf:capability:skip-notification-data:1.0")));