Turn SchemaContextHandler into a component
[netconf.git] / restconf / restconf-nb / 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.ArgumentMatchers.any;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
19
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;
42
43 /**
44  * Tests for handling {@link SchemaContext}.
45  */
46 @RunWith(MockitoJUnitRunner.StrictStubs.class)
47 public class SchemaContextHandlerTest {
48
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";
51
52     private static EffectiveModelContext SCHEMA_CONTEXT;
53
54     private SchemaContextHandler schemaContextHandler;
55
56     @Mock
57     private DOMSchemaService mockDOMSchemaService;
58     @Mock
59     private ListenerRegistration<?> mockListenerReg;
60
61     @BeforeClass
62     public static void beforeClass() throws FileNotFoundException {
63         SCHEMA_CONTEXT = YangParserTestUtils.parseYangFiles(
64             TestRestconfUtils.loadFiles(PATH_FOR_ACTUAL_SCHEMA_CONTEXT));
65     }
66
67     @AfterClass
68     public static void afterClass() {
69         SCHEMA_CONTEXT = null;
70     }
71
72     @Before
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();
78
79         doReturn(mockListenerReg).when(mockDOMSchemaService).registerSchemaContextListener(any());
80
81         schemaContextHandler = new SchemaContextHandler(dataBroker, mockDOMSchemaService);
82         verify(mockDOMSchemaService).registerSchemaContextListener(schemaContextHandler);
83
84         schemaContextHandler.onModelContextUpdated(SCHEMA_CONTEXT);
85     }
86
87     /**
88      * Testing init and close.
89      */
90     @Test
91     public void testInitAndClose() {
92         schemaContextHandler.close();
93         verify(mockListenerReg).close();
94     }
95
96     /**
97      * Test getting actual {@link SchemaContext}.
98      *
99      * <p>
100      * Get <code>SchemaContext</code> from <code>SchemaContextHandler</code> and compare it to actual
101      * <code>SchemaContext</code>.
102      */
103     @Test
104     public void getSchemaContextTest() {
105         assertEquals("SchemaContextHandler should has reference to actual SchemaContext",
106                 SCHEMA_CONTEXT, schemaContextHandler.get());
107     }
108
109     /**
110      * Test updating of {@link SchemaContext}.
111      *
112      * <p>
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.
115      */
116     @Test
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);
122
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());
127     }
128
129     @Test
130     public void restconfStateCapabilitesTest() {
131         final ContainerNode normNode = SchemaContextHandler.mapCapabilites();
132
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()))
137             .findFirst()
138             .map(ContainerNode.class::cast)
139             .orElseThrow()
140             // Find 'capability' leaf-list
141             .body().stream()
142             .filter(child -> SchemaContextHandler.CAPABILITY_QNAME.equals(child.getIdentifier().getNodeType()))
143             .findFirst()
144             .orElseThrow();
145
146         assertThat(
147             capability.body().stream().map(entry -> ((LeafSetEntryNode<?>) entry).body()).collect(Collectors.toList()),
148             containsInAnyOrder(
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")));
157     }
158
159 }