Maintaining code - Move buildRpcError method into AbstractRpcListener
[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         new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, null);\r
70     }\r
71 \r
72     /**\r
73      * Test object creation\r
74      */\r
75     @Test\r
76     public void testDiscard() {\r
77         RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
78         ResponseExpectedRpcListener<OfHeader> listener =\r
79                 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);\r
80         listener.discard();\r
81         RpcError rpcError = AbstractRpcListener.buildRpcError("Failed to send message",\r
82                 ErrorSeverity.ERROR, "check switch connection", new TimeoutException("Request timed out"));\r
83         SettableFuture<RpcResult<?>> result = SettableFuture.create();\r
84         result.set(Rpcs.getRpcResult(false, null, Collections.singletonList(rpcError)));\r
85         try {\r
86             Assert.assertEquals("Wrong result", result.get().getErrors().iterator().next().getMessage(),\r
87                     listener.getResult().get().getErrors().iterator().next().getMessage());\r
88             Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());\r
89             Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());\r
90         } catch (InterruptedException | ExecutionException e) {\r
91             fail("Problem accessing result");\r
92         }\r
93     }\r
94 \r
95     /**\r
96      * Test object creation\r
97      */\r
98     @Test\r
99     public void testCompleted() {\r
100         RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
101         ResponseExpectedRpcListener<OfHeader> listener =\r
102                 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);\r
103         BarrierInputBuilder barrierBuilder = new BarrierInputBuilder();\r
104         BarrierInput barrierInput = barrierBuilder.build();\r
105         listener.completed(barrierInput);\r
106         SettableFuture<RpcResult<?>> result = SettableFuture.create();\r
107         result.set(Rpcs.getRpcResult(true, barrierInput, Collections.<RpcError>emptyList()));\r
108         try {\r
109             Assert.assertEquals("Wrong result", result.get().getErrors(), listener.getResult().get().getErrors());\r
110             Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());\r
111             Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());\r
112         } catch (InterruptedException | ExecutionException e) {\r
113             fail("Problem accessing result");\r
114         }\r
115     }\r
116 \r
117     /**\r
118      * Test object creation\r
119      */\r
120     @Test\r
121     public void testOperationSuccessful() {\r
122         RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
123         ResponseExpectedRpcListener<OfHeader> listener =\r
124                 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);\r
125         listener.operationSuccessful();\r
126         ResponseExpectedRpcListener<?> present = responseCache.getIfPresent(key);\r
127         Assert.assertEquals(present, listener);\r
128     }\r
129 }