e1402a9207bf68222057334c7ea5b71841ca7ef2
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / Netconf799Test.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech s.r.o. 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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNull;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.ArgumentMatchers.eq;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18
19 import com.google.common.util.concurrent.Futures;
20 import java.io.FileNotFoundException;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.junit.MockitoJUnitRunner;
24 import org.opendaylight.mdsal.dom.api.DOMActionService;
25 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
26 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
27 import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
28 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
29 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
30 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
31 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
32 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
33 import org.opendaylight.restconf.nb.rfc8040.streams.Configuration;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.common.Uint32;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
38 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
41 import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
42 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
43 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
44 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45
46 @RunWith(MockitoJUnitRunner.StrictStubs.class)
47 public class Netconf799Test {
48     private static final QName CONT_QNAME = QName.create("instance:identifier:module", "2014-01-17", "cont");
49     private static final QName CONT1_QNAME = QName.create(CONT_QNAME, "cont1");
50     private static final QName RESET_QNAME = QName.create(CONT_QNAME, "reset");
51
52     private static final QName DELAY_QNAME = QName.create(CONT_QNAME, "delay");
53     private static final QName INPUT_QNAME = QName.create(CONT_QNAME, "input");
54     private static final QName OUTPUT_QNAME = QName.create(CONT_QNAME, "output");
55     private static final YangInstanceIdentifier ACTION_YII = YangInstanceIdentifier.of(CONT_QNAME).node(CONT1_QNAME);
56
57     @Test
58     public void testInvokeAction() throws FileNotFoundException {
59         final EffectiveModelContext contextRef = YangParserTestUtils.parseYangFiles(
60             TestRestconfUtils.loadFiles("/instanceidentifier/yang"));
61
62         final DOMDataBroker mockDataBroker = mock(DOMDataBroker.class);
63
64         final DOMActionService actionService = mock(DOMActionService.class);
65         doReturn(Futures.immediateFuture(new SimpleDOMActionResult(
66             Builders.containerBuilder().withNodeIdentifier(NodeIdentifier.create(OUTPUT_QNAME)).build())))
67             .when(actionService).invokeAction(eq(Absolute.of(CONT_QNAME, CONT1_QNAME, RESET_QNAME)), any(), any());
68
69         final RestconfDataServiceImpl dataService = new RestconfDataServiceImpl(
70             () -> DatabindContext.ofModel(contextRef), mockDataBroker, mock(DOMMountPointService.class),
71             mock(RestconfStreamsSubscriptionService.class), actionService, mock(Configuration.class));
72
73         final var nodeAndStack = DataSchemaContextTree.from(contextRef).enterPath(ACTION_YII).orElseThrow();
74         final var node = nodeAndStack.node().getDataSchemaNode();
75         assertThat(node, instanceOf(ActionNodeContainer.class));
76         final var actionNode = ((ActionNodeContainer) node).findAction(RESET_QNAME).orElseThrow();
77         final var stack = nodeAndStack.stack();
78         stack.enterSchemaTree(RESET_QNAME);
79
80         final var response = dataService.invokeAction(NormalizedNodePayload.of(
81             InstanceIdentifierContext.ofPath(stack, actionNode, ACTION_YII, null),
82             Builders.containerBuilder()
83                 .withNodeIdentifier(NodeIdentifier.create(INPUT_QNAME))
84                 .withChild(ImmutableNodes.leafNode(DELAY_QNAME, Uint32.TEN))
85                 .build()));
86
87         assertEquals(204, response.getStatus());
88         assertNull(response.getEntity());
89     }
90 }