2 * Copyright (c) 2018 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.javav2.dom.adapter.impl.operation;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
14 import com.google.common.collect.ImmutableSet;
15 import java.util.concurrent.CountDownLatch;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.TimeUnit;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.mdsal.binding.javav2.api.DataTreeIdentifier;
21 import org.opendaylight.mdsal.binding.javav2.dom.adapter.test.BindingBrokerTestFactory;
22 import org.opendaylight.mdsal.binding.javav2.dom.adapter.test.BindingTestContext;
23 import org.opendaylight.mdsal.binding.javav2.spec.base.InstanceIdentifier;
24 import org.opendaylight.mdsal.binding.javav2.spec.base.RpcCallback;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.mdsal.dom.api.DOMActionNotAvailableException;
27 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
28 import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.TestOperationServiceInContAction;
29 import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.TestOperationServiceMyRpcRpc;
30 import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.data.MyCont;
31 import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.data.my_cont.in_cont.InContInput;
32 import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.data.my_cont.in_cont.InContOutput;
33 import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.data.my_rpc.MyRpcInput;
34 import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.data.my_rpc.MyRpcOutput;
35 import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.dto.my_cont.in_cont.InContInputBuilder;
36 import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.dto.my_cont.in_cont.InContOutputBuilder;
37 import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.dto.my_rpc.MyRpcInputBuilder;
38 import org.opendaylight.mdsal.gen.javav2.urn.test.operation.rev170621.dto.my_rpc.MyRpcOutputBuilder;
39 import org.opendaylight.yangtools.concepts.ObjectRegistration;
42 public class BindingDOMOperationAdapterTest {
43 private static final MyRpcOutput RPC_OUTPUT = new MyRpcOutputBuilder().setRpcOutputLeaf("rpc_output_leaf").build();
44 private static final InContOutput ACTION_OUTPUT =
45 new InContOutputBuilder().setOutputLeaf("action_output_leaf").build();
46 private BindingDOMOperationProviderServiceAdapter adapter;
47 private BindingDOMOperationServiceAdapter serviceAdapter;
50 public void setUp() throws Exception {
51 final BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
52 testFactory.setExecutor(Executors.newCachedThreadPool());
54 final BindingTestContext testContext = testFactory.getTestContext();
57 this.adapter = new BindingDOMOperationProviderServiceAdapter(testContext.getDomRpcRegistry(),
58 testContext.getDomActionRegistry(), testContext.getCodec());
60 this.serviceAdapter = new BindingDOMOperationServiceAdapter(testContext.getDomRpcInvoker(),
61 testContext.getDomActionService(), testContext.getCodec());
65 public void actionTest() throws Exception {
66 final CountDownLatch invokeLatch = new CountDownLatch(1);
67 final ObjectRegistration<?> registration = adapter.registerActionImplementation(
68 TestOperationServiceInContAction.class, new TestActionImpl(), LogicalDatastoreType.OPERATIONAL,
70 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(MyCont.class))));
71 assertNotNull(registration);
73 final TestOperationServiceInContAction action =
74 serviceAdapter.getActionService(TestOperationServiceInContAction.class);
75 assertNotNull(action);
77 action.invoke(new InContInputBuilder().setInputLeaf("in").build(),
78 InstanceIdentifier.create(MyCont.class),
79 new RpcCallback<InContOutput>() {
81 public void onSuccess(InContOutput output) {
82 assertEquals(ACTION_OUTPUT, output);
83 invokeLatch.countDown();
87 public void onFailure(Throwable cause) {
91 assertEquals("Invoke complete", true, invokeLatch.await(5, TimeUnit.SECONDS));
95 public void actionFailedTest() throws Exception {
97 final CountDownLatch invokeLatch = new CountDownLatch(1);
99 final ObjectRegistration<?> registration = adapter.registerActionImplementation(
100 TestOperationServiceInContAction.class, new TestActionFailedImpl(), LogicalDatastoreType.OPERATIONAL,
101 ImmutableSet.of(DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
102 InstanceIdentifier.create(MyCont.class))));
103 assertNotNull(registration);
105 final TestOperationServiceInContAction action =
106 serviceAdapter.getActionService(TestOperationServiceInContAction.class);
107 assertNotNull(action);
109 action.invoke(new InContInputBuilder().setInputLeaf("in").build(),
110 InstanceIdentifier.create(MyCont.class),
111 new RpcCallback<InContOutput>() {
113 public void onSuccess(InContOutput output) {
118 public void onFailure(Throwable cause) {
119 assertTrue(cause instanceof RuntimeException);
120 assertEquals(cause.getMessage(), "TestActionFailedImpl invoke failed.");
121 invokeLatch.countDown();
124 assertEquals("Invoke complete", true, invokeLatch.await(5, TimeUnit.SECONDS));
128 public void actionNotAvaliableTest() throws Exception {
130 final CountDownLatch invokeLatch = new CountDownLatch(1);
132 final TestOperationServiceInContAction action =
133 serviceAdapter.getActionService(TestOperationServiceInContAction.class);
134 assertNotNull(action);
136 action.invoke(new InContInputBuilder().setInputLeaf("in").build(),
137 InstanceIdentifier.create(MyCont.class),
138 new RpcCallback<InContOutput>() {
140 public void onSuccess(InContOutput output) {
145 public void onFailure(Throwable cause) {
146 assertTrue(cause instanceof DOMActionNotAvailableException);
147 invokeLatch.countDown();
150 assertEquals("Invoke complete", true, invokeLatch.await(5, TimeUnit.SECONDS));
154 public void rpcTest() throws Exception {
155 final CountDownLatch invokeLatch = new CountDownLatch(1);
157 assertNotNull(adapter.registerRpcImplementation(TestOperationServiceMyRpcRpc.class,
159 final TestOperationServiceMyRpcRpc rpc =
160 serviceAdapter.getRpcService(TestOperationServiceMyRpcRpc.class);
163 rpc.invoke(new MyRpcInputBuilder().setRpcInputLeaf("123").build(),
164 new RpcCallback<MyRpcOutput>() {
166 public void onSuccess(MyRpcOutput output) {
167 assertEquals(RPC_OUTPUT, output);
168 invokeLatch.countDown();
172 public void onFailure(Throwable cause) {
175 assertEquals("Invoke complete", true, invokeLatch.await(5, TimeUnit.SECONDS));
179 public void rpcFailedTest() throws Exception {
181 final CountDownLatch invokeLatch = new CountDownLatch(1);
183 assertNotNull(adapter.registerRpcImplementation(TestOperationServiceMyRpcRpc.class,
184 new TestRpcFailedImpl()));
185 final TestOperationServiceMyRpcRpc rpc =
186 serviceAdapter.getRpcService(TestOperationServiceMyRpcRpc.class);
189 rpc.invoke(new MyRpcInputBuilder().setRpcInputLeaf("123").build(),
190 new RpcCallback<MyRpcOutput>() {
192 public void onSuccess(MyRpcOutput output) {
197 public void onFailure(Throwable cause) {
198 assertTrue(cause instanceof RuntimeException);
199 assertEquals(cause.getMessage(), "TestRpcFailedImpl invoke failed.");
200 invokeLatch.countDown();
203 assertEquals("Invoke complete", true, invokeLatch.await(5, TimeUnit.SECONDS));
207 public void rpcNotAvaliableTest() throws Exception {
208 final CountDownLatch invokeLatch = new CountDownLatch(1);
210 final TestOperationServiceMyRpcRpc rpc =
211 serviceAdapter.getRpcService(TestOperationServiceMyRpcRpc.class);
214 rpc.invoke(new MyRpcInputBuilder().setRpcInputLeaf("123").build(),
215 new RpcCallback<MyRpcOutput>() {
217 public void onSuccess(MyRpcOutput output) {
222 public void onFailure(Throwable cause) {
223 assertTrue(cause instanceof DOMRpcImplementationNotAvailableException);
224 invokeLatch.countDown();
227 assertEquals("Invoke complete", true, invokeLatch.await(5, TimeUnit.SECONDS));
230 private class TestRpcImpl implements TestOperationServiceMyRpcRpc {
233 public void invoke(MyRpcInput input, RpcCallback<MyRpcOutput> callback) {
234 callback.onSuccess(RPC_OUTPUT);
238 private class TestRpcFailedImpl implements TestOperationServiceMyRpcRpc {
241 public void invoke(MyRpcInput input, RpcCallback<MyRpcOutput> callback) {
242 callback.onFailure(new RuntimeException("TestRpcFailedImpl invoke failed."));
246 private class TestActionImpl implements TestOperationServiceInContAction {
249 public void invoke(InContInput input, InstanceIdentifier<MyCont> ii, RpcCallback<InContOutput> callback) {
250 callback.onSuccess(ACTION_OUTPUT);
254 private class TestActionFailedImpl implements TestOperationServiceInContAction {
257 public void invoke(InContInput input, InstanceIdentifier<MyCont> ii, RpcCallback<InContOutput> callback) {
258 callback.onFailure(new RuntimeException("TestActionFailedImpl invoke failed."));