Fixup checkstyle
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / AbstractOpsTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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.controller.remote.rpc;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import akka.actor.ActorRef;
16 import akka.actor.ActorSystem;
17 import akka.testkit.javadsl.TestKit;
18 import java.net.URI;
19 import org.junit.AfterClass;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.mdsal.dom.api.DOMActionService;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
27 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
28 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
29 import org.opendaylight.mdsal.dom.api.DOMRpcService;
30 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
31 import org.opendaylight.yangtools.yang.common.ErrorType;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
42
43 /**
44  * Base class for RPC tests.
45  *
46  * @author Thomas Pantelis
47  */
48 public class AbstractOpsTest {
49     static final String TEST_REV = "2014-08-28";
50     static final String TEST_NS = "urn:test";
51     static final URI TEST_URI = URI.create(TEST_NS);
52     static final QName TEST_RPC = QName.create(TEST_NS, TEST_REV, "test-something");
53     static final QName TEST_RPC_INPUT = QName.create(TEST_NS, TEST_REV, "input");
54     static final QName TEST_RPC_INPUT_DATA = QName.create(TEST_NS, TEST_REV, "input-data");
55     static final QName TEST_RPC_OUTPUT = QName.create(TEST_NS, TEST_REV, "output");
56
57
58     static final Absolute TEST_RPC_TYPE = Absolute.of(TEST_RPC);
59     static final YangInstanceIdentifier TEST_PATH = YangInstanceIdentifier.of(TEST_RPC);
60     public static final DOMRpcIdentifier TEST_RPC_ID = DOMRpcIdentifier.create(TEST_RPC, TEST_PATH);
61     public static final DOMDataTreeIdentifier TEST_DATA_TREE_ID =
62         DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, TEST_PATH);
63
64     static ActorSystem node1;
65     static ActorSystem node2;
66     static RemoteOpsProviderConfig config1;
67     static RemoteOpsProviderConfig config2;
68
69     protected ActorRef rpcInvoker1;
70     protected TestKit rpcRegistry1Probe;
71     protected ActorRef rpcInvoker2;
72     protected TestKit rpcRegistry2Probe;
73     protected SchemaContext schemaContext;
74     protected RemoteRpcImplementation remoteRpcImpl1;
75     protected RemoteRpcImplementation remoteRpcImpl2;
76     protected RemoteActionImplementation remoteActionImpl1;
77     protected RemoteActionImplementation remoteActionImpl2;
78
79     @Mock
80     protected DOMRpcService domRpcService1;
81     @Mock
82     protected DOMActionService domActionService1;
83     @Mock
84     protected DOMRpcService domRpcService2;
85     @Mock
86     protected DOMActionService domActionService2;
87
88     @BeforeClass
89     public static void setup() {
90         config1 = new RemoteOpsProviderConfig.Builder("memberA").build();
91         config2 = new RemoteOpsProviderConfig.Builder("memberB").build();
92         node1 = ActorSystem.create("opendaylight-rpc", config1.get());
93         node2 = ActorSystem.create("opendaylight-rpc", config2.get());
94     }
95
96     @AfterClass
97     public static void teardown() {
98         TestKit.shutdownActorSystem(node1);
99         TestKit.shutdownActorSystem(node2);
100         node1 = null;
101         node2 = null;
102     }
103
104     @Before
105     public void setUp() {
106         schemaContext = YangParserTestUtils.parseYangResources(AbstractOpsTest.class, "/test-rpc.yang");
107
108         MockitoAnnotations.initMocks(this);
109
110         rpcRegistry1Probe = new TestKit(node1);
111         rpcInvoker1 = node1.actorOf(OpsInvoker.props(domRpcService1, domActionService1));
112         rpcRegistry2Probe = new TestKit(node2);
113         rpcInvoker2 = node2.actorOf(OpsInvoker.props(domRpcService2, domActionService2));
114         remoteRpcImpl1 = new RemoteRpcImplementation(rpcInvoker2, config1);
115         remoteRpcImpl2 = new RemoteRpcImplementation(rpcInvoker1, config2);
116         remoteActionImpl1 = new RemoteActionImplementation(rpcInvoker2, config1);
117         remoteActionImpl2 = new RemoteActionImplementation(rpcInvoker1, config2);
118     }
119
120     static void assertRpcErrorEquals(final RpcError rpcError, final ErrorSeverity severity,
121                                      final ErrorType errorType, final String tag, final String message,
122                                      final String applicationTag, final String info, final String causeMsg) {
123         assertEquals("getSeverity", severity, rpcError.getSeverity());
124         assertEquals("getErrorType", errorType, rpcError.getErrorType());
125         assertEquals("getTag", tag, rpcError.getTag());
126         assertTrue("getMessage contains " + message, rpcError.getMessage().contains(message));
127         assertEquals("getApplicationTag", applicationTag, rpcError.getApplicationTag());
128         assertEquals("getInfo", info, rpcError.getInfo());
129
130         if (causeMsg == null) {
131             assertNull("Unexpected cause " + rpcError.getCause(), rpcError.getCause());
132         } else {
133             assertEquals("Cause message", causeMsg, rpcError.getCause().getMessage());
134         }
135     }
136
137     static void assertCompositeNodeEquals(final NormalizedNode exp, final NormalizedNode actual) {
138         assertEquals(exp, actual);
139     }
140
141     public static ContainerNode makeRPCInput(final String data) {
142         return ImmutableNodes.newContainerBuilder()
143             .withNodeIdentifier(new NodeIdentifier(TEST_RPC_INPUT))
144             .withChild(ImmutableNodes.leafNode(TEST_RPC_INPUT_DATA, data))
145             .build();
146
147     }
148
149     public static ContainerNode makeRPCOutput(final String data) {
150         return ImmutableNodes.newContainerBuilder()
151             .withNodeIdentifier(new NodeIdentifier(TEST_RPC_OUTPUT))
152             .withChild(ImmutableNodes.leafNode(TEST_RPC_OUTPUT, data))
153             .build();
154     }
155
156     static void assertFailedRpcResult(final DOMRpcResult rpcResult, final ErrorSeverity severity,
157                                       final ErrorType errorType, final String tag, final String message,
158                                       final String applicationTag, final String info, final String causeMsg) {
159         assertNotNull("RpcResult was null", rpcResult);
160         final var rpcErrors = rpcResult.errors();
161         assertEquals("RpcErrors count", 1, rpcErrors.size());
162         assertRpcErrorEquals(rpcErrors.iterator().next(), severity, errorType, tag, message,
163                 applicationTag, info, causeMsg);
164     }
165
166     static void assertSuccessfulRpcResult(final DOMRpcResult rpcResult, final NormalizedNode expOutput) {
167         assertNotNull("RpcResult was null", rpcResult);
168         assertCompositeNodeEquals(expOutput, rpcResult.value());
169     }
170
171     static class TestException extends Exception {
172         private static final long serialVersionUID = 1L;
173
174         static final String MESSAGE = "mock error";
175
176         TestException() {
177             super(MESSAGE);
178         }
179     }
180 }