2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.core.connection;
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;
31 * @author michal.polkorab
34 public class ResponseExpectedRpcListenerTest {
36 private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
37 new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
39 public void onRemoval(
40 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
41 notification.getValue().discard();
44 private static final int RPC_RESPONSE_EXPIRATION = 1;
45 private final Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> responseCache = CacheBuilder.newBuilder()
47 .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
48 .removalListener(REMOVAL_LISTENER).build();
51 * Test object creation
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);
60 * Test object creation
62 @Test(expected=NullPointerException.class)
63 public void testCreation2() {
64 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, null);
68 * Test object creation
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);
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());
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");
91 * Test object creation
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());
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");
113 * Test object creation
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);