2 * Copyright (C) 2014 Cisco Systems, Inc.
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 * Authors : Thomas Bachman
11 package org.opendaylight.groupbasedpolicy.renderer.opflex.jsonrpc;
13 import static io.netty.buffer.Unpooled.copiedBuffer;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17 import io.netty.channel.embedded.EmbeddedChannel;
18 import io.netty.util.CharsetUtil;
20 import java.util.ArrayList;
21 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.fasterxml.jackson.annotation.JsonIgnore;
29 import com.fasterxml.jackson.databind.DeserializationFeature;
30 import com.fasterxml.jackson.databind.JsonNode;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
33 import com.google.common.util.concurrent.ListenableFuture;
35 public class JsonRpcEndpointTest implements RpcBroker, RpcBroker.RpcCallback {
36 protected static final Logger logger = LoggerFactory.getLogger(JsonRpcEndpoint.class);
38 static final String TEST_JSON_CLASS_NAME = "send_identity";
39 // Used for message generation, single property
40 static final String simpleMessage = "{\"otherstuff\": \"foobar\"}";
41 // Used for testing valid incoming JSONRPC request messages
42 static final String testRequest =
43 "{ \"id\":\"2da9e3d7-0bbe-4099-b343-12783777452f\"," +
44 "\"method\":" + "\"" + TEST_JSON_CLASS_NAME + "\",\"params\":null}";
45 // Used for testing invalid incoming JSONRPC request messages
46 static final String testBadRequest =
47 "{ \"id\":\"2da9e3d7-0bbe-4099-b343-12783777452f\"," +
48 "\"method\":\"foobar\",\"params\":[]}";
49 // Used for testing valid incoming JSONRPC echo request messages
50 static final String testEchoRequest =
51 "{ \"id\":\"2da9e3d7-0bbe-4099-b343-12783777452f\"," +
52 "\"method\":\"echo\",\"params\":[]}";
53 // Used for testing invalid incoming JSONRPC response messages
54 static final String unknownResponse =
55 "{ \"id\":\"2da9e3d7-0bbe-4099-b343-12783777452f\"," +
56 "\"result\":\"foobar\",\"error\":null}";
57 static final String opflexIdentityRequest =
58 "{ \"id\":\"2da9e3d7-0bbe-4099-b343-12783777452f\"," +
59 "\"method\":" + "\"" + TEST_JSON_CLASS_NAME + "\",\"params\": [ {" +
60 "\"name\": \"will\", \"domain\": \"robinson\"," +
61 "\"my_role\": [\"policy_element\", \"policy_repository\"]} ] }";
64 private JsonRpcDecoder decoder;
65 private EmbeddedChannel channel;
66 private JsonRpcEndpoint endpoint;
67 private RpcMessageMap messageMap;
68 private boolean testTriggerFlag;
71 public void subscribe(RpcMessage message, RpcCallback callback) {
75 public void publish(JsonRpcEndpoint ep, RpcMessage message) {
76 testTriggerFlag = true;
77 callback(ep, message);
81 static final public class Params {
83 private String domain;
84 private List<String> my_role;
85 public String getName() {
88 public void setName(String name) {
91 public String getDomain() {
94 public void setDomain(String domain) {
97 public List<String> getMy_role() {
100 public void setMy_role(List<String> my_role) {
101 this.my_role = my_role;
104 my_role = new ArrayList<String>();
109 static final class OpflexTest extends RpcMessage {
112 private String method;
114 private List<Params> params;
115 private String otherstuff;
119 public OpflexTest() {
120 this.name = TEST_JSON_CLASS_NAME;
123 public void setOtherstuff ( String otherstuff ) {
124 this.otherstuff = otherstuff;
126 public String getOtherstuff() {
127 return this.otherstuff;
130 public void setParams(List<Params> params) {
131 this.params = params;
134 public List<Params> getParams() {
139 public String getName() {
144 public void setName(String name) {
149 public JsonNode getId() {
154 public void setId(JsonNode id) {
159 public String getMethod() {
164 public void setMethod(String method) {
165 this.method = method;
169 public boolean valid() {
175 public void callback(JsonRpcEndpoint ep, RpcMessage message) {
177 if (message != null && message instanceof JsonRpcEndpointTest.OpflexTest) {
178 JsonRpcEndpointTest.OpflexTest msg = (JsonRpcEndpointTest.OpflexTest)message;
179 if ( msg.getParams() == null) {
187 public void setUp() throws Exception {
188 ObjectMapper objectMapper = new ObjectMapper();
189 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
192 * Create the message map, populating with just our test message
194 messageMap = new RpcMessageMap();
195 JsonRpcEndpointTest.OpflexTest rpcMethod =
196 new JsonRpcEndpointTest.OpflexTest();
197 rpcMethod.setName(TEST_JSON_CLASS_NAME);
198 messageMap.add(rpcMethod);
200 decoder = new JsonRpcDecoder(1000);
201 JsonRpcServiceBinderHandler binderHandler =
202 new JsonRpcServiceBinderHandler(null);
203 channel = new EmbeddedChannel(decoder, binderHandler);
205 endpoint = new JsonRpcEndpoint(channel.localAddress().toString(), null,
206 objectMapper, channel, messageMap, this);
207 binderHandler.setEndpoint(endpoint);
212 public void testOutbound() throws Exception {
213 ObjectMapper objectMapper = new ObjectMapper();
214 JsonRpcEndpointTest.OpflexTest testRpc = objectMapper.
215 readValue(simpleMessage, JsonRpcEndpointTest.OpflexTest.class);
216 testRpc.setName(TEST_JSON_CLASS_NAME);
218 endpoint.sendRequest(testRpc);
219 Object result = channel.readOutbound();
220 assertTrue(result != null);
221 assertTrue(result.toString().contains("id"));
222 assertTrue(result.toString().contains("method"));
223 assertTrue(result.toString().contains("params"));
225 } catch ( Exception e ) {
231 public void testInboundRequestMatch() throws Exception {
232 testTriggerFlag = false;
233 channel.writeInbound(copiedBuffer(testRequest, CharsetUtil.UTF_8));
234 assertTrue(testTriggerFlag);
239 public void testInboundRequestNoMatch() throws Exception {
240 testTriggerFlag = false;
241 channel.writeInbound(copiedBuffer(testBadRequest, CharsetUtil.UTF_8));
242 assertFalse(testTriggerFlag);
247 public void testInboundResponseNoMatch() throws Exception {
248 testTriggerFlag = false;
249 channel.writeInbound(copiedBuffer(unknownResponse, CharsetUtil.UTF_8));
250 assertFalse(testTriggerFlag);
255 public void testInboundResponseMatch() throws Exception {
256 ObjectMapper objectMapper = new ObjectMapper();
257 JsonRpcEndpointTest.OpflexTest testRpc = objectMapper.
258 readValue(simpleMessage, JsonRpcEndpointTest.OpflexTest.class);
259 testRpc.setName(TEST_JSON_CLASS_NAME);
262 ListenableFuture<Object> lf = endpoint.sendRequest(testRpc);
263 String result = channel.readOutbound().toString();
264 JsonNode node = objectMapper.readValue(result, JsonNode.class);
265 String idValue = node.path("id").textValue();
266 String foo = "{ \"id\":\"" + idValue +
267 "\",\"result\":\"foobar\",\"error\":null}";
268 testTriggerFlag = false;
269 channel.writeInbound(copiedBuffer(foo, CharsetUtil.UTF_8));
270 Object tmp = lf.get();
271 assertTrue(tmp instanceof JsonRpcEndpointTest.OpflexTest);
273 } catch ( Exception e ) {
279 public void testInboundEchoRequest() throws Exception {
280 channel.writeInbound(copiedBuffer(testEchoRequest, CharsetUtil.UTF_8));
281 Object result = channel.readOutbound();
282 assertTrue(result != null);
283 assertTrue(result.toString().contains("id"));
284 assertTrue(result.toString().contains("result"));
285 assertTrue(result.toString().contains("error"));
290 public void testOpflexIdentityRequest() throws Exception {
291 testTriggerFlag = false;
292 System.out.println("OpflexIdentity Test");
293 channel.writeInbound(copiedBuffer(opflexIdentityRequest, CharsetUtil.UTF_8));
295 assertTrue(testTriggerFlag);