Add method to register listener for unknown msg
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFEncoderTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. 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
9 package org.opendaylight.openflowjava.protocol.impl.core;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyShort;
13 import static org.mockito.Mockito.doThrow;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.channel.ChannelHandlerContext;
19 import io.netty.util.concurrent.Future;
20 import io.netty.util.concurrent.GenericFutureListener;
21
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
28 import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageListenerWrapper;
29 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32
33 /**
34  *
35  * @author jameshall
36  */
37 public class OFEncoderTest {
38
39     @Mock ChannelHandlerContext mockChHndlrCtx ;
40     @Mock SerializationFactory mockSerializationFactory ;
41     @Mock MessageListenerWrapper wrapper;
42     @Mock OfHeader mockMsg ;
43     @Mock ByteBuf mockOut ;
44     @Mock Future<Void> future;
45     @Mock GenericFutureListener<Future<Void>> listener;
46
47     OFEncoder ofEncoder = new OFEncoder() ;
48
49     /**
50      * Sets up test environment
51      */
52     @Before
53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55         ofEncoder = new OFEncoder() ;
56         ofEncoder.setSerializationFactory( mockSerializationFactory ) ;
57     }
58
59     /**
60      * Test successful write (no clear)
61      */
62     @Test
63     public void testEncodeSuccess() {
64         when(mockOut.readableBytes()).thenReturn(1);
65         when(wrapper.getMsg()).thenReturn(mockMsg);
66         when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
67         try {
68             ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
69         } catch (Exception e) {
70             Assert.fail();
71         }
72
73         // Verify that the channel was flushed after the ByteBuf was retained.
74         verify(mockOut, times(0)).clear();
75     }
76
77     /**
78      * Test Bytebuf clearing after serialization failure
79      */
80     @Test
81     public void testEncodeSerializationException() {
82         when(wrapper.getMsg()).thenReturn(mockMsg);
83         when(wrapper.getListener()).thenReturn(listener);
84         when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
85         doThrow(new IllegalArgumentException()).when(mockSerializationFactory).messageToBuffer(anyShort(),any(ByteBuf.class), any(DataObject.class));
86         try {
87             ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
88         } catch (Exception e) {
89             Assert.fail();
90         }
91
92         // Verify that the output message buf was cleared...
93         verify(mockOut, times(1)).clear();
94     }
95
96     /**
97      * Test no action on empty bytebuf
98      */
99     @Test
100     public void testEncodeSerializesNoBytes() {
101         when(mockOut.readableBytes()).thenReturn(0);
102         when(wrapper.getMsg()).thenReturn(mockMsg);
103         when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
104         try {
105             ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
106         } catch (Exception e) {
107             Assert.fail();
108         }
109
110         // Verify that the output message buf was cleared...
111         verify(mockOut, times(0)).clear();
112         verify(mockChHndlrCtx, times(0)).writeAndFlush(mockOut);
113         verify(mockOut, times(0)).retain();
114     }
115 }