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