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