Modernize DataBrokerTestModule
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / test / Mdsal500Test.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.mdsal.binding.dom.adapter.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNotSame;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertTrue;
15
16 import com.google.common.collect.HashMultimap;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.collect.Multimap;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.MoreExecutors;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.Future;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.TimeoutException;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
29 import org.opendaylight.mdsal.binding.api.RpcProviderService;
30 import org.opendaylight.mdsal.binding.dom.adapter.test.util.BindingBrokerTestFactory;
31 import org.opendaylight.mdsal.binding.dom.adapter.test.util.BindingTestContext;
32 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
33 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
34 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
35 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
36 import org.opendaylight.mdsal.dom.api.DOMRpcService;
37 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
38 import org.opendaylight.yang.gen.v1.rpc.norev.Mdsal500Service;
39 import org.opendaylight.yang.gen.v1.rpc.norev.SwitchInput;
40 import org.opendaylight.yang.gen.v1.rpc.norev.SwitchInputBuilder;
41 import org.opendaylight.yang.gen.v1.rpc.norev.SwitchOutput;
42 import org.opendaylight.yang.gen.v1.rpc.norev.SwitchOutputBuilder;
43 import org.opendaylight.yangtools.concepts.ObjectRegistration;
44 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
45 import org.opendaylight.yangtools.yang.common.QName;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
49 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
50
51 public class Mdsal500Test {
52     private static final String FOO = "foo";
53
54     private static final QName SWITCH_QNAME = QName.create(SwitchOutput.QNAME, "switch");
55     private static final SchemaPath SWITCH_PATH = SchemaPath.create(true, SWITCH_QNAME);
56
57     private RpcProviderService baRpcProviderService;
58     private RpcConsumerRegistry baRpcConsumerService;
59     private DOMRpcProviderService biRpcProviderService;
60     private BindingTestContext testContext;
61     private DOMRpcService biRpcService;
62     private final Mdsal500ServiceImpl switchRpcImpl = new Mdsal500ServiceImpl();
63
64     @Before
65     public void setup() throws Exception {
66         BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
67         testFactory.setExecutor(MoreExecutors.newDirectExecutorService());
68         testContext = testFactory.getTestContext();
69
70         testContext.setSchemaModuleInfos(ImmutableSet.of(
71                 BindingReflections.getModuleInfo(Mdsal500Service.class)));
72         testContext.start();
73         baRpcProviderService = testContext.getBindingRpcProviderRegistry();
74         baRpcConsumerService = testContext.getBindingRpcConsumerRegistry();
75         biRpcProviderService = testContext.getDomRpcRegistry();
76         biRpcService = testContext.getDomRpcInvoker();
77     }
78
79     @Test
80     public void testBindingRegistrationWithDOMInvocation() throws Exception {
81         switchRpcImpl.registerTo(baRpcProviderService).setSwitchResult(switchResult(true));
82
83         final Mdsal500Service baSwitchService = baRpcConsumerService.getRpcService(Mdsal500Service.class);
84         assertNotSame(switchRpcImpl, baSwitchService);
85
86         SwitchInput baSwitchInput = switchBuilder(FOO).build();
87
88         ContainerNode biSwitchInput = toDOMSwitchInput(baSwitchInput);
89         DOMRpcResult domResult = biRpcService.invokeRpc(SWITCH_PATH, biSwitchInput).get(5, TimeUnit.SECONDS);
90         assertNotNull(domResult);
91         assertNotNull(domResult.getResult());
92         assertTrue("Binding KnockKnock service was not invoked",
93                 switchRpcImpl.getReceivedSwitch().containsKey(FOO));
94         assertEquals(baSwitchInput, switchRpcImpl.getReceivedSwitch().get(FOO).iterator().next());
95     }
96
97     @Test
98     public void testDOMRegistrationWithBindingInvocation()
99             throws InterruptedException, ExecutionException, TimeoutException {
100         SwitchOutput baSwitchOutput = new SwitchOutputBuilder().build();
101
102         biRpcProviderService.registerRpcImplementation((rpc, input) ->
103             FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult(testContext.getCodec().currentSerializer()
104                     .toNormalizedNodeRpcData(baSwitchOutput))),
105             DOMRpcIdentifier.create(SWITCH_PATH));
106
107         final Mdsal500Service baSwitchService =
108                 baRpcConsumerService.getRpcService(Mdsal500Service.class);
109         Future<RpcResult<SwitchOutput>> baResult = baSwitchService.switch$(switchBuilder(FOO)
110             .build());
111         assertNotNull(baResult);
112         assertEquals(baSwitchOutput, baResult.get(5, TimeUnit.SECONDS).getResult());
113     }
114
115     @Test
116     public void testBindingRpcShortcut() throws InterruptedException, ExecutionException, TimeoutException {
117         final ListenableFuture<RpcResult<SwitchOutput>> baSwitchResult = switchResult(true);
118         switchRpcImpl.registerTo(baRpcProviderService).setSwitchResult(baSwitchResult);
119
120         final Mdsal500Service baSwitchService = baRpcConsumerService.getRpcService(Mdsal500Service.class);
121
122         SwitchInput baSwitchInput = switchBuilder(FOO).build();
123         ListenableFuture<RpcResult<SwitchOutput>> future = baSwitchService.switch$(baSwitchInput);
124
125         final RpcResult<SwitchOutput> rpcResult = future.get(5, TimeUnit.SECONDS);
126
127         assertEquals(baSwitchResult.get().getResult().getClass(), rpcResult.getResult().getClass());
128         assertSame(baSwitchResult.get().getResult(), rpcResult.getResult());
129         assertSame(baSwitchInput, switchRpcImpl.getReceivedSwitch().get(FOO).iterator().next());
130     }
131
132     private static ListenableFuture<RpcResult<SwitchOutput>> switchResult(final boolean success) {
133         return Futures.immediateFuture(RpcResultBuilder.<SwitchOutput>status(success)
134             .withResult(new SwitchOutputBuilder().build())
135             .build());
136     }
137
138     private static SwitchInputBuilder switchBuilder(final String foo) {
139         return new SwitchInputBuilder().setFoo(foo);
140     }
141
142     private ContainerNode toDOMSwitchInput(final SwitchInput from) {
143         return testContext.getCodec().currentSerializer().toNormalizedNodeRpcData(from);
144     }
145
146     private static class Mdsal500ServiceImpl implements Mdsal500Service {
147         private ListenableFuture<RpcResult<SwitchOutput>> switchResult;
148         private final Multimap<String, SwitchInput> receivedSwitch = HashMultimap.create();
149
150         Mdsal500ServiceImpl setSwitchResult(final ListenableFuture<RpcResult<SwitchOutput>> switchOutput) {
151             this.switchResult = switchOutput;
152             return this;
153         }
154
155         Multimap<String, SwitchInput> getReceivedSwitch() {
156             return receivedSwitch;
157         }
158
159         Mdsal500ServiceImpl registerTo(final RpcProviderService registry) {
160             final ObjectRegistration<Mdsal500ServiceImpl> registration =
161                     registry.registerRpcImplementation(Mdsal500Service.class, this);
162             assertNotNull(registration);
163             return this;
164         }
165
166         @Override
167         public ListenableFuture<RpcResult<SwitchOutput>> switch$(final SwitchInput switchInput) {
168             receivedSwitch.put(switchInput.getFoo(), switchInput);
169             return switchResult;
170         }
171     }
172
173 }