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.core.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.openflowjava.protocol.impl.core.connection.AbstractRpcListener;
\r
22 import org.opendaylight.openflowjava.protocol.impl.core.connection.ResponseExpectedRpcListener;
\r
23 import org.opendaylight.openflowjava.protocol.impl.core.connection.RpcResponseKey;
\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
\r
28 import org.opendaylight.yangtools.yang.common.RpcError;
\r
29 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
\r
30 import org.opendaylight.yangtools.yang.common.RpcResult;
\r
32 import com.google.common.cache.Cache;
\r
33 import com.google.common.cache.CacheBuilder;
\r
34 import com.google.common.cache.RemovalListener;
\r
35 import com.google.common.cache.RemovalNotification;
\r
36 import com.google.common.util.concurrent.SettableFuture;
\r
39 * @author michal.polkorab
\r
42 public class ResponseExpectedRpcListenerTest {
\r
44 private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
\r
45 new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
\r
47 public void onRemoval(
\r
48 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
\r
49 notification.getValue().discard();
\r
52 private static final int RPC_RESPONSE_EXPIRATION = 1;
\r
53 private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> responseCache = CacheBuilder.newBuilder()
\r
54 .concurrencyLevel(1)
\r
55 .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
\r
56 .removalListener(REMOVAL_LISTENER).build();
\r
59 * Test object creation
\r
61 @Test(expected=NullPointerException.class)
\r
62 public void testCreation() {
\r
63 RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());
\r
64 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", null, key);
\r
68 * Test object creation
\r
70 @Test(expected=NullPointerException.class)
\r
71 public void testCreation2() {
\r
72 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, null);
\r
76 * Test object creation
\r
79 public void testDiscard() {
\r
80 RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());
\r
81 ResponseExpectedRpcListener<OfHeader> listener =
\r
82 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);
\r
84 RpcError rpcError = AbstractRpcListener.buildRpcError("Failed to send message",
\r
85 ErrorSeverity.ERROR, "check switch connection", new TimeoutException("Request timed out"));
\r
86 SettableFuture<RpcResult<?>> result = SettableFuture.create();
\r
87 result.set(Rpcs.getRpcResult(false, null, Collections.singletonList(rpcError)));
\r
89 Assert.assertEquals("Wrong result", result.get().getErrors().iterator().next().getMessage(),
\r
90 listener.getResult().get().getErrors().iterator().next().getMessage());
\r
91 Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());
\r
92 Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());
\r
93 } catch (InterruptedException | ExecutionException e) {
\r
94 fail("Problem accessing result");
\r
99 * Test object creation
\r
102 public void testCompleted() {
\r
103 RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());
\r
104 ResponseExpectedRpcListener<OfHeader> listener =
\r
105 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);
\r
106 BarrierInputBuilder barrierBuilder = new BarrierInputBuilder();
\r
107 BarrierInput barrierInput = barrierBuilder.build();
\r
108 listener.completed(barrierInput);
\r
109 SettableFuture<RpcResult<?>> result = SettableFuture.create();
\r
110 result.set(Rpcs.getRpcResult(true, barrierInput, Collections.<RpcError>emptyList()));
\r
112 Assert.assertEquals("Wrong result", result.get().getErrors(), listener.getResult().get().getErrors());
\r
113 Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());
\r
114 Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());
\r
115 } catch (InterruptedException | ExecutionException e) {
\r
116 fail("Problem accessing result");
\r
121 * Test object creation
\r
124 public void testOperationSuccessful() {
\r
125 RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());
\r
126 ResponseExpectedRpcListener<OfHeader> listener =
\r
127 new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);
\r
128 listener.operationSuccessful();
\r
129 ResponseExpectedRpcListener<?> present = responseCache.getIfPresent(key);
\r
130 Assert.assertEquals(present, listener);
\r