32b6b93189c4a187a8691a762d129fcdbdeeac90
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / connection / ResponseExpectedRpcListenerTest.java
1 /*\r
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.openflowjava.protocol.impl.core.connection;\r
10 \r
11 import static org.junit.Assert.fail;\r
12 \r
13 import java.util.Collections;\r
14 import java.util.concurrent.ExecutionException;\r
15 import java.util.concurrent.TimeUnit;\r
16 import java.util.concurrent.TimeoutException;\r
17 \r
18 import org.junit.Assert;\r
19 import org.junit.Test;\r
20 import org.opendaylight.controller.sal.common.util.Rpcs;\r
21 import org.opendaylight.openflowjava.protocol.impl.core.connection.AbstractRpcListener;\r
22 import org.opendaylight.openflowjava.protocol.impl.core.connection.ResponseExpectedRpcListener;\r
23 import org.opendaylight.openflowjava.protocol.impl.core.connection.RpcResponseKey;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;\r
28 import org.opendaylight.yangtools.yang.common.RpcError;\r
29 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;\r
30 import org.opendaylight.yangtools.yang.common.RpcResult;\r
31 \r
32 import com.google.common.cache.Cache;\r
33 import com.google.common.cache.CacheBuilder;\r
34 import com.google.common.cache.RemovalListener;\r
35 import com.google.common.cache.RemovalNotification;\r
36 import com.google.common.util.concurrent.SettableFuture;\r
37 \r
38 /**\r
39  * @author michal.polkorab\r
40  *\r
41  */\r
42 public class ResponseExpectedRpcListenerTest {\r
43 \r
44     private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =\r
45             new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {\r
46         @Override\r
47         public void onRemoval(\r
48                 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {\r
49             notification.getValue().discard();\r
50         }\r
51     };\r
52     private static final int RPC_RESPONSE_EXPIRATION = 1;\r
53     private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> responseCache  = CacheBuilder.newBuilder()\r
54             .concurrencyLevel(1)\r
55             .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)\r
56             .removalListener(REMOVAL_LISTENER).build();\r
57 \r
58     /**\r
59      * Test object creation\r
60      */\r
61     @Test(expected=NullPointerException.class)\r
62     public void testCreation() {\r
63         RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
64         new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", null, key);\r
65     }\r
66 \r
67     /**\r
68      * Test object creation\r
69      */\r
70     @Test(expected=NullPointerException.class)\r
71     public void testCreation2() {\r
72         new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, null);\r
73     }\r
74 \r
75     /**\r
76      * Test object creation\r
77      */\r
78     @Test\r
79     public void testDiscard() {\r
80         RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
81         ResponseExpectedRpcListener<OfHeader> listener =\r
82                 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);\r
83         listener.discard();\r
84         RpcError rpcError = AbstractRpcListener.buildRpcError("Failed to send message",\r
85                 ErrorSeverity.ERROR, "check switch connection", new TimeoutException("Request timed out"));\r
86         SettableFuture<RpcResult<?>> result = SettableFuture.create();\r
87         result.set(Rpcs.getRpcResult(false, null, Collections.singletonList(rpcError)));\r
88         try {\r
89             Assert.assertEquals("Wrong result", result.get().getErrors().iterator().next().getMessage(),\r
90                     listener.getResult().get().getErrors().iterator().next().getMessage());\r
91             Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());\r
92             Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());\r
93         } catch (InterruptedException | ExecutionException e) {\r
94             fail("Problem accessing result");\r
95         }\r
96     }\r
97 \r
98     /**\r
99      * Test object creation\r
100      */\r
101     @Test\r
102     public void testCompleted() {\r
103         RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
104         ResponseExpectedRpcListener<OfHeader> listener =\r
105                 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);\r
106         BarrierInputBuilder barrierBuilder = new BarrierInputBuilder();\r
107         BarrierInput barrierInput = barrierBuilder.build();\r
108         listener.completed(barrierInput);\r
109         SettableFuture<RpcResult<?>> result = SettableFuture.create();\r
110         result.set(Rpcs.getRpcResult(true, barrierInput, Collections.<RpcError>emptyList()));\r
111         try {\r
112             Assert.assertEquals("Wrong result", result.get().getErrors(), listener.getResult().get().getErrors());\r
113             Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());\r
114             Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());\r
115         } catch (InterruptedException | ExecutionException e) {\r
116             fail("Problem accessing result");\r
117         }\r
118     }\r
119 \r
120     /**\r
121      * Test object creation\r
122      */\r
123     @Test\r
124     public void testOperationSuccessful() {\r
125         RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
126         ResponseExpectedRpcListener<OfHeader> listener =\r
127                 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);\r
128         listener.operationSuccessful();\r
129         ResponseExpectedRpcListener<?> present = responseCache.getIfPresent(key);\r
130         Assert.assertEquals(present, listener);\r
131     }\r
132 }