433684041f49d37173444d16b30c95d68e5f2231
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestconfImplTest.java
1 /*
2  * Copyright (c) 2014 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.controller.sal.restconf.impl.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyBoolean;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
19
20 import com.google.common.base.Optional;
21 import com.google.common.collect.Lists;
22 import com.google.common.collect.Sets;
23 import com.google.common.util.concurrent.Futures;
24 import java.io.FileNotFoundException;
25 import java.net.URI;
26 import java.text.ParseException;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import java.util.Set;
34 import javax.ws.rs.core.MultivaluedHashMap;
35 import javax.ws.rs.core.MultivaluedMap;
36 import javax.ws.rs.core.UriBuilder;
37 import javax.ws.rs.core.UriInfo;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.mockito.Mockito;
42 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
43 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
44 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
45 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
46 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
47 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
48 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
49 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
50 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
51 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
52 import org.opendaylight.restconf.common.errors.RestconfError;
53 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
54 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
55 import org.opendaylight.yangtools.yang.common.QName;
56 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
58 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
60 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
62 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
64 import org.opendaylight.yangtools.yang.model.api.Module;
65 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
67 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
68 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
69 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
70
71 /**
72  * See {@link InvokeRpcMethodTest}.
73  */
74 public class RestconfImplTest {
75
76     private RestconfImpl restconfImpl = null;
77     private static ControllerContext controllerContext = null;
78
79     @BeforeClass
80     public static void init() throws FileNotFoundException, ReactorException {
81         final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
82
83         final Set<Module> allModules = schemaContext.getModules();
84         assertNotNull(allModules);
85
86         controllerContext = ControllerContext.getInstance();
87         controllerContext.setSchemas(schemaContext);
88     }
89
90     @Before
91     public void initMethod() {
92         this.restconfImpl = RestconfImpl.getInstance();
93         this.restconfImpl.setControllerContext(controllerContext);
94     }
95
96     @Test
97     public void binaryKeyTest() {
98         final List<Byte> al = new ArrayList<>();
99         al.add(new Byte((byte) 1));
100         binaryKeyTest(al, al);
101     }
102
103     private void binaryKeyTest(final List<Byte> al, final List<Byte> al2) {
104
105         final QName keyDef = QName.create("test:key:binary", "2017-08-14", "b1");
106
107         final Map<QName, Object> uriKeyValues = new HashMap<>();
108         uriKeyValues.put(keyDef, al.toArray());
109
110         final MapEntryNode payload = mock(MapEntryNode.class);
111         final NodeIdentifierWithPredicates nodeIdWithPred =
112                 new NodeIdentifierWithPredicates(keyDef, keyDef, al2.toArray());
113         when(payload.getIdentifier()).thenReturn(nodeIdWithPred);
114
115         final List<QName> keyDefinitions = new ArrayList<>();
116         keyDefinitions.add(keyDef);
117         RestconfImpl.isEqualUriAndPayloadKeyValues(uriKeyValues, payload, keyDefinitions);
118     }
119
120     @Test
121     public void binaryKeyFailTest() {
122         final List<Byte> al = new ArrayList<>();
123         al.add(new Byte((byte) 1));
124         final List<Byte> al2 = new ArrayList<>();
125         try {
126             binaryKeyTest(al, al2);
127         } catch (final RestconfDocumentedException e) {
128             final RestconfError err = e.getErrors().iterator().next();
129             assertEquals(ErrorType.PROTOCOL, err.getErrorType());
130             assertEquals(ErrorTag.INVALID_VALUE, err.getErrorTag());
131         }
132     }
133
134     @SuppressWarnings("unchecked")
135     @Test
136     public void testExample() throws FileNotFoundException, ParseException {
137         @SuppressWarnings("rawtypes")
138         final NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
139         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
140         when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(normalizedNodeData);
141         assertEquals(normalizedNodeData,
142                 brokerFacade.readOperationalData(null));
143     }
144
145     @Test
146     public void testRpcForMountpoint() throws Exception {
147         final UriInfo uriInfo = mock(UriInfo.class);
148         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());
149
150         final NormalizedNodeContext ctx = mock(NormalizedNodeContext.class);
151         final InstanceIdentifierContext iiCtx = mock(InstanceIdentifierContext.class);
152         doReturn(iiCtx).when(ctx).getInstanceIdentifierContext();
153         final SchemaNode schemaNode = mock(SchemaNode.class);
154         doReturn(schemaNode).when(iiCtx).getSchemaNode();
155         doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
156         doReturn(QName.create("namespace", "2010-10-10", "localname")).when(schemaNode).getQName();
157
158         final DOMMountPoint mount = mock(DOMMountPoint.class);
159         doReturn(mount).when(iiCtx).getMountPoint();
160         final DOMRpcService rpcService = mock(DOMRpcService.class);
161         doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
162         doReturn(Futures.immediateCheckedFuture(mock(DOMRpcResult.class))).when(rpcService)
163                 .invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
164         this.restconfImpl.invokeRpc("randomId", ctx, uriInfo);
165         this.restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
166         verify(rpcService, times(2)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
167     }
168
169     /**
170      * Create notification stream for toaster module.
171      */
172     @Test
173     public void createNotificationStreamTest() {
174         final NormalizedNodeContext payload = mock(NormalizedNodeContext.class);
175         final InstanceIdentifierContext iiCtx = mock(InstanceIdentifierContext.class);
176         doReturn(iiCtx).when(payload).getInstanceIdentifierContext();
177
178         final SchemaNode schemaNode = mock(SchemaNode.class,
179                 Mockito.withSettings().extraInterfaces(RpcDefinition.class));
180         doReturn(schemaNode).when(iiCtx).getSchemaNode();
181         doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
182
183         doReturn(QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote",
184                 "2014-01-14", "create-notification-stream")).when(schemaNode).getQName();
185         doReturn(null).when(iiCtx).getMountPoint();
186
187         final Set<DataContainerChild<?, ?>> children = Sets.newHashSet();
188         final DataContainerChild<?, ?> child = mock(DataContainerChild.class,
189                 Mockito.withSettings().extraInterfaces(LeafSetNode.class));
190
191         final LeafSetEntryNode entryNode = mock(LeafSetEntryNode.class);
192         when(entryNode.getValue()).thenReturn("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)toastDone");
193         when(((LeafSetNode) child).getValue()).thenReturn(Sets.newHashSet(entryNode));
194         children.add(child);
195
196         final NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class,
197                 Mockito.withSettings().extraInterfaces(ContainerNode.class));
198         doReturn(normalizedNode).when(payload).getData();
199         doReturn(children).when(normalizedNode).getValue();
200
201         // register notification
202         final NormalizedNodeContext context = this.restconfImpl
203                 .invokeRpc("sal-remote:create-notification-stream", payload, null);
204         assertNotNull(context);
205     }
206
207     /**
208      * Subscribe for notification stream of toaster module.
209      */
210     @Test
211     public void subscribeToNotificationStreamTest() throws Exception {
212         final String identifier = "create-notification-stream/toaster:toastDone";
213
214         // register test notification stream
215         final SchemaPath path = SchemaPath.create(
216                 true, QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toastDone"));
217         Notificator.createNotificationListener(Lists.newArrayList(path), identifier, "XML");
218
219         final UriInfo uriInfo = mock(UriInfo.class);
220         final UriBuilder uriBuilder = mock(UriBuilder.class);
221         when(uriBuilder.port(8181)).thenReturn(uriBuilder);
222         when(uriBuilder.replacePath(identifier)).thenReturn(uriBuilder);
223         when(uriBuilder.build()).thenReturn(new URI(""));
224         when(uriBuilder.scheme("ws")).thenReturn(uriBuilder);
225         when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
226         final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
227         final Set<Entry<String, List<String>>> set = new HashSet<>();
228         when(map.entrySet()).thenReturn(set);
229         when(uriInfo.getQueryParameters()).thenReturn(map);
230
231         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
232         this.restconfImpl.setBroker(brokerFacade);
233
234         // subscribe to stream and verify response
235         final NormalizedNodeContext response = this.restconfImpl.subscribeToStream(identifier, uriInfo);
236
237         // remove test notification stream
238         Notificator.removeAllListeners();
239     }
240 }