a535c0851b59066018e5f1f5ccecb2a5363bb9e4
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / 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.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.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;\r
25 import org.opendaylight.yangtools.yang.common.RpcError;\r
26 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;\r
27 import org.opendaylight.yangtools.yang.common.RpcResult;\r
28 \r
29 import com.google.common.cache.Cache;\r
30 import com.google.common.cache.CacheBuilder;\r
31 import com.google.common.cache.RemovalListener;\r
32 import com.google.common.cache.RemovalNotification;\r
33 import com.google.common.util.concurrent.SettableFuture;\r
34 \r
35 /**\r
36  * @author michal.polkorab\r
37  *\r
38  */\r
39 public class ResponseExpectedRpcListenerTest {\r
40 \r
41     private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =\r
42             new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {\r
43         @Override\r
44         public void onRemoval(\r
45                 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {\r
46             notification.getValue().discard();\r
47         }\r
48     };\r
49     private static final int RPC_RESPONSE_EXPIRATION = 1;\r
50     private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> responseCache  = CacheBuilder.newBuilder()\r
51             .concurrencyLevel(1)\r
52             .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)\r
53             .removalListener(REMOVAL_LISTENER).build();\r
54 \r
55     /**\r
56      * Test object creation\r
57      */\r
58     @Test(expected=NullPointerException.class)\r
59     public void testCreation() {\r
60         RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
61         new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", null, key);\r
62     }\r
63 \r
64     /**\r
65      * Test object creation\r
66      */\r
67     @Test(expected=NullPointerException.class)\r
68     public void testCreation2() {\r
69         RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
70         new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, null);\r
71     }\r
72 \r
73     /**\r
74      * Test object creation\r
75      */\r
76     @Test\r
77     public void testDiscard() {\r
78         RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
79         ResponseExpectedRpcListener<OfHeader> listener =\r
80                 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);\r
81         listener.discard();\r
82         RpcError rpcError = ConnectionAdapterImpl.buildRpcError("Failed to send message",\r
83                 ErrorSeverity.ERROR, "check switch connection", new TimeoutException("Request timed out"));\r
84         SettableFuture<RpcResult<?>> result = SettableFuture.create();\r
85         result.set(Rpcs.getRpcResult(false, null, Collections.singletonList(rpcError)));\r
86         try {\r
87             Assert.assertEquals("Wrong result", result.get().getErrors().iterator().next().getMessage(),\r
88                     listener.getResult().get().getErrors().iterator().next().getMessage());\r
89             Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());\r
90             Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());\r
91         } catch (InterruptedException | ExecutionException e) {\r
92             fail("Problem accessing result");\r
93         }\r
94     }\r
95 \r
96     /**\r
97      * Test object creation\r
98      */\r
99     @Test\r
100     public void testCompleted() {\r
101         RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
102         ResponseExpectedRpcListener<OfHeader> listener =\r
103                 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);\r
104         BarrierInputBuilder barrierBuilder = new BarrierInputBuilder();\r
105         BarrierInput barrierInput = barrierBuilder.build();\r
106         listener.completed(barrierInput);\r
107         SettableFuture<RpcResult<?>> result = SettableFuture.create();\r
108         result.set(Rpcs.getRpcResult(true, barrierInput, Collections.<RpcError>emptyList()));\r
109         try {\r
110             Assert.assertEquals("Wrong result", result.get().getErrors(), listener.getResult().get().getErrors());\r
111             Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());\r
112             Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());\r
113         } catch (InterruptedException | ExecutionException e) {\r
114             fail("Problem accessing result");\r
115         }\r
116     }\r
117 \r
118     /**\r
119      * Test object creation\r
120      */\r
121     @Test\r
122     public void testOperationSuccessful() {\r
123         RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
124         ResponseExpectedRpcListener<OfHeader> listener =\r
125                 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);\r
126         listener.operationSuccessful();\r
127         ResponseExpectedRpcListener<?> present = responseCache.getIfPresent(key);\r
128         Assert.assertEquals(present, listener);\r
129     }\r
130 }