Merge "Remove redundant type specifiers"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / connection / ChannelOutboundQueue02Test.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowjava.protocol.impl.core.connection;
9
10 import com.google.common.cache.Cache;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.RemovalListener;
13 import io.netty.channel.ChannelHandler;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.ChannelOutboundHandlerAdapter;
16 import io.netty.channel.ChannelPromise;
17 import io.netty.channel.embedded.EmbeddedChannel;
18 import java.net.InetSocketAddress;
19 import java.util.concurrent.TimeUnit;
20 import org.junit.After;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
30
31 /**
32  * Unit tests for ChannelOutboundQueue02.
33  *
34  * @author madamjak
35  */
36 public class ChannelOutboundQueue02Test {
37     private static int counter;
38     private static final int CHANNEL_OUTBOUND_QUEUE_SIZE = 1024;
39     private static final int RPC_RESPONSE_EXPIRATION = 1;
40     private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
41         notification -> notification.getValue().discard();
42
43     @Mock EchoInput echoInput;
44     @Mock BarrierInput barrierInput;
45     @Mock EchoReplyInput echoReplyInput;
46     @Mock ExperimenterInput experimenterInput;
47     private ConnectionAdapterImpl adapter;
48     private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache;
49
50     /**
51      * Initialize mocks.
52      */
53     @Before
54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56     }
57
58     /**
59      * Disconnect adapter after each test.
60      */
61     @After
62     public void tierDown() {
63         if (adapter != null && adapter.isAlive()) {
64             adapter.disconnect();
65         }
66     }
67
68     /**
69      * Test write to closed / opened channel.
70      */
71     @Test
72     public void test01() throws Exception {
73         final EmbeddedChannel ec = new EmbeddedChannel(new EmbededChannelHandler());
74         adapter = new ConnectionAdapterImpl(ec, InetSocketAddress.createUnresolved("localhost", 9876), true,
75                 CHANNEL_OUTBOUND_QUEUE_SIZE);
76         cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(
77                 RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES).removalListener(REMOVAL_LISTENER).build();
78         adapter.setResponseCache(cache);
79         final ChannelOutboundQueue cq = (ChannelOutboundQueue) ec.pipeline().last();
80         counter = 0;
81         adapter.barrier(barrierInput);
82         adapter.echo(echoInput);
83         cq.channelInactive(ec.pipeline().lastContext());
84         ec.runPendingTasks();
85         Assert.assertEquals("Wrong - ChannelOutboundHandlerAdapter.write was invoked on closed channel",0, counter);
86         cq.channelActive(ec.pipeline().lastContext());
87         counter = 0;
88         adapter.barrier(barrierInput);
89         adapter.experimenter(experimenterInput);
90         ec.runPendingTasks();
91         Assert.assertEquals("Wrong - ChannelOutboundHandlerAdapter.write has not been invoked on opened channel",
92                 2, counter);
93     }
94
95     /**
96      * Test write to read only / writable channel.
97      */
98     @Test
99     public void test02() {
100         final ChangeWritableEmbededChannel ec = new ChangeWritableEmbededChannel(new EmbededChannelHandler());
101         adapter = new ConnectionAdapterImpl(ec, InetSocketAddress.createUnresolved("localhost", 9876), true,
102                 CHANNEL_OUTBOUND_QUEUE_SIZE);
103         cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(
104                 RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES) .removalListener(REMOVAL_LISTENER).build();
105         adapter.setResponseCache(cache);
106         ec.setReadOnly();
107         counter = 0;
108         adapter.barrier(barrierInput);
109         adapter.echo(echoInput);
110         ec.runPendingTasks();
111         Assert.assertEquals("Wrong - write to readonly channel",0, counter);
112         ec.setWritable();
113         adapter.echoReply(echoReplyInput);
114         adapter.echo(echoInput);
115         ec.runPendingTasks();
116         Assert.assertEquals("Wrong - write to writtable channel",4, counter);
117     }
118
119     /**
120      * Channel Handler for testing.
121      * @author madamjak
122      */
123     private class EmbededChannelHandler extends ChannelOutboundHandlerAdapter {
124         @Override
125         public void write(final ChannelHandlerContext ctx, final Object msg,
126                 final ChannelPromise promise) throws Exception {
127             if (msg instanceof MessageListenerWrapper) {
128                 counter++;
129             }
130         }
131     }
132
133     /**
134      * Class for testing - channel can change state to read only or writable.
135      * @author madamjak
136      */
137     private static class ChangeWritableEmbededChannel extends EmbeddedChannel {
138         private boolean isWrittable;
139
140         ChangeWritableEmbededChannel(final ChannelHandler channelHandler) {
141             super(channelHandler);
142             setReadOnly();
143         }
144
145         @Override
146         public boolean isWritable() {
147             return isWrittable;
148         }
149
150         public void setWritable() {
151             isWrittable = true;
152         }
153
154         public void setReadOnly() {
155             isWrittable = false;
156         }
157     }
158 }