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