Feature uses features-parent as parent
[groupbasedpolicy.git] / renderers / opflex / src / test / java / org / opendaylight / groupbasedpolicy / renderer / opflex / jsonrpc / JsonRpcEndpointTest.java
1 /*
2  * Copyright (C) 2014 Cisco Systems, Inc.
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  *  Authors : Thomas Bachman
9  */
10
11 package org.opendaylight.groupbasedpolicy.renderer.opflex.jsonrpc;
12
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;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
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;
34
35 public class JsonRpcEndpointTest implements RpcBroker, RpcBroker.RpcCallback {
36     protected static final Logger logger = LoggerFactory.getLogger(JsonRpcEndpoint.class);
37
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\"]} ] }";
62
63
64     private JsonRpcDecoder decoder;
65     private EmbeddedChannel channel;
66     private JsonRpcEndpoint endpoint;
67     private RpcMessageMap messageMap;
68     private boolean testTriggerFlag;
69
70     @Override
71     public void subscribe(RpcMessage message, RpcCallback callback) {
72     }
73
74     @Override
75     public void publish(JsonRpcEndpoint ep, RpcMessage message) {
76         testTriggerFlag = true;
77         callback(ep, message);
78     }
79
80     @JsonDeserialize
81     static final public class Params {
82         private String name;
83         private String domain;
84         private List<String> my_role;
85         public String getName() {
86             return name;
87         }
88         public void setName(String name) {
89             this.name = name;
90         }
91         public String getDomain() {
92             return domain;
93         }
94         public void setDomain(String domain) {
95             this.domain = domain;
96         }
97         public List<String> getMy_role() {
98             return my_role;
99         }
100         public void setMy_role(List<String> my_role) {
101             this.my_role = my_role;
102         }
103         public Params() {
104             my_role = new ArrayList<String>();
105         }
106     }
107
108     @JsonDeserialize
109     static final class OpflexTest extends RpcMessage {
110
111         private JsonNode id;
112         private String method;
113
114         private List<Params> params;
115         private String otherstuff;
116         @JsonIgnore
117         private String name;
118
119         public OpflexTest() {
120             this.name = TEST_JSON_CLASS_NAME;
121         }
122
123         public void setOtherstuff ( String otherstuff ) {
124             this.otherstuff = otherstuff;
125         }
126         public String getOtherstuff() {
127             return this.otherstuff;
128         }
129
130         public void setParams(List<Params> params) {
131             this.params = params;
132         }
133
134         public List<Params> getParams() {
135             return params;
136         }
137
138         @Override
139         public String getName() {
140             return this.name;
141         }
142
143         @Override
144         public void setName(String name) {
145             this.name = name;
146         }
147
148         @Override
149         public JsonNode getId() {
150             return id;
151         }
152
153         @Override
154         public void setId(JsonNode id) {
155             this.id = id;
156         }
157
158         @Override
159         public String getMethod() {
160             return method;
161         }
162
163         @Override
164         public void setMethod(String method) {
165             this.method = method;
166         }
167         @JsonIgnore
168         @Override
169         public boolean valid() {
170             return true;
171         }
172     }
173
174     @Override
175     public void callback(JsonRpcEndpoint ep, RpcMessage message) {
176
177         if (message != null && message instanceof JsonRpcEndpointTest.OpflexTest) {
178             JsonRpcEndpointTest.OpflexTest msg = (JsonRpcEndpointTest.OpflexTest)message;
179             if ( msg.getParams() == null) {
180                 return;
181             }
182         }
183     }
184
185
186     @Before
187     public void setUp() throws Exception {
188         ObjectMapper objectMapper = new ObjectMapper();
189         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
190
191         /*
192          * Create the message map, populating with just our test message
193          */
194         messageMap = new RpcMessageMap();
195         JsonRpcEndpointTest.OpflexTest rpcMethod =
196                 new JsonRpcEndpointTest.OpflexTest();
197         rpcMethod.setName(TEST_JSON_CLASS_NAME);
198         messageMap.add(rpcMethod);
199
200         decoder = new JsonRpcDecoder(1000);
201         JsonRpcServiceBinderHandler binderHandler =
202                 new JsonRpcServiceBinderHandler(null);
203         channel = new EmbeddedChannel(decoder, binderHandler);
204
205         endpoint = new JsonRpcEndpoint(channel.localAddress().toString(), null,
206                 objectMapper, channel, messageMap, this);
207         binderHandler.setEndpoint(endpoint);
208     }
209
210
211     @Test
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);
217         try {
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"));
224             channel.finish();
225         } catch ( Exception e ) {
226             fail();
227         }
228     }
229
230     @Test
231     public void testInboundRequestMatch() throws Exception {
232         testTriggerFlag = false;
233         channel.writeInbound(copiedBuffer(testRequest, CharsetUtil.UTF_8));
234         assertTrue(testTriggerFlag);
235         channel.finish();
236     }
237
238     @Test
239     public void testInboundRequestNoMatch() throws Exception {
240         testTriggerFlag = false;
241         channel.writeInbound(copiedBuffer(testBadRequest, CharsetUtil.UTF_8));
242         assertFalse(testTriggerFlag);
243         channel.finish();
244     }
245
246     @Test
247     public void testInboundResponseNoMatch() throws Exception {
248         testTriggerFlag = false;
249         channel.writeInbound(copiedBuffer(unknownResponse, CharsetUtil.UTF_8));
250         assertFalse(testTriggerFlag);
251         channel.finish();
252     }
253
254     @Test
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);
260
261         try {
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);
272             channel.finish();
273          } catch ( Exception e ) {
274             fail();
275         }
276     }
277
278     @Test
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"));
286         channel.finish();
287     }
288
289     @Test
290     public void testOpflexIdentityRequest() throws Exception {
291         testTriggerFlag = false;
292         System.out.println("OpflexIdentity Test");
293         channel.writeInbound(copiedBuffer(opflexIdentityRequest, CharsetUtil.UTF_8));
294         channel.finish();
295         assertTrue(testTriggerFlag);
296     }
297 }