Expose streams with all supported encodings
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / streams / RestconfStateStreamsTest.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.streams;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
13
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Set;
17 import org.junit.jupiter.api.Test;
18 import org.opendaylight.restconf.nb.rfc8040.streams.RestconfStream.EncodingName;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.module.Deviation;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.Revision;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Unit tests for {@link RestconfStateStreams}.
34  */
35 class RestconfStateStreamsTest {
36     private static final Logger LOG = LoggerFactory.getLogger(RestconfStateStreamsTest.class);
37     private static final EffectiveModelContext CONTEXT =
38         // TODO: assemble these from dependencies?
39         YangParserTestUtils.parseYangResourceDirectory("/modules/restconf-module-testing");
40
41     @Test
42     void toStreamEntryNodeTest() throws Exception {
43         final var outputType = "XML";
44         final var uri = "uri";
45         final var streamName = "/nested-module:depth1-cont/depth2-leaf1";
46
47         assertMappedData(prepareMap(streamName, uri, outputType),
48             ListenersBroker.streamEntry(streamName, "description", "location", Set.of(new EncodingName(outputType))));
49     }
50
51     @Test
52     void toStreamEntryNodeNotifiTest() throws Exception {
53         final var outputType = "JSON";
54         final var uri = "uri";
55
56         assertMappedData(prepareMap("notifi", uri, outputType),
57             ListenersBroker.streamEntry("notifi", "description", "location", Set.of(new EncodingName(outputType))));
58     }
59
60     private static Map<QName, Object> prepareMap(final String name, final String uri, final String outputType) {
61         return Map.of(
62             ListenersBroker.NAME_QNAME, name,
63             ListenersBroker.LOCATION_QNAME, uri,
64             ListenersBroker.ENCODING_QNAME, outputType,
65             ListenersBroker.DESCRIPTION_QNAME, "description");
66     }
67
68     private static void assertMappedData(final Map<QName, Object> map, final MapEntryNode mappedData) {
69         assertNotNull(mappedData);
70         for (var child : mappedData.body()) {
71             if (child instanceof LeafNode<?> leaf) {
72                 assertTrue(map.containsKey(leaf.name().getNodeType()));
73                 assertEquals(map.get(leaf.name().getNodeType()), leaf.body());
74             }
75         }
76     }
77
78     /**
79      * Verify whether the loaded modules contain any deviations.
80      *
81      * @param containerNode
82      *             modules
83      */
84     // FIXME: what is this supposed to verify?
85     private static void verifyDeviations(final ContainerNode containerNode) {
86         int deviationsFound = 0;
87         for (var child : containerNode.body()) {
88             if (child instanceof MapNode mapChild) {
89                 for (var mapEntryNode : mapChild.body()) {
90                     for (var dataContainerChild : mapEntryNode.body()) {
91                         if (dataContainerChild.name().getNodeType().equals(Deviation.QNAME)) {
92                             deviationsFound++;
93                         }
94                     }
95                 }
96             }
97         }
98         assertTrue(deviationsFound > 0);
99     }
100
101     /**
102      * Verify loaded modules.
103      *
104      * @param containerNode
105      *             modules
106      */
107     // FIXME: what is this supposed to verify?
108     private static void verifyLoadedModules(final ContainerNode containerNode) {
109         final var loadedModules = new HashMap<String, String>();
110
111         for (var child : containerNode.body()) {
112             if (child instanceof LeafNode) {
113                 assertEquals(QName.create(Module.QNAME, "module-set-id"), child.name().getNodeType());
114             }
115             if (child instanceof MapNode mapChild) {
116                 assertEquals(Module.QNAME, child.name().getNodeType());
117                 for (var mapEntryNode : mapChild.body()) {
118                     String name = "";
119                     String revision = "";
120                     for (var dataContainerChild : mapEntryNode.body()) {
121                         switch (dataContainerChild.name().getNodeType().getLocalName()) {
122                             case "name":
123                                 name = String.valueOf(dataContainerChild.body());
124                                 break;
125                             case "revision":
126                                 revision = String.valueOf(dataContainerChild.body());
127                                 break;
128                             default :
129                                 LOG.info("Unknown local name '{}' of node.",
130                                     dataContainerChild.name().getNodeType().getLocalName());
131                                 break;
132                         }
133                     }
134                     loadedModules.put(name, revision);
135                 }
136             }
137         }
138
139         final var expectedModules = CONTEXT.getModules();
140         assertEquals(expectedModules.size(), loadedModules.size());
141         for (var m : expectedModules) {
142             final String name = m.getName();
143             final String revision = loadedModules.get(name);
144             assertNotNull("Expected module not found", revision);
145             assertEquals(Revision.ofNullable(revision), m.getRevision());
146
147             loadedModules.remove(name);
148         }
149     }
150 }