31f1638d9c7f87c290923aaacbc61f99cc1a424b
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / 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.nb.rfc8040.rests.services.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.util.Set;
15 import javax.ws.rs.core.UriInfo;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.junit.MockitoJUnitRunner;
21 import org.opendaylight.mdsal.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.SchemaContextHandler;
26 import org.opendaylight.yangtools.yang.common.Empty;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.QNameModule;
29 import org.opendaylight.yangtools.yang.common.XMLNamespace;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
34
35 @RunWith(MockitoJUnitRunner.StrictStubs.class)
36 public class RestconfOperationsServiceTest {
37
38     @Mock
39     private DOMMountPointService domMountPointService;
40
41     @Mock
42     private UriInfo uriInfo;
43
44     private EffectiveModelContext schemaContext;
45     private SchemaContextHandler schemaContextHandler;
46
47     private Set<QName> listOfRpcsNames;
48
49     @Before
50     public void init() throws Exception {
51         this.schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/modules"));
52         this.schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
53
54         final QNameModule module1 = QNameModule.create(XMLNamespace.of("module:1"));
55         final QNameModule module2 = QNameModule.create(XMLNamespace.of("module:2"));
56
57         this.listOfRpcsNames = ImmutableSet.of(QName.create(module1, "dummy-rpc1-module1"),
58                 QName.create(module1, "dummy-rpc2-module1"), QName.create(module2, "dummy-rpc1-module2"),
59                 QName.create(module2, "dummy-rpc2-module2"));
60     }
61
62     @Test
63     public void getOperationsTest() {
64         final RestconfOperationsServiceImpl oper =
65                 new RestconfOperationsServiceImpl(this.schemaContextHandler, this.domMountPointService);
66         final NormalizedNodeContext operations = oper.getOperations(this.uriInfo);
67         final ContainerNode data = (ContainerNode) operations.getData();
68         assertEquals("urn:ietf:params:xml:ns:yang:ietf-restconf",
69             data.getIdentifier().getNodeType().getNamespace().toString());
70         assertEquals("operations", data.getIdentifier().getNodeType().getLocalName());
71
72         assertEquals(4, data.body().size());
73
74         for (final DataContainerChild child : data.body()) {
75             assertEquals(Empty.getInstance(), child.body());
76
77             final QName qname = child.getIdentifier().getNodeType().withoutRevision();
78             assertTrue(this.listOfRpcsNames.contains(qname));
79         }
80     }
81 }