Split Restconf implementations (draft02 and RFC) - tests
[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.assertNull;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.util.concurrent.Futures;
16 import java.net.URI;
17 import java.util.Set;
18 import javax.ws.rs.core.UriInfo;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
26 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
27 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
28 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
29 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
30 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
31 import org.opendaylight.restconf.nb.rfc8040.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 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
39
40 public class RestconfOperationsServiceTest {
41
42     @Mock
43     private DOMMountPointService domMountPointService;
44
45     @Mock
46     private UriInfo uriInfo;
47
48     private SchemaContext schemaContext;
49     private SchemaContextHandler schemaContextHandler;
50     private DOMMountPointServiceHandler domMountPointServiceHandler;
51
52     private Set<QName> listOfRpcsNames;
53
54     @Before
55     public void init() throws Exception {
56         MockitoAnnotations.initMocks(this);
57         this.schemaContext = YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles("/modules"));
58
59         final TransactionChainHandler txHandler = Mockito.mock(TransactionChainHandler.class);
60         final DOMTransactionChain domTx = Mockito.mock(DOMTransactionChain.class);
61         Mockito.when(txHandler.get()).thenReturn(domTx);
62         final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
63         Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
64         Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
65         this.schemaContextHandler = new SchemaContextHandler(txHandler);
66         this.schemaContextHandler.onGlobalContextUpdated(this.schemaContext);
67
68         this.domMountPointServiceHandler = new DOMMountPointServiceHandler(this.domMountPointService);
69
70         final QNameModule module1 = QNameModule.create(new URI("module:1"), null);
71         final QNameModule module2 = QNameModule.create(new URI("module:2"), null);
72
73         this.listOfRpcsNames = ImmutableSet.of(QName.create(module1, "dummy-rpc1-module1"),
74                 QName.create(module1, "dummy-rpc2-module1"), QName.create(module2, "dummy-rpc1-module2"),
75                 QName.create(module2, "dummy-rpc2-module2"));
76     }
77
78     @Test
79     public void getOperationsTest() {
80         final RestconfOperationsServiceImpl oper =
81                 new RestconfOperationsServiceImpl(this.schemaContextHandler, this.domMountPointServiceHandler);
82         final NormalizedNodeContext operations = oper.getOperations(this.uriInfo);
83         final ContainerNode data = (ContainerNode) operations.getData();
84         assertEquals("urn:ietf:params:xml:ns:yang:ietf-restconf", data.getNodeType().getNamespace().toString());
85         assertEquals("operations", data.getNodeType().getLocalName());
86
87         assertEquals(4, data.getValue().size());
88
89         for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
90             assertNull(child.getValue());
91
92             final QName qname = child.getNodeType().withoutRevision();
93             assertTrue(this.listOfRpcsNames.contains(qname));
94         }
95     }
96 }