Split Restconf implementations (draft02 and RFC) - tests
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / NotificationListenerTest.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.listeners;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
17 import com.google.common.collect.Lists;
18 import java.net.URI;
19 import java.text.ParseException;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
29 import org.opendaylight.restconf.nb.rfc8040.TestUtils;
30 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
31 import org.opendaylight.yangtools.util.SingletonSet;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.QNameModule;
34 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
39 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
42 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47
48 public class NotificationListenerTest {
49     private static final QNameModule MODULE;
50
51     static {
52         try {
53             MODULE = QNameModule.create(URI.create("notifi:mod"),
54                 SimpleDateFormatUtil.getRevisionFormat().parse("2016-11-23"));
55         } catch (final ParseException e) {
56             throw new ExceptionInInitializerError(e);
57         }
58     }
59
60     private SchemaContext schmeaCtx;
61
62     @Before
63     public void init() throws Exception {
64         MockitoAnnotations.initMocks(this);
65         this.schmeaCtx = TestUtils.loadSchemaContext("/notifications");
66     }
67
68     @Test
69     public void notifi_leafTest() throws Exception {
70         final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-leaf"));
71
72         final DOMNotification notificationData = mock(DOMNotification.class);
73
74         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
75         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
76
77         when(notificationData.getType()).thenReturn(schemaPathNotifi);
78         when(notificationData.getBody()).thenReturn(notifiBody);
79
80         final String result = prepareJson(notificationData, schemaPathNotifi);
81
82         assertTrue(result.contains("ietf-restconf:notification"));
83         assertTrue(result.contains("event-time"));
84         assertTrue(result.contains("notifi-module:notifi-leaf"));
85         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
86     }
87
88     @Test
89     public void notifi_cont_leafTest() throws Exception {
90         final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-cont"));
91
92         final DOMNotification notificationData = mock(DOMNotification.class);
93
94         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
95         final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
96         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), cont);
97
98         when(notificationData.getType()).thenReturn(schemaPathNotifi);
99         when(notificationData.getBody()).thenReturn(notifiBody);
100
101         final String result = prepareJson(notificationData, schemaPathNotifi);
102
103         assertTrue(result.contains("ietf-restconf:notification"));
104         assertTrue(result.contains("event-time"));
105         assertTrue(result.contains("notifi-module:notifi-cont"));
106         assertTrue(result.contains("cont"));
107         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
108     }
109
110     @Test
111     public void notifi_list_Test() throws Exception {
112         final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-list"));
113
114         final DOMNotification notificationData = mock(DOMNotification.class);
115
116         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
117         final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
118         final MapNode list = mockList(QName.create(MODULE, "lst"), entry);
119         final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), list);
120         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), cont);
121
122         when(notificationData.getType()).thenReturn(schemaPathNotifi);
123         when(notificationData.getBody()).thenReturn(notifiBody);
124
125         final String result = prepareJson(notificationData, schemaPathNotifi);
126
127         assertTrue(result.contains("ietf-restconf:notification"));
128         assertTrue(result.contains("event-time"));
129         assertTrue(result.contains("notifi-module:notifi-list"));
130         assertTrue(result.contains("lst"));
131         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
132     }
133
134     @Test
135     public void notifi_grpTest() throws Exception {
136         final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-grp"));
137
138         final DOMNotification notificationData = mock(DOMNotification.class);
139
140         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
141         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
142
143         when(notificationData.getType()).thenReturn(schemaPathNotifi);
144         when(notificationData.getBody()).thenReturn(notifiBody);
145
146         final String result = prepareJson(notificationData, schemaPathNotifi);
147
148         assertTrue(result.contains("ietf-restconf:notification"));
149         assertTrue(result.contains("event-time"));
150         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
151     }
152
153     @Test
154     public void notifi_augmTest() throws Exception {
155         final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-augm"));
156
157         final DOMNotification notificationData = mock(DOMNotification.class);
158
159         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
160         final AugmentationNode augm = mockAugm(leaf);
161         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), augm);
162
163         when(notificationData.getType()).thenReturn(schemaPathNotifi);
164         when(notificationData.getBody()).thenReturn(notifiBody);
165
166         final String result = prepareJson(notificationData, schemaPathNotifi);
167
168         assertTrue(result.contains("ietf-restconf:notification"));
169         assertTrue(result.contains("event-time"));
170         assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
171     }
172
173     private static AugmentationNode mockAugm(final LeafNode<String> leaf) {
174         final AugmentationNode augm = mock(AugmentationNode.class);
175         final AugmentationIdentifier augmId = new AugmentationIdentifier(SingletonSet.of(leaf.getNodeType()));
176         when(augm.getIdentifier()).thenReturn(augmId);
177
178         final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
179         childs.add(leaf);
180
181         when(augm.getValue()).thenReturn(childs);
182         return augm;
183     }
184
185     private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
186         final MapEntryNode entry = mock(MapEntryNode.class);
187         final Map<QName, Object> keyValues = new HashMap<>();
188         keyValues.put(leaf.getNodeType(), "value");
189         final NodeIdentifierWithPredicates nodeId = new NodeIdentifierWithPredicates(leaf.getNodeType(), keyValues);
190         when(entry.getIdentifier()).thenReturn(nodeId);
191         when(entry.getChild(any())).thenReturn(Optional.of(leaf));
192
193         final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
194         childs.add(leaf);
195
196         when(entry.getValue()).thenReturn(childs);
197         return entry;
198     }
199
200     private static MapNode mockList(final QName listQName, final MapEntryNode... entries) {
201         final MapNode list = mock(MapNode.class);
202         when(list.getIdentifier()).thenReturn(NodeIdentifier.create(listQName));
203         when(list.getNodeType()).thenReturn(listQName);
204         when(list.getValue()).thenReturn(Lists.newArrayList(entries));
205         return list;
206     }
207
208     private static ContainerNode mockCont(final QName contQName,
209                                           final DataContainerChild<? extends PathArgument, ?> child) {
210         final ContainerNode cont = mock(ContainerNode.class);
211         when(cont.getIdentifier()).thenReturn(NodeIdentifier.create(contQName));
212         when(cont.getNodeType()).thenReturn(contQName);
213
214         final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
215         childs.add(child);
216         when(cont.getValue()).thenReturn(childs);
217         return cont;
218     }
219
220     private static LeafNode<String> mockLeaf(final QName leafQName) {
221         final LeafNode<String> child = mock(LeafNode.class);
222         when(child.getNodeType()).thenReturn(leafQName);
223         when(child.getIdentifier()).thenReturn(NodeIdentifier.create(leafQName));
224         when(child.getValue()).thenReturn("value");
225         return child;
226     }
227
228     private String prepareJson(final DOMNotification notificationData, final SchemaPath schemaPathNotifi)
229             throws Exception {
230         final List<SchemaPath> paths = new ArrayList<>();
231         paths.add(schemaPathNotifi);
232         final List<NotificationListenerAdapter> listNotifi =
233                 Notificator.createNotificationListener(paths, "stream-name", NotificationOutputType.JSON.toString());
234         final NotificationListenerAdapter notifi = listNotifi.get(0);
235         notifi.setNotification(notificationData);
236         notifi.setSchemaContext(this.schmeaCtx);
237         final String result = notifi.prepareJson();
238         return Preconditions.checkNotNull(result);
239     }
240 }