Bug 5679 - add new module ietf-restconf
[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 import com.google.common.collect.ImmutableSet;
14 import com.google.common.util.concurrent.CheckedFuture;
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.Mockito;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
24 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
25 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
26 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
27 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
28 import org.opendaylight.restconf.base.services.impl.RestconfOperationsServiceImpl;
29 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
30 import org.opendaylight.restconf.handlers.SchemaContextHandler;
31 import org.opendaylight.restconf.handlers.TransactionChainHandler;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.QNameModule;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38
39 public class RestconfOperationsServiceTest {
40
41     @Mock
42     private DOMMountPointService domMountPointService;
43
44     @Mock
45     private UriInfo uriInfo;
46
47     private SchemaContext schemaContext;
48     private SchemaContextHandler schemaContextHandler;
49     private DOMMountPointServiceHandler domMountPointServiceHandler;
50
51     private Set<QName> listOfRpcsNames;
52
53     @Before
54     public void init() throws Exception {
55         MockitoAnnotations.initMocks(this);
56         this.schemaContext = TestRestconfUtils.loadSchemaContext("/modules");
57
58         final TransactionChainHandler txHandler = Mockito.mock(TransactionChainHandler.class);
59         final DOMTransactionChain domTx = Mockito.mock(DOMTransactionChain.class);
60         Mockito.when(txHandler.get()).thenReturn(domTx);
61         final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
62         Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
63         final CheckedFuture checked = Mockito.mock(CheckedFuture.class);
64         Mockito.when(wTx.submit()).thenReturn(checked);
65         final Object valueObj = null;
66         Mockito.when(checked.checkedGet()).thenReturn(valueObj);
67         this.schemaContextHandler = new SchemaContextHandler(txHandler);
68         this.schemaContextHandler.onGlobalContextUpdated(this.schemaContext);
69
70         this.domMountPointServiceHandler = new DOMMountPointServiceHandler(this.domMountPointService);
71
72         final QNameModule module1 = QNameModule.create(new URI("module:1"), null);
73         final QNameModule module2 = QNameModule.create(new URI("module:2"), null);
74
75         this.listOfRpcsNames = ImmutableSet.of(QName.create(module1, "dummy-rpc1-module1"),
76                 QName.create(module1, "dummy-rpc2-module1"), QName.create(module2, "dummy-rpc1-module2"),
77                 QName.create(module2, "dummy-rpc2-module2"));
78     }
79
80     @Test
81     public void getOperationsTest() {
82         final RestconfOperationsServiceImpl oper =
83                 new RestconfOperationsServiceImpl(this.schemaContextHandler, this.domMountPointServiceHandler);
84         final NormalizedNodeContext operations = oper.getOperations(this.uriInfo);
85         final ContainerNode data = (ContainerNode) operations.getData();
86         assertEquals("urn:ietf:params:xml:ns:yang:ietf-restconf", data.getNodeType().getNamespace().toString());
87         assertEquals("operations", data.getNodeType().getLocalName());
88
89         assertEquals(4, data.getValue().size());
90
91         for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
92             assertNull(child.getValue());
93
94             final QName qname = child.getNodeType().withoutRevision();
95             assertTrue(this.listOfRpcsNames.contains(qname));
96         }
97     }
98 }