-/*\r
- * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-\r
-package org.opendaylight.openflowjava.protocol.impl.core.connection;\r
-\r
-import static org.junit.Assert.fail;\r
-\r
-import java.util.Collections;\r
-import java.util.concurrent.ExecutionException;\r
-import java.util.concurrent.TimeUnit;\r
-import java.util.concurrent.TimeoutException;\r
-\r
-import org.junit.Assert;\r
-import org.junit.Test;\r
-import org.opendaylight.controller.sal.common.util.Rpcs;\r
-import org.opendaylight.openflowjava.protocol.impl.core.connection.AbstractRpcListener;\r
-import org.opendaylight.openflowjava.protocol.impl.core.connection.ResponseExpectedRpcListener;\r
-import org.opendaylight.openflowjava.protocol.impl.core.connection.RpcResponseKey;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;\r
-import org.opendaylight.yangtools.yang.common.RpcError;\r
-import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;\r
-import org.opendaylight.yangtools.yang.common.RpcResult;\r
-\r
-import com.google.common.cache.Cache;\r
-import com.google.common.cache.CacheBuilder;\r
-import com.google.common.cache.RemovalListener;\r
-import com.google.common.cache.RemovalNotification;\r
-import com.google.common.util.concurrent.SettableFuture;\r
-\r
-/**\r
- * @author michal.polkorab\r
- *\r
- */\r
-public class ResponseExpectedRpcListenerTest {\r
-\r
- private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =\r
- new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {\r
- @Override\r
- public void onRemoval(\r
- final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {\r
- notification.getValue().discard();\r
- }\r
- };\r
- private static final int RPC_RESPONSE_EXPIRATION = 1;\r
- private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> responseCache = CacheBuilder.newBuilder()\r
- .concurrencyLevel(1)\r
- .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)\r
- .removalListener(REMOVAL_LISTENER).build();\r
-\r
- /**\r
- * Test object creation\r
- */\r
- @Test(expected=NullPointerException.class)\r
- public void testCreation() {\r
- RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
- new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", null, key);\r
- }\r
-\r
- /**\r
- * Test object creation\r
- */\r
- @Test(expected=NullPointerException.class)\r
- public void testCreation2() {\r
- new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, null);\r
- }\r
-\r
- /**\r
- * Test object creation\r
- */\r
- @Test\r
- public void testDiscard() {\r
- RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
- ResponseExpectedRpcListener<OfHeader> listener =\r
- new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);\r
- listener.discard();\r
- RpcError rpcError = AbstractRpcListener.buildRpcError("Failed to send message",\r
- ErrorSeverity.ERROR, "check switch connection", new TimeoutException("Request timed out"));\r
- SettableFuture<RpcResult<?>> result = SettableFuture.create();\r
- result.set(Rpcs.getRpcResult(false, null, Collections.singletonList(rpcError)));\r
- try {\r
- Assert.assertEquals("Wrong result", result.get().getErrors().iterator().next().getMessage(),\r
- listener.getResult().get().getErrors().iterator().next().getMessage());\r
- Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());\r
- Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());\r
- } catch (InterruptedException | ExecutionException e) {\r
- fail("Problem accessing result");\r
- }\r
- }\r
-\r
- /**\r
- * Test object creation\r
- */\r
- @Test\r
- public void testCompleted() {\r
- RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
- ResponseExpectedRpcListener<OfHeader> listener =\r
- new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);\r
- BarrierInputBuilder barrierBuilder = new BarrierInputBuilder();\r
- BarrierInput barrierInput = barrierBuilder.build();\r
- listener.completed(barrierInput);\r
- SettableFuture<RpcResult<?>> result = SettableFuture.create();\r
- result.set(Rpcs.getRpcResult(true, barrierInput, Collections.<RpcError>emptyList()));\r
- try {\r
- Assert.assertEquals("Wrong result", result.get().getErrors(), listener.getResult().get().getErrors());\r
- Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());\r
- Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());\r
- } catch (InterruptedException | ExecutionException e) {\r
- fail("Problem accessing result");\r
- }\r
- }\r
-\r
- /**\r
- * Test object creation\r
- */\r
- @Test\r
- public void testOperationSuccessful() {\r
- RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());\r
- ResponseExpectedRpcListener<OfHeader> listener =\r
- new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);\r
- listener.operationSuccessful();\r
- ResponseExpectedRpcListener<?> present = responseCache.getIfPresent(key);\r
- Assert.assertEquals(present, listener);\r
- }\r
+/*
+ * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowjava.protocol.impl.core.connection;
+
+import static org.junit.Assert.fail;
+
+import java.util.Collections;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.controller.sal.common.util.Rpcs;
+import org.opendaylight.openflowjava.protocol.impl.core.connection.AbstractRpcListener;
+import org.opendaylight.openflowjava.protocol.impl.core.connection.ResponseExpectedRpcListener;
+import org.opendaylight.openflowjava.protocol.impl.core.connection.RpcResponseKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
+import org.opendaylight.yangtools.yang.common.RpcError;
+import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.RemovalNotification;
+import com.google.common.util.concurrent.SettableFuture;
+
+/**
+ * @author michal.polkorab
+ *
+ */
+public class ResponseExpectedRpcListenerTest {
+
+ private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
+ new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
+ @Override
+ public void onRemoval(
+ final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
+ notification.getValue().discard();
+ }
+ };
+ private static final int RPC_RESPONSE_EXPIRATION = 1;
+ private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> responseCache = CacheBuilder.newBuilder()
+ .concurrencyLevel(1)
+ .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
+ .removalListener(REMOVAL_LISTENER).build();
+
+ /**
+ * Test object creation
+ */
+ @Test(expected=NullPointerException.class)
+ public void testCreation() {
+ RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());
+ new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", null, key);
+ }
+
+ /**
+ * Test object creation
+ */
+ @Test(expected=NullPointerException.class)
+ public void testCreation2() {
+ new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, null);
+ }
+
+ /**
+ * Test object creation
+ */
+ @Test
+ public void testDiscard() {
+ RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());
+ ResponseExpectedRpcListener<OfHeader> listener =
+ new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);
+ listener.discard();
+ RpcError rpcError = AbstractRpcListener.buildRpcError("Failed to send message",
+ ErrorSeverity.ERROR, "check switch connection", new TimeoutException("Request timed out"));
+ SettableFuture<RpcResult<?>> result = SettableFuture.create();
+ result.set(Rpcs.getRpcResult(false, null, Collections.singletonList(rpcError)));
+ try {
+ Assert.assertEquals("Wrong result", result.get().getErrors().iterator().next().getMessage(),
+ listener.getResult().get().getErrors().iterator().next().getMessage());
+ Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());
+ Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());
+ } catch (InterruptedException | ExecutionException e) {
+ fail("Problem accessing result");
+ }
+ }
+
+ /**
+ * Test object creation
+ */
+ @Test
+ public void testCompleted() {
+ RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());
+ ResponseExpectedRpcListener<OfHeader> listener =
+ new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);
+ BarrierInputBuilder barrierBuilder = new BarrierInputBuilder();
+ BarrierInput barrierInput = barrierBuilder.build();
+ listener.completed(barrierInput);
+ SettableFuture<RpcResult<?>> result = SettableFuture.create();
+ result.set(Rpcs.getRpcResult(true, barrierInput, Collections.<RpcError>emptyList()));
+ try {
+ Assert.assertEquals("Wrong result", result.get().getErrors(), listener.getResult().get().getErrors());
+ Assert.assertEquals("Wrong result", result.get().getResult(), listener.getResult().get().getResult());
+ Assert.assertEquals("Wrong result", result.get().isSuccessful(), listener.getResult().get().isSuccessful());
+ } catch (InterruptedException | ExecutionException e) {
+ fail("Problem accessing result");
+ }
+ }
+
+ /**
+ * Test object creation
+ */
+ @Test
+ public void testOperationSuccessful() {
+ RpcResponseKey key = new RpcResponseKey(12345L, BarrierOutput.class.getName());
+ ResponseExpectedRpcListener<OfHeader> listener =
+ new ResponseExpectedRpcListener<>("MESSAGE", "Failed to send message", responseCache, key);
+ listener.operationSuccessful();
+ ResponseExpectedRpcListener<?> present = responseCache.getIfPresent(key);
+ Assert.assertEquals(present, listener);
+ }
}
\ No newline at end of file