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;
13 import java.util.Collections;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
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;
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;
39 * @author michal.polkorab
42 public class ResponseExpectedRpcListenerTest {
44 private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
45 new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
47 public void onRemoval(
48 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
49 notification.getValue().discard();
52 private static final int RPC_RESPONSE_EXPIRATION = 1;
53 private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> responseCache = CacheBuilder.newBuilder()
55 .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
56 .removalListener(REMOVAL_LISTENER).build();
59 * Test object creation
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);
68 * Test object creation
70 @Test(expected=NullPointerException.class)
71 public void testCreation2() {
72 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, null);
76 * Test object creation
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);
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)));
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");
99 * Test object creation
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()));
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");
121 * Test object creation
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);