TestUtils is a utility class
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / GlobalDOMRpcRoutingTableEntryTest.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.mdsal.dom.broker;
9
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.CoreMatchers.instanceOf;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertThrows;
16 import static org.junit.Assert.assertTrue;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import static org.opendaylight.mdsal.dom.broker.TestUtils.EXCEPTION_TEXT;
20 import static org.opendaylight.mdsal.dom.broker.TestUtils.TEST_CONTAINER;
21 import static org.opendaylight.mdsal.dom.broker.TestUtils.getTestRpcImplementation;
22
23 import com.google.common.util.concurrent.ListenableFuture;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.TimeUnit;
30 import org.junit.Test;
31 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
32 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
33 import org.opendaylight.mdsal.dom.broker.DOMRpcRouter.OperationInvocation;
34 import org.opendaylight.mdsal.dom.broker.util.TestModel;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
37
38 public class GlobalDOMRpcRoutingTableEntryTest {
39     @Test
40     public void basicTest() {
41         final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> rpcImplementations = new HashMap<>();
42         final List<DOMRpcImplementation> rpcImplementation = new ArrayList<>();
43         final RpcDefinition rpcDefinition = mock(RpcDefinition.class);
44         final YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.builder().build();
45
46         doReturn(TestModel.TEST2_QNAME).when(rpcDefinition).getQName();
47         final GlobalDOMRpcRoutingTableEntry globalDOMRpcRoutingTableEntry = new GlobalDOMRpcRoutingTableEntry(
48                 rpcDefinition, new HashMap<>());
49         rpcImplementation.add(getTestRpcImplementation());
50         rpcImplementations.put(yangInstanceIdentifier, rpcImplementation);
51
52         assertEquals(TestModel.TEST2_QNAME, globalDOMRpcRoutingTableEntry.getType());
53         assertTrue(globalDOMRpcRoutingTableEntry.getImplementations().isEmpty());
54         assertFalse(globalDOMRpcRoutingTableEntry.newInstance(rpcImplementations).getImplementations().isEmpty());
55         assertTrue(globalDOMRpcRoutingTableEntry.newInstance(rpcImplementations).getImplementations().containsValue(
56                 rpcImplementation));
57
58         final ListenableFuture<?> future = OperationInvocation.invoke(
59             globalDOMRpcRoutingTableEntry.newInstance(rpcImplementations), TEST_CONTAINER);
60
61         final ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get(5, TimeUnit.SECONDS));
62         final Throwable cause = ex.getCause();
63         assertThat(cause, instanceOf(DOMRpcImplementationNotAvailableException.class));
64         assertThat(cause.getMessage(), containsString(EXCEPTION_TEXT));
65     }
66 }