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