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