2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
\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
9 package org.opendaylight.openflowjava.protocol.impl.connection;
\r
11 import static org.junit.Assert.fail;
\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
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
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
36 * @author michal.polkorab
\r
39 public class ResponseExpectedRpcListenerTest {
\r
41 private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
\r
42 new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
\r
44 public void onRemoval(
\r
45 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
\r
46 notification.getValue().discard();
\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
56 * Test object creation
\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
65 * Test object creation
\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
74 * Test object creation
\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
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
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
97 * Test object creation
\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
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
119 * Test object creation
\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