f650f85a567426cce83910cff4a9b8b03fec7ebc
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / utils / mapping / RestconfMappingNodeUtilTest.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.utils.mapping;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.when;
13 import java.net.URI;
14 import java.text.SimpleDateFormat;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Date;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
31 import org.opendaylight.restconf.Draft18.IetfYangLibrary;
32 import org.opendaylight.restconf.Draft18.MonitoringModule;
33 import org.opendaylight.restconf.Draft18.MonitoringModule.QueryParams;
34 import org.opendaylight.restconf.Draft18.RestconfModule;
35 import org.opendaylight.restconf.utils.parser.ParserIdentifier;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
41 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
43 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
49 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.Module;
52 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
53
54 /**
55  * Unit tests for {@link RestconfMappingNodeUtil}
56  */
57 public class RestconfMappingNodeUtilTest {
58     @Rule
59     public ExpectedException thrown = ExpectedException.none();
60
61     @Mock private ListSchemaNode mockStreamList;
62     @Mock private LeafSchemaNode leafName;
63     @Mock private LeafSchemaNode leafDescription;
64     @Mock private LeafSchemaNode leafReplaySupport;
65     @Mock private LeafSchemaNode leafReplayLog;
66     @Mock private LeafSchemaNode leafEvents;
67
68     private static Set<Module> modules;
69     private static SchemaContext schemaContext;
70     private static SchemaContext schemaContextMonitoring;
71
72     private static Set<Module> modulesRest;
73
74     @BeforeClass
75     public static void loadTestSchemaContextAndModules() throws Exception {
76         schemaContext = TestRestconfUtils.loadSchemaContext(
77                 "/modules/restconf-module-testing");
78         schemaContextMonitoring = TestRestconfUtils.loadSchemaContext("/modules");
79         modules = schemaContextMonitoring.getModules();
80         modulesRest = TestRestconfUtils.loadSchemaContext("/modules/restconf-module-testing").getModules();
81     }
82
83     @Before
84     public void setup() throws Exception {
85         MockitoAnnotations.initMocks(this);
86
87         when(this.leafName.getQName()).thenReturn(QName.create("", RestconfMappingNodeConstants.NAME));
88         when(this.leafDescription.getQName()).thenReturn(QName.create("", RestconfMappingNodeConstants.DESCRIPTION));
89         when(this.leafReplaySupport.getQName()).thenReturn(
90                 QName.create("", RestconfMappingNodeConstants.REPLAY_SUPPORT));
91         when(this.leafReplayLog.getQName()).thenReturn(QName.create(RestconfMappingNodeConstants.REPLAY_LOG));
92         when(this.leafEvents.getQName()).thenReturn(QName.create("", RestconfMappingNodeConstants.EVENTS));
93     }
94
95     /**
96      * Test of writing modules into {@link RestconfModule#MODULE_LIST_SCHEMA_NODE} and checking if modules were
97      * correctly written.
98      */
99     @Test
100     public void restconfMappingNodeTest() {
101         // write modules into list module in Restconf
102         final Module ietfYangLibMod =
103                 schemaContext.findModuleByNamespaceAndRevision(IetfYangLibrary.URI_MODULE, IetfYangLibrary.DATE);
104         final NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> modules =
105                 RestconfMappingNodeUtil.mapModulesByIetfYangLibraryYang(RestconfMappingNodeUtilTest.modules,
106                         ietfYangLibMod, schemaContext, "1");
107
108         // verify loaded modules
109         verifyLoadedModules((ContainerNode) modules);
110     }
111
112     @Test
113     public void restconfStateCapabilitesTest() {
114         final Module monitoringModule = schemaContextMonitoring
115                 .findModuleByNamespaceAndRevision(MonitoringModule.URI_MODULE, MonitoringModule.DATE);
116         final NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode =
117                 RestconfMappingNodeUtil.mapCapabilites(monitoringModule);
118         assertNotNull(normNode);
119         final List<Object> listOfValues = new ArrayList<>();
120
121         for (final DataContainerChild<? extends PathArgument, ?> child : ((ContainerNode) normNode).getValue()) {
122             if (child.getNodeType().equals(MonitoringModule.CONT_CAPABILITES_QNAME)) {
123                 for (final DataContainerChild<? extends PathArgument, ?> dataContainerChild : ((ContainerNode) child)
124                         .getValue()) {
125                     for (final Object entry : ((LeafSetNode) dataContainerChild).getValue()) {
126                         listOfValues.add(((LeafSetEntryNode) entry).getValue());
127                     }
128                 }
129             }
130         }
131         Assert.assertTrue(listOfValues.contains(QueryParams.DEPTH));
132         Assert.assertTrue(listOfValues.contains(QueryParams.FIELDS));
133         Assert.assertTrue(listOfValues.contains(QueryParams.FILTER));
134         Assert.assertTrue(listOfValues.contains(QueryParams.REPLAY));
135         Assert.assertTrue(listOfValues.contains(QueryParams.WITH_DEFAULTS));
136     }
137
138     @Test
139     public void toStreamEntryNodeTest() throws Exception {
140         final YangInstanceIdentifier path =
141                 ParserIdentifier.toInstanceIdentifier("nested-module:depth1-cont/depth2-leaf1", schemaContextMonitoring, null).getInstanceIdentifier();
142         final Date start = new Date();
143         final String outputType = "XML";
144         final URI uri = new URI("uri");
145         final Module monitoringModule = schemaContextMonitoring
146                 .findModuleByNamespaceAndRevision(MonitoringModule.URI_MODULE, MonitoringModule.DATE);
147         final boolean exist = true;
148
149         final Map<QName, Object> map =
150                 prepareMap(path.getLastPathArgument().getNodeType().getLocalName(), uri, start, outputType);
151
152         final NormalizedNode mappedData = RestconfMappingNodeUtil.mapDataChangeNotificationStreamByIetfRestconfMonitoring(path, start, outputType, uri,
153                 monitoringModule, exist, schemaContextMonitoring);
154         assertNotNull(mappedData);
155         testData(map, mappedData);
156     }
157
158     @Test
159     public void toStreamEntryNodeNotifiTest() throws Exception {
160         final Date start = new Date();
161         final String outputType = "JSON";
162         final URI uri = new URI("uri");
163         final Module monitoringModule = schemaContextMonitoring
164                 .findModuleByNamespaceAndRevision(MonitoringModule.URI_MODULE, MonitoringModule.DATE);
165         final boolean exist = true;
166
167         final Map<QName, Object> map = prepareMap("notifi", uri, start, outputType);
168         map.put(MonitoringModule.LEAF_DESCR_STREAM_QNAME, "Notifi");
169
170         final QName notifiQName = QName.create("urn:nested:module", "2014-06-3", "notifi");
171         final NormalizedNode mappedData =
172                 RestconfMappingNodeUtil.mapYangNotificationStreamByIetfRestconfMonitoring(notifiQName, schemaContextMonitoring.getNotifications(), start,
173                         outputType, uri, monitoringModule, exist);
174         assertNotNull(mappedData);
175         testData(map, mappedData);
176     }
177
178     private Map<QName, Object> prepareMap(final String name, final URI uri, final Date start, final String outputType) {
179         final Map<QName, Object> map = new HashMap<>();
180         map.put(MonitoringModule.LEAF_NAME_STREAM_QNAME, name);
181         map.put(MonitoringModule.LEAF_LOCATION_ACCESS_QNAME, uri.toString());
182         map.put(MonitoringModule.LEAF_REPLAY_SUPP_STREAM_QNAME, true);
183         map.put(MonitoringModule.LEAF_START_TIME_STREAM_QNAME,
184                 new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'XXX").format(start));
185         map.put(MonitoringModule.LEAF_ENCODING_ACCESS_QNAME, outputType);
186         return map;
187     }
188
189     private void testData(final Map<QName, Object> map, final NormalizedNode mappedData) {
190         for (final DataContainerChild<? extends PathArgument, ?> child : ((MapEntryNode) mappedData).getValue()) {
191             if (child instanceof LeafNode) {
192                 final LeafNode leaf = ((LeafNode) child);
193                 Assert.assertTrue(map.containsKey(leaf.getNodeType()));
194                 Assert.assertEquals(map.get(leaf.getNodeType()), leaf.getValue());
195             }
196         }
197     }
198
199     /**
200      * Verify loaded modules
201      *
202      * @param containerNode
203      *            - modules
204      */
205     private void verifyLoadedModules(final ContainerNode containerNode) {
206
207         final Map<String, String> loadedModules = new HashMap<>();
208
209         for (final DataContainerChild<? extends PathArgument, ?> child : containerNode.getValue()) {
210             if (child instanceof LeafNode) {
211                 assertEquals(IetfYangLibrary.MODULE_SET_ID_LEAF_QNAME, ((LeafNode) child).getNodeType());
212             }
213             if (child instanceof MapNode) {
214                 assertEquals(IetfYangLibrary.MODULE_QNAME_LIST, ((MapNode) child).getNodeType());
215                 for (final MapEntryNode mapEntryNode : ((MapNode) child).getValue()) {
216                     String name = "";
217                     String revision = "";
218                     for (final DataContainerChild<? extends PathArgument, ?> dataContainerChild : mapEntryNode
219                             .getValue()) {
220                         switch (dataContainerChild.getNodeType().getLocalName()) {
221                             case IetfYangLibrary.SPECIFIC_MODULE_NAME_LEAF:
222                                 name = String.valueOf(((LeafNode) dataContainerChild).getValue());
223                                 break;
224                             case IetfYangLibrary.SPECIFIC_MODULE_REVISION_LEAF:
225                                 revision = String.valueOf(((LeafNode) dataContainerChild).getValue());
226                                 break;
227                         }
228                     }
229                     loadedModules.put(name, revision);
230                 }
231             }
232         }
233
234         verifyLoadedModules(RestconfMappingNodeUtilTest.modulesRest, loadedModules);
235     }
236
237     /**
238      * Verify if correct modules were loaded into Restconf module by comparison with modules from
239      * <code>SchemaContext</code>.
240      * @param expectedModules Modules from <code>SchemaContext</code>
241      * @param loadedModules Loaded modules into Restconf module
242      */
243     private final void verifyLoadedModules(final Set<Module> expectedModules,
244                                            final Map<String, String> loadedModules) {
245         assertEquals("Number of loaded modules is not as expected", expectedModules.size(), loadedModules.size());
246         for (final Module m : expectedModules) {
247             final String name = m.getName();
248
249             final String revision = loadedModules.get(name);
250             assertNotNull("Expected module not found", revision);
251             assertEquals("Not correct revision of loaded module",
252                     SimpleDateFormatUtil.getRevisionFormat().format(m.getRevision()), revision);
253
254             loadedModules.remove(name);
255         }
256     }
257 }