690cb3fa08e232e42c228c568995482ecd17a2e2
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / rest / services / impl / RestconfOperationsServiceTest.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.rest.services.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.collect.ImmutableSet;
15 import java.net.URI;
16 import java.util.Set;
17 import javax.ws.rs.core.UriInfo;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
23 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
24 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
25 import org.opendaylight.restconf.base.services.impl.RestconfOperationsServiceImpl;
26 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
27 import org.opendaylight.restconf.handlers.SchemaContextHandler;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.QNameModule;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34
35 public class RestconfOperationsServiceTest {
36
37     @Mock
38     private DOMMountPointService domMountPointService;
39
40     @Mock
41     private UriInfo uriInfo;
42
43     private SchemaContext schemaContext;
44     private SchemaContextHandler schemaContextHandler;
45     private DOMMountPointServiceHandler domMountPointServiceHandler;
46
47     private Set<QName> listOfRpcsNames;
48
49     @Before
50     public void init() throws Exception {
51         MockitoAnnotations.initMocks(this);
52         this.schemaContext = TestRestconfUtils.loadSchemaContext("/modules");
53         this.schemaContextHandler = new SchemaContextHandler();
54         this.schemaContextHandler.onGlobalContextUpdated(this.schemaContext);
55         this.domMountPointServiceHandler = new DOMMountPointServiceHandler(this.domMountPointService);
56
57         final QNameModule module1 = QNameModule.create(new URI("module:1"), null);
58         final QNameModule module2 = QNameModule.create(new URI("module:2"), null);
59
60         listOfRpcsNames = ImmutableSet.of(
61             QName.create(module1, "dummy-rpc1-module1"), QName.create(module1, "dummy-rpc2-module1"),
62             QName.create(module2, "dummy-rpc1-module2"), QName.create(module2, "dummy-rpc2-module2"));
63     }
64
65     @Test
66     public void getOperationsTest() {
67         final RestconfOperationsServiceImpl oper = new RestconfOperationsServiceImpl(this.schemaContextHandler,
68             this.domMountPointServiceHandler);
69         final NormalizedNodeContext operations = oper.getOperations(this.uriInfo);
70         final ContainerNode data = (ContainerNode) operations.getData();
71         assertEquals("urn:ietf:params:xml:ns:yang:ietf-restconf", data.getNodeType().getNamespace().toString());
72         assertEquals("operations", data.getNodeType().getLocalName());
73
74         assertEquals(4, data.getValue().size());
75
76         for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
77             assertNull(child.getValue());
78
79             final QName qname = child.getNodeType().withoutRevision();
80             assertTrue(listOfRpcsNames.contains(qname));
81         }
82     }
83 }