2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.monitoring;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
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.HashMap;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.module.Deviation;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.Revision;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
33 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
34 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Unit tests for {@link RestconfStateStreams}.
41 public class RestconfStateStreamsTest {
42 private static final Logger LOG = LoggerFactory.getLogger(RestconfStateStreamsTest.class);
44 private static EffectiveModelContext schemaContext;
45 private static EffectiveModelContext schemaContextMonitoring;
48 public static void loadTestSchemaContextAndModules() throws Exception {
49 // FIXME: assemble these from dependencies
50 schemaContext = YangParserTestUtils.parseYangResourceDirectory("/modules/restconf-module-testing");
51 schemaContextMonitoring = YangParserTestUtils.parseYangResourceDirectory("/modules");
55 public void toStreamEntryNodeTest() throws Exception {
56 final YangInstanceIdentifier path = ParserIdentifier.toInstanceIdentifier(
57 "nested-module:depth1-cont/depth2-leaf1", schemaContextMonitoring, null).getInstanceIdentifier();
58 final Instant start = Instant.now();
59 final String outputType = "XML";
60 final URI uri = new URI("uri");
61 final String streamName = "/nested-module:depth1-cont/depth2-leaf1";
63 final Map<QName, Object> map = prepareMap(streamName, uri, start, outputType);
64 final MapEntryNode mappedData = RestconfStateStreams.dataChangeStreamEntry(path, start, outputType, uri,
65 schemaContextMonitoring, streamName);
66 assertMappedData(map, mappedData);
70 public void toStreamEntryNodeNotifiTest() throws Exception {
71 final Instant start = Instant.now();
72 final String outputType = "JSON";
73 final URI uri = new URI("uri");
75 final Map<QName, Object> map = prepareMap("notifi", uri, start, outputType);
76 map.put(RestconfStateStreams.DESCRIPTION_QNAME, "Notifi");
78 final MapEntryNode mappedData = RestconfStateStreams.notificationStreamEntry(schemaContextMonitoring,
79 QName.create("urn:nested:module", "2014-06-03", "notifi"), start, outputType, uri);
80 assertMappedData(map, mappedData);
83 private static Map<QName, Object> prepareMap(final String name, final URI uri, final Instant start,
84 final String outputType) {
85 final Map<QName, Object> map = new HashMap<>();
86 map.put(RestconfStateStreams.NAME_QNAME, name);
87 map.put(RestconfStateStreams.LOCATION_QNAME, uri.toString());
88 map.put(RestconfStateStreams.REPLAY_SUPPORT_QNAME, Boolean.TRUE);
89 map.put(RestconfStateStreams.REPLAY_LOG_CREATION_TIME, DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(
90 OffsetDateTime.ofInstant(start, ZoneId.systemDefault())));
91 map.put(RestconfStateStreams.ENCODING_QNAME, outputType);
95 private static void assertMappedData(final Map<QName, Object> map, final MapEntryNode mappedData) {
96 assertNotNull(mappedData);
97 for (var child : mappedData.body()) {
98 if (child instanceof LeafNode<?> leaf) {
99 assertTrue(map.containsKey(leaf.name().getNodeType()));
100 assertEquals(map.get(leaf.name().getNodeType()), leaf.body());
106 * Verify whether the loaded modules contain any deviations.
108 * @param containerNode
111 private static void verifyDeviations(final ContainerNode containerNode) {
112 int deviationsFound = 0;
113 for (var child : containerNode.body()) {
114 if (child instanceof MapNode mapChild) {
115 for (var mapEntryNode : mapChild.body()) {
116 for (var dataContainerChild : mapEntryNode.body()) {
117 if (dataContainerChild.name().getNodeType().equals(Deviation.QNAME)) {
124 assertTrue(deviationsFound > 0);
128 * Verify loaded modules.
130 * @param containerNode
133 private static void verifyLoadedModules(final ContainerNode containerNode) {
134 final Map<String, String> loadedModules = new HashMap<>();
136 for (var child : containerNode.body()) {
137 if (child instanceof LeafNode) {
138 assertEquals(QName.create(Module.QNAME, "module-set-id"), child.name().getNodeType());
140 if (child instanceof MapNode mapChild) {
141 assertEquals(Module.QNAME, child.name().getNodeType());
142 for (var mapEntryNode : mapChild.body()) {
144 String revision = "";
145 for (var dataContainerChild : mapEntryNode.body()) {
146 switch (dataContainerChild.name().getNodeType().getLocalName()) {
148 name = String.valueOf(dataContainerChild.body());
151 revision = String.valueOf(dataContainerChild.body());
154 LOG.info("Unknown local name '{}' of node.",
155 dataContainerChild.name().getNodeType().getLocalName());
159 loadedModules.put(name, revision);
164 final var expectedModules = schemaContext.getModules();
165 assertEquals("Number of loaded modules is not as expected", expectedModules.size(), loadedModules.size());
166 for (var m : expectedModules) {
167 final String name = m.getName();
168 final String revision = loadedModules.get(name);
169 assertNotNull("Expected module not found", revision);
170 assertEquals("Incorrect revision of loaded module", Revision.ofNullable(revision), m.getRevision());
172 loadedModules.remove(name);