Advertize skip-notification-data capability
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / handlers / SchemaContextHandlerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.handlers;
9
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;
18
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;
41
42 /**
43  * Tests for handling {@link SchemaContext}.
44  */
45 @RunWith(MockitoJUnitRunner.StrictStubs.class)
46 public class SchemaContextHandlerTest {
47
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";
50
51     private static EffectiveModelContext SCHEMA_CONTEXT;
52
53     private SchemaContextHandler schemaContextHandler;
54
55     @Mock
56     private DOMSchemaService mockDOMSchemaService;
57
58     @BeforeClass
59     public static void beforeClass() throws FileNotFoundException {
60         SCHEMA_CONTEXT = YangParserTestUtils.parseYangFiles(
61             TestRestconfUtils.loadFiles(PATH_FOR_ACTUAL_SCHEMA_CONTEXT));
62     }
63
64     @AfterClass
65     public static void afterClass() {
66         SCHEMA_CONTEXT = null;
67     }
68
69     @Before
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();
75
76         schemaContextHandler = new SchemaContextHandler(dataBroker, mockDOMSchemaService);
77
78         schemaContextHandler.onModelContextUpdated(SCHEMA_CONTEXT);
79     }
80
81     /**
82      * Testing init and close.
83      */
84     @Test
85     public void testInitAndClose() {
86         ListenerRegistration<?> mockListenerReg = mock(ListenerRegistration.class);
87         doReturn(mockListenerReg).when(mockDOMSchemaService)
88             .registerSchemaContextListener(schemaContextHandler);
89
90         schemaContextHandler.init();
91
92         verify(mockDOMSchemaService).registerSchemaContextListener(schemaContextHandler);
93
94         schemaContextHandler.close();
95
96         verify(mockListenerReg).close();
97     }
98
99     /**
100      * Test getting actual {@link SchemaContext}.
101      *
102      * <p>
103      * Get <code>SchemaContext</code> from <code>SchemaContextHandler</code> and compare it to actual
104      * <code>SchemaContext</code>.
105      */
106     @Test
107     public void getSchemaContextTest() {
108         assertEquals("SchemaContextHandler should has reference to actual SchemaContext",
109                 SCHEMA_CONTEXT, schemaContextHandler.get());
110     }
111
112     /**
113      * Test updating of {@link SchemaContext}.
114      *
115      * <p>
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.
118      */
119     @Test
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);
125
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());
130     }
131
132     @Test
133     public void restconfStateCapabilitesTest() {
134         final ContainerNode normNode = SchemaContextHandler.mapCapabilites();
135
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()))
140             .findFirst()
141             .map(ContainerNode.class::cast)
142             .orElseThrow()
143             // Find 'capability' leaf-list
144             .body().stream()
145             .filter(child -> SchemaContextHandler.CAPABILITY_QNAME.equals(child.getIdentifier().getNodeType()))
146             .findFirst()
147             .orElseThrow();
148
149         assertThat(
150             capability.body().stream().map(entry -> ((LeafSetEntryNode<?>) entry).body()).collect(Collectors.toList()),
151             containsInAnyOrder(
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")));
158     }
159
160 }